blob: fff80abb80286cb8611906f9e30f1106f80be3cc [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Craig Tiller6169d5f2016-03-31 07:46:18 -07003 * Copyright 2015, Google Inc.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08004 * 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 Tillerd4c98332016-03-31 13:45:47 -070034#include "src/core/ext/client_config/client_channel.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080035
Mark D. Roth4c0fe492016-08-31 13:51:55 -070036#include <stdbool.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080037#include <stdio.h>
Craig Tillereb3b12e2015-06-26 14:42:49 -070038#include <string.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080039
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080040#include <grpc/support/alloc.h>
41#include <grpc/support/log.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080042#include <grpc/support/sync.h>
43#include <grpc/support/useful.h>
44
Mark D. Roth4c0fe492016-08-31 13:51:55 -070045#include "src/core/ext/client_config/subchannel.h"
Craig Tiller9533d042016-03-25 17:11:06 -070046#include "src/core/lib/channel/channel_args.h"
47#include "src/core/lib/channel/connected_channel.h"
Craig Tiller9533d042016-03-25 17:11:06 -070048#include "src/core/lib/iomgr/iomgr.h"
Mark D. Roth4c0fe492016-08-31 13:51:55 -070049#include "src/core/lib/iomgr/polling_entity.h"
Craig Tiller9533d042016-03-25 17:11:06 -070050#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 Tiller8910ac62015-10-08 16:49:15 -070054
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080055/* Client channel implementation */
56
Mark D. Roth2a5959f2016-09-01 08:20:27 -070057/*************************************************************************
58 * CHANNEL-WIDE FUNCTIONS
59 */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080060
Craig Tiller800dacb2015-10-06 09:10:26 -070061typedef struct client_channel_channel_data {
Craig Tillerf5f17122015-06-25 08:47:26 -070062 /** resolver for this channel */
63 grpc_resolver *resolver;
Craig Tiller20a3c352015-08-05 08:39:50 -070064 /** have we started resolving this channel */
Mark D. Roth4c0fe492016-08-31 13:51:55 -070065 bool started_resolving;
Craig Tillerf5f17122015-06-25 08:47:26 -070066
Craig Tiller9d94b602015-07-01 14:23:18 -070067 /** mutex protecting client configuration, including all
68 variables below in this data structure */
Mark D. Rothff4df062016-08-22 15:02:49 -070069 gpr_mu mu;
70 /** currently active load balancer - guarded by mu */
Craig Tillerf5f17122015-06-25 08:47:26 -070071 grpc_lb_policy *lb_policy;
Mark D. Rothff4df062016-08-22 15:02:49 -070072 /** incoming resolver result - set by resolver.next(), guarded by mu */
73 grpc_resolver_result *resolver_result;
Craig Tiller3f475422015-06-25 10:43:05 -070074 /** a list of closures that are all waiting for config to come in */
Craig Tillerd9ccbbf2015-09-22 09:30:00 -070075 grpc_closure_list waiting_for_config_closures;
Craig Tiller3f475422015-06-25 10:43:05 -070076 /** resolver callback */
Mark D. Rothff4df062016-08-22 15:02:49 -070077 grpc_closure on_resolver_result_changed;
Craig Tiller3f475422015-06-25 10:43:05 -070078 /** connectivity state being tracked */
Craig Tillerca3e9d32015-06-27 18:37:27 -070079 grpc_connectivity_state_tracker state_tracker;
Craig Tiller48cb07c2015-07-15 16:16:15 -070080 /** when an lb_policy arrives, should we try to exit idle */
Mark D. Roth4c0fe492016-08-31 13:51:55 -070081 bool exit_idle_when_lb_policy_arrives;
Craig Tiller906e3bc2015-11-24 07:31:31 -080082 /** owning stack */
83 grpc_channel_stack *owning_stack;
Craig Tiller69b093b2016-02-25 19:04:07 -080084 /** interested parties (owned) */
85 grpc_pollset_set *interested_parties;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080086} channel_data;
87
Craig Tillerd6c98df2015-08-18 09:33:44 -070088/** We create one watcher for each new lb_policy that is returned from a
Mark D. Roth4c0fe492016-08-31 13:51:55 -070089 resolver, to watch for state changes from the lb_policy. When a state
90 change is seen, we update the channel, and create a new watcher. */
Craig Tillera82950e2015-09-22 12:33:20 -070091typedef struct {
Craig Tiller1ada6ad2015-07-16 16:19:14 -070092 channel_data *chand;
Craig Tiller33825112015-09-18 07:44:19 -070093 grpc_closure on_changed;
Craig Tiller1ada6ad2015-07-16 16:19:14 -070094 grpc_connectivity_state state;
95 grpc_lb_policy *lb_policy;
96} lb_policy_connectivity_watcher;
97
Craig Tillera82950e2015-09-22 12:33:20 -070098static void watch_lb_policy(grpc_exec_ctx *exec_ctx, channel_data *chand,
99 grpc_lb_policy *lb_policy,
100 grpc_connectivity_state current_state);
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700101
Craig Tiller8c0d96f2016-03-11 14:27:52 -0800102static void set_channel_connectivity_state_locked(grpc_exec_ctx *exec_ctx,
103 channel_data *chand,
104 grpc_connectivity_state state,
Craig Tiller804ff712016-05-05 16:25:40 -0700105 grpc_error *error,
Craig Tiller8c0d96f2016-03-11 14:27:52 -0800106 const char *reason) {
107 if ((state == GRPC_CHANNEL_TRANSIENT_FAILURE ||
Craig Tiller48ed92e2016-06-02 11:07:12 -0700108 state == GRPC_CHANNEL_SHUTDOWN) &&
Craig Tiller8c0d96f2016-03-11 14:27:52 -0800109 chand->lb_policy != NULL) {
110 /* cancel fail-fast picks */
111 grpc_lb_policy_cancel_picks(
112 exec_ctx, chand->lb_policy,
113 /* mask= */ GRPC_INITIAL_METADATA_IGNORE_CONNECTIVITY,
114 /* check= */ 0);
115 }
Craig Tiller9ccf5f12016-05-07 21:41:01 -0700116 grpc_connectivity_state_set(exec_ctx, &chand->state_tracker, state, error,
117 reason);
Craig Tiller8c0d96f2016-03-11 14:27:52 -0800118}
119
Craig Tiller804ff712016-05-05 16:25:40 -0700120static void on_lb_policy_state_changed_locked(grpc_exec_ctx *exec_ctx,
121 lb_policy_connectivity_watcher *w,
122 grpc_error *error) {
Craig Tillercb2609f2015-11-24 17:19:19 -0800123 grpc_connectivity_state publish_state = w->state;
Craig Tiller5795da72015-09-17 15:27:13 -0700124 /* check if the notification is for a stale policy */
Craig Tillera82950e2015-09-22 12:33:20 -0700125 if (w->lb_policy != w->chand->lb_policy) return;
Craig Tiller5795da72015-09-17 15:27:13 -0700126
Craig Tiller48ed92e2016-06-02 11:07:12 -0700127 if (publish_state == GRPC_CHANNEL_SHUTDOWN && w->chand->resolver != NULL) {
Craig Tillercb2609f2015-11-24 17:19:19 -0800128 publish_state = GRPC_CHANNEL_TRANSIENT_FAILURE;
129 grpc_resolver_channel_saw_error(exec_ctx, w->chand->resolver);
Craig Tillerf62c4d52015-12-04 07:43:07 -0800130 GRPC_LB_POLICY_UNREF(exec_ctx, w->chand->lb_policy, "channel");
131 w->chand->lb_policy = NULL;
Craig Tillercb2609f2015-11-24 17:19:19 -0800132 }
Craig Tiller8c0d96f2016-03-11 14:27:52 -0800133 set_channel_connectivity_state_locked(exec_ctx, w->chand, publish_state,
Craig Tillerfc353d62016-05-10 12:58:03 -0700134 GRPC_ERROR_REF(error), "lb_changed");
Craig Tiller48ed92e2016-06-02 11:07:12 -0700135 if (w->state != GRPC_CHANNEL_SHUTDOWN) {
Craig Tillera82950e2015-09-22 12:33:20 -0700136 watch_lb_policy(exec_ctx, w->chand, w->lb_policy, w->state);
137 }
Craig Tiller5795da72015-09-17 15:27:13 -0700138}
139
Craig Tillera82950e2015-09-22 12:33:20 -0700140static void on_lb_policy_state_changed(grpc_exec_ctx *exec_ctx, void *arg,
Craig Tiller804ff712016-05-05 16:25:40 -0700141 grpc_error *error) {
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700142 lb_policy_connectivity_watcher *w = arg;
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700143
Mark D. Rothff4df062016-08-22 15:02:49 -0700144 gpr_mu_lock(&w->chand->mu);
Craig Tiller804ff712016-05-05 16:25:40 -0700145 on_lb_policy_state_changed_locked(exec_ctx, w, error);
Mark D. Rothff4df062016-08-22 15:02:49 -0700146 gpr_mu_unlock(&w->chand->mu);
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700147
Craig Tiller906e3bc2015-11-24 07:31:31 -0800148 GRPC_CHANNEL_STACK_UNREF(exec_ctx, w->chand->owning_stack, "watch_lb_policy");
Craig Tillera82950e2015-09-22 12:33:20 -0700149 gpr_free(w);
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700150}
151
Craig Tillera82950e2015-09-22 12:33:20 -0700152static void watch_lb_policy(grpc_exec_ctx *exec_ctx, channel_data *chand,
153 grpc_lb_policy *lb_policy,
154 grpc_connectivity_state current_state) {
155 lb_policy_connectivity_watcher *w = gpr_malloc(sizeof(*w));
Craig Tiller906e3bc2015-11-24 07:31:31 -0800156 GRPC_CHANNEL_STACK_REF(chand->owning_stack, "watch_lb_policy");
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700157
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700158 w->chand = chand;
Craig Tillera82950e2015-09-22 12:33:20 -0700159 grpc_closure_init(&w->on_changed, on_lb_policy_state_changed, w);
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700160 w->state = current_state;
161 w->lb_policy = lb_policy;
Craig Tillera82950e2015-09-22 12:33:20 -0700162 grpc_lb_policy_notify_on_state_change(exec_ctx, lb_policy, &w->state,
163 &w->on_changed);
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700164}
165
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700166static void on_resolver_result_changed(grpc_exec_ctx *exec_ctx, void *arg,
167 grpc_error *error) {
Craig Tiller3f475422015-06-25 10:43:05 -0700168 channel_data *chand = arg;
169 grpc_lb_policy *lb_policy = NULL;
170 grpc_lb_policy *old_lb_policy;
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700171 grpc_connectivity_state state = GRPC_CHANNEL_TRANSIENT_FAILURE;
Mark D. Roth4c0fe492016-08-31 13:51:55 -0700172 bool exit_idle = false;
Craig Tiller804ff712016-05-05 16:25:40 -0700173 grpc_error *state_error = GRPC_ERROR_CREATE("No load balancing policy");
Craig Tiller3f475422015-06-25 10:43:05 -0700174
Mark D. Rothff4df062016-08-22 15:02:49 -0700175 if (chand->resolver_result != NULL) {
Mark D. Rotha275aea2016-08-23 12:30:45 -0700176 lb_policy = grpc_resolver_result_get_lb_policy(chand->resolver_result);
Craig Tillera82950e2015-09-22 12:33:20 -0700177 if (lb_policy != NULL) {
178 GRPC_LB_POLICY_REF(lb_policy, "channel");
179 GRPC_LB_POLICY_REF(lb_policy, "config_change");
Craig Tillerf707d622016-05-06 14:26:12 -0700180 GRPC_ERROR_UNREF(state_error);
Craig Tiller804ff712016-05-05 16:25:40 -0700181 state =
182 grpc_lb_policy_check_connectivity(exec_ctx, lb_policy, &state_error);
Craig Tiller45724b32015-09-22 10:42:19 -0700183 }
Craig Tiller3f475422015-06-25 10:43:05 -0700184
Mark D. Rothff4df062016-08-22 15:02:49 -0700185 grpc_resolver_result_unref(exec_ctx, chand->resolver_result);
Craig Tillera82950e2015-09-22 12:33:20 -0700186 }
187
Mark D. Rothff4df062016-08-22 15:02:49 -0700188 chand->resolver_result = NULL;
Craig Tiller3f475422015-06-25 10:43:05 -0700189
Craig Tiller86c99582015-11-25 15:22:26 -0800190 if (lb_policy != NULL) {
Craig Tiller69b093b2016-02-25 19:04:07 -0800191 grpc_pollset_set_add_pollset_set(exec_ctx, lb_policy->interested_parties,
192 chand->interested_parties);
Craig Tiller86c99582015-11-25 15:22:26 -0800193 }
194
Mark D. Rothff4df062016-08-22 15:02:49 -0700195 gpr_mu_lock(&chand->mu);
Craig Tiller3f475422015-06-25 10:43:05 -0700196 old_lb_policy = chand->lb_policy;
197 chand->lb_policy = lb_policy;
Craig Tiller0ede5452016-04-23 12:21:45 -0700198 if (lb_policy != NULL) {
199 grpc_exec_ctx_enqueue_list(exec_ctx, &chand->waiting_for_config_closures,
200 NULL);
201 } else if (chand->resolver == NULL /* disconnected */) {
Craig Tiller804ff712016-05-05 16:25:40 -0700202 grpc_closure_list_fail_all(
203 &chand->waiting_for_config_closures,
204 GRPC_ERROR_CREATE_REFERENCING("Channel disconnected", &error, 1));
Craig Tiller6c396862016-01-28 13:53:40 -0800205 grpc_exec_ctx_enqueue_list(exec_ctx, &chand->waiting_for_config_closures,
206 NULL);
Craig Tillera82950e2015-09-22 12:33:20 -0700207 }
208 if (lb_policy != NULL && chand->exit_idle_when_lb_policy_arrives) {
209 GRPC_LB_POLICY_REF(lb_policy, "exit_idle");
Mark D. Roth4c0fe492016-08-31 13:51:55 -0700210 exit_idle = true;
211 chand->exit_idle_when_lb_policy_arrives = false;
Craig Tillera82950e2015-09-22 12:33:20 -0700212 }
Craig Tiller98465032015-06-29 14:36:42 -0700213
Craig Tiller804ff712016-05-05 16:25:40 -0700214 if (error == GRPC_ERROR_NONE && chand->resolver) {
Craig Tiller9ccf5f12016-05-07 21:41:01 -0700215 set_channel_connectivity_state_locked(
216 exec_ctx, chand, state, GRPC_ERROR_REF(state_error), "new_lb+resolver");
Craig Tillera82950e2015-09-22 12:33:20 -0700217 if (lb_policy != NULL) {
218 watch_lb_policy(exec_ctx, chand, lb_policy, state);
Craig Tiller45724b32015-09-22 10:42:19 -0700219 }
Craig Tiller906e3bc2015-11-24 07:31:31 -0800220 GRPC_CHANNEL_STACK_REF(chand->owning_stack, "resolver");
Mark D. Rotha275aea2016-08-23 12:30:45 -0700221 grpc_resolver_next(exec_ctx, chand->resolver, &chand->resolver_result,
Mark D. Rothff4df062016-08-22 15:02:49 -0700222 &chand->on_resolver_result_changed);
223 gpr_mu_unlock(&chand->mu);
Craig Tillera82950e2015-09-22 12:33:20 -0700224 } else {
Craig Tiller76a5c0e2016-03-09 09:05:30 -0800225 if (chand->resolver != NULL) {
226 grpc_resolver_shutdown(exec_ctx, chand->resolver);
227 GRPC_RESOLVER_UNREF(exec_ctx, chand->resolver, "channel");
228 chand->resolver = NULL;
229 }
Craig Tiller804ff712016-05-05 16:25:40 -0700230 grpc_error *refs[] = {error, state_error};
Craig Tiller8c0d96f2016-03-11 14:27:52 -0800231 set_channel_connectivity_state_locked(
Craig Tillerd925c932016-06-06 08:38:50 -0700232 exec_ctx, chand, GRPC_CHANNEL_SHUTDOWN,
Craig Tiller804ff712016-05-05 16:25:40 -0700233 GRPC_ERROR_CREATE_REFERENCING("Got config after disconnection", refs,
234 GPR_ARRAY_SIZE(refs)),
235 "resolver_gone");
Mark D. Rothff4df062016-08-22 15:02:49 -0700236 gpr_mu_unlock(&chand->mu);
Craig Tillera82950e2015-09-22 12:33:20 -0700237 }
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700238
Craig Tillera82950e2015-09-22 12:33:20 -0700239 if (exit_idle) {
240 grpc_lb_policy_exit_idle(exec_ctx, lb_policy);
241 GRPC_LB_POLICY_UNREF(exec_ctx, lb_policy, "exit_idle");
242 }
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700243
Craig Tillera82950e2015-09-22 12:33:20 -0700244 if (old_lb_policy != NULL) {
Craig Tiller69b093b2016-02-25 19:04:07 -0800245 grpc_pollset_set_del_pollset_set(
246 exec_ctx, old_lb_policy->interested_parties, chand->interested_parties);
Craig Tillera82950e2015-09-22 12:33:20 -0700247 GRPC_LB_POLICY_UNREF(exec_ctx, old_lb_policy, "channel");
248 }
Craig Tiller000cd8f2015-09-18 07:20:29 -0700249
Craig Tillera82950e2015-09-22 12:33:20 -0700250 if (lb_policy != NULL) {
251 GRPC_LB_POLICY_UNREF(exec_ctx, lb_policy, "config_change");
252 }
Craig Tiller45724b32015-09-22 10:42:19 -0700253
Craig Tiller906e3bc2015-11-24 07:31:31 -0800254 GRPC_CHANNEL_STACK_UNREF(exec_ctx, chand->owning_stack, "resolver");
Craig Tiller9ccf5f12016-05-07 21:41:01 -0700255 GRPC_ERROR_UNREF(state_error);
Craig Tiller3f475422015-06-25 10:43:05 -0700256}
257
Craig Tillera82950e2015-09-22 12:33:20 -0700258static void cc_start_transport_op(grpc_exec_ctx *exec_ctx,
259 grpc_channel_element *elem,
260 grpc_transport_op *op) {
Craig Tillerca3e9d32015-06-27 18:37:27 -0700261 channel_data *chand = elem->channel_data;
Craig Tiller000cd8f2015-09-18 07:20:29 -0700262
Craig Tiller332f1b32016-05-24 13:21:21 -0700263 grpc_exec_ctx_sched(exec_ctx, op->on_consumed, GRPC_ERROR_NONE, NULL);
Craig Tillerca3e9d32015-06-27 18:37:27 -0700264
Craig Tillerd7f12e32016-03-03 10:08:31 -0800265 GPR_ASSERT(op->set_accept_stream == false);
Craig Tiller28bf8912015-12-07 16:07:04 -0800266 if (op->bind_pollset != NULL) {
Craig Tiller69b093b2016-02-25 19:04:07 -0800267 grpc_pollset_set_add_pollset(exec_ctx, chand->interested_parties,
Craig Tillere2c62372015-12-07 16:11:03 -0800268 op->bind_pollset);
Craig Tiller28bf8912015-12-07 16:07:04 -0800269 }
Craig Tillerca3e9d32015-06-27 18:37:27 -0700270
Mark D. Rothff4df062016-08-22 15:02:49 -0700271 gpr_mu_lock(&chand->mu);
Craig Tillera82950e2015-09-22 12:33:20 -0700272 if (op->on_connectivity_state_change != NULL) {
273 grpc_connectivity_state_notify_on_state_change(
274 exec_ctx, &chand->state_tracker, op->connectivity_state,
275 op->on_connectivity_state_change);
276 op->on_connectivity_state_change = NULL;
277 op->connectivity_state = NULL;
278 }
279
Craig Tiller26dab312015-12-07 14:43:47 -0800280 if (op->send_ping != NULL) {
Craig Tiller87b71e22015-12-07 15:14:14 -0800281 if (chand->lb_policy == NULL) {
Craig Tiller332f1b32016-05-24 13:21:21 -0700282 grpc_exec_ctx_sched(exec_ctx, op->send_ping,
283 GRPC_ERROR_CREATE("Ping with no load balancing"),
284 NULL);
Craig Tiller26dab312015-12-07 14:43:47 -0800285 } else {
Craig Tiller28bf8912015-12-07 16:07:04 -0800286 grpc_lb_policy_ping_one(exec_ctx, chand->lb_policy, op->send_ping);
Craig Tiller26dab312015-12-07 14:43:47 -0800287 op->bind_pollset = NULL;
288 }
289 op->send_ping = NULL;
290 }
291
Craig Tiller1c51edc2016-05-07 16:18:43 -0700292 if (op->disconnect_with_error != GRPC_ERROR_NONE) {
293 if (chand->resolver != NULL) {
294 set_channel_connectivity_state_locked(
Craig Tillerd925c932016-06-06 08:38:50 -0700295 exec_ctx, chand, GRPC_CHANNEL_SHUTDOWN,
Craig Tiller1c51edc2016-05-07 16:18:43 -0700296 GRPC_ERROR_REF(op->disconnect_with_error), "disconnect");
297 grpc_resolver_shutdown(exec_ctx, chand->resolver);
298 GRPC_RESOLVER_UNREF(exec_ctx, chand->resolver, "channel");
299 chand->resolver = NULL;
300 if (!chand->started_resolving) {
301 grpc_closure_list_fail_all(&chand->waiting_for_config_closures,
302 GRPC_ERROR_REF(op->disconnect_with_error));
Craig Tiller9ccf5f12016-05-07 21:41:01 -0700303 grpc_exec_ctx_enqueue_list(exec_ctx,
304 &chand->waiting_for_config_closures, NULL);
Craig Tiller1c51edc2016-05-07 16:18:43 -0700305 }
306 if (chand->lb_policy != NULL) {
307 grpc_pollset_set_del_pollset_set(exec_ctx,
308 chand->lb_policy->interested_parties,
309 chand->interested_parties);
310 GRPC_LB_POLICY_UNREF(exec_ctx, chand->lb_policy, "channel");
311 chand->lb_policy = NULL;
312 }
Craig Tillerb12d22a2016-04-23 12:50:21 -0700313 }
Craig Tiller1c51edc2016-05-07 16:18:43 -0700314 GRPC_ERROR_UNREF(op->disconnect_with_error);
Craig Tillera82950e2015-09-22 12:33:20 -0700315 }
Mark D. Rothff4df062016-08-22 15:02:49 -0700316 gpr_mu_unlock(&chand->mu);
Craig Tillerca3e9d32015-06-27 18:37:27 -0700317}
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800318
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700319/* Constructor for channel_data */
320static void cc_init_channel_elem(grpc_exec_ctx *exec_ctx,
321 grpc_channel_element *elem,
322 grpc_channel_element_args *args) {
323 channel_data *chand = elem->channel_data;
324
325 memset(chand, 0, sizeof(*chand));
326
327 GPR_ASSERT(args->is_last);
328 GPR_ASSERT(elem->filter == &grpc_client_channel_filter);
329
330 gpr_mu_init(&chand->mu);
331 grpc_closure_init(&chand->on_resolver_result_changed,
332 on_resolver_result_changed, chand);
333 chand->owning_stack = args->channel_stack;
334
335 grpc_connectivity_state_init(&chand->state_tracker, GRPC_CHANNEL_IDLE,
336 "client_channel");
337 chand->interested_parties = grpc_pollset_set_create();
338}
339
340/* Destructor for channel_data */
341static void cc_destroy_channel_elem(grpc_exec_ctx *exec_ctx,
342 grpc_channel_element *elem) {
343 channel_data *chand = elem->channel_data;
344
345 if (chand->resolver != NULL) {
346 grpc_resolver_shutdown(exec_ctx, chand->resolver);
347 GRPC_RESOLVER_UNREF(exec_ctx, chand->resolver, "channel");
348 }
349 if (chand->lb_policy != NULL) {
350 grpc_pollset_set_del_pollset_set(exec_ctx,
351 chand->lb_policy->interested_parties,
352 chand->interested_parties);
353 GRPC_LB_POLICY_UNREF(exec_ctx, chand->lb_policy, "channel");
354 }
355 grpc_connectivity_state_destroy(exec_ctx, &chand->state_tracker);
356 grpc_pollset_set_destroy(chand->interested_parties);
357 gpr_mu_destroy(&chand->mu);
358}
359
360/*************************************************************************
361 * PER-CALL FUNCTIONS
362 */
363
364#define GET_CALL(call_data) \
365 ((grpc_subchannel_call *)(gpr_atm_acq_load(&(call_data)->subchannel_call)))
366
367#define CANCELLED_CALL ((grpc_subchannel_call *)1)
368
369typedef enum {
370 GRPC_SUBCHANNEL_CALL_HOLDER_NOT_CREATING,
371 GRPC_SUBCHANNEL_CALL_HOLDER_PICKING_SUBCHANNEL
372} subchannel_creation_phase;
373
374/** Call data. Holds a pointer to grpc_subchannel_call and the
375 associated machinery to create such a pointer.
376 Handles queueing of stream ops until a call object is ready, waiting
377 for initial metadata before trying to create a call object,
378 and handling cancellation gracefully. */
379typedef struct client_channel_call_data {
380 /** either 0 for no call, 1 for cancelled, or a pointer to a
381 grpc_subchannel_call */
382 gpr_atm subchannel_call;
383
384 gpr_mu mu;
385
386 subchannel_creation_phase creation_phase;
387 grpc_connected_subchannel *connected_subchannel;
388 grpc_polling_entity *pollent;
389
390 grpc_transport_stream_op *waiting_ops;
391 size_t waiting_ops_count;
392 size_t waiting_ops_capacity;
393
394 grpc_closure next_step;
395
396 grpc_call_stack *owning_call;
397} call_data;
398
399static void add_waiting_locked(call_data *calld, grpc_transport_stream_op *op) {
400 GPR_TIMER_BEGIN("add_waiting_locked", 0);
401 if (calld->waiting_ops_count == calld->waiting_ops_capacity) {
402 calld->waiting_ops_capacity = GPR_MAX(3, 2 * calld->waiting_ops_capacity);
403 calld->waiting_ops =
404 gpr_realloc(calld->waiting_ops,
405 calld->waiting_ops_capacity * sizeof(*calld->waiting_ops));
406 }
407 calld->waiting_ops[calld->waiting_ops_count++] = *op;
408 GPR_TIMER_END("add_waiting_locked", 0);
409}
410
411static void fail_locked(grpc_exec_ctx *exec_ctx, call_data *calld,
412 grpc_error *error) {
413 size_t i;
414 for (i = 0; i < calld->waiting_ops_count; i++) {
415 grpc_transport_stream_op_finish_with_failure(
416 exec_ctx, &calld->waiting_ops[i], GRPC_ERROR_REF(error));
417 }
418 calld->waiting_ops_count = 0;
419 GRPC_ERROR_UNREF(error);
420}
421
422typedef struct {
423 grpc_transport_stream_op *ops;
424 size_t nops;
425 grpc_subchannel_call *call;
426} retry_ops_args;
427
428static void retry_ops(grpc_exec_ctx *exec_ctx, void *args, grpc_error *error) {
429 retry_ops_args *a = args;
430 size_t i;
431 for (i = 0; i < a->nops; i++) {
432 grpc_subchannel_call_process_op(exec_ctx, a->call, &a->ops[i]);
433 }
434 GRPC_SUBCHANNEL_CALL_UNREF(exec_ctx, a->call, "retry_ops");
435 gpr_free(a->ops);
436 gpr_free(a);
437}
438
439static void retry_waiting_locked(grpc_exec_ctx *exec_ctx, call_data *calld) {
440 retry_ops_args *a = gpr_malloc(sizeof(*a));
441 a->ops = calld->waiting_ops;
442 a->nops = calld->waiting_ops_count;
443 a->call = GET_CALL(calld);
444 if (a->call == CANCELLED_CALL) {
445 gpr_free(a);
446 fail_locked(exec_ctx, calld, GRPC_ERROR_CANCELLED);
447 return;
448 }
449 calld->waiting_ops = NULL;
450 calld->waiting_ops_count = 0;
451 calld->waiting_ops_capacity = 0;
452 GRPC_SUBCHANNEL_CALL_REF(a->call, "retry_ops");
453 grpc_exec_ctx_sched(exec_ctx, grpc_closure_create(retry_ops, a),
454 GRPC_ERROR_NONE, NULL);
455}
456
457static void subchannel_ready(grpc_exec_ctx *exec_ctx, void *arg,
458 grpc_error *error) {
459 call_data *calld = arg;
460 gpr_mu_lock(&calld->mu);
461 GPR_ASSERT(calld->creation_phase ==
462 GRPC_SUBCHANNEL_CALL_HOLDER_PICKING_SUBCHANNEL);
463 calld->creation_phase = GRPC_SUBCHANNEL_CALL_HOLDER_NOT_CREATING;
464 if (calld->connected_subchannel == NULL) {
465 gpr_atm_no_barrier_store(&calld->subchannel_call, 1);
466 fail_locked(exec_ctx, calld, GRPC_ERROR_CREATE_REFERENCING(
467 "Failed to create subchannel", &error, 1));
468 } else if (1 == gpr_atm_acq_load(&calld->subchannel_call)) {
469 /* already cancelled before subchannel became ready */
470 fail_locked(exec_ctx, calld,
471 GRPC_ERROR_CREATE_REFERENCING(
472 "Cancelled before creating subchannel", &error, 1));
473 } else {
474 grpc_subchannel_call *subchannel_call = NULL;
475 grpc_error *new_error = grpc_connected_subchannel_create_call(
476 exec_ctx, calld->connected_subchannel, calld->pollent,
477 &subchannel_call);
478 if (new_error != GRPC_ERROR_NONE) {
479 new_error = grpc_error_add_child(new_error, error);
480 subchannel_call = CANCELLED_CALL;
481 fail_locked(exec_ctx, calld, new_error);
482 }
483 gpr_atm_rel_store(&calld->subchannel_call,
484 (gpr_atm)(uintptr_t)subchannel_call);
485 retry_waiting_locked(exec_ctx, calld);
486 }
487 gpr_mu_unlock(&calld->mu);
488 GRPC_CALL_STACK_UNREF(exec_ctx, calld->owning_call, "pick_subchannel");
489}
490
491static char *cc_get_peer(grpc_exec_ctx *exec_ctx, grpc_call_element *elem) {
492 call_data *calld = elem->call_data;
493 grpc_subchannel_call *subchannel_call = GET_CALL(calld);
494 if (subchannel_call == NULL || subchannel_call == CANCELLED_CALL) {
495 return NULL;
496 } else {
497 return grpc_subchannel_call_get_peer(exec_ctx, subchannel_call);
498 }
499}
500
Craig Tiller577c9b22015-11-02 14:11:15 -0800501typedef struct {
502 grpc_metadata_batch *initial_metadata;
Craig Tiller8c0d96f2016-03-11 14:27:52 -0800503 uint32_t initial_metadata_flags;
Craig Tillerb5585d42015-11-17 07:18:31 -0800504 grpc_connected_subchannel **connected_subchannel;
Craig Tiller577c9b22015-11-02 14:11:15 -0800505 grpc_closure *on_ready;
506 grpc_call_element *elem;
507 grpc_closure closure;
508} continue_picking_args;
509
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700510static bool pick_subchannel(grpc_exec_ctx *exec_ctx, grpc_call_element *elem,
511 grpc_metadata_batch *initial_metadata,
512 uint32_t initial_metadata_flags,
513 grpc_connected_subchannel **connected_subchannel,
514 grpc_closure *on_ready);
Craig Tiller577c9b22015-11-02 14:11:15 -0800515
Craig Tiller804ff712016-05-05 16:25:40 -0700516static void continue_picking(grpc_exec_ctx *exec_ctx, void *arg,
517 grpc_error *error) {
Craig Tiller577c9b22015-11-02 14:11:15 -0800518 continue_picking_args *cpa = arg;
Craig Tiller0ede5452016-04-23 12:21:45 -0700519 if (cpa->connected_subchannel == NULL) {
Craig Tiller577c9b22015-11-02 14:11:15 -0800520 /* cancelled, do nothing */
Craig Tiller804ff712016-05-05 16:25:40 -0700521 } else if (error != GRPC_ERROR_NONE) {
Craig Tiller332f1b32016-05-24 13:21:21 -0700522 grpc_exec_ctx_sched(exec_ctx, cpa->on_ready, GRPC_ERROR_REF(error), NULL);
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700523 } else if (pick_subchannel(exec_ctx, cpa->elem, cpa->initial_metadata,
524 cpa->initial_metadata_flags,
525 cpa->connected_subchannel, cpa->on_ready)) {
Craig Tiller332f1b32016-05-24 13:21:21 -0700526 grpc_exec_ctx_sched(exec_ctx, cpa->on_ready, GRPC_ERROR_NONE, NULL);
Craig Tiller577c9b22015-11-02 14:11:15 -0800527 }
528 gpr_free(cpa);
529}
530
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700531static bool pick_subchannel(grpc_exec_ctx *exec_ctx, grpc_call_element *elem,
532 grpc_metadata_batch *initial_metadata,
533 uint32_t initial_metadata_flags,
534 grpc_connected_subchannel **connected_subchannel,
535 grpc_closure *on_ready) {
536 GPR_TIMER_BEGIN("pick_subchannel", 0);
Craig Tillerbfc9adc2016-06-27 13:16:22 -0700537
Craig Tiller577c9b22015-11-02 14:11:15 -0800538 channel_data *chand = elem->channel_data;
539 call_data *calld = elem->call_data;
540 continue_picking_args *cpa;
541 grpc_closure *closure;
542
Craig Tillerb5585d42015-11-17 07:18:31 -0800543 GPR_ASSERT(connected_subchannel);
Craig Tiller577c9b22015-11-02 14:11:15 -0800544
Mark D. Rothff4df062016-08-22 15:02:49 -0700545 gpr_mu_lock(&chand->mu);
Craig Tiller577c9b22015-11-02 14:11:15 -0800546 if (initial_metadata == NULL) {
547 if (chand->lb_policy != NULL) {
Craig Tillerab33b482015-11-21 08:11:04 -0800548 grpc_lb_policy_cancel_pick(exec_ctx, chand->lb_policy,
549 connected_subchannel);
Craig Tiller577c9b22015-11-02 14:11:15 -0800550 }
551 for (closure = chand->waiting_for_config_closures.head; closure != NULL;
Craig Tiller804ff712016-05-05 16:25:40 -0700552 closure = closure->next_data.next) {
Craig Tiller577c9b22015-11-02 14:11:15 -0800553 cpa = closure->cb_arg;
Craig Tillerb5585d42015-11-17 07:18:31 -0800554 if (cpa->connected_subchannel == connected_subchannel) {
555 cpa->connected_subchannel = NULL;
Craig Tiller332f1b32016-05-24 13:21:21 -0700556 grpc_exec_ctx_sched(exec_ctx, cpa->on_ready,
557 GRPC_ERROR_CREATE("Pick cancelled"), NULL);
Craig Tiller577c9b22015-11-02 14:11:15 -0800558 }
559 }
Mark D. Rothff4df062016-08-22 15:02:49 -0700560 gpr_mu_unlock(&chand->mu);
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700561 GPR_TIMER_END("pick_subchannel", 0);
Mark D. Roth4c0fe492016-08-31 13:51:55 -0700562 return true;
Craig Tiller577c9b22015-11-02 14:11:15 -0800563 }
564 if (chand->lb_policy != NULL) {
Craig Tiller86c0f8a2015-12-01 20:05:40 -0800565 grpc_lb_policy *lb_policy = chand->lb_policy;
566 int r;
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700567 GRPC_LB_POLICY_REF(lb_policy, "pick_subchannel");
Mark D. Rothff4df062016-08-22 15:02:49 -0700568 gpr_mu_unlock(&chand->mu);
David Garcia Quintas2a50dfe2016-05-31 15:09:12 -0700569 r = grpc_lb_policy_pick(exec_ctx, lb_policy, calld->pollent,
Craig Tiller8c0d96f2016-03-11 14:27:52 -0800570 initial_metadata, initial_metadata_flags,
571 connected_subchannel, on_ready);
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700572 GRPC_LB_POLICY_UNREF(exec_ctx, lb_policy, "pick_subchannel");
573 GPR_TIMER_END("pick_subchannel", 0);
Craig Tiller577c9b22015-11-02 14:11:15 -0800574 return r;
575 }
576 if (chand->resolver != NULL && !chand->started_resolving) {
Mark D. Roth4c0fe492016-08-31 13:51:55 -0700577 chand->started_resolving = true;
Craig Tiller906e3bc2015-11-24 07:31:31 -0800578 GRPC_CHANNEL_STACK_REF(chand->owning_stack, "resolver");
Mark D. Rotha275aea2016-08-23 12:30:45 -0700579 grpc_resolver_next(exec_ctx, chand->resolver, &chand->resolver_result,
Mark D. Rothff4df062016-08-22 15:02:49 -0700580 &chand->on_resolver_result_changed);
Craig Tiller577c9b22015-11-02 14:11:15 -0800581 }
Craig Tiller0eab6972016-04-23 12:59:57 -0700582 if (chand->resolver != NULL) {
583 cpa = gpr_malloc(sizeof(*cpa));
584 cpa->initial_metadata = initial_metadata;
585 cpa->initial_metadata_flags = initial_metadata_flags;
586 cpa->connected_subchannel = connected_subchannel;
587 cpa->on_ready = on_ready;
588 cpa->elem = elem;
589 grpc_closure_init(&cpa->closure, continue_picking, cpa);
Craig Tiller804ff712016-05-05 16:25:40 -0700590 grpc_closure_list_append(&chand->waiting_for_config_closures, &cpa->closure,
591 GRPC_ERROR_NONE);
Craig Tiller0eab6972016-04-23 12:59:57 -0700592 } else {
Craig Tiller332f1b32016-05-24 13:21:21 -0700593 grpc_exec_ctx_sched(exec_ctx, on_ready, GRPC_ERROR_CREATE("Disconnected"),
594 NULL);
Craig Tiller0eab6972016-04-23 12:59:57 -0700595 }
Mark D. Rothff4df062016-08-22 15:02:49 -0700596 gpr_mu_unlock(&chand->mu);
Craig Tillerbfc9adc2016-06-27 13:16:22 -0700597
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700598 GPR_TIMER_END("pick_subchannel", 0);
Mark D. Roth4c0fe492016-08-31 13:51:55 -0700599 return false;
Craig Tiller577c9b22015-11-02 14:11:15 -0800600}
601
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700602// The logic here is fairly complicated, due to (a) the fact that we
603// need to handle the case where we receive the send op before the
604// initial metadata op, and (b) the need for efficiency, especially in
605// the streaming case.
606// TODO(ctiller): Explain this more thoroughly.
607static void cc_start_transport_stream_op(grpc_exec_ctx *exec_ctx,
608 grpc_call_element *elem,
609 grpc_transport_stream_op *op) {
610 call_data *calld = elem->call_data;
611 GRPC_CALL_LOG_OP(GPR_INFO, elem, op);
612 /* try to (atomically) get the call */
613 grpc_subchannel_call *call = GET_CALL(calld);
614 GPR_TIMER_BEGIN("cc_start_transport_stream_op", 0);
615 if (call == CANCELLED_CALL) {
616 grpc_transport_stream_op_finish_with_failure(exec_ctx, op,
617 GRPC_ERROR_CANCELLED);
618 GPR_TIMER_END("cc_start_transport_stream_op", 0);
619 return;
620 }
621 if (call != NULL) {
622 grpc_subchannel_call_process_op(exec_ctx, call, op);
623 GPR_TIMER_END("cc_start_transport_stream_op", 0);
624 return;
625 }
626 /* we failed; lock and figure out what to do */
627 gpr_mu_lock(&calld->mu);
628retry:
629 /* need to recheck that another thread hasn't set the call */
630 call = GET_CALL(calld);
631 if (call == CANCELLED_CALL) {
632 gpr_mu_unlock(&calld->mu);
633 grpc_transport_stream_op_finish_with_failure(exec_ctx, op,
634 GRPC_ERROR_CANCELLED);
635 GPR_TIMER_END("cc_start_transport_stream_op", 0);
636 return;
637 }
638 if (call != NULL) {
639 gpr_mu_unlock(&calld->mu);
640 grpc_subchannel_call_process_op(exec_ctx, call, op);
641 GPR_TIMER_END("cc_start_transport_stream_op", 0);
642 return;
643 }
644 /* if this is a cancellation, then we can raise our cancelled flag */
645 if (op->cancel_error != GRPC_ERROR_NONE) {
646 if (!gpr_atm_rel_cas(&calld->subchannel_call, 0,
647 (gpr_atm)(uintptr_t)CANCELLED_CALL)) {
648 goto retry;
649 } else {
650 switch (calld->creation_phase) {
651 case GRPC_SUBCHANNEL_CALL_HOLDER_NOT_CREATING:
652 fail_locked(exec_ctx, calld, GRPC_ERROR_REF(op->cancel_error));
653 break;
654 case GRPC_SUBCHANNEL_CALL_HOLDER_PICKING_SUBCHANNEL:
655 pick_subchannel(exec_ctx, elem, NULL, 0,
656 &calld->connected_subchannel, NULL);
657 break;
658 }
659 gpr_mu_unlock(&calld->mu);
660 grpc_transport_stream_op_finish_with_failure(exec_ctx, op,
661 GRPC_ERROR_CANCELLED);
662 GPR_TIMER_END("cc_start_transport_stream_op", 0);
663 return;
664 }
665 }
666 /* if we don't have a subchannel, try to get one */
667 if (calld->creation_phase == GRPC_SUBCHANNEL_CALL_HOLDER_NOT_CREATING &&
668 calld->connected_subchannel == NULL &&
669 op->send_initial_metadata != NULL) {
670 calld->creation_phase = GRPC_SUBCHANNEL_CALL_HOLDER_PICKING_SUBCHANNEL;
671 grpc_closure_init(&calld->next_step, subchannel_ready, calld);
672 GRPC_CALL_STACK_REF(calld->owning_call, "pick_subchannel");
673 if (pick_subchannel(
674 exec_ctx, elem, op->send_initial_metadata,
675 op->send_initial_metadata_flags, &calld->connected_subchannel,
676 &calld->next_step)) {
677 calld->creation_phase = GRPC_SUBCHANNEL_CALL_HOLDER_NOT_CREATING;
678 GRPC_CALL_STACK_UNREF(exec_ctx, calld->owning_call, "pick_subchannel");
679 }
680 }
681 /* if we've got a subchannel, then let's ask it to create a call */
682 if (calld->creation_phase == GRPC_SUBCHANNEL_CALL_HOLDER_NOT_CREATING &&
683 calld->connected_subchannel != NULL) {
684 grpc_subchannel_call *subchannel_call = NULL;
685 grpc_error *error = grpc_connected_subchannel_create_call(
686 exec_ctx, calld->connected_subchannel, calld->pollent,
687 &subchannel_call);
688 if (error != GRPC_ERROR_NONE) {
689 subchannel_call = CANCELLED_CALL;
690 fail_locked(exec_ctx, calld, GRPC_ERROR_REF(error));
691 grpc_transport_stream_op_finish_with_failure(exec_ctx, op, error);
692 }
693 gpr_atm_rel_store(&calld->subchannel_call,
694 (gpr_atm)(uintptr_t)subchannel_call);
695 retry_waiting_locked(exec_ctx, calld);
696 goto retry;
697 }
698 /* nothing to be done but wait */
699 add_waiting_locked(calld, op);
700 gpr_mu_unlock(&calld->mu);
701 GPR_TIMER_END("cc_start_transport_stream_op", 0);
702}
703
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800704/* Constructor for call_data */
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700705static grpc_error *cc_init_call_elem(grpc_exec_ctx *exec_ctx,
706 grpc_call_element *elem,
707 grpc_call_element_args *args) {
Mark D. Roth4c0fe492016-08-31 13:51:55 -0700708 call_data *calld = elem->call_data;
709 gpr_atm_rel_store(&calld->subchannel_call, 0);
Mark D. Roth4c0fe492016-08-31 13:51:55 -0700710 gpr_mu_init(&calld->mu);
711 calld->connected_subchannel = NULL;
712 calld->waiting_ops = NULL;
713 calld->waiting_ops_count = 0;
714 calld->waiting_ops_capacity = 0;
715 calld->creation_phase = GRPC_SUBCHANNEL_CALL_HOLDER_NOT_CREATING;
716 calld->owning_call = args->call_stack;
717 calld->pollent = NULL;
Mark D. Roth0badbe82016-06-23 10:15:12 -0700718 return GRPC_ERROR_NONE;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800719}
720
721/* Destructor for call_data */
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700722static void cc_destroy_call_elem(grpc_exec_ctx *exec_ctx,
723 grpc_call_element *elem,
724 const grpc_call_final_info *final_info,
725 void *and_free_memory) {
Mark D. Roth4c0fe492016-08-31 13:51:55 -0700726 call_data *calld = elem->call_data;
727 grpc_subchannel_call *call = GET_CALL(calld);
728 if (call != NULL && call != CANCELLED_CALL) {
729 GRPC_SUBCHANNEL_CALL_UNREF(exec_ctx, call, "client_channel_destroy_call");
730 }
731 GPR_ASSERT(calld->creation_phase == GRPC_SUBCHANNEL_CALL_HOLDER_NOT_CREATING);
732 gpr_mu_destroy(&calld->mu);
733 GPR_ASSERT(calld->waiting_ops_count == 0);
734 gpr_free(calld->waiting_ops);
Craig Tiller2c8063c2016-03-22 22:12:15 -0700735 gpr_free(and_free_memory);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800736}
737
David Garcia Quintasf72eb972016-05-03 18:28:09 -0700738static void cc_set_pollset_or_pollset_set(grpc_exec_ctx *exec_ctx,
739 grpc_call_element *elem,
David Garcia Quintas2a50dfe2016-05-31 15:09:12 -0700740 grpc_polling_entity *pollent) {
Craig Tiller577c9b22015-11-02 14:11:15 -0800741 call_data *calld = elem->call_data;
David Garcia Quintas2a50dfe2016-05-31 15:09:12 -0700742 calld->pollent = pollent;
Craig Tiller577c9b22015-11-02 14:11:15 -0800743}
744
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700745/*************************************************************************
746 * EXPORTED SYMBOLS
747 */
748
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800749const grpc_channel_filter grpc_client_channel_filter = {
Craig Tillerf40df232016-03-25 13:38:14 -0700750 cc_start_transport_stream_op,
751 cc_start_transport_op,
752 sizeof(call_data),
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700753 cc_init_call_elem,
David Garcia Quintas4afce7e2016-04-18 16:25:17 -0700754 cc_set_pollset_or_pollset_set,
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700755 cc_destroy_call_elem,
Craig Tillerf40df232016-03-25 13:38:14 -0700756 sizeof(channel_data),
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700757 cc_init_channel_elem,
758 cc_destroy_channel_elem,
Craig Tillerf40df232016-03-25 13:38:14 -0700759 cc_get_peer,
760 "client-channel",
Craig Tiller87d5b192015-04-16 14:37:57 -0700761};
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800762
Craig Tillera82950e2015-09-22 12:33:20 -0700763void grpc_client_channel_set_resolver(grpc_exec_ctx *exec_ctx,
764 grpc_channel_stack *channel_stack,
765 grpc_resolver *resolver) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800766 /* post construction initialization: set the transport setup pointer */
Craig Tillera82950e2015-09-22 12:33:20 -0700767 grpc_channel_element *elem = grpc_channel_stack_last_element(channel_stack);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800768 channel_data *chand = elem->channel_data;
Mark D. Rothff4df062016-08-22 15:02:49 -0700769 gpr_mu_lock(&chand->mu);
Craig Tillera82950e2015-09-22 12:33:20 -0700770 GPR_ASSERT(!chand->resolver);
Craig Tillerf5f17122015-06-25 08:47:26 -0700771 chand->resolver = resolver;
Craig Tillera82950e2015-09-22 12:33:20 -0700772 GRPC_RESOLVER_REF(resolver, "channel");
773 if (!grpc_closure_list_empty(chand->waiting_for_config_closures) ||
774 chand->exit_idle_when_lb_policy_arrives) {
Mark D. Roth4c0fe492016-08-31 13:51:55 -0700775 chand->started_resolving = true;
Craig Tiller906e3bc2015-11-24 07:31:31 -0800776 GRPC_CHANNEL_STACK_REF(chand->owning_stack, "resolver");
Mark D. Rothff4df062016-08-22 15:02:49 -0700777 grpc_resolver_next(exec_ctx, resolver, &chand->resolver_result,
778 &chand->on_resolver_result_changed);
Craig Tillera82950e2015-09-22 12:33:20 -0700779 }
Mark D. Rothff4df062016-08-22 15:02:49 -0700780 gpr_mu_unlock(&chand->mu);
Craig Tiller190d3602015-02-18 09:23:38 -0800781}
Craig Tiller48cb07c2015-07-15 16:16:15 -0700782
Craig Tillera82950e2015-09-22 12:33:20 -0700783grpc_connectivity_state grpc_client_channel_check_connectivity_state(
784 grpc_exec_ctx *exec_ctx, grpc_channel_element *elem, int try_to_connect) {
Craig Tiller48cb07c2015-07-15 16:16:15 -0700785 channel_data *chand = elem->channel_data;
786 grpc_connectivity_state out;
Mark D. Rothff4df062016-08-22 15:02:49 -0700787 gpr_mu_lock(&chand->mu);
Craig Tiller804ff712016-05-05 16:25:40 -0700788 out = grpc_connectivity_state_check(&chand->state_tracker, NULL);
Craig Tillera82950e2015-09-22 12:33:20 -0700789 if (out == GRPC_CHANNEL_IDLE && try_to_connect) {
790 if (chand->lb_policy != NULL) {
791 grpc_lb_policy_exit_idle(exec_ctx, chand->lb_policy);
792 } else {
Mark D. Roth4c0fe492016-08-31 13:51:55 -0700793 chand->exit_idle_when_lb_policy_arrives = true;
Craig Tillera82950e2015-09-22 12:33:20 -0700794 if (!chand->started_resolving && chand->resolver != NULL) {
Craig Tiller906e3bc2015-11-24 07:31:31 -0800795 GRPC_CHANNEL_STACK_REF(chand->owning_stack, "resolver");
Mark D. Roth4c0fe492016-08-31 13:51:55 -0700796 chand->started_resolving = true;
Mark D. Rotha275aea2016-08-23 12:30:45 -0700797 grpc_resolver_next(exec_ctx, chand->resolver, &chand->resolver_result,
Mark D. Rothff4df062016-08-22 15:02:49 -0700798 &chand->on_resolver_result_changed);
Craig Tillera82950e2015-09-22 12:33:20 -0700799 }
Craig Tiller48cb07c2015-07-15 16:16:15 -0700800 }
Craig Tillera82950e2015-09-22 12:33:20 -0700801 }
Mark D. Rothff4df062016-08-22 15:02:49 -0700802 gpr_mu_unlock(&chand->mu);
Craig Tiller48cb07c2015-07-15 16:16:15 -0700803 return out;
804}
805
Craig Tiller86c99582015-11-25 15:22:26 -0800806typedef struct {
807 channel_data *chand;
808 grpc_pollset *pollset;
809 grpc_closure *on_complete;
810 grpc_closure my_closure;
811} external_connectivity_watcher;
812
Craig Tiller1d881fb2015-12-01 07:39:04 -0800813static void on_external_watch_complete(grpc_exec_ctx *exec_ctx, void *arg,
Craig Tiller804ff712016-05-05 16:25:40 -0700814 grpc_error *error) {
Craig Tiller86c99582015-11-25 15:22:26 -0800815 external_connectivity_watcher *w = arg;
816 grpc_closure *follow_up = w->on_complete;
Craig Tiller69b093b2016-02-25 19:04:07 -0800817 grpc_pollset_set_del_pollset(exec_ctx, w->chand->interested_parties,
Craig Tiller1d881fb2015-12-01 07:39:04 -0800818 w->pollset);
819 GRPC_CHANNEL_STACK_UNREF(exec_ctx, w->chand->owning_stack,
820 "external_connectivity_watcher");
Craig Tiller86c99582015-11-25 15:22:26 -0800821 gpr_free(w);
Craig Tiller804ff712016-05-05 16:25:40 -0700822 follow_up->cb(exec_ctx, follow_up->cb_arg, error);
Craig Tiller86c99582015-11-25 15:22:26 -0800823}
824
Craig Tillera82950e2015-09-22 12:33:20 -0700825void grpc_client_channel_watch_connectivity_state(
Craig Tiller906e3bc2015-11-24 07:31:31 -0800826 grpc_exec_ctx *exec_ctx, grpc_channel_element *elem, grpc_pollset *pollset,
Craig Tillera82950e2015-09-22 12:33:20 -0700827 grpc_connectivity_state *state, grpc_closure *on_complete) {
Craig Tiller48cb07c2015-07-15 16:16:15 -0700828 channel_data *chand = elem->channel_data;
Craig Tiller86c99582015-11-25 15:22:26 -0800829 external_connectivity_watcher *w = gpr_malloc(sizeof(*w));
830 w->chand = chand;
831 w->pollset = pollset;
832 w->on_complete = on_complete;
Craig Tiller69b093b2016-02-25 19:04:07 -0800833 grpc_pollset_set_add_pollset(exec_ctx, chand->interested_parties, pollset);
Craig Tiller86c99582015-11-25 15:22:26 -0800834 grpc_closure_init(&w->my_closure, on_external_watch_complete, w);
Craig Tiller1d881fb2015-12-01 07:39:04 -0800835 GRPC_CHANNEL_STACK_REF(w->chand->owning_stack,
836 "external_connectivity_watcher");
Mark D. Rothff4df062016-08-22 15:02:49 -0700837 gpr_mu_lock(&chand->mu);
Craig Tillera82950e2015-09-22 12:33:20 -0700838 grpc_connectivity_state_notify_on_state_change(
Craig Tiller86c99582015-11-25 15:22:26 -0800839 exec_ctx, &chand->state_tracker, state, &w->my_closure);
Mark D. Rothff4df062016-08-22 15:02:49 -0700840 gpr_mu_unlock(&chand->mu);
Craig Tiller48cb07c2015-07-15 16:16:15 -0700841}