blob: 2c6867e76b902f9632400f29918751ce0b099ed9 [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. Roth0e48a9a2016-09-08 14:14:39 -070045#include "src/core/ext/client_config/lb_policy_registry.h"
Mark D. Roth046cf762016-09-26 11:13:51 -070046#include "src/core/ext/client_config/method_config.h"
Mark D. Roth4c0fe492016-08-31 13:51:55 -070047#include "src/core/ext/client_config/subchannel.h"
Craig Tiller9533d042016-03-25 17:11:06 -070048#include "src/core/lib/channel/channel_args.h"
49#include "src/core/lib/channel/connected_channel.h"
Mark D. Roth72f6da82016-09-02 13:42:38 -070050#include "src/core/lib/channel/deadline_filter.h"
Craig Tiller9533d042016-03-25 17:11:06 -070051#include "src/core/lib/iomgr/iomgr.h"
Mark D. Roth4c0fe492016-08-31 13:51:55 -070052#include "src/core/lib/iomgr/polling_entity.h"
Craig Tiller9533d042016-03-25 17:11:06 -070053#include "src/core/lib/profiling/timers.h"
54#include "src/core/lib/support/string.h"
55#include "src/core/lib/surface/channel.h"
56#include "src/core/lib/transport/connectivity_state.h"
Mark D. Roth9fe284e2016-09-12 11:22:27 -070057#include "src/core/lib/transport/metadata.h"
58#include "src/core/lib/transport/metadata_batch.h"
59#include "src/core/lib/transport/static_metadata.h"
Craig Tiller8910ac62015-10-08 16:49:15 -070060
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080061/* Client channel implementation */
62
Mark D. Roth9d480942016-10-19 14:18:05 -070063typedef enum {
64 WAIT_FOR_READY_UNSET,
65 WAIT_FOR_READY_FALSE,
66 WAIT_FOR_READY_TRUE
67} wait_for_ready_value;
68
69typedef struct method_parameters {
70 gpr_timespec timeout;
71 wait_for_ready_value wait_for_ready;
72} method_parameters;
73
74static void *method_parameters_copy(void *value) {
75 void *new_value = gpr_malloc(sizeof(method_parameters));
76 memcpy(new_value, value, sizeof(method_parameters));
77 return new_value;
78}
79
80static int method_parameters_cmp(void *value1, void *value2) {
81 const method_parameters *v1 = value1;
82 const method_parameters *v2 = value2;
83 const int retval = gpr_time_cmp(v1->timeout, v2->timeout);
84 if (retval != 0) return retval;
85 if (v1->wait_for_ready > v2->wait_for_ready) return 1;
86 if (v1->wait_for_ready < v2->wait_for_ready) return -1;
87 return 0;
88}
89
90static const grpc_mdstr_hash_table_vtable method_parameters_vtable = {
91 gpr_free, method_parameters_copy, method_parameters_cmp};
92
93static void *method_config_convert_value(
94 const grpc_method_config *method_config) {
95 method_parameters *value = gpr_malloc(sizeof(method_parameters));
96 const gpr_timespec *timeout = grpc_method_config_get_timeout(method_config);
97 value->timeout = timeout != NULL ? *timeout : gpr_time_0(GPR_TIMESPAN);
98 const bool *wait_for_ready =
99 grpc_method_config_get_wait_for_ready(method_config);
100 value->wait_for_ready =
101 wait_for_ready == NULL
102 ? WAIT_FOR_READY_UNSET
103 : (wait_for_ready ? WAIT_FOR_READY_TRUE : WAIT_FOR_READY_FALSE);
104 return value;
105}
106
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700107/*************************************************************************
108 * CHANNEL-WIDE FUNCTIONS
109 */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800110
Craig Tiller800dacb2015-10-06 09:10:26 -0700111typedef struct client_channel_channel_data {
Craig Tillerf5f17122015-06-25 08:47:26 -0700112 /** resolver for this channel */
113 grpc_resolver *resolver;
Craig Tiller20a3c352015-08-05 08:39:50 -0700114 /** have we started resolving this channel */
Mark D. Roth4c0fe492016-08-31 13:51:55 -0700115 bool started_resolving;
Mark D. Roth0e48a9a2016-09-08 14:14:39 -0700116 /** client channel factory */
117 grpc_client_channel_factory *client_channel_factory;
Craig Tillerf5f17122015-06-25 08:47:26 -0700118
Mark D. Roth046cf762016-09-26 11:13:51 -0700119 /** mutex protecting all variables below in this data structure */
Mark D. Rothff4df062016-08-22 15:02:49 -0700120 gpr_mu mu;
Mark D. Roth046cf762016-09-26 11:13:51 -0700121 /** currently active load balancer */
Craig Tillerf5f17122015-06-25 08:47:26 -0700122 grpc_lb_policy *lb_policy;
Mark D. Roth9d480942016-10-19 14:18:05 -0700123 /** maps method names to method_parameters structs */
124 grpc_mdstr_hash_table *method_params_table;
Mark D. Roth046cf762016-09-26 11:13:51 -0700125 /** incoming resolver result - set by resolver.next() */
126 grpc_resolver_result *resolver_result;
Craig Tiller3f475422015-06-25 10:43:05 -0700127 /** a list of closures that are all waiting for config to come in */
Craig Tillerd9ccbbf2015-09-22 09:30:00 -0700128 grpc_closure_list waiting_for_config_closures;
Craig Tiller3f475422015-06-25 10:43:05 -0700129 /** resolver callback */
Mark D. Rothff4df062016-08-22 15:02:49 -0700130 grpc_closure on_resolver_result_changed;
Craig Tiller3f475422015-06-25 10:43:05 -0700131 /** connectivity state being tracked */
Craig Tillerca3e9d32015-06-27 18:37:27 -0700132 grpc_connectivity_state_tracker state_tracker;
Craig Tiller48cb07c2015-07-15 16:16:15 -0700133 /** when an lb_policy arrives, should we try to exit idle */
Mark D. Roth4c0fe492016-08-31 13:51:55 -0700134 bool exit_idle_when_lb_policy_arrives;
Craig Tiller906e3bc2015-11-24 07:31:31 -0800135 /** owning stack */
136 grpc_channel_stack *owning_stack;
Craig Tiller69b093b2016-02-25 19:04:07 -0800137 /** interested parties (owned) */
138 grpc_pollset_set *interested_parties;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800139} channel_data;
140
Craig Tillerd6c98df2015-08-18 09:33:44 -0700141/** We create one watcher for each new lb_policy that is returned from a
Mark D. Roth4c0fe492016-08-31 13:51:55 -0700142 resolver, to watch for state changes from the lb_policy. When a state
143 change is seen, we update the channel, and create a new watcher. */
Craig Tillera82950e2015-09-22 12:33:20 -0700144typedef struct {
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700145 channel_data *chand;
Craig Tiller33825112015-09-18 07:44:19 -0700146 grpc_closure on_changed;
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700147 grpc_connectivity_state state;
148 grpc_lb_policy *lb_policy;
149} lb_policy_connectivity_watcher;
150
Craig Tillera82950e2015-09-22 12:33:20 -0700151static void watch_lb_policy(grpc_exec_ctx *exec_ctx, channel_data *chand,
152 grpc_lb_policy *lb_policy,
153 grpc_connectivity_state current_state);
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700154
Craig Tiller8c0d96f2016-03-11 14:27:52 -0800155static void set_channel_connectivity_state_locked(grpc_exec_ctx *exec_ctx,
156 channel_data *chand,
157 grpc_connectivity_state state,
Craig Tiller804ff712016-05-05 16:25:40 -0700158 grpc_error *error,
Craig Tiller8c0d96f2016-03-11 14:27:52 -0800159 const char *reason) {
160 if ((state == GRPC_CHANNEL_TRANSIENT_FAILURE ||
Craig Tiller48ed92e2016-06-02 11:07:12 -0700161 state == GRPC_CHANNEL_SHUTDOWN) &&
Craig Tiller8c0d96f2016-03-11 14:27:52 -0800162 chand->lb_policy != NULL) {
Mark D. Roth59c9f902016-09-28 13:33:21 -0700163 /* cancel picks with wait_for_ready=false */
Craig Tiller8c0d96f2016-03-11 14:27:52 -0800164 grpc_lb_policy_cancel_picks(
165 exec_ctx, chand->lb_policy,
Mark D. Roth59c9f902016-09-28 13:33:21 -0700166 /* mask= */ GRPC_INITIAL_METADATA_WAIT_FOR_READY,
Mark D. Roth58f52b72016-09-09 13:55:18 -0700167 /* check= */ 0, GRPC_ERROR_REF(error));
Craig Tiller8c0d96f2016-03-11 14:27:52 -0800168 }
Craig Tiller9ccf5f12016-05-07 21:41:01 -0700169 grpc_connectivity_state_set(exec_ctx, &chand->state_tracker, state, error,
170 reason);
Craig Tiller8c0d96f2016-03-11 14:27:52 -0800171}
172
Craig Tiller804ff712016-05-05 16:25:40 -0700173static void on_lb_policy_state_changed_locked(grpc_exec_ctx *exec_ctx,
174 lb_policy_connectivity_watcher *w,
175 grpc_error *error) {
Craig Tillercb2609f2015-11-24 17:19:19 -0800176 grpc_connectivity_state publish_state = w->state;
Craig Tiller5795da72015-09-17 15:27:13 -0700177 /* check if the notification is for a stale policy */
Craig Tillera82950e2015-09-22 12:33:20 -0700178 if (w->lb_policy != w->chand->lb_policy) return;
Craig Tiller5795da72015-09-17 15:27:13 -0700179
Craig Tiller48ed92e2016-06-02 11:07:12 -0700180 if (publish_state == GRPC_CHANNEL_SHUTDOWN && w->chand->resolver != NULL) {
Craig Tillercb2609f2015-11-24 17:19:19 -0800181 publish_state = GRPC_CHANNEL_TRANSIENT_FAILURE;
182 grpc_resolver_channel_saw_error(exec_ctx, w->chand->resolver);
Craig Tillerf62c4d52015-12-04 07:43:07 -0800183 GRPC_LB_POLICY_UNREF(exec_ctx, w->chand->lb_policy, "channel");
184 w->chand->lb_policy = NULL;
Craig Tillercb2609f2015-11-24 17:19:19 -0800185 }
Craig Tiller8c0d96f2016-03-11 14:27:52 -0800186 set_channel_connectivity_state_locked(exec_ctx, w->chand, publish_state,
Craig Tillerfc353d62016-05-10 12:58:03 -0700187 GRPC_ERROR_REF(error), "lb_changed");
Craig Tiller48ed92e2016-06-02 11:07:12 -0700188 if (w->state != GRPC_CHANNEL_SHUTDOWN) {
Craig Tillera82950e2015-09-22 12:33:20 -0700189 watch_lb_policy(exec_ctx, w->chand, w->lb_policy, w->state);
190 }
Craig Tiller5795da72015-09-17 15:27:13 -0700191}
192
Craig Tillera82950e2015-09-22 12:33:20 -0700193static void on_lb_policy_state_changed(grpc_exec_ctx *exec_ctx, void *arg,
Craig Tiller804ff712016-05-05 16:25:40 -0700194 grpc_error *error) {
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700195 lb_policy_connectivity_watcher *w = arg;
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700196
Mark D. Rothff4df062016-08-22 15:02:49 -0700197 gpr_mu_lock(&w->chand->mu);
Craig Tiller804ff712016-05-05 16:25:40 -0700198 on_lb_policy_state_changed_locked(exec_ctx, w, error);
Mark D. Rothff4df062016-08-22 15:02:49 -0700199 gpr_mu_unlock(&w->chand->mu);
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700200
Craig Tiller906e3bc2015-11-24 07:31:31 -0800201 GRPC_CHANNEL_STACK_UNREF(exec_ctx, w->chand->owning_stack, "watch_lb_policy");
Craig Tillera82950e2015-09-22 12:33:20 -0700202 gpr_free(w);
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700203}
204
Craig Tillera82950e2015-09-22 12:33:20 -0700205static void watch_lb_policy(grpc_exec_ctx *exec_ctx, channel_data *chand,
206 grpc_lb_policy *lb_policy,
207 grpc_connectivity_state current_state) {
208 lb_policy_connectivity_watcher *w = gpr_malloc(sizeof(*w));
Craig Tiller906e3bc2015-11-24 07:31:31 -0800209 GRPC_CHANNEL_STACK_REF(chand->owning_stack, "watch_lb_policy");
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700210
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700211 w->chand = chand;
Craig Tillera82950e2015-09-22 12:33:20 -0700212 grpc_closure_init(&w->on_changed, on_lb_policy_state_changed, w);
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700213 w->state = current_state;
214 w->lb_policy = lb_policy;
Craig Tillera82950e2015-09-22 12:33:20 -0700215 grpc_lb_policy_notify_on_state_change(exec_ctx, lb_policy, &w->state,
216 &w->on_changed);
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700217}
218
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700219static void on_resolver_result_changed(grpc_exec_ctx *exec_ctx, void *arg,
220 grpc_error *error) {
Craig Tiller3f475422015-06-25 10:43:05 -0700221 channel_data *chand = arg;
222 grpc_lb_policy *lb_policy = NULL;
223 grpc_lb_policy *old_lb_policy;
Mark D. Roth9d480942016-10-19 14:18:05 -0700224 grpc_mdstr_hash_table *method_params_table = NULL;
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700225 grpc_connectivity_state state = GRPC_CHANNEL_TRANSIENT_FAILURE;
Mark D. Roth4c0fe492016-08-31 13:51:55 -0700226 bool exit_idle = false;
Craig Tiller804ff712016-05-05 16:25:40 -0700227 grpc_error *state_error = GRPC_ERROR_CREATE("No load balancing policy");
Craig Tiller3f475422015-06-25 10:43:05 -0700228
Mark D. Roth046cf762016-09-26 11:13:51 -0700229 if (chand->resolver_result != NULL) {
Mark D. Roth0e48a9a2016-09-08 14:14:39 -0700230 grpc_lb_policy_args lb_policy_args;
Mark D. Roth933c9552016-09-21 08:12:11 -0700231 lb_policy_args.server_name =
Mark D. Roth046cf762016-09-26 11:13:51 -0700232 grpc_resolver_result_get_server_name(chand->resolver_result);
Mark D. Roth0e48a9a2016-09-08 14:14:39 -0700233 lb_policy_args.addresses =
Mark D. Roth046cf762016-09-26 11:13:51 -0700234 grpc_resolver_result_get_addresses(chand->resolver_result);
235 lb_policy_args.additional_args =
236 grpc_resolver_result_get_lb_policy_args(chand->resolver_result);
Mark D. Roth0e48a9a2016-09-08 14:14:39 -0700237 lb_policy_args.client_channel_factory = chand->client_channel_factory;
Mark D. Roth88405f72016-10-03 08:24:52 -0700238
239 // Special case: If all of the addresses are balancer addresses,
240 // assume that we should use the grpclb policy, regardless of what the
241 // resolver actually specified.
Mark D. Roth07aab592016-10-04 10:02:00 -0700242 const char *lb_policy_name =
Mark D. Roth88405f72016-10-03 08:24:52 -0700243 grpc_resolver_result_get_lb_policy_name(chand->resolver_result);
244 bool found_backend_address = false;
245 for (size_t i = 0; i < lb_policy_args.addresses->num_addresses; ++i) {
246 if (!lb_policy_args.addresses->addresses[i].is_balancer) {
247 found_backend_address = true;
248 break;
249 }
250 }
251 if (!found_backend_address) {
252 if (lb_policy_name != NULL && strcmp(lb_policy_name, "grpclb") != 0) {
253 gpr_log(GPR_INFO,
254 "resolver requested LB policy %s but provided only balancer "
255 "addresses, no backend addresses -- forcing use of grpclb LB "
Mark D. Roth07aab592016-10-04 10:02:00 -0700256 "policy",
257 (lb_policy_name == NULL ? "(none)" : lb_policy_name));
Mark D. Roth88405f72016-10-03 08:24:52 -0700258 }
259 lb_policy_name = "grpclb";
260 }
261 // Use pick_first if nothing was specified and we didn't select grpclb
262 // above.
263 if (lb_policy_name == NULL) lb_policy_name = "pick_first";
264
265 lb_policy =
266 grpc_lb_policy_create(exec_ctx, lb_policy_name, &lb_policy_args);
Craig Tillera82950e2015-09-22 12:33:20 -0700267 if (lb_policy != NULL) {
Craig Tillera82950e2015-09-22 12:33:20 -0700268 GRPC_LB_POLICY_REF(lb_policy, "config_change");
Craig Tillerf707d622016-05-06 14:26:12 -0700269 GRPC_ERROR_UNREF(state_error);
Craig Tiller804ff712016-05-05 16:25:40 -0700270 state =
271 grpc_lb_policy_check_connectivity(exec_ctx, lb_policy, &state_error);
Craig Tiller45724b32015-09-22 10:42:19 -0700272 }
Mark D. Roth046cf762016-09-26 11:13:51 -0700273 const grpc_arg *channel_arg = grpc_channel_args_find(
274 lb_policy_args.additional_args, GRPC_ARG_SERVICE_CONFIG);
275 if (channel_arg != NULL) {
276 GPR_ASSERT(channel_arg->type == GRPC_ARG_POINTER);
Mark D. Roth9d480942016-10-19 14:18:05 -0700277 method_params_table = grpc_method_config_table_convert(
278 (grpc_method_config_table *)channel_arg->value.pointer.p,
279 method_config_convert_value, &method_parameters_vtable);
Mark D. Roth9fe284e2016-09-12 11:22:27 -0700280 }
Mark D. Roth046cf762016-09-26 11:13:51 -0700281 grpc_resolver_result_unref(exec_ctx, chand->resolver_result);
282 chand->resolver_result = NULL;
Craig Tillera82950e2015-09-22 12:33:20 -0700283 }
284
Craig Tiller86c99582015-11-25 15:22:26 -0800285 if (lb_policy != NULL) {
Craig Tiller69b093b2016-02-25 19:04:07 -0800286 grpc_pollset_set_add_pollset_set(exec_ctx, lb_policy->interested_parties,
287 chand->interested_parties);
Craig Tiller86c99582015-11-25 15:22:26 -0800288 }
289
Mark D. Rothff4df062016-08-22 15:02:49 -0700290 gpr_mu_lock(&chand->mu);
Craig Tiller3f475422015-06-25 10:43:05 -0700291 old_lb_policy = chand->lb_policy;
292 chand->lb_policy = lb_policy;
Mark D. Roth9d480942016-10-19 14:18:05 -0700293 if (chand->method_params_table != NULL) {
294 grpc_mdstr_hash_table_unref(chand->method_params_table);
Mark D. Roth046cf762016-09-26 11:13:51 -0700295 }
Mark D. Roth9d480942016-10-19 14:18:05 -0700296 chand->method_params_table = method_params_table;
Craig Tiller0ede5452016-04-23 12:21:45 -0700297 if (lb_policy != NULL) {
298 grpc_exec_ctx_enqueue_list(exec_ctx, &chand->waiting_for_config_closures,
299 NULL);
300 } else if (chand->resolver == NULL /* disconnected */) {
Craig Tiller804ff712016-05-05 16:25:40 -0700301 grpc_closure_list_fail_all(
302 &chand->waiting_for_config_closures,
303 GRPC_ERROR_CREATE_REFERENCING("Channel disconnected", &error, 1));
Craig Tiller6c396862016-01-28 13:53:40 -0800304 grpc_exec_ctx_enqueue_list(exec_ctx, &chand->waiting_for_config_closures,
305 NULL);
Craig Tillera82950e2015-09-22 12:33:20 -0700306 }
307 if (lb_policy != NULL && chand->exit_idle_when_lb_policy_arrives) {
308 GRPC_LB_POLICY_REF(lb_policy, "exit_idle");
Mark D. Roth4c0fe492016-08-31 13:51:55 -0700309 exit_idle = true;
310 chand->exit_idle_when_lb_policy_arrives = false;
Craig Tillera82950e2015-09-22 12:33:20 -0700311 }
Craig Tiller98465032015-06-29 14:36:42 -0700312
Craig Tiller804ff712016-05-05 16:25:40 -0700313 if (error == GRPC_ERROR_NONE && chand->resolver) {
Craig Tiller9ccf5f12016-05-07 21:41:01 -0700314 set_channel_connectivity_state_locked(
315 exec_ctx, chand, state, GRPC_ERROR_REF(state_error), "new_lb+resolver");
Craig Tillera82950e2015-09-22 12:33:20 -0700316 if (lb_policy != NULL) {
317 watch_lb_policy(exec_ctx, chand, lb_policy, state);
Craig Tiller45724b32015-09-22 10:42:19 -0700318 }
Craig Tiller906e3bc2015-11-24 07:31:31 -0800319 GRPC_CHANNEL_STACK_REF(chand->owning_stack, "resolver");
Mark D. Roth046cf762016-09-26 11:13:51 -0700320 grpc_resolver_next(exec_ctx, chand->resolver, &chand->resolver_result,
Mark D. Rothff4df062016-08-22 15:02:49 -0700321 &chand->on_resolver_result_changed);
322 gpr_mu_unlock(&chand->mu);
Craig Tillera82950e2015-09-22 12:33:20 -0700323 } else {
Craig Tiller76a5c0e2016-03-09 09:05:30 -0800324 if (chand->resolver != NULL) {
325 grpc_resolver_shutdown(exec_ctx, chand->resolver);
326 GRPC_RESOLVER_UNREF(exec_ctx, chand->resolver, "channel");
327 chand->resolver = NULL;
328 }
Craig Tiller804ff712016-05-05 16:25:40 -0700329 grpc_error *refs[] = {error, state_error};
Craig Tiller8c0d96f2016-03-11 14:27:52 -0800330 set_channel_connectivity_state_locked(
Craig Tillerd925c932016-06-06 08:38:50 -0700331 exec_ctx, chand, GRPC_CHANNEL_SHUTDOWN,
Craig Tiller804ff712016-05-05 16:25:40 -0700332 GRPC_ERROR_CREATE_REFERENCING("Got config after disconnection", refs,
333 GPR_ARRAY_SIZE(refs)),
334 "resolver_gone");
Mark D. Rothff4df062016-08-22 15:02:49 -0700335 gpr_mu_unlock(&chand->mu);
Craig Tillera82950e2015-09-22 12:33:20 -0700336 }
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700337
Craig Tillera82950e2015-09-22 12:33:20 -0700338 if (exit_idle) {
339 grpc_lb_policy_exit_idle(exec_ctx, lb_policy);
340 GRPC_LB_POLICY_UNREF(exec_ctx, lb_policy, "exit_idle");
341 }
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700342
Craig Tillera82950e2015-09-22 12:33:20 -0700343 if (old_lb_policy != NULL) {
Craig Tiller69b093b2016-02-25 19:04:07 -0800344 grpc_pollset_set_del_pollset_set(
345 exec_ctx, old_lb_policy->interested_parties, chand->interested_parties);
Craig Tillera82950e2015-09-22 12:33:20 -0700346 GRPC_LB_POLICY_UNREF(exec_ctx, old_lb_policy, "channel");
347 }
Craig Tiller000cd8f2015-09-18 07:20:29 -0700348
Craig Tillera82950e2015-09-22 12:33:20 -0700349 if (lb_policy != NULL) {
350 GRPC_LB_POLICY_UNREF(exec_ctx, lb_policy, "config_change");
351 }
Craig Tiller45724b32015-09-22 10:42:19 -0700352
Craig Tiller906e3bc2015-11-24 07:31:31 -0800353 GRPC_CHANNEL_STACK_UNREF(exec_ctx, chand->owning_stack, "resolver");
Craig Tiller9ccf5f12016-05-07 21:41:01 -0700354 GRPC_ERROR_UNREF(state_error);
Craig Tiller3f475422015-06-25 10:43:05 -0700355}
356
Craig Tillera82950e2015-09-22 12:33:20 -0700357static void cc_start_transport_op(grpc_exec_ctx *exec_ctx,
358 grpc_channel_element *elem,
359 grpc_transport_op *op) {
Craig Tillerca3e9d32015-06-27 18:37:27 -0700360 channel_data *chand = elem->channel_data;
Craig Tiller000cd8f2015-09-18 07:20:29 -0700361
Craig Tiller332f1b32016-05-24 13:21:21 -0700362 grpc_exec_ctx_sched(exec_ctx, op->on_consumed, GRPC_ERROR_NONE, NULL);
Craig Tillerca3e9d32015-06-27 18:37:27 -0700363
Craig Tillerd7f12e32016-03-03 10:08:31 -0800364 GPR_ASSERT(op->set_accept_stream == false);
Craig Tiller28bf8912015-12-07 16:07:04 -0800365 if (op->bind_pollset != NULL) {
Craig Tiller69b093b2016-02-25 19:04:07 -0800366 grpc_pollset_set_add_pollset(exec_ctx, chand->interested_parties,
Craig Tillere2c62372015-12-07 16:11:03 -0800367 op->bind_pollset);
Craig Tiller28bf8912015-12-07 16:07:04 -0800368 }
Craig Tillerca3e9d32015-06-27 18:37:27 -0700369
Mark D. Rothff4df062016-08-22 15:02:49 -0700370 gpr_mu_lock(&chand->mu);
Craig Tillera82950e2015-09-22 12:33:20 -0700371 if (op->on_connectivity_state_change != NULL) {
372 grpc_connectivity_state_notify_on_state_change(
373 exec_ctx, &chand->state_tracker, op->connectivity_state,
374 op->on_connectivity_state_change);
375 op->on_connectivity_state_change = NULL;
376 op->connectivity_state = NULL;
377 }
378
Craig Tiller26dab312015-12-07 14:43:47 -0800379 if (op->send_ping != NULL) {
Craig Tiller87b71e22015-12-07 15:14:14 -0800380 if (chand->lb_policy == NULL) {
Craig Tiller332f1b32016-05-24 13:21:21 -0700381 grpc_exec_ctx_sched(exec_ctx, op->send_ping,
382 GRPC_ERROR_CREATE("Ping with no load balancing"),
383 NULL);
Craig Tiller26dab312015-12-07 14:43:47 -0800384 } else {
Craig Tiller28bf8912015-12-07 16:07:04 -0800385 grpc_lb_policy_ping_one(exec_ctx, chand->lb_policy, op->send_ping);
Craig Tiller26dab312015-12-07 14:43:47 -0800386 op->bind_pollset = NULL;
387 }
388 op->send_ping = NULL;
389 }
390
Craig Tiller1c51edc2016-05-07 16:18:43 -0700391 if (op->disconnect_with_error != GRPC_ERROR_NONE) {
392 if (chand->resolver != NULL) {
393 set_channel_connectivity_state_locked(
Craig Tillerd925c932016-06-06 08:38:50 -0700394 exec_ctx, chand, GRPC_CHANNEL_SHUTDOWN,
Craig Tiller1c51edc2016-05-07 16:18:43 -0700395 GRPC_ERROR_REF(op->disconnect_with_error), "disconnect");
396 grpc_resolver_shutdown(exec_ctx, chand->resolver);
397 GRPC_RESOLVER_UNREF(exec_ctx, chand->resolver, "channel");
398 chand->resolver = NULL;
399 if (!chand->started_resolving) {
400 grpc_closure_list_fail_all(&chand->waiting_for_config_closures,
401 GRPC_ERROR_REF(op->disconnect_with_error));
Craig Tiller9ccf5f12016-05-07 21:41:01 -0700402 grpc_exec_ctx_enqueue_list(exec_ctx,
403 &chand->waiting_for_config_closures, NULL);
Craig Tiller1c51edc2016-05-07 16:18:43 -0700404 }
405 if (chand->lb_policy != NULL) {
406 grpc_pollset_set_del_pollset_set(exec_ctx,
407 chand->lb_policy->interested_parties,
408 chand->interested_parties);
409 GRPC_LB_POLICY_UNREF(exec_ctx, chand->lb_policy, "channel");
410 chand->lb_policy = NULL;
411 }
Craig Tillerb12d22a2016-04-23 12:50:21 -0700412 }
Craig Tiller1c51edc2016-05-07 16:18:43 -0700413 GRPC_ERROR_UNREF(op->disconnect_with_error);
Craig Tillera82950e2015-09-22 12:33:20 -0700414 }
Mark D. Rothff4df062016-08-22 15:02:49 -0700415 gpr_mu_unlock(&chand->mu);
Craig Tillerca3e9d32015-06-27 18:37:27 -0700416}
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800417
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700418/* Constructor for channel_data */
419static void cc_init_channel_elem(grpc_exec_ctx *exec_ctx,
420 grpc_channel_element *elem,
421 grpc_channel_element_args *args) {
422 channel_data *chand = elem->channel_data;
423
424 memset(chand, 0, sizeof(*chand));
425
426 GPR_ASSERT(args->is_last);
427 GPR_ASSERT(elem->filter == &grpc_client_channel_filter);
428
429 gpr_mu_init(&chand->mu);
430 grpc_closure_init(&chand->on_resolver_result_changed,
431 on_resolver_result_changed, chand);
432 chand->owning_stack = args->channel_stack;
433
434 grpc_connectivity_state_init(&chand->state_tracker, GRPC_CHANNEL_IDLE,
435 "client_channel");
436 chand->interested_parties = grpc_pollset_set_create();
437}
438
439/* Destructor for channel_data */
440static void cc_destroy_channel_elem(grpc_exec_ctx *exec_ctx,
441 grpc_channel_element *elem) {
442 channel_data *chand = elem->channel_data;
443
444 if (chand->resolver != NULL) {
445 grpc_resolver_shutdown(exec_ctx, chand->resolver);
446 GRPC_RESOLVER_UNREF(exec_ctx, chand->resolver, "channel");
447 }
Mark D. Roth0e48a9a2016-09-08 14:14:39 -0700448 if (chand->client_channel_factory != NULL) {
449 grpc_client_channel_factory_unref(exec_ctx, chand->client_channel_factory);
450 }
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700451 if (chand->lb_policy != NULL) {
452 grpc_pollset_set_del_pollset_set(exec_ctx,
453 chand->lb_policy->interested_parties,
454 chand->interested_parties);
455 GRPC_LB_POLICY_UNREF(exec_ctx, chand->lb_policy, "channel");
456 }
Mark D. Roth9d480942016-10-19 14:18:05 -0700457 if (chand->method_params_table != NULL) {
458 grpc_mdstr_hash_table_unref(chand->method_params_table);
Mark D. Roth9fe284e2016-09-12 11:22:27 -0700459 }
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700460 grpc_connectivity_state_destroy(exec_ctx, &chand->state_tracker);
461 grpc_pollset_set_destroy(chand->interested_parties);
462 gpr_mu_destroy(&chand->mu);
463}
464
465/*************************************************************************
466 * PER-CALL FUNCTIONS
467 */
468
469#define GET_CALL(call_data) \
470 ((grpc_subchannel_call *)(gpr_atm_acq_load(&(call_data)->subchannel_call)))
471
472#define CANCELLED_CALL ((grpc_subchannel_call *)1)
473
474typedef enum {
475 GRPC_SUBCHANNEL_CALL_HOLDER_NOT_CREATING,
476 GRPC_SUBCHANNEL_CALL_HOLDER_PICKING_SUBCHANNEL
477} subchannel_creation_phase;
478
479/** Call data. Holds a pointer to grpc_subchannel_call and the
480 associated machinery to create such a pointer.
481 Handles queueing of stream ops until a call object is ready, waiting
482 for initial metadata before trying to create a call object,
483 and handling cancellation gracefully. */
484typedef struct client_channel_call_data {
Mark D. Roth72f6da82016-09-02 13:42:38 -0700485 // State for handling deadlines.
486 // The code in deadline_filter.c requires this to be the first field.
Mark D. Roth72f6da82016-09-02 13:42:38 -0700487 // TODO(roth): This is slightly sub-optimal in that grpc_deadline_state
488 // and this struct both independently store a pointer to the call
489 // stack and each has its own mutex. If/when we have time, find a way
Mark D. Roth6ad99172016-09-09 07:52:48 -0700490 // to avoid this without breaking the grpc_deadline_state abstraction.
Mark D. Roth72f6da82016-09-02 13:42:38 -0700491 grpc_deadline_state deadline_state;
Mark D. Rothf28763c2016-09-14 15:18:40 -0700492
Mark D. Rothe40dd292016-10-05 14:58:37 -0700493 grpc_mdstr *path; // Request path.
494 gpr_timespec call_start_time;
495 gpr_timespec deadline;
Mark D. Roth9d480942016-10-19 14:18:05 -0700496 wait_for_ready_value wait_for_ready_from_service_config;
Mark D. Rothe40dd292016-10-05 14:58:37 -0700497 grpc_closure read_service_config;
Mark D. Rothaa850a72016-09-26 13:38:02 -0700498
Mark D. Rothf28763c2016-09-14 15:18:40 -0700499 grpc_error *cancel_error;
Mark D. Roth72f6da82016-09-02 13:42:38 -0700500
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700501 /** either 0 for no call, 1 for cancelled, or a pointer to a
502 grpc_subchannel_call */
503 gpr_atm subchannel_call;
504
505 gpr_mu mu;
506
507 subchannel_creation_phase creation_phase;
508 grpc_connected_subchannel *connected_subchannel;
509 grpc_polling_entity *pollent;
510
Craig Tiller57726ca2016-09-12 11:59:45 -0700511 grpc_transport_stream_op **waiting_ops;
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700512 size_t waiting_ops_count;
513 size_t waiting_ops_capacity;
514
515 grpc_closure next_step;
516
517 grpc_call_stack *owning_call;
David Garcia Quintasd1a47f12016-09-02 12:46:44 +0200518
519 grpc_linked_mdelem lb_token_mdelem;
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700520} call_data;
521
522static void add_waiting_locked(call_data *calld, grpc_transport_stream_op *op) {
523 GPR_TIMER_BEGIN("add_waiting_locked", 0);
524 if (calld->waiting_ops_count == calld->waiting_ops_capacity) {
525 calld->waiting_ops_capacity = GPR_MAX(3, 2 * calld->waiting_ops_capacity);
526 calld->waiting_ops =
527 gpr_realloc(calld->waiting_ops,
528 calld->waiting_ops_capacity * sizeof(*calld->waiting_ops));
529 }
Craig Tiller57726ca2016-09-12 11:59:45 -0700530 calld->waiting_ops[calld->waiting_ops_count++] = op;
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700531 GPR_TIMER_END("add_waiting_locked", 0);
532}
533
534static void fail_locked(grpc_exec_ctx *exec_ctx, call_data *calld,
535 grpc_error *error) {
536 size_t i;
537 for (i = 0; i < calld->waiting_ops_count; i++) {
538 grpc_transport_stream_op_finish_with_failure(
Craig Tiller57726ca2016-09-12 11:59:45 -0700539 exec_ctx, calld->waiting_ops[i], GRPC_ERROR_REF(error));
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700540 }
541 calld->waiting_ops_count = 0;
542 GRPC_ERROR_UNREF(error);
543}
544
545typedef struct {
Craig Tiller57726ca2016-09-12 11:59:45 -0700546 grpc_transport_stream_op **ops;
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700547 size_t nops;
548 grpc_subchannel_call *call;
549} retry_ops_args;
550
551static void retry_ops(grpc_exec_ctx *exec_ctx, void *args, grpc_error *error) {
552 retry_ops_args *a = args;
553 size_t i;
554 for (i = 0; i < a->nops; i++) {
Craig Tiller57726ca2016-09-12 11:59:45 -0700555 grpc_subchannel_call_process_op(exec_ctx, a->call, a->ops[i]);
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700556 }
557 GRPC_SUBCHANNEL_CALL_UNREF(exec_ctx, a->call, "retry_ops");
558 gpr_free(a->ops);
559 gpr_free(a);
560}
561
562static void retry_waiting_locked(grpc_exec_ctx *exec_ctx, call_data *calld) {
Craig Tiller57726ca2016-09-12 11:59:45 -0700563 if (calld->waiting_ops_count == 0) {
564 return;
565 }
566
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700567 retry_ops_args *a = gpr_malloc(sizeof(*a));
568 a->ops = calld->waiting_ops;
569 a->nops = calld->waiting_ops_count;
570 a->call = GET_CALL(calld);
571 if (a->call == CANCELLED_CALL) {
572 gpr_free(a);
573 fail_locked(exec_ctx, calld, GRPC_ERROR_CANCELLED);
574 return;
575 }
576 calld->waiting_ops = NULL;
577 calld->waiting_ops_count = 0;
578 calld->waiting_ops_capacity = 0;
579 GRPC_SUBCHANNEL_CALL_REF(a->call, "retry_ops");
580 grpc_exec_ctx_sched(exec_ctx, grpc_closure_create(retry_ops, a),
581 GRPC_ERROR_NONE, NULL);
582}
583
584static void subchannel_ready(grpc_exec_ctx *exec_ctx, void *arg,
585 grpc_error *error) {
Yuchen Zeng19656b12016-09-01 18:00:45 -0700586 grpc_call_element *elem = arg;
587 call_data *calld = elem->call_data;
588 channel_data *chand = elem->channel_data;
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700589 gpr_mu_lock(&calld->mu);
590 GPR_ASSERT(calld->creation_phase ==
591 GRPC_SUBCHANNEL_CALL_HOLDER_PICKING_SUBCHANNEL);
Yuchen Zeng19656b12016-09-01 18:00:45 -0700592 grpc_polling_entity_del_from_pollset_set(exec_ctx, calld->pollent,
593 chand->interested_parties);
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700594 calld->creation_phase = GRPC_SUBCHANNEL_CALL_HOLDER_NOT_CREATING;
595 if (calld->connected_subchannel == NULL) {
596 gpr_atm_no_barrier_store(&calld->subchannel_call, 1);
597 fail_locked(exec_ctx, calld, GRPC_ERROR_CREATE_REFERENCING(
598 "Failed to create subchannel", &error, 1));
Mark D. Roth72f6da82016-09-02 13:42:38 -0700599 } else if (GET_CALL(calld) == CANCELLED_CALL) {
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700600 /* already cancelled before subchannel became ready */
601 fail_locked(exec_ctx, calld,
602 GRPC_ERROR_CREATE_REFERENCING(
603 "Cancelled before creating subchannel", &error, 1));
604 } else {
Mark D. Roth9fe284e2016-09-12 11:22:27 -0700605 /* Create call on subchannel. */
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700606 grpc_subchannel_call *subchannel_call = NULL;
607 grpc_error *new_error = grpc_connected_subchannel_create_call(
Mark D. Rothaa850a72016-09-26 13:38:02 -0700608 exec_ctx, calld->connected_subchannel, calld->pollent, calld->path,
609 calld->deadline, &subchannel_call);
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700610 if (new_error != GRPC_ERROR_NONE) {
611 new_error = grpc_error_add_child(new_error, error);
612 subchannel_call = CANCELLED_CALL;
613 fail_locked(exec_ctx, calld, new_error);
614 }
615 gpr_atm_rel_store(&calld->subchannel_call,
616 (gpr_atm)(uintptr_t)subchannel_call);
617 retry_waiting_locked(exec_ctx, calld);
618 }
619 gpr_mu_unlock(&calld->mu);
620 GRPC_CALL_STACK_UNREF(exec_ctx, calld->owning_call, "pick_subchannel");
621}
622
623static char *cc_get_peer(grpc_exec_ctx *exec_ctx, grpc_call_element *elem) {
624 call_data *calld = elem->call_data;
625 grpc_subchannel_call *subchannel_call = GET_CALL(calld);
626 if (subchannel_call == NULL || subchannel_call == CANCELLED_CALL) {
627 return NULL;
628 } else {
629 return grpc_subchannel_call_get_peer(exec_ctx, subchannel_call);
630 }
631}
632
Craig Tiller577c9b22015-11-02 14:11:15 -0800633typedef struct {
634 grpc_metadata_batch *initial_metadata;
Craig Tiller8c0d96f2016-03-11 14:27:52 -0800635 uint32_t initial_metadata_flags;
Craig Tillerb5585d42015-11-17 07:18:31 -0800636 grpc_connected_subchannel **connected_subchannel;
Craig Tiller577c9b22015-11-02 14:11:15 -0800637 grpc_closure *on_ready;
638 grpc_call_element *elem;
639 grpc_closure closure;
640} continue_picking_args;
641
Yuchen Zeng144ce652016-09-01 18:19:34 -0700642/** Return true if subchannel is available immediately (in which case on_ready
643 should not be called), or false otherwise (in which case on_ready should be
644 called when the subchannel is available). */
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700645static bool pick_subchannel(grpc_exec_ctx *exec_ctx, grpc_call_element *elem,
646 grpc_metadata_batch *initial_metadata,
647 uint32_t initial_metadata_flags,
648 grpc_connected_subchannel **connected_subchannel,
Mark D. Roth72f6da82016-09-02 13:42:38 -0700649 grpc_closure *on_ready, grpc_error *error);
Craig Tiller577c9b22015-11-02 14:11:15 -0800650
Craig Tiller804ff712016-05-05 16:25:40 -0700651static void continue_picking(grpc_exec_ctx *exec_ctx, void *arg,
652 grpc_error *error) {
Craig Tiller577c9b22015-11-02 14:11:15 -0800653 continue_picking_args *cpa = arg;
Craig Tiller0ede5452016-04-23 12:21:45 -0700654 if (cpa->connected_subchannel == NULL) {
Craig Tiller577c9b22015-11-02 14:11:15 -0800655 /* cancelled, do nothing */
Craig Tiller804ff712016-05-05 16:25:40 -0700656 } else if (error != GRPC_ERROR_NONE) {
Craig Tiller332f1b32016-05-24 13:21:21 -0700657 grpc_exec_ctx_sched(exec_ctx, cpa->on_ready, GRPC_ERROR_REF(error), NULL);
Mark D. Roth9dab7d52016-10-07 07:48:03 -0700658 } else {
659 call_data *calld = cpa->elem->call_data;
660 gpr_mu_lock(&calld->mu);
661 if (pick_subchannel(exec_ctx, cpa->elem, cpa->initial_metadata,
Mark D. Rothfd2ddd22016-10-07 10:11:10 -0700662 cpa->initial_metadata_flags, cpa->connected_subchannel,
663 cpa->on_ready, GRPC_ERROR_NONE)) {
Mark D. Roth9dab7d52016-10-07 07:48:03 -0700664 grpc_exec_ctx_sched(exec_ctx, cpa->on_ready, GRPC_ERROR_NONE, NULL);
665 }
666 gpr_mu_unlock(&calld->mu);
Craig Tiller577c9b22015-11-02 14:11:15 -0800667 }
668 gpr_free(cpa);
669}
670
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700671static bool pick_subchannel(grpc_exec_ctx *exec_ctx, grpc_call_element *elem,
672 grpc_metadata_batch *initial_metadata,
673 uint32_t initial_metadata_flags,
674 grpc_connected_subchannel **connected_subchannel,
Mark D. Roth72f6da82016-09-02 13:42:38 -0700675 grpc_closure *on_ready, grpc_error *error) {
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700676 GPR_TIMER_BEGIN("pick_subchannel", 0);
Craig Tillerbfc9adc2016-06-27 13:16:22 -0700677
Craig Tiller577c9b22015-11-02 14:11:15 -0800678 channel_data *chand = elem->channel_data;
679 call_data *calld = elem->call_data;
680 continue_picking_args *cpa;
681 grpc_closure *closure;
682
Craig Tillerb5585d42015-11-17 07:18:31 -0800683 GPR_ASSERT(connected_subchannel);
Craig Tiller577c9b22015-11-02 14:11:15 -0800684
Mark D. Rothff4df062016-08-22 15:02:49 -0700685 gpr_mu_lock(&chand->mu);
Craig Tiller577c9b22015-11-02 14:11:15 -0800686 if (initial_metadata == NULL) {
687 if (chand->lb_policy != NULL) {
Craig Tillerab33b482015-11-21 08:11:04 -0800688 grpc_lb_policy_cancel_pick(exec_ctx, chand->lb_policy,
Mark D. Roth5f844002016-09-08 08:20:53 -0700689 connected_subchannel, GRPC_ERROR_REF(error));
Craig Tiller577c9b22015-11-02 14:11:15 -0800690 }
691 for (closure = chand->waiting_for_config_closures.head; closure != NULL;
Craig Tiller804ff712016-05-05 16:25:40 -0700692 closure = closure->next_data.next) {
Craig Tiller577c9b22015-11-02 14:11:15 -0800693 cpa = closure->cb_arg;
Craig Tillerb5585d42015-11-17 07:18:31 -0800694 if (cpa->connected_subchannel == connected_subchannel) {
695 cpa->connected_subchannel = NULL;
Mark D. Roth932b10c2016-09-09 08:44:30 -0700696 grpc_exec_ctx_sched(
697 exec_ctx, cpa->on_ready,
698 GRPC_ERROR_CREATE_REFERENCING("Pick cancelled", &error, 1), NULL);
Craig Tiller577c9b22015-11-02 14:11:15 -0800699 }
700 }
Mark D. Rothff4df062016-08-22 15:02:49 -0700701 gpr_mu_unlock(&chand->mu);
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700702 GPR_TIMER_END("pick_subchannel", 0);
Mark D. Roth697a1f62016-09-07 13:35:07 -0700703 GRPC_ERROR_UNREF(error);
Mark D. Roth4c0fe492016-08-31 13:51:55 -0700704 return true;
Craig Tiller577c9b22015-11-02 14:11:15 -0800705 }
Mark D. Roth697a1f62016-09-07 13:35:07 -0700706 GPR_ASSERT(error == GRPC_ERROR_NONE);
Craig Tiller577c9b22015-11-02 14:11:15 -0800707 if (chand->lb_policy != NULL) {
Craig Tiller86c0f8a2015-12-01 20:05:40 -0800708 grpc_lb_policy *lb_policy = chand->lb_policy;
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700709 GRPC_LB_POLICY_REF(lb_policy, "pick_subchannel");
Mark D. Rothff4df062016-08-22 15:02:49 -0700710 gpr_mu_unlock(&chand->mu);
Mark D. Rothe40dd292016-10-05 14:58:37 -0700711 // If the application explicitly set wait_for_ready, use that.
712 // Otherwise, if the service config specified a value for this
713 // method, use that.
Mark D. Rothc1c38582016-10-11 11:03:27 -0700714 const bool wait_for_ready_set_from_api =
715 initial_metadata_flags &
716 GRPC_INITIAL_METADATA_WAIT_FOR_READY_EXPLICITLY_SET;
717 const bool wait_for_ready_set_from_service_config =
718 calld->wait_for_ready_from_service_config != WAIT_FOR_READY_UNSET;
719 if (!wait_for_ready_set_from_api &&
720 wait_for_ready_set_from_service_config) {
Mark D. Rothe40dd292016-10-05 14:58:37 -0700721 if (calld->wait_for_ready_from_service_config == WAIT_FOR_READY_TRUE) {
722 initial_metadata_flags |= GRPC_INITIAL_METADATA_WAIT_FOR_READY;
723 } else {
724 initial_metadata_flags &= ~GRPC_INITIAL_METADATA_WAIT_FOR_READY;
725 }
726 }
David Garcia Quintas61c58012016-10-03 14:44:20 -0700727 // TODO(dgq): make this deadline configurable somehow.
David Garcia Quintas92eb6b92016-09-30 14:07:39 -0700728 const grpc_lb_policy_pick_args inputs = {
Yuchen Zengac8bc422016-10-05 14:00:02 -0700729 initial_metadata, initial_metadata_flags, &calld->lb_token_mdelem,
730 gpr_inf_future(GPR_CLOCK_MONOTONIC)};
Mark D. Roth55f25b62016-10-12 14:55:20 -0700731 const bool result = grpc_lb_policy_pick(
732 exec_ctx, lb_policy, &inputs, connected_subchannel, NULL, on_ready);
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700733 GRPC_LB_POLICY_UNREF(exec_ctx, lb_policy, "pick_subchannel");
734 GPR_TIMER_END("pick_subchannel", 0);
Mark D. Roth9dab7d52016-10-07 07:48:03 -0700735 return result;
Craig Tiller577c9b22015-11-02 14:11:15 -0800736 }
737 if (chand->resolver != NULL && !chand->started_resolving) {
Mark D. Roth4c0fe492016-08-31 13:51:55 -0700738 chand->started_resolving = true;
Craig Tiller906e3bc2015-11-24 07:31:31 -0800739 GRPC_CHANNEL_STACK_REF(chand->owning_stack, "resolver");
Mark D. Roth046cf762016-09-26 11:13:51 -0700740 grpc_resolver_next(exec_ctx, chand->resolver, &chand->resolver_result,
Mark D. Rothff4df062016-08-22 15:02:49 -0700741 &chand->on_resolver_result_changed);
Craig Tiller577c9b22015-11-02 14:11:15 -0800742 }
Craig Tiller0eab6972016-04-23 12:59:57 -0700743 if (chand->resolver != NULL) {
744 cpa = gpr_malloc(sizeof(*cpa));
745 cpa->initial_metadata = initial_metadata;
746 cpa->initial_metadata_flags = initial_metadata_flags;
747 cpa->connected_subchannel = connected_subchannel;
748 cpa->on_ready = on_ready;
749 cpa->elem = elem;
750 grpc_closure_init(&cpa->closure, continue_picking, cpa);
Craig Tiller804ff712016-05-05 16:25:40 -0700751 grpc_closure_list_append(&chand->waiting_for_config_closures, &cpa->closure,
752 GRPC_ERROR_NONE);
Craig Tiller0eab6972016-04-23 12:59:57 -0700753 } else {
Craig Tiller332f1b32016-05-24 13:21:21 -0700754 grpc_exec_ctx_sched(exec_ctx, on_ready, GRPC_ERROR_CREATE("Disconnected"),
755 NULL);
Craig Tiller0eab6972016-04-23 12:59:57 -0700756 }
Mark D. Rothff4df062016-08-22 15:02:49 -0700757 gpr_mu_unlock(&chand->mu);
Craig Tillerbfc9adc2016-06-27 13:16:22 -0700758
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700759 GPR_TIMER_END("pick_subchannel", 0);
Mark D. Roth4c0fe492016-08-31 13:51:55 -0700760 return false;
Craig Tiller577c9b22015-11-02 14:11:15 -0800761}
762
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700763// The logic here is fairly complicated, due to (a) the fact that we
764// need to handle the case where we receive the send op before the
765// initial metadata op, and (b) the need for efficiency, especially in
766// the streaming case.
767// TODO(ctiller): Explain this more thoroughly.
768static void cc_start_transport_stream_op(grpc_exec_ctx *exec_ctx,
769 grpc_call_element *elem,
770 grpc_transport_stream_op *op) {
771 call_data *calld = elem->call_data;
Yuchen Zeng19656b12016-09-01 18:00:45 -0700772 channel_data *chand = elem->channel_data;
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700773 GRPC_CALL_LOG_OP(GPR_INFO, elem, op);
Mark D. Roth72f6da82016-09-02 13:42:38 -0700774 grpc_deadline_state_client_start_transport_stream_op(exec_ctx, elem, op);
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700775 /* try to (atomically) get the call */
776 grpc_subchannel_call *call = GET_CALL(calld);
777 GPR_TIMER_BEGIN("cc_start_transport_stream_op", 0);
778 if (call == CANCELLED_CALL) {
Mark D. Rothf28763c2016-09-14 15:18:40 -0700779 grpc_transport_stream_op_finish_with_failure(
780 exec_ctx, op, GRPC_ERROR_REF(calld->cancel_error));
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700781 GPR_TIMER_END("cc_start_transport_stream_op", 0);
782 return;
783 }
784 if (call != NULL) {
785 grpc_subchannel_call_process_op(exec_ctx, call, op);
786 GPR_TIMER_END("cc_start_transport_stream_op", 0);
787 return;
788 }
789 /* we failed; lock and figure out what to do */
790 gpr_mu_lock(&calld->mu);
791retry:
792 /* need to recheck that another thread hasn't set the call */
793 call = GET_CALL(calld);
794 if (call == CANCELLED_CALL) {
795 gpr_mu_unlock(&calld->mu);
Mark D. Rothf28763c2016-09-14 15:18:40 -0700796 grpc_transport_stream_op_finish_with_failure(
797 exec_ctx, op, GRPC_ERROR_REF(calld->cancel_error));
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700798 GPR_TIMER_END("cc_start_transport_stream_op", 0);
799 return;
800 }
801 if (call != NULL) {
802 gpr_mu_unlock(&calld->mu);
803 grpc_subchannel_call_process_op(exec_ctx, call, op);
804 GPR_TIMER_END("cc_start_transport_stream_op", 0);
805 return;
806 }
807 /* if this is a cancellation, then we can raise our cancelled flag */
808 if (op->cancel_error != GRPC_ERROR_NONE) {
809 if (!gpr_atm_rel_cas(&calld->subchannel_call, 0,
810 (gpr_atm)(uintptr_t)CANCELLED_CALL)) {
811 goto retry;
812 } else {
Mark D. Rothf28763c2016-09-14 15:18:40 -0700813 // Stash a copy of cancel_error in our call data, so that we can use
814 // it for subsequent operations. This ensures that if the call is
815 // cancelled before any ops are passed down (e.g., if the deadline
816 // is in the past when the call starts), we can return the right
817 // error to the caller when the first op does get passed down.
818 calld->cancel_error = GRPC_ERROR_REF(op->cancel_error);
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700819 switch (calld->creation_phase) {
820 case GRPC_SUBCHANNEL_CALL_HOLDER_NOT_CREATING:
821 fail_locked(exec_ctx, calld, GRPC_ERROR_REF(op->cancel_error));
822 break;
823 case GRPC_SUBCHANNEL_CALL_HOLDER_PICKING_SUBCHANNEL:
Mark D. Rothd4c0f552016-09-01 09:25:32 -0700824 pick_subchannel(exec_ctx, elem, NULL, 0, &calld->connected_subchannel,
Mark D. Roth72f6da82016-09-02 13:42:38 -0700825 NULL, GRPC_ERROR_REF(op->cancel_error));
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700826 break;
827 }
828 gpr_mu_unlock(&calld->mu);
Mark D. Roth72f6da82016-09-02 13:42:38 -0700829 grpc_transport_stream_op_finish_with_failure(
830 exec_ctx, op, GRPC_ERROR_REF(op->cancel_error));
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700831 GPR_TIMER_END("cc_start_transport_stream_op", 0);
832 return;
833 }
834 }
835 /* if we don't have a subchannel, try to get one */
836 if (calld->creation_phase == GRPC_SUBCHANNEL_CALL_HOLDER_NOT_CREATING &&
837 calld->connected_subchannel == NULL &&
838 op->send_initial_metadata != NULL) {
839 calld->creation_phase = GRPC_SUBCHANNEL_CALL_HOLDER_PICKING_SUBCHANNEL;
Yuchen Zeng19656b12016-09-01 18:00:45 -0700840 grpc_closure_init(&calld->next_step, subchannel_ready, elem);
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700841 GRPC_CALL_STACK_REF(calld->owning_call, "pick_subchannel");
Yuchen Zeng144ce652016-09-01 18:19:34 -0700842 /* If a subchannel is not available immediately, the polling entity from
843 call_data should be provided to channel_data's interested_parties, so
844 that IO of the lb_policy and resolver could be done under it. */
Mark D. Rothd4c0f552016-09-01 09:25:32 -0700845 if (pick_subchannel(exec_ctx, elem, op->send_initial_metadata,
Mark D. Rothe40dd292016-10-05 14:58:37 -0700846 op->send_initial_metadata_flags,
847 &calld->connected_subchannel, &calld->next_step,
848 GRPC_ERROR_NONE)) {
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700849 calld->creation_phase = GRPC_SUBCHANNEL_CALL_HOLDER_NOT_CREATING;
850 GRPC_CALL_STACK_UNREF(exec_ctx, calld->owning_call, "pick_subchannel");
Yuchen Zeng19656b12016-09-01 18:00:45 -0700851 } else {
Yuchen Zeng19656b12016-09-01 18:00:45 -0700852 grpc_polling_entity_add_to_pollset_set(exec_ctx, calld->pollent,
853 chand->interested_parties);
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700854 }
855 }
856 /* if we've got a subchannel, then let's ask it to create a call */
857 if (calld->creation_phase == GRPC_SUBCHANNEL_CALL_HOLDER_NOT_CREATING &&
858 calld->connected_subchannel != NULL) {
859 grpc_subchannel_call *subchannel_call = NULL;
860 grpc_error *error = grpc_connected_subchannel_create_call(
Mark D. Rothaa850a72016-09-26 13:38:02 -0700861 exec_ctx, calld->connected_subchannel, calld->pollent, calld->path,
862 calld->deadline, &subchannel_call);
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700863 if (error != GRPC_ERROR_NONE) {
864 subchannel_call = CANCELLED_CALL;
865 fail_locked(exec_ctx, calld, GRPC_ERROR_REF(error));
866 grpc_transport_stream_op_finish_with_failure(exec_ctx, op, error);
867 }
868 gpr_atm_rel_store(&calld->subchannel_call,
869 (gpr_atm)(uintptr_t)subchannel_call);
870 retry_waiting_locked(exec_ctx, calld);
871 goto retry;
872 }
873 /* nothing to be done but wait */
874 add_waiting_locked(calld, op);
875 gpr_mu_unlock(&calld->mu);
876 GPR_TIMER_END("cc_start_transport_stream_op", 0);
877}
878
Mark D. Rothe40dd292016-10-05 14:58:37 -0700879// Gets data from the service config. Invoked when the resolver returns
880// its initial result.
881static void read_service_config(grpc_exec_ctx *exec_ctx, void *arg,
882 grpc_error *error) {
883 grpc_call_element *elem = arg;
884 channel_data *chand = elem->channel_data;
885 call_data *calld = elem->call_data;
886 // If this is an error, there's no point in looking at the service config.
Mark D. Roth196387a2016-10-12 14:53:36 -0700887 if (error == GRPC_ERROR_NONE) {
888 // Get the method config table from channel data.
889 gpr_mu_lock(&chand->mu);
Mark D. Roth9d480942016-10-19 14:18:05 -0700890 grpc_mdstr_hash_table *method_params_table = NULL;
891 if (chand->method_params_table != NULL) {
892 method_params_table =
893 grpc_mdstr_hash_table_ref(chand->method_params_table);
Mark D. Rothe40dd292016-10-05 14:58:37 -0700894 }
Mark D. Roth196387a2016-10-12 14:53:36 -0700895 gpr_mu_unlock(&chand->mu);
896 // If the method config table was present, use it.
Mark D. Roth9d480942016-10-19 14:18:05 -0700897 if (method_params_table != NULL) {
898 const method_parameters *method_params =
899 grpc_method_config_table_get(method_params_table, calld->path);
900 if (method_params != NULL) {
901 const bool have_method_timeout =
902 gpr_time_cmp(method_params->timeout, gpr_time_0(GPR_TIMESPAN)) != 0;
903 if (have_method_timeout ||
904 method_params->wait_for_ready != WAIT_FOR_READY_UNSET) {
Mark D. Roth196387a2016-10-12 14:53:36 -0700905 gpr_mu_lock(&calld->mu);
Mark D. Roth9d480942016-10-19 14:18:05 -0700906 if (have_method_timeout) {
907 const gpr_timespec per_method_deadline =
908 gpr_time_add(calld->call_start_time, method_params->timeout);
Mark D. Roth196387a2016-10-12 14:53:36 -0700909 if (gpr_time_cmp(per_method_deadline, calld->deadline) < 0) {
910 calld->deadline = per_method_deadline;
911 // Reset deadline timer.
912 grpc_deadline_state_reset(exec_ctx, elem, calld->deadline);
913 }
914 }
Mark D. Roth9d480942016-10-19 14:18:05 -0700915 if (method_params->wait_for_ready != WAIT_FOR_READY_UNSET) {
Mark D. Roth196387a2016-10-12 14:53:36 -0700916 calld->wait_for_ready_from_service_config =
Mark D. Roth9d480942016-10-19 14:18:05 -0700917 method_params->wait_for_ready;
Mark D. Roth196387a2016-10-12 14:53:36 -0700918 }
919 gpr_mu_unlock(&calld->mu);
920 }
921 }
Mark D. Roth9d480942016-10-19 14:18:05 -0700922 grpc_mdstr_hash_table_unref(method_params_table);
Mark D. Roth196387a2016-10-12 14:53:36 -0700923 }
Mark D. Rothe40dd292016-10-05 14:58:37 -0700924 }
Mark D. Roth31292f22016-10-12 13:14:07 -0700925 GRPC_CALL_STACK_UNREF(exec_ctx, calld->owning_call, "read_service_config");
Mark D. Rothe40dd292016-10-05 14:58:37 -0700926}
927
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800928/* Constructor for call_data */
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700929static grpc_error *cc_init_call_elem(grpc_exec_ctx *exec_ctx,
930 grpc_call_element *elem,
931 grpc_call_element_args *args) {
Mark D. Rothaa850a72016-09-26 13:38:02 -0700932 channel_data *chand = elem->channel_data;
Mark D. Roth4c0fe492016-08-31 13:51:55 -0700933 call_data *calld = elem->call_data;
Mark D. Rothe40dd292016-10-05 14:58:37 -0700934 // Initialize data members.
935 grpc_deadline_state_init(exec_ctx, elem, args->call_stack);
Mark D. Rothaa850a72016-09-26 13:38:02 -0700936 calld->path = GRPC_MDSTR_REF(args->path);
Mark D. Rothff08f332016-10-14 13:01:01 -0700937 calld->call_start_time = args->start_time;
Mark D. Rothe40dd292016-10-05 14:58:37 -0700938 calld->deadline = gpr_convert_clock_type(args->deadline, GPR_CLOCK_MONOTONIC);
939 calld->wait_for_ready_from_service_config = WAIT_FOR_READY_UNSET;
Mark D. Rothf28763c2016-09-14 15:18:40 -0700940 calld->cancel_error = GRPC_ERROR_NONE;
Mark D. Roth4c0fe492016-08-31 13:51:55 -0700941 gpr_atm_rel_store(&calld->subchannel_call, 0);
Mark D. Roth4c0fe492016-08-31 13:51:55 -0700942 gpr_mu_init(&calld->mu);
943 calld->connected_subchannel = NULL;
944 calld->waiting_ops = NULL;
945 calld->waiting_ops_count = 0;
946 calld->waiting_ops_capacity = 0;
947 calld->creation_phase = GRPC_SUBCHANNEL_CALL_HOLDER_NOT_CREATING;
948 calld->owning_call = args->call_stack;
949 calld->pollent = NULL;
Mark D. Rothe40dd292016-10-05 14:58:37 -0700950 // If the resolver has already returned results, then we can access
951 // the service config parameters immediately. Otherwise, we need to
952 // defer that work until the resolver returns an initial result.
953 // TODO(roth): This code is almost but not quite identical to the code
954 // in read_service_config() above. It would be nice to find a way to
955 // combine them, to avoid having to maintain it twice.
956 gpr_mu_lock(&chand->mu);
957 if (chand->lb_policy != NULL) {
958 // We already have a resolver result, so check for service config.
Mark D. Roth9d480942016-10-19 14:18:05 -0700959 if (chand->method_params_table != NULL) {
960 grpc_mdstr_hash_table *method_params_table =
961 grpc_mdstr_hash_table_ref(chand->method_params_table);
Mark D. Rothe40dd292016-10-05 14:58:37 -0700962 gpr_mu_unlock(&chand->mu);
Mark D. Roth9d480942016-10-19 14:18:05 -0700963 method_parameters *method_params =
964 grpc_method_config_table_get(method_params_table, args->path);
965 if (method_params != NULL) {
966 if (gpr_time_cmp(method_params->timeout,
967 gpr_time_0(GPR_CLOCK_MONOTONIC)) != 0) {
Mark D. Rothe40dd292016-10-05 14:58:37 -0700968 gpr_timespec per_method_deadline =
Mark D. Roth9d480942016-10-19 14:18:05 -0700969 gpr_time_add(calld->call_start_time, method_params->timeout);
Mark D. Rothe40dd292016-10-05 14:58:37 -0700970 calld->deadline = gpr_time_min(calld->deadline, per_method_deadline);
971 }
Mark D. Roth9d480942016-10-19 14:18:05 -0700972 if (method_params->wait_for_ready != WAIT_FOR_READY_UNSET) {
Mark D. Rothe40dd292016-10-05 14:58:37 -0700973 calld->wait_for_ready_from_service_config =
Mark D. Roth9d480942016-10-19 14:18:05 -0700974 method_params->wait_for_ready;
Mark D. Rothe40dd292016-10-05 14:58:37 -0700975 }
976 }
Mark D. Roth9d480942016-10-19 14:18:05 -0700977 grpc_mdstr_hash_table_unref(method_params_table);
Mark D. Rothe40dd292016-10-05 14:58:37 -0700978 } else {
979 gpr_mu_unlock(&chand->mu);
980 }
981 } else {
982 // We don't yet have a resolver result, so register a callback to
983 // get the service config data once the resolver returns.
Mark D. Roth31292f22016-10-12 13:14:07 -0700984 // Take a reference to the call stack to be owned by the callback.
985 GRPC_CALL_STACK_REF(calld->owning_call, "read_service_config");
Mark D. Rothe40dd292016-10-05 14:58:37 -0700986 grpc_closure_init(&calld->read_service_config, read_service_config, elem);
987 grpc_closure_list_append(&chand->waiting_for_config_closures,
988 &calld->read_service_config, GRPC_ERROR_NONE);
989 gpr_mu_unlock(&chand->mu);
990 }
991 // Start the deadline timer with the current deadline value. If we
992 // do not yet have service config data, then the timer may be reset
993 // later.
994 grpc_deadline_state_start(exec_ctx, elem, calld->deadline);
Mark D. Roth0badbe82016-06-23 10:15:12 -0700995 return GRPC_ERROR_NONE;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800996}
997
998/* Destructor for call_data */
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700999static void cc_destroy_call_elem(grpc_exec_ctx *exec_ctx,
1000 grpc_call_element *elem,
1001 const grpc_call_final_info *final_info,
1002 void *and_free_memory) {
Mark D. Roth4c0fe492016-08-31 13:51:55 -07001003 call_data *calld = elem->call_data;
Mark D. Rothf28763c2016-09-14 15:18:40 -07001004 grpc_deadline_state_destroy(exec_ctx, elem);
Mark D. Rothaa850a72016-09-26 13:38:02 -07001005 GRPC_MDSTR_UNREF(calld->path);
Mark D. Rothf28763c2016-09-14 15:18:40 -07001006 GRPC_ERROR_UNREF(calld->cancel_error);
Mark D. Roth4c0fe492016-08-31 13:51:55 -07001007 grpc_subchannel_call *call = GET_CALL(calld);
1008 if (call != NULL && call != CANCELLED_CALL) {
1009 GRPC_SUBCHANNEL_CALL_UNREF(exec_ctx, call, "client_channel_destroy_call");
1010 }
1011 GPR_ASSERT(calld->creation_phase == GRPC_SUBCHANNEL_CALL_HOLDER_NOT_CREATING);
1012 gpr_mu_destroy(&calld->mu);
1013 GPR_ASSERT(calld->waiting_ops_count == 0);
1014 gpr_free(calld->waiting_ops);
Craig Tiller2c8063c2016-03-22 22:12:15 -07001015 gpr_free(and_free_memory);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001016}
1017
David Garcia Quintasf72eb972016-05-03 18:28:09 -07001018static void cc_set_pollset_or_pollset_set(grpc_exec_ctx *exec_ctx,
1019 grpc_call_element *elem,
David Garcia Quintas2a50dfe2016-05-31 15:09:12 -07001020 grpc_polling_entity *pollent) {
Craig Tiller577c9b22015-11-02 14:11:15 -08001021 call_data *calld = elem->call_data;
David Garcia Quintas2a50dfe2016-05-31 15:09:12 -07001022 calld->pollent = pollent;
Craig Tiller577c9b22015-11-02 14:11:15 -08001023}
1024
Mark D. Roth2a5959f2016-09-01 08:20:27 -07001025/*************************************************************************
1026 * EXPORTED SYMBOLS
1027 */
1028
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001029const grpc_channel_filter grpc_client_channel_filter = {
Craig Tillerf40df232016-03-25 13:38:14 -07001030 cc_start_transport_stream_op,
1031 cc_start_transport_op,
1032 sizeof(call_data),
Mark D. Roth2a5959f2016-09-01 08:20:27 -07001033 cc_init_call_elem,
David Garcia Quintas4afce7e2016-04-18 16:25:17 -07001034 cc_set_pollset_or_pollset_set,
Mark D. Roth2a5959f2016-09-01 08:20:27 -07001035 cc_destroy_call_elem,
Craig Tillerf40df232016-03-25 13:38:14 -07001036 sizeof(channel_data),
Mark D. Roth2a5959f2016-09-01 08:20:27 -07001037 cc_init_channel_elem,
1038 cc_destroy_channel_elem,
Craig Tillerf40df232016-03-25 13:38:14 -07001039 cc_get_peer,
1040 "client-channel",
Craig Tiller87d5b192015-04-16 14:37:57 -07001041};
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001042
Mark D. Roth60534972016-09-20 08:37:12 -07001043void grpc_client_channel_finish_initialization(
Mark D. Roth0e48a9a2016-09-08 14:14:39 -07001044 grpc_exec_ctx *exec_ctx, grpc_channel_stack *channel_stack,
1045 grpc_resolver *resolver,
1046 grpc_client_channel_factory *client_channel_factory) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001047 /* post construction initialization: set the transport setup pointer */
Mark D. Roth7f7d1652016-09-20 10:46:15 -07001048 GPR_ASSERT(client_channel_factory != NULL);
Craig Tillera82950e2015-09-22 12:33:20 -07001049 grpc_channel_element *elem = grpc_channel_stack_last_element(channel_stack);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001050 channel_data *chand = elem->channel_data;
Mark D. Rothff4df062016-08-22 15:02:49 -07001051 gpr_mu_lock(&chand->mu);
Craig Tillera82950e2015-09-22 12:33:20 -07001052 GPR_ASSERT(!chand->resolver);
Craig Tillerf5f17122015-06-25 08:47:26 -07001053 chand->resolver = resolver;
Craig Tillera82950e2015-09-22 12:33:20 -07001054 GRPC_RESOLVER_REF(resolver, "channel");
1055 if (!grpc_closure_list_empty(chand->waiting_for_config_closures) ||
1056 chand->exit_idle_when_lb_policy_arrives) {
Mark D. Roth4c0fe492016-08-31 13:51:55 -07001057 chand->started_resolving = true;
Craig Tiller906e3bc2015-11-24 07:31:31 -08001058 GRPC_CHANNEL_STACK_REF(chand->owning_stack, "resolver");
Mark D. Roth046cf762016-09-26 11:13:51 -07001059 grpc_resolver_next(exec_ctx, resolver, &chand->resolver_result,
Mark D. Rothff4df062016-08-22 15:02:49 -07001060 &chand->on_resolver_result_changed);
Craig Tillera82950e2015-09-22 12:33:20 -07001061 }
Mark D. Roth0e48a9a2016-09-08 14:14:39 -07001062 chand->client_channel_factory = client_channel_factory;
1063 grpc_client_channel_factory_ref(client_channel_factory);
Mark D. Rothff4df062016-08-22 15:02:49 -07001064 gpr_mu_unlock(&chand->mu);
Craig Tiller190d3602015-02-18 09:23:38 -08001065}
Craig Tiller48cb07c2015-07-15 16:16:15 -07001066
Craig Tillera82950e2015-09-22 12:33:20 -07001067grpc_connectivity_state grpc_client_channel_check_connectivity_state(
1068 grpc_exec_ctx *exec_ctx, grpc_channel_element *elem, int try_to_connect) {
Craig Tiller48cb07c2015-07-15 16:16:15 -07001069 channel_data *chand = elem->channel_data;
1070 grpc_connectivity_state out;
Mark D. Rothff4df062016-08-22 15:02:49 -07001071 gpr_mu_lock(&chand->mu);
Craig Tiller804ff712016-05-05 16:25:40 -07001072 out = grpc_connectivity_state_check(&chand->state_tracker, NULL);
Craig Tillera82950e2015-09-22 12:33:20 -07001073 if (out == GRPC_CHANNEL_IDLE && try_to_connect) {
1074 if (chand->lb_policy != NULL) {
1075 grpc_lb_policy_exit_idle(exec_ctx, chand->lb_policy);
1076 } else {
Mark D. Roth4c0fe492016-08-31 13:51:55 -07001077 chand->exit_idle_when_lb_policy_arrives = true;
Craig Tillera82950e2015-09-22 12:33:20 -07001078 if (!chand->started_resolving && chand->resolver != NULL) {
Craig Tiller906e3bc2015-11-24 07:31:31 -08001079 GRPC_CHANNEL_STACK_REF(chand->owning_stack, "resolver");
Mark D. Roth4c0fe492016-08-31 13:51:55 -07001080 chand->started_resolving = true;
Mark D. Roth046cf762016-09-26 11:13:51 -07001081 grpc_resolver_next(exec_ctx, chand->resolver, &chand->resolver_result,
Mark D. Rothff4df062016-08-22 15:02:49 -07001082 &chand->on_resolver_result_changed);
Craig Tillera82950e2015-09-22 12:33:20 -07001083 }
Craig Tiller48cb07c2015-07-15 16:16:15 -07001084 }
Craig Tillera82950e2015-09-22 12:33:20 -07001085 }
Mark D. Rothff4df062016-08-22 15:02:49 -07001086 gpr_mu_unlock(&chand->mu);
Craig Tiller48cb07c2015-07-15 16:16:15 -07001087 return out;
1088}
1089
Craig Tiller86c99582015-11-25 15:22:26 -08001090typedef struct {
1091 channel_data *chand;
1092 grpc_pollset *pollset;
1093 grpc_closure *on_complete;
1094 grpc_closure my_closure;
1095} external_connectivity_watcher;
1096
Craig Tiller1d881fb2015-12-01 07:39:04 -08001097static void on_external_watch_complete(grpc_exec_ctx *exec_ctx, void *arg,
Craig Tiller804ff712016-05-05 16:25:40 -07001098 grpc_error *error) {
Craig Tiller86c99582015-11-25 15:22:26 -08001099 external_connectivity_watcher *w = arg;
1100 grpc_closure *follow_up = w->on_complete;
Craig Tiller69b093b2016-02-25 19:04:07 -08001101 grpc_pollset_set_del_pollset(exec_ctx, w->chand->interested_parties,
Craig Tiller1d881fb2015-12-01 07:39:04 -08001102 w->pollset);
1103 GRPC_CHANNEL_STACK_UNREF(exec_ctx, w->chand->owning_stack,
1104 "external_connectivity_watcher");
Craig Tiller86c99582015-11-25 15:22:26 -08001105 gpr_free(w);
Craig Tiller804ff712016-05-05 16:25:40 -07001106 follow_up->cb(exec_ctx, follow_up->cb_arg, error);
Craig Tiller86c99582015-11-25 15:22:26 -08001107}
1108
Craig Tillera82950e2015-09-22 12:33:20 -07001109void grpc_client_channel_watch_connectivity_state(
Craig Tiller906e3bc2015-11-24 07:31:31 -08001110 grpc_exec_ctx *exec_ctx, grpc_channel_element *elem, grpc_pollset *pollset,
Craig Tillera82950e2015-09-22 12:33:20 -07001111 grpc_connectivity_state *state, grpc_closure *on_complete) {
Craig Tiller48cb07c2015-07-15 16:16:15 -07001112 channel_data *chand = elem->channel_data;
Craig Tiller86c99582015-11-25 15:22:26 -08001113 external_connectivity_watcher *w = gpr_malloc(sizeof(*w));
1114 w->chand = chand;
1115 w->pollset = pollset;
1116 w->on_complete = on_complete;
Craig Tiller69b093b2016-02-25 19:04:07 -08001117 grpc_pollset_set_add_pollset(exec_ctx, chand->interested_parties, pollset);
Craig Tiller86c99582015-11-25 15:22:26 -08001118 grpc_closure_init(&w->my_closure, on_external_watch_complete, w);
Craig Tiller1d881fb2015-12-01 07:39:04 -08001119 GRPC_CHANNEL_STACK_REF(w->chand->owning_stack,
1120 "external_connectivity_watcher");
Mark D. Rothff4df062016-08-22 15:02:49 -07001121 gpr_mu_lock(&chand->mu);
Craig Tillera82950e2015-09-22 12:33:20 -07001122 grpc_connectivity_state_notify_on_state_change(
Craig Tiller86c99582015-11-25 15:22:26 -08001123 exec_ctx, &chand->state_tracker, state, &w->my_closure);
Mark D. Rothff4df062016-08-22 15:02:49 -07001124 gpr_mu_unlock(&chand->mu);
Craig Tiller48cb07c2015-07-15 16:16:15 -07001125}