blob: 8f1c821ebb0b35f9463dc1c1c494d0135d78457a [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;
David Garcia Quintasd1a47f12016-09-02 12:46:44 +0200397
398 grpc_linked_mdelem lb_token_mdelem;
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700399} call_data;
400
401static void add_waiting_locked(call_data *calld, grpc_transport_stream_op *op) {
402 GPR_TIMER_BEGIN("add_waiting_locked", 0);
403 if (calld->waiting_ops_count == calld->waiting_ops_capacity) {
404 calld->waiting_ops_capacity = GPR_MAX(3, 2 * calld->waiting_ops_capacity);
405 calld->waiting_ops =
406 gpr_realloc(calld->waiting_ops,
407 calld->waiting_ops_capacity * sizeof(*calld->waiting_ops));
408 }
409 calld->waiting_ops[calld->waiting_ops_count++] = *op;
410 GPR_TIMER_END("add_waiting_locked", 0);
411}
412
413static void fail_locked(grpc_exec_ctx *exec_ctx, call_data *calld,
414 grpc_error *error) {
415 size_t i;
416 for (i = 0; i < calld->waiting_ops_count; i++) {
417 grpc_transport_stream_op_finish_with_failure(
418 exec_ctx, &calld->waiting_ops[i], GRPC_ERROR_REF(error));
419 }
420 calld->waiting_ops_count = 0;
421 GRPC_ERROR_UNREF(error);
422}
423
424typedef struct {
425 grpc_transport_stream_op *ops;
426 size_t nops;
427 grpc_subchannel_call *call;
428} retry_ops_args;
429
430static void retry_ops(grpc_exec_ctx *exec_ctx, void *args, grpc_error *error) {
431 retry_ops_args *a = args;
432 size_t i;
433 for (i = 0; i < a->nops; i++) {
434 grpc_subchannel_call_process_op(exec_ctx, a->call, &a->ops[i]);
435 }
436 GRPC_SUBCHANNEL_CALL_UNREF(exec_ctx, a->call, "retry_ops");
437 gpr_free(a->ops);
438 gpr_free(a);
439}
440
441static void retry_waiting_locked(grpc_exec_ctx *exec_ctx, call_data *calld) {
442 retry_ops_args *a = gpr_malloc(sizeof(*a));
443 a->ops = calld->waiting_ops;
444 a->nops = calld->waiting_ops_count;
445 a->call = GET_CALL(calld);
446 if (a->call == CANCELLED_CALL) {
447 gpr_free(a);
448 fail_locked(exec_ctx, calld, GRPC_ERROR_CANCELLED);
449 return;
450 }
451 calld->waiting_ops = NULL;
452 calld->waiting_ops_count = 0;
453 calld->waiting_ops_capacity = 0;
454 GRPC_SUBCHANNEL_CALL_REF(a->call, "retry_ops");
455 grpc_exec_ctx_sched(exec_ctx, grpc_closure_create(retry_ops, a),
456 GRPC_ERROR_NONE, NULL);
457}
458
459static void subchannel_ready(grpc_exec_ctx *exec_ctx, void *arg,
460 grpc_error *error) {
461 call_data *calld = arg;
462 gpr_mu_lock(&calld->mu);
463 GPR_ASSERT(calld->creation_phase ==
464 GRPC_SUBCHANNEL_CALL_HOLDER_PICKING_SUBCHANNEL);
465 calld->creation_phase = GRPC_SUBCHANNEL_CALL_HOLDER_NOT_CREATING;
466 if (calld->connected_subchannel == NULL) {
467 gpr_atm_no_barrier_store(&calld->subchannel_call, 1);
468 fail_locked(exec_ctx, calld, GRPC_ERROR_CREATE_REFERENCING(
469 "Failed to create subchannel", &error, 1));
470 } else if (1 == gpr_atm_acq_load(&calld->subchannel_call)) {
471 /* already cancelled before subchannel became ready */
472 fail_locked(exec_ctx, calld,
473 GRPC_ERROR_CREATE_REFERENCING(
474 "Cancelled before creating subchannel", &error, 1));
475 } else {
476 grpc_subchannel_call *subchannel_call = NULL;
477 grpc_error *new_error = grpc_connected_subchannel_create_call(
478 exec_ctx, calld->connected_subchannel, calld->pollent,
479 &subchannel_call);
480 if (new_error != GRPC_ERROR_NONE) {
481 new_error = grpc_error_add_child(new_error, error);
482 subchannel_call = CANCELLED_CALL;
483 fail_locked(exec_ctx, calld, new_error);
484 }
485 gpr_atm_rel_store(&calld->subchannel_call,
486 (gpr_atm)(uintptr_t)subchannel_call);
487 retry_waiting_locked(exec_ctx, calld);
488 }
489 gpr_mu_unlock(&calld->mu);
490 GRPC_CALL_STACK_UNREF(exec_ctx, calld->owning_call, "pick_subchannel");
491}
492
493static char *cc_get_peer(grpc_exec_ctx *exec_ctx, grpc_call_element *elem) {
494 call_data *calld = elem->call_data;
495 grpc_subchannel_call *subchannel_call = GET_CALL(calld);
496 if (subchannel_call == NULL || subchannel_call == CANCELLED_CALL) {
497 return NULL;
498 } else {
499 return grpc_subchannel_call_get_peer(exec_ctx, subchannel_call);
500 }
501}
502
Craig Tiller577c9b22015-11-02 14:11:15 -0800503typedef struct {
504 grpc_metadata_batch *initial_metadata;
Craig Tiller8c0d96f2016-03-11 14:27:52 -0800505 uint32_t initial_metadata_flags;
Craig Tillerb5585d42015-11-17 07:18:31 -0800506 grpc_connected_subchannel **connected_subchannel;
Craig Tiller577c9b22015-11-02 14:11:15 -0800507 grpc_closure *on_ready;
508 grpc_call_element *elem;
509 grpc_closure closure;
510} continue_picking_args;
511
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700512static bool pick_subchannel(grpc_exec_ctx *exec_ctx, grpc_call_element *elem,
513 grpc_metadata_batch *initial_metadata,
514 uint32_t initial_metadata_flags,
515 grpc_connected_subchannel **connected_subchannel,
516 grpc_closure *on_ready);
Craig Tiller577c9b22015-11-02 14:11:15 -0800517
Craig Tiller804ff712016-05-05 16:25:40 -0700518static void continue_picking(grpc_exec_ctx *exec_ctx, void *arg,
519 grpc_error *error) {
Craig Tiller577c9b22015-11-02 14:11:15 -0800520 continue_picking_args *cpa = arg;
Craig Tiller0ede5452016-04-23 12:21:45 -0700521 if (cpa->connected_subchannel == NULL) {
Craig Tiller577c9b22015-11-02 14:11:15 -0800522 /* cancelled, do nothing */
Craig Tiller804ff712016-05-05 16:25:40 -0700523 } else if (error != GRPC_ERROR_NONE) {
Craig Tiller332f1b32016-05-24 13:21:21 -0700524 grpc_exec_ctx_sched(exec_ctx, cpa->on_ready, GRPC_ERROR_REF(error), NULL);
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700525 } else if (pick_subchannel(exec_ctx, cpa->elem, cpa->initial_metadata,
526 cpa->initial_metadata_flags,
527 cpa->connected_subchannel, cpa->on_ready)) {
Craig Tiller332f1b32016-05-24 13:21:21 -0700528 grpc_exec_ctx_sched(exec_ctx, cpa->on_ready, GRPC_ERROR_NONE, NULL);
Craig Tiller577c9b22015-11-02 14:11:15 -0800529 }
530 gpr_free(cpa);
531}
532
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700533static bool pick_subchannel(grpc_exec_ctx *exec_ctx, grpc_call_element *elem,
534 grpc_metadata_batch *initial_metadata,
535 uint32_t initial_metadata_flags,
536 grpc_connected_subchannel **connected_subchannel,
537 grpc_closure *on_ready) {
538 GPR_TIMER_BEGIN("pick_subchannel", 0);
Craig Tillerbfc9adc2016-06-27 13:16:22 -0700539
Craig Tiller577c9b22015-11-02 14:11:15 -0800540 channel_data *chand = elem->channel_data;
541 call_data *calld = elem->call_data;
542 continue_picking_args *cpa;
543 grpc_closure *closure;
544
Craig Tillerb5585d42015-11-17 07:18:31 -0800545 GPR_ASSERT(connected_subchannel);
Craig Tiller577c9b22015-11-02 14:11:15 -0800546
Mark D. Rothff4df062016-08-22 15:02:49 -0700547 gpr_mu_lock(&chand->mu);
Craig Tiller577c9b22015-11-02 14:11:15 -0800548 if (initial_metadata == NULL) {
549 if (chand->lb_policy != NULL) {
Craig Tillerab33b482015-11-21 08:11:04 -0800550 grpc_lb_policy_cancel_pick(exec_ctx, chand->lb_policy,
551 connected_subchannel);
Craig Tiller577c9b22015-11-02 14:11:15 -0800552 }
553 for (closure = chand->waiting_for_config_closures.head; closure != NULL;
Craig Tiller804ff712016-05-05 16:25:40 -0700554 closure = closure->next_data.next) {
Craig Tiller577c9b22015-11-02 14:11:15 -0800555 cpa = closure->cb_arg;
Craig Tillerb5585d42015-11-17 07:18:31 -0800556 if (cpa->connected_subchannel == connected_subchannel) {
557 cpa->connected_subchannel = NULL;
Craig Tiller332f1b32016-05-24 13:21:21 -0700558 grpc_exec_ctx_sched(exec_ctx, cpa->on_ready,
559 GRPC_ERROR_CREATE("Pick cancelled"), NULL);
Craig Tiller577c9b22015-11-02 14:11:15 -0800560 }
561 }
Mark D. Rothff4df062016-08-22 15:02:49 -0700562 gpr_mu_unlock(&chand->mu);
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700563 GPR_TIMER_END("pick_subchannel", 0);
Mark D. Roth4c0fe492016-08-31 13:51:55 -0700564 return true;
Craig Tiller577c9b22015-11-02 14:11:15 -0800565 }
566 if (chand->lb_policy != NULL) {
Craig Tiller86c0f8a2015-12-01 20:05:40 -0800567 grpc_lb_policy *lb_policy = chand->lb_policy;
568 int r;
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700569 GRPC_LB_POLICY_REF(lb_policy, "pick_subchannel");
Mark D. Rothff4df062016-08-22 15:02:49 -0700570 gpr_mu_unlock(&chand->mu);
David Garcia Quintas8aace512016-08-15 14:55:12 -0700571 const grpc_lb_policy_pick_args inputs = {calld->pollent, initial_metadata,
David Garcia Quintas5b0e9462016-08-15 19:38:39 -0700572 initial_metadata_flags,
573 &calld->lb_token_mdelem};
David Garcia Quintas8aace512016-08-15 14:55:12 -0700574 r = grpc_lb_policy_pick(exec_ctx, lb_policy, &inputs, connected_subchannel,
575 on_ready);
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700576 GRPC_LB_POLICY_UNREF(exec_ctx, lb_policy, "pick_subchannel");
577 GPR_TIMER_END("pick_subchannel", 0);
Craig Tiller577c9b22015-11-02 14:11:15 -0800578 return r;
579 }
580 if (chand->resolver != NULL && !chand->started_resolving) {
Mark D. Roth4c0fe492016-08-31 13:51:55 -0700581 chand->started_resolving = true;
Craig Tiller906e3bc2015-11-24 07:31:31 -0800582 GRPC_CHANNEL_STACK_REF(chand->owning_stack, "resolver");
Mark D. Rotha275aea2016-08-23 12:30:45 -0700583 grpc_resolver_next(exec_ctx, chand->resolver, &chand->resolver_result,
Mark D. Rothff4df062016-08-22 15:02:49 -0700584 &chand->on_resolver_result_changed);
Craig Tiller577c9b22015-11-02 14:11:15 -0800585 }
Craig Tiller0eab6972016-04-23 12:59:57 -0700586 if (chand->resolver != NULL) {
587 cpa = gpr_malloc(sizeof(*cpa));
588 cpa->initial_metadata = initial_metadata;
589 cpa->initial_metadata_flags = initial_metadata_flags;
590 cpa->connected_subchannel = connected_subchannel;
591 cpa->on_ready = on_ready;
592 cpa->elem = elem;
593 grpc_closure_init(&cpa->closure, continue_picking, cpa);
Craig Tiller804ff712016-05-05 16:25:40 -0700594 grpc_closure_list_append(&chand->waiting_for_config_closures, &cpa->closure,
595 GRPC_ERROR_NONE);
Craig Tiller0eab6972016-04-23 12:59:57 -0700596 } else {
Craig Tiller332f1b32016-05-24 13:21:21 -0700597 grpc_exec_ctx_sched(exec_ctx, on_ready, GRPC_ERROR_CREATE("Disconnected"),
598 NULL);
Craig Tiller0eab6972016-04-23 12:59:57 -0700599 }
Mark D. Rothff4df062016-08-22 15:02:49 -0700600 gpr_mu_unlock(&chand->mu);
Craig Tillerbfc9adc2016-06-27 13:16:22 -0700601
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700602 GPR_TIMER_END("pick_subchannel", 0);
Mark D. Roth4c0fe492016-08-31 13:51:55 -0700603 return false;
Craig Tiller577c9b22015-11-02 14:11:15 -0800604}
605
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700606// The logic here is fairly complicated, due to (a) the fact that we
607// need to handle the case where we receive the send op before the
608// initial metadata op, and (b) the need for efficiency, especially in
609// the streaming case.
610// TODO(ctiller): Explain this more thoroughly.
611static void cc_start_transport_stream_op(grpc_exec_ctx *exec_ctx,
612 grpc_call_element *elem,
613 grpc_transport_stream_op *op) {
614 call_data *calld = elem->call_data;
615 GRPC_CALL_LOG_OP(GPR_INFO, elem, op);
616 /* try to (atomically) get the call */
617 grpc_subchannel_call *call = GET_CALL(calld);
618 GPR_TIMER_BEGIN("cc_start_transport_stream_op", 0);
619 if (call == CANCELLED_CALL) {
620 grpc_transport_stream_op_finish_with_failure(exec_ctx, op,
621 GRPC_ERROR_CANCELLED);
622 GPR_TIMER_END("cc_start_transport_stream_op", 0);
623 return;
624 }
625 if (call != NULL) {
626 grpc_subchannel_call_process_op(exec_ctx, call, op);
627 GPR_TIMER_END("cc_start_transport_stream_op", 0);
628 return;
629 }
630 /* we failed; lock and figure out what to do */
631 gpr_mu_lock(&calld->mu);
632retry:
633 /* need to recheck that another thread hasn't set the call */
634 call = GET_CALL(calld);
635 if (call == CANCELLED_CALL) {
636 gpr_mu_unlock(&calld->mu);
637 grpc_transport_stream_op_finish_with_failure(exec_ctx, op,
638 GRPC_ERROR_CANCELLED);
639 GPR_TIMER_END("cc_start_transport_stream_op", 0);
640 return;
641 }
642 if (call != NULL) {
643 gpr_mu_unlock(&calld->mu);
644 grpc_subchannel_call_process_op(exec_ctx, call, op);
645 GPR_TIMER_END("cc_start_transport_stream_op", 0);
646 return;
647 }
648 /* if this is a cancellation, then we can raise our cancelled flag */
649 if (op->cancel_error != GRPC_ERROR_NONE) {
650 if (!gpr_atm_rel_cas(&calld->subchannel_call, 0,
651 (gpr_atm)(uintptr_t)CANCELLED_CALL)) {
652 goto retry;
653 } else {
654 switch (calld->creation_phase) {
655 case GRPC_SUBCHANNEL_CALL_HOLDER_NOT_CREATING:
656 fail_locked(exec_ctx, calld, GRPC_ERROR_REF(op->cancel_error));
657 break;
658 case GRPC_SUBCHANNEL_CALL_HOLDER_PICKING_SUBCHANNEL:
Mark D. Rothd4c0f552016-09-01 09:25:32 -0700659 pick_subchannel(exec_ctx, elem, NULL, 0, &calld->connected_subchannel,
660 NULL);
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700661 break;
662 }
663 gpr_mu_unlock(&calld->mu);
664 grpc_transport_stream_op_finish_with_failure(exec_ctx, op,
665 GRPC_ERROR_CANCELLED);
666 GPR_TIMER_END("cc_start_transport_stream_op", 0);
667 return;
668 }
669 }
670 /* if we don't have a subchannel, try to get one */
671 if (calld->creation_phase == GRPC_SUBCHANNEL_CALL_HOLDER_NOT_CREATING &&
672 calld->connected_subchannel == NULL &&
673 op->send_initial_metadata != NULL) {
674 calld->creation_phase = GRPC_SUBCHANNEL_CALL_HOLDER_PICKING_SUBCHANNEL;
675 grpc_closure_init(&calld->next_step, subchannel_ready, calld);
676 GRPC_CALL_STACK_REF(calld->owning_call, "pick_subchannel");
Mark D. Rothd4c0f552016-09-01 09:25:32 -0700677 if (pick_subchannel(exec_ctx, elem, op->send_initial_metadata,
678 op->send_initial_metadata_flags,
679 &calld->connected_subchannel, &calld->next_step)) {
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700680 calld->creation_phase = GRPC_SUBCHANNEL_CALL_HOLDER_NOT_CREATING;
681 GRPC_CALL_STACK_UNREF(exec_ctx, calld->owning_call, "pick_subchannel");
682 }
683 }
684 /* if we've got a subchannel, then let's ask it to create a call */
685 if (calld->creation_phase == GRPC_SUBCHANNEL_CALL_HOLDER_NOT_CREATING &&
686 calld->connected_subchannel != NULL) {
687 grpc_subchannel_call *subchannel_call = NULL;
688 grpc_error *error = grpc_connected_subchannel_create_call(
689 exec_ctx, calld->connected_subchannel, calld->pollent,
690 &subchannel_call);
691 if (error != GRPC_ERROR_NONE) {
692 subchannel_call = CANCELLED_CALL;
693 fail_locked(exec_ctx, calld, GRPC_ERROR_REF(error));
694 grpc_transport_stream_op_finish_with_failure(exec_ctx, op, error);
695 }
696 gpr_atm_rel_store(&calld->subchannel_call,
697 (gpr_atm)(uintptr_t)subchannel_call);
698 retry_waiting_locked(exec_ctx, calld);
699 goto retry;
700 }
701 /* nothing to be done but wait */
702 add_waiting_locked(calld, op);
703 gpr_mu_unlock(&calld->mu);
704 GPR_TIMER_END("cc_start_transport_stream_op", 0);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800705}
706
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800707/* Constructor for call_data */
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700708static grpc_error *cc_init_call_elem(grpc_exec_ctx *exec_ctx,
709 grpc_call_element *elem,
710 grpc_call_element_args *args) {
Mark D. Roth4c0fe492016-08-31 13:51:55 -0700711 call_data *calld = elem->call_data;
712 gpr_atm_rel_store(&calld->subchannel_call, 0);
Mark D. Roth4c0fe492016-08-31 13:51:55 -0700713 gpr_mu_init(&calld->mu);
714 calld->connected_subchannel = NULL;
715 calld->waiting_ops = NULL;
716 calld->waiting_ops_count = 0;
717 calld->waiting_ops_capacity = 0;
718 calld->creation_phase = GRPC_SUBCHANNEL_CALL_HOLDER_NOT_CREATING;
719 calld->owning_call = args->call_stack;
720 calld->pollent = NULL;
Mark D. Roth0badbe82016-06-23 10:15:12 -0700721 return GRPC_ERROR_NONE;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800722}
723
724/* Destructor for call_data */
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700725static void cc_destroy_call_elem(grpc_exec_ctx *exec_ctx,
726 grpc_call_element *elem,
727 const grpc_call_final_info *final_info,
728 void *and_free_memory) {
Mark D. Roth4c0fe492016-08-31 13:51:55 -0700729 call_data *calld = elem->call_data;
730 grpc_subchannel_call *call = GET_CALL(calld);
731 if (call != NULL && call != CANCELLED_CALL) {
732 GRPC_SUBCHANNEL_CALL_UNREF(exec_ctx, call, "client_channel_destroy_call");
733 }
734 GPR_ASSERT(calld->creation_phase == GRPC_SUBCHANNEL_CALL_HOLDER_NOT_CREATING);
735 gpr_mu_destroy(&calld->mu);
736 GPR_ASSERT(calld->waiting_ops_count == 0);
737 gpr_free(calld->waiting_ops);
Craig Tiller2c8063c2016-03-22 22:12:15 -0700738 gpr_free(and_free_memory);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800739}
740
David Garcia Quintasf72eb972016-05-03 18:28:09 -0700741static void cc_set_pollset_or_pollset_set(grpc_exec_ctx *exec_ctx,
742 grpc_call_element *elem,
David Garcia Quintas2a50dfe2016-05-31 15:09:12 -0700743 grpc_polling_entity *pollent) {
Craig Tiller577c9b22015-11-02 14:11:15 -0800744 call_data *calld = elem->call_data;
David Garcia Quintas2a50dfe2016-05-31 15:09:12 -0700745 calld->pollent = pollent;
Craig Tiller577c9b22015-11-02 14:11:15 -0800746}
747
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700748/*************************************************************************
749 * EXPORTED SYMBOLS
750 */
751
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800752const grpc_channel_filter grpc_client_channel_filter = {
Craig Tillerf40df232016-03-25 13:38:14 -0700753 cc_start_transport_stream_op,
754 cc_start_transport_op,
755 sizeof(call_data),
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700756 cc_init_call_elem,
David Garcia Quintas4afce7e2016-04-18 16:25:17 -0700757 cc_set_pollset_or_pollset_set,
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700758 cc_destroy_call_elem,
Craig Tillerf40df232016-03-25 13:38:14 -0700759 sizeof(channel_data),
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700760 cc_init_channel_elem,
761 cc_destroy_channel_elem,
Craig Tillerf40df232016-03-25 13:38:14 -0700762 cc_get_peer,
763 "client-channel",
Craig Tiller87d5b192015-04-16 14:37:57 -0700764};
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800765
Craig Tillera82950e2015-09-22 12:33:20 -0700766void grpc_client_channel_set_resolver(grpc_exec_ctx *exec_ctx,
767 grpc_channel_stack *channel_stack,
768 grpc_resolver *resolver) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800769 /* post construction initialization: set the transport setup pointer */
Craig Tillera82950e2015-09-22 12:33:20 -0700770 grpc_channel_element *elem = grpc_channel_stack_last_element(channel_stack);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800771 channel_data *chand = elem->channel_data;
Mark D. Rothff4df062016-08-22 15:02:49 -0700772 gpr_mu_lock(&chand->mu);
Craig Tillera82950e2015-09-22 12:33:20 -0700773 GPR_ASSERT(!chand->resolver);
Craig Tillerf5f17122015-06-25 08:47:26 -0700774 chand->resolver = resolver;
Craig Tillera82950e2015-09-22 12:33:20 -0700775 GRPC_RESOLVER_REF(resolver, "channel");
776 if (!grpc_closure_list_empty(chand->waiting_for_config_closures) ||
777 chand->exit_idle_when_lb_policy_arrives) {
Mark D. Roth4c0fe492016-08-31 13:51:55 -0700778 chand->started_resolving = true;
Craig Tiller906e3bc2015-11-24 07:31:31 -0800779 GRPC_CHANNEL_STACK_REF(chand->owning_stack, "resolver");
Mark D. Rothff4df062016-08-22 15:02:49 -0700780 grpc_resolver_next(exec_ctx, resolver, &chand->resolver_result,
781 &chand->on_resolver_result_changed);
Craig Tillera82950e2015-09-22 12:33:20 -0700782 }
Mark D. Rothff4df062016-08-22 15:02:49 -0700783 gpr_mu_unlock(&chand->mu);
Craig Tiller190d3602015-02-18 09:23:38 -0800784}
Craig Tiller48cb07c2015-07-15 16:16:15 -0700785
Craig Tillera82950e2015-09-22 12:33:20 -0700786grpc_connectivity_state grpc_client_channel_check_connectivity_state(
787 grpc_exec_ctx *exec_ctx, grpc_channel_element *elem, int try_to_connect) {
Craig Tiller48cb07c2015-07-15 16:16:15 -0700788 channel_data *chand = elem->channel_data;
789 grpc_connectivity_state out;
Mark D. Rothff4df062016-08-22 15:02:49 -0700790 gpr_mu_lock(&chand->mu);
Craig Tiller804ff712016-05-05 16:25:40 -0700791 out = grpc_connectivity_state_check(&chand->state_tracker, NULL);
Craig Tillera82950e2015-09-22 12:33:20 -0700792 if (out == GRPC_CHANNEL_IDLE && try_to_connect) {
793 if (chand->lb_policy != NULL) {
794 grpc_lb_policy_exit_idle(exec_ctx, chand->lb_policy);
795 } else {
Mark D. Roth4c0fe492016-08-31 13:51:55 -0700796 chand->exit_idle_when_lb_policy_arrives = true;
Craig Tillera82950e2015-09-22 12:33:20 -0700797 if (!chand->started_resolving && chand->resolver != NULL) {
Craig Tiller906e3bc2015-11-24 07:31:31 -0800798 GRPC_CHANNEL_STACK_REF(chand->owning_stack, "resolver");
Mark D. Roth4c0fe492016-08-31 13:51:55 -0700799 chand->started_resolving = true;
Mark D. Rotha275aea2016-08-23 12:30:45 -0700800 grpc_resolver_next(exec_ctx, chand->resolver, &chand->resolver_result,
Mark D. Rothff4df062016-08-22 15:02:49 -0700801 &chand->on_resolver_result_changed);
Craig Tillera82950e2015-09-22 12:33:20 -0700802 }
Craig Tiller48cb07c2015-07-15 16:16:15 -0700803 }
Craig Tillera82950e2015-09-22 12:33:20 -0700804 }
Mark D. Rothff4df062016-08-22 15:02:49 -0700805 gpr_mu_unlock(&chand->mu);
Craig Tiller48cb07c2015-07-15 16:16:15 -0700806 return out;
807}
808
Craig Tiller86c99582015-11-25 15:22:26 -0800809typedef struct {
810 channel_data *chand;
811 grpc_pollset *pollset;
812 grpc_closure *on_complete;
813 grpc_closure my_closure;
814} external_connectivity_watcher;
815
Craig Tiller1d881fb2015-12-01 07:39:04 -0800816static void on_external_watch_complete(grpc_exec_ctx *exec_ctx, void *arg,
Craig Tiller804ff712016-05-05 16:25:40 -0700817 grpc_error *error) {
Craig Tiller86c99582015-11-25 15:22:26 -0800818 external_connectivity_watcher *w = arg;
819 grpc_closure *follow_up = w->on_complete;
Craig Tiller69b093b2016-02-25 19:04:07 -0800820 grpc_pollset_set_del_pollset(exec_ctx, w->chand->interested_parties,
Craig Tiller1d881fb2015-12-01 07:39:04 -0800821 w->pollset);
822 GRPC_CHANNEL_STACK_UNREF(exec_ctx, w->chand->owning_stack,
823 "external_connectivity_watcher");
Craig Tiller86c99582015-11-25 15:22:26 -0800824 gpr_free(w);
Craig Tiller804ff712016-05-05 16:25:40 -0700825 follow_up->cb(exec_ctx, follow_up->cb_arg, error);
Craig Tiller86c99582015-11-25 15:22:26 -0800826}
827
Craig Tillera82950e2015-09-22 12:33:20 -0700828void grpc_client_channel_watch_connectivity_state(
Craig Tiller906e3bc2015-11-24 07:31:31 -0800829 grpc_exec_ctx *exec_ctx, grpc_channel_element *elem, grpc_pollset *pollset,
Craig Tillera82950e2015-09-22 12:33:20 -0700830 grpc_connectivity_state *state, grpc_closure *on_complete) {
Craig Tiller48cb07c2015-07-15 16:16:15 -0700831 channel_data *chand = elem->channel_data;
Craig Tiller86c99582015-11-25 15:22:26 -0800832 external_connectivity_watcher *w = gpr_malloc(sizeof(*w));
833 w->chand = chand;
834 w->pollset = pollset;
835 w->on_complete = on_complete;
Craig Tiller69b093b2016-02-25 19:04:07 -0800836 grpc_pollset_set_add_pollset(exec_ctx, chand->interested_parties, pollset);
Craig Tiller86c99582015-11-25 15:22:26 -0800837 grpc_closure_init(&w->my_closure, on_external_watch_complete, w);
Craig Tiller1d881fb2015-12-01 07:39:04 -0800838 GRPC_CHANNEL_STACK_REF(w->chand->owning_stack,
839 "external_connectivity_watcher");
Mark D. Rothff4df062016-08-22 15:02:49 -0700840 gpr_mu_lock(&chand->mu);
Craig Tillera82950e2015-09-22 12:33:20 -0700841 grpc_connectivity_state_notify_on_state_change(
Craig Tiller86c99582015-11-25 15:22:26 -0800842 exec_ctx, &chand->state_tracker, state, &w->my_closure);
Mark D. Rothff4df062016-08-22 15:02:49 -0700843 gpr_mu_unlock(&chand->mu);
Craig Tiller48cb07c2015-07-15 16:16:15 -0700844}