blob: 13d8036b8d6dde4f145d90e0b11c3b192b047c65 [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
Mark D. Roth2137cd82016-09-14 09:04:00 -070034#include "src/core/ext/client_channel/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>
Mark D. Rothb2d24882016-10-27 15:44:07 -070042#include <grpc/support/string_util.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080043#include <grpc/support/sync.h>
44#include <grpc/support/useful.h>
45
Mark D. Roth15195742016-10-07 09:02:28 -070046#include "src/core/ext/client_channel/lb_policy_registry.h"
Mark D. Roth21d4b2d2016-11-18 09:53:41 -080047#include "src/core/ext/client_channel/resolver_registry.h"
Mark D. Roth2137cd82016-09-14 09:04:00 -070048#include "src/core/ext/client_channel/subchannel.h"
Craig Tiller9533d042016-03-25 17:11:06 -070049#include "src/core/lib/channel/channel_args.h"
50#include "src/core/lib/channel/connected_channel.h"
Mark D. Roth72f6da82016-09-02 13:42:38 -070051#include "src/core/lib/channel/deadline_filter.h"
Craig Tiller9533d042016-03-25 17:11:06 -070052#include "src/core/lib/iomgr/iomgr.h"
Mark D. Roth4c0fe492016-08-31 13:51:55 -070053#include "src/core/lib/iomgr/polling_entity.h"
Craig Tiller9533d042016-03-25 17:11:06 -070054#include "src/core/lib/profiling/timers.h"
55#include "src/core/lib/support/string.h"
56#include "src/core/lib/surface/channel.h"
57#include "src/core/lib/transport/connectivity_state.h"
Mark D. Roth9fe284e2016-09-12 11:22:27 -070058#include "src/core/lib/transport/metadata.h"
59#include "src/core/lib/transport/metadata_batch.h"
Mark D. Rothbfe56802016-10-26 12:45:20 -070060#include "src/core/lib/transport/method_config.h"
Mark D. Roth9fe284e2016-09-12 11:22:27 -070061#include "src/core/lib/transport/static_metadata.h"
Craig Tiller8910ac62015-10-08 16:49:15 -070062
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080063/* Client channel implementation */
64
Mark D. Roth26b7be42016-10-24 10:08:07 -070065/*************************************************************************
66 * METHOD-CONFIG TABLE
67 */
68
Mark D. Roth9d480942016-10-19 14:18:05 -070069typedef enum {
70 WAIT_FOR_READY_UNSET,
71 WAIT_FOR_READY_FALSE,
72 WAIT_FOR_READY_TRUE
73} wait_for_ready_value;
74
75typedef struct method_parameters {
76 gpr_timespec timeout;
77 wait_for_ready_value wait_for_ready;
78} method_parameters;
79
80static void *method_parameters_copy(void *value) {
81 void *new_value = gpr_malloc(sizeof(method_parameters));
82 memcpy(new_value, value, sizeof(method_parameters));
83 return new_value;
84}
85
86static int method_parameters_cmp(void *value1, void *value2) {
87 const method_parameters *v1 = value1;
88 const method_parameters *v2 = value2;
89 const int retval = gpr_time_cmp(v1->timeout, v2->timeout);
90 if (retval != 0) return retval;
91 if (v1->wait_for_ready > v2->wait_for_ready) return 1;
92 if (v1->wait_for_ready < v2->wait_for_ready) return -1;
93 return 0;
94}
95
96static const grpc_mdstr_hash_table_vtable method_parameters_vtable = {
97 gpr_free, method_parameters_copy, method_parameters_cmp};
98
99static void *method_config_convert_value(
100 const grpc_method_config *method_config) {
101 method_parameters *value = gpr_malloc(sizeof(method_parameters));
102 const gpr_timespec *timeout = grpc_method_config_get_timeout(method_config);
103 value->timeout = timeout != NULL ? *timeout : gpr_time_0(GPR_TIMESPAN);
104 const bool *wait_for_ready =
105 grpc_method_config_get_wait_for_ready(method_config);
106 value->wait_for_ready =
107 wait_for_ready == NULL
108 ? WAIT_FOR_READY_UNSET
109 : (wait_for_ready ? WAIT_FOR_READY_TRUE : WAIT_FOR_READY_FALSE);
110 return value;
111}
112
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700113/*************************************************************************
114 * CHANNEL-WIDE FUNCTIONS
115 */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800116
Craig Tiller800dacb2015-10-06 09:10:26 -0700117typedef struct client_channel_channel_data {
Craig Tillerf5f17122015-06-25 08:47:26 -0700118 /** resolver for this channel */
119 grpc_resolver *resolver;
Craig Tiller20a3c352015-08-05 08:39:50 -0700120 /** have we started resolving this channel */
Mark D. Roth4c0fe492016-08-31 13:51:55 -0700121 bool started_resolving;
Mark D. Roth0e48a9a2016-09-08 14:14:39 -0700122 /** client channel factory */
123 grpc_client_channel_factory *client_channel_factory;
Craig Tillerf5f17122015-06-25 08:47:26 -0700124
Mark D. Roth046cf762016-09-26 11:13:51 -0700125 /** mutex protecting all variables below in this data structure */
Mark D. Rothff4df062016-08-22 15:02:49 -0700126 gpr_mu mu;
Mark D. Roth046cf762016-09-26 11:13:51 -0700127 /** currently active load balancer */
Mark D. Roth78afd772016-11-04 12:49:49 -0700128 char *lb_policy_name;
Craig Tillerf5f17122015-06-25 08:47:26 -0700129 grpc_lb_policy *lb_policy;
Mark D. Roth9d480942016-10-19 14:18:05 -0700130 /** maps method names to method_parameters structs */
131 grpc_mdstr_hash_table *method_params_table;
Mark D. Roth046cf762016-09-26 11:13:51 -0700132 /** incoming resolver result - set by resolver.next() */
Mark D. Rothaf842452016-10-21 15:05:15 -0700133 grpc_channel_args *resolver_result;
Craig Tiller3f475422015-06-25 10:43:05 -0700134 /** a list of closures that are all waiting for config to come in */
Craig Tillerd9ccbbf2015-09-22 09:30:00 -0700135 grpc_closure_list waiting_for_config_closures;
Craig Tiller3f475422015-06-25 10:43:05 -0700136 /** resolver callback */
Mark D. Rothff4df062016-08-22 15:02:49 -0700137 grpc_closure on_resolver_result_changed;
Craig Tiller3f475422015-06-25 10:43:05 -0700138 /** connectivity state being tracked */
Craig Tillerca3e9d32015-06-27 18:37:27 -0700139 grpc_connectivity_state_tracker state_tracker;
Craig Tiller48cb07c2015-07-15 16:16:15 -0700140 /** when an lb_policy arrives, should we try to exit idle */
Mark D. Roth4c0fe492016-08-31 13:51:55 -0700141 bool exit_idle_when_lb_policy_arrives;
Craig Tiller906e3bc2015-11-24 07:31:31 -0800142 /** owning stack */
143 grpc_channel_stack *owning_stack;
Craig Tiller69b093b2016-02-25 19:04:07 -0800144 /** interested parties (owned) */
145 grpc_pollset_set *interested_parties;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800146} channel_data;
147
Craig Tillerd6c98df2015-08-18 09:33:44 -0700148/** We create one watcher for each new lb_policy that is returned from a
Mark D. Roth4c0fe492016-08-31 13:51:55 -0700149 resolver, to watch for state changes from the lb_policy. When a state
150 change is seen, we update the channel, and create a new watcher. */
Craig Tillera82950e2015-09-22 12:33:20 -0700151typedef struct {
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700152 channel_data *chand;
Craig Tiller33825112015-09-18 07:44:19 -0700153 grpc_closure on_changed;
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700154 grpc_connectivity_state state;
155 grpc_lb_policy *lb_policy;
156} lb_policy_connectivity_watcher;
157
Craig Tillera82950e2015-09-22 12:33:20 -0700158static void watch_lb_policy(grpc_exec_ctx *exec_ctx, channel_data *chand,
159 grpc_lb_policy *lb_policy,
160 grpc_connectivity_state current_state);
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700161
Craig Tiller8c0d96f2016-03-11 14:27:52 -0800162static void set_channel_connectivity_state_locked(grpc_exec_ctx *exec_ctx,
163 channel_data *chand,
164 grpc_connectivity_state state,
Craig Tiller804ff712016-05-05 16:25:40 -0700165 grpc_error *error,
Craig Tiller8c0d96f2016-03-11 14:27:52 -0800166 const char *reason) {
167 if ((state == GRPC_CHANNEL_TRANSIENT_FAILURE ||
Craig Tiller48ed92e2016-06-02 11:07:12 -0700168 state == GRPC_CHANNEL_SHUTDOWN) &&
Craig Tiller8c0d96f2016-03-11 14:27:52 -0800169 chand->lb_policy != NULL) {
Mark D. Roth59c9f902016-09-28 13:33:21 -0700170 /* cancel picks with wait_for_ready=false */
Craig Tiller8c0d96f2016-03-11 14:27:52 -0800171 grpc_lb_policy_cancel_picks(
172 exec_ctx, chand->lb_policy,
Mark D. Roth59c9f902016-09-28 13:33:21 -0700173 /* mask= */ GRPC_INITIAL_METADATA_WAIT_FOR_READY,
Mark D. Roth58f52b72016-09-09 13:55:18 -0700174 /* check= */ 0, GRPC_ERROR_REF(error));
Craig Tiller8c0d96f2016-03-11 14:27:52 -0800175 }
Craig Tiller9ccf5f12016-05-07 21:41:01 -0700176 grpc_connectivity_state_set(exec_ctx, &chand->state_tracker, state, error,
177 reason);
Craig Tiller8c0d96f2016-03-11 14:27:52 -0800178}
179
Craig Tiller804ff712016-05-05 16:25:40 -0700180static void on_lb_policy_state_changed_locked(grpc_exec_ctx *exec_ctx,
181 lb_policy_connectivity_watcher *w,
182 grpc_error *error) {
Craig Tillercb2609f2015-11-24 17:19:19 -0800183 grpc_connectivity_state publish_state = w->state;
Craig Tiller5795da72015-09-17 15:27:13 -0700184 /* check if the notification is for a stale policy */
Craig Tillera82950e2015-09-22 12:33:20 -0700185 if (w->lb_policy != w->chand->lb_policy) return;
Craig Tiller5795da72015-09-17 15:27:13 -0700186
Craig Tiller48ed92e2016-06-02 11:07:12 -0700187 if (publish_state == GRPC_CHANNEL_SHUTDOWN && w->chand->resolver != NULL) {
Craig Tillercb2609f2015-11-24 17:19:19 -0800188 publish_state = GRPC_CHANNEL_TRANSIENT_FAILURE;
189 grpc_resolver_channel_saw_error(exec_ctx, w->chand->resolver);
Craig Tillerf62c4d52015-12-04 07:43:07 -0800190 GRPC_LB_POLICY_UNREF(exec_ctx, w->chand->lb_policy, "channel");
191 w->chand->lb_policy = NULL;
Craig Tillercb2609f2015-11-24 17:19:19 -0800192 }
Craig Tiller8c0d96f2016-03-11 14:27:52 -0800193 set_channel_connectivity_state_locked(exec_ctx, w->chand, publish_state,
Craig Tillerfc353d62016-05-10 12:58:03 -0700194 GRPC_ERROR_REF(error), "lb_changed");
Craig Tiller48ed92e2016-06-02 11:07:12 -0700195 if (w->state != GRPC_CHANNEL_SHUTDOWN) {
Craig Tillera82950e2015-09-22 12:33:20 -0700196 watch_lb_policy(exec_ctx, w->chand, w->lb_policy, w->state);
197 }
Craig Tiller5795da72015-09-17 15:27:13 -0700198}
199
Craig Tillera82950e2015-09-22 12:33:20 -0700200static void on_lb_policy_state_changed(grpc_exec_ctx *exec_ctx, void *arg,
Craig Tiller804ff712016-05-05 16:25:40 -0700201 grpc_error *error) {
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700202 lb_policy_connectivity_watcher *w = arg;
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700203
Mark D. Rothff4df062016-08-22 15:02:49 -0700204 gpr_mu_lock(&w->chand->mu);
Craig Tiller804ff712016-05-05 16:25:40 -0700205 on_lb_policy_state_changed_locked(exec_ctx, w, error);
Mark D. Rothff4df062016-08-22 15:02:49 -0700206 gpr_mu_unlock(&w->chand->mu);
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700207
Craig Tiller906e3bc2015-11-24 07:31:31 -0800208 GRPC_CHANNEL_STACK_UNREF(exec_ctx, w->chand->owning_stack, "watch_lb_policy");
Craig Tillera82950e2015-09-22 12:33:20 -0700209 gpr_free(w);
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700210}
211
Craig Tillera82950e2015-09-22 12:33:20 -0700212static void watch_lb_policy(grpc_exec_ctx *exec_ctx, channel_data *chand,
213 grpc_lb_policy *lb_policy,
214 grpc_connectivity_state current_state) {
215 lb_policy_connectivity_watcher *w = gpr_malloc(sizeof(*w));
Craig Tiller906e3bc2015-11-24 07:31:31 -0800216 GRPC_CHANNEL_STACK_REF(chand->owning_stack, "watch_lb_policy");
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700217
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700218 w->chand = chand;
Craig Tillera82950e2015-09-22 12:33:20 -0700219 grpc_closure_init(&w->on_changed, on_lb_policy_state_changed, w);
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700220 w->state = current_state;
221 w->lb_policy = lb_policy;
Craig Tillera82950e2015-09-22 12:33:20 -0700222 grpc_lb_policy_notify_on_state_change(exec_ctx, lb_policy, &w->state,
223 &w->on_changed);
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700224}
225
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700226static void on_resolver_result_changed(grpc_exec_ctx *exec_ctx, void *arg,
227 grpc_error *error) {
Craig Tiller3f475422015-06-25 10:43:05 -0700228 channel_data *chand = arg;
Mark D. Rothb2d24882016-10-27 15:44:07 -0700229 char *lb_policy_name = NULL;
Craig Tiller3f475422015-06-25 10:43:05 -0700230 grpc_lb_policy *lb_policy = NULL;
231 grpc_lb_policy *old_lb_policy;
Mark D. Roth9d480942016-10-19 14:18:05 -0700232 grpc_mdstr_hash_table *method_params_table = NULL;
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700233 grpc_connectivity_state state = GRPC_CHANNEL_TRANSIENT_FAILURE;
Mark D. Roth4c0fe492016-08-31 13:51:55 -0700234 bool exit_idle = false;
Craig Tiller804ff712016-05-05 16:25:40 -0700235 grpc_error *state_error = GRPC_ERROR_CREATE("No load balancing policy");
Craig Tiller3f475422015-06-25 10:43:05 -0700236
Mark D. Roth046cf762016-09-26 11:13:51 -0700237 if (chand->resolver_result != NULL) {
Mark D. Roth0e48a9a2016-09-08 14:14:39 -0700238 grpc_lb_policy_args lb_policy_args;
Mark D. Rothaf842452016-10-21 15:05:15 -0700239 lb_policy_args.args = chand->resolver_result;
Mark D. Roth0e48a9a2016-09-08 14:14:39 -0700240 lb_policy_args.client_channel_factory = chand->client_channel_factory;
Mark D. Roth88405f72016-10-03 08:24:52 -0700241
Mark D. Roth5bd7be02016-10-21 14:19:50 -0700242 // Find LB policy name.
Mark D. Rothaf842452016-10-21 15:05:15 -0700243 const grpc_arg *channel_arg =
Mark D. Roth5bd7be02016-10-21 14:19:50 -0700244 grpc_channel_args_find(lb_policy_args.args, GRPC_ARG_LB_POLICY_NAME);
Mark D. Rothaf842452016-10-21 15:05:15 -0700245 if (channel_arg != NULL) {
246 GPR_ASSERT(channel_arg->type == GRPC_ARG_STRING);
247 lb_policy_name = channel_arg->value.string;
Mark D. Roth5bd7be02016-10-21 14:19:50 -0700248 }
Mark D. Roth88405f72016-10-03 08:24:52 -0700249 // Special case: If all of the addresses are balancer addresses,
250 // assume that we should use the grpclb policy, regardless of what the
251 // resolver actually specified.
Mark D. Rothaf842452016-10-21 15:05:15 -0700252 channel_arg =
253 grpc_channel_args_find(lb_policy_args.args, GRPC_ARG_LB_ADDRESSES);
254 if (channel_arg != NULL) {
255 GPR_ASSERT(channel_arg->type == GRPC_ARG_POINTER);
Mark D. Roth557c9902016-10-24 11:12:05 -0700256 grpc_lb_addresses *addresses = channel_arg->value.pointer.p;
Mark D. Rothaf842452016-10-21 15:05:15 -0700257 bool found_backend_address = false;
258 for (size_t i = 0; i < addresses->num_addresses; ++i) {
259 if (!addresses->addresses[i].is_balancer) {
260 found_backend_address = true;
261 break;
262 }
Mark D. Roth88405f72016-10-03 08:24:52 -0700263 }
Mark D. Rothaf842452016-10-21 15:05:15 -0700264 if (!found_backend_address) {
265 if (lb_policy_name != NULL && strcmp(lb_policy_name, "grpclb") != 0) {
266 gpr_log(GPR_INFO,
267 "resolver requested LB policy %s but provided only balancer "
268 "addresses, no backend addresses -- forcing use of grpclb LB "
269 "policy",
Mark D. Roth5f40e5d2016-10-24 13:09:05 -0700270 lb_policy_name);
Mark D. Rothaf842452016-10-21 15:05:15 -0700271 }
272 lb_policy_name = "grpclb";
Mark D. Roth88405f72016-10-03 08:24:52 -0700273 }
Mark D. Roth88405f72016-10-03 08:24:52 -0700274 }
275 // Use pick_first if nothing was specified and we didn't select grpclb
276 // above.
277 if (lb_policy_name == NULL) lb_policy_name = "pick_first";
278
279 lb_policy =
280 grpc_lb_policy_create(exec_ctx, lb_policy_name, &lb_policy_args);
Craig Tillera82950e2015-09-22 12:33:20 -0700281 if (lb_policy != NULL) {
Craig Tillera82950e2015-09-22 12:33:20 -0700282 GRPC_LB_POLICY_REF(lb_policy, "config_change");
Craig Tillerf707d622016-05-06 14:26:12 -0700283 GRPC_ERROR_UNREF(state_error);
Craig Tiller804ff712016-05-05 16:25:40 -0700284 state =
285 grpc_lb_policy_check_connectivity(exec_ctx, lb_policy, &state_error);
Craig Tiller45724b32015-09-22 10:42:19 -0700286 }
Mark D. Rothaf842452016-10-21 15:05:15 -0700287 channel_arg =
288 grpc_channel_args_find(lb_policy_args.args, GRPC_ARG_SERVICE_CONFIG);
Mark D. Roth046cf762016-09-26 11:13:51 -0700289 if (channel_arg != NULL) {
290 GPR_ASSERT(channel_arg->type == GRPC_ARG_POINTER);
Mark D. Roth9d480942016-10-19 14:18:05 -0700291 method_params_table = grpc_method_config_table_convert(
292 (grpc_method_config_table *)channel_arg->value.pointer.p,
293 method_config_convert_value, &method_parameters_vtable);
Mark D. Roth9fe284e2016-09-12 11:22:27 -0700294 }
Mark D. Rothf79ce7d2016-11-04 08:43:36 -0700295 // Before we clean up, save a copy of lb_policy_name, since it might
296 // be pointing to data inside chand->resolver_result.
297 // The copy will be saved in chand->lb_policy_name below.
298 lb_policy_name = gpr_strdup(lb_policy_name);
Mark D. Rothaf842452016-10-21 15:05:15 -0700299 grpc_channel_args_destroy(chand->resolver_result);
Mark D. Roth046cf762016-09-26 11:13:51 -0700300 chand->resolver_result = NULL;
Craig Tillera82950e2015-09-22 12:33:20 -0700301 }
302
Craig Tiller86c99582015-11-25 15:22:26 -0800303 if (lb_policy != NULL) {
Craig Tiller69b093b2016-02-25 19:04:07 -0800304 grpc_pollset_set_add_pollset_set(exec_ctx, lb_policy->interested_parties,
305 chand->interested_parties);
Craig Tiller86c99582015-11-25 15:22:26 -0800306 }
307
Mark D. Rothff4df062016-08-22 15:02:49 -0700308 gpr_mu_lock(&chand->mu);
Mark D. Rothb2d24882016-10-27 15:44:07 -0700309 if (lb_policy_name != NULL) {
310 gpr_free(chand->lb_policy_name);
311 chand->lb_policy_name = lb_policy_name;
312 }
Craig Tiller3f475422015-06-25 10:43:05 -0700313 old_lb_policy = chand->lb_policy;
314 chand->lb_policy = lb_policy;
Mark D. Roth9d480942016-10-19 14:18:05 -0700315 if (chand->method_params_table != NULL) {
316 grpc_mdstr_hash_table_unref(chand->method_params_table);
Mark D. Roth046cf762016-09-26 11:13:51 -0700317 }
Mark D. Roth9d480942016-10-19 14:18:05 -0700318 chand->method_params_table = method_params_table;
Craig Tiller0ede5452016-04-23 12:21:45 -0700319 if (lb_policy != NULL) {
320 grpc_exec_ctx_enqueue_list(exec_ctx, &chand->waiting_for_config_closures,
321 NULL);
322 } else if (chand->resolver == NULL /* disconnected */) {
Craig Tiller804ff712016-05-05 16:25:40 -0700323 grpc_closure_list_fail_all(
324 &chand->waiting_for_config_closures,
325 GRPC_ERROR_CREATE_REFERENCING("Channel disconnected", &error, 1));
Craig Tiller6c396862016-01-28 13:53:40 -0800326 grpc_exec_ctx_enqueue_list(exec_ctx, &chand->waiting_for_config_closures,
327 NULL);
Craig Tillera82950e2015-09-22 12:33:20 -0700328 }
329 if (lb_policy != NULL && chand->exit_idle_when_lb_policy_arrives) {
330 GRPC_LB_POLICY_REF(lb_policy, "exit_idle");
Mark D. Roth4c0fe492016-08-31 13:51:55 -0700331 exit_idle = true;
332 chand->exit_idle_when_lb_policy_arrives = false;
Craig Tillera82950e2015-09-22 12:33:20 -0700333 }
Craig Tiller98465032015-06-29 14:36:42 -0700334
Craig Tiller804ff712016-05-05 16:25:40 -0700335 if (error == GRPC_ERROR_NONE && chand->resolver) {
Craig Tiller9ccf5f12016-05-07 21:41:01 -0700336 set_channel_connectivity_state_locked(
337 exec_ctx, chand, state, GRPC_ERROR_REF(state_error), "new_lb+resolver");
Craig Tillera82950e2015-09-22 12:33:20 -0700338 if (lb_policy != NULL) {
339 watch_lb_policy(exec_ctx, chand, lb_policy, state);
Craig Tiller45724b32015-09-22 10:42:19 -0700340 }
Craig Tiller906e3bc2015-11-24 07:31:31 -0800341 GRPC_CHANNEL_STACK_REF(chand->owning_stack, "resolver");
Mark D. Roth046cf762016-09-26 11:13:51 -0700342 grpc_resolver_next(exec_ctx, chand->resolver, &chand->resolver_result,
Mark D. Rothff4df062016-08-22 15:02:49 -0700343 &chand->on_resolver_result_changed);
344 gpr_mu_unlock(&chand->mu);
Craig Tillera82950e2015-09-22 12:33:20 -0700345 } else {
Craig Tiller76a5c0e2016-03-09 09:05:30 -0800346 if (chand->resolver != NULL) {
347 grpc_resolver_shutdown(exec_ctx, chand->resolver);
348 GRPC_RESOLVER_UNREF(exec_ctx, chand->resolver, "channel");
349 chand->resolver = NULL;
350 }
Craig Tiller804ff712016-05-05 16:25:40 -0700351 grpc_error *refs[] = {error, state_error};
Craig Tiller8c0d96f2016-03-11 14:27:52 -0800352 set_channel_connectivity_state_locked(
Craig Tillerd925c932016-06-06 08:38:50 -0700353 exec_ctx, chand, GRPC_CHANNEL_SHUTDOWN,
Craig Tiller804ff712016-05-05 16:25:40 -0700354 GRPC_ERROR_CREATE_REFERENCING("Got config after disconnection", refs,
355 GPR_ARRAY_SIZE(refs)),
356 "resolver_gone");
Mark D. Rothff4df062016-08-22 15:02:49 -0700357 gpr_mu_unlock(&chand->mu);
Craig Tillera82950e2015-09-22 12:33:20 -0700358 }
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700359
Craig Tillera82950e2015-09-22 12:33:20 -0700360 if (exit_idle) {
361 grpc_lb_policy_exit_idle(exec_ctx, lb_policy);
362 GRPC_LB_POLICY_UNREF(exec_ctx, lb_policy, "exit_idle");
363 }
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700364
Craig Tillera82950e2015-09-22 12:33:20 -0700365 if (old_lb_policy != NULL) {
Craig Tiller69b093b2016-02-25 19:04:07 -0800366 grpc_pollset_set_del_pollset_set(
367 exec_ctx, old_lb_policy->interested_parties, chand->interested_parties);
Craig Tillera82950e2015-09-22 12:33:20 -0700368 GRPC_LB_POLICY_UNREF(exec_ctx, old_lb_policy, "channel");
369 }
Craig Tiller000cd8f2015-09-18 07:20:29 -0700370
Craig Tillera82950e2015-09-22 12:33:20 -0700371 if (lb_policy != NULL) {
372 GRPC_LB_POLICY_UNREF(exec_ctx, lb_policy, "config_change");
373 }
Craig Tiller45724b32015-09-22 10:42:19 -0700374
Craig Tiller906e3bc2015-11-24 07:31:31 -0800375 GRPC_CHANNEL_STACK_UNREF(exec_ctx, chand->owning_stack, "resolver");
Craig Tiller9ccf5f12016-05-07 21:41:01 -0700376 GRPC_ERROR_UNREF(state_error);
Craig Tiller3f475422015-06-25 10:43:05 -0700377}
378
Craig Tillera82950e2015-09-22 12:33:20 -0700379static void cc_start_transport_op(grpc_exec_ctx *exec_ctx,
380 grpc_channel_element *elem,
381 grpc_transport_op *op) {
Craig Tillerca3e9d32015-06-27 18:37:27 -0700382 channel_data *chand = elem->channel_data;
Craig Tiller000cd8f2015-09-18 07:20:29 -0700383
Craig Tiller332f1b32016-05-24 13:21:21 -0700384 grpc_exec_ctx_sched(exec_ctx, op->on_consumed, GRPC_ERROR_NONE, NULL);
Craig Tillerca3e9d32015-06-27 18:37:27 -0700385
Craig Tillerd7f12e32016-03-03 10:08:31 -0800386 GPR_ASSERT(op->set_accept_stream == false);
Craig Tiller28bf8912015-12-07 16:07:04 -0800387 if (op->bind_pollset != NULL) {
Craig Tiller69b093b2016-02-25 19:04:07 -0800388 grpc_pollset_set_add_pollset(exec_ctx, chand->interested_parties,
Craig Tillere2c62372015-12-07 16:11:03 -0800389 op->bind_pollset);
Craig Tiller28bf8912015-12-07 16:07:04 -0800390 }
Craig Tillerca3e9d32015-06-27 18:37:27 -0700391
Mark D. Rothff4df062016-08-22 15:02:49 -0700392 gpr_mu_lock(&chand->mu);
Craig Tillera82950e2015-09-22 12:33:20 -0700393 if (op->on_connectivity_state_change != NULL) {
394 grpc_connectivity_state_notify_on_state_change(
395 exec_ctx, &chand->state_tracker, op->connectivity_state,
396 op->on_connectivity_state_change);
397 op->on_connectivity_state_change = NULL;
398 op->connectivity_state = NULL;
399 }
400
Craig Tiller26dab312015-12-07 14:43:47 -0800401 if (op->send_ping != NULL) {
Craig Tiller87b71e22015-12-07 15:14:14 -0800402 if (chand->lb_policy == NULL) {
Craig Tiller332f1b32016-05-24 13:21:21 -0700403 grpc_exec_ctx_sched(exec_ctx, op->send_ping,
404 GRPC_ERROR_CREATE("Ping with no load balancing"),
405 NULL);
Craig Tiller26dab312015-12-07 14:43:47 -0800406 } else {
Craig Tiller28bf8912015-12-07 16:07:04 -0800407 grpc_lb_policy_ping_one(exec_ctx, chand->lb_policy, op->send_ping);
Craig Tiller26dab312015-12-07 14:43:47 -0800408 op->bind_pollset = NULL;
409 }
410 op->send_ping = NULL;
411 }
412
Craig Tiller1c51edc2016-05-07 16:18:43 -0700413 if (op->disconnect_with_error != GRPC_ERROR_NONE) {
414 if (chand->resolver != NULL) {
415 set_channel_connectivity_state_locked(
Craig Tillerd925c932016-06-06 08:38:50 -0700416 exec_ctx, chand, GRPC_CHANNEL_SHUTDOWN,
Craig Tiller1c51edc2016-05-07 16:18:43 -0700417 GRPC_ERROR_REF(op->disconnect_with_error), "disconnect");
418 grpc_resolver_shutdown(exec_ctx, chand->resolver);
419 GRPC_RESOLVER_UNREF(exec_ctx, chand->resolver, "channel");
420 chand->resolver = NULL;
421 if (!chand->started_resolving) {
422 grpc_closure_list_fail_all(&chand->waiting_for_config_closures,
423 GRPC_ERROR_REF(op->disconnect_with_error));
Craig Tiller9ccf5f12016-05-07 21:41:01 -0700424 grpc_exec_ctx_enqueue_list(exec_ctx,
425 &chand->waiting_for_config_closures, NULL);
Craig Tiller1c51edc2016-05-07 16:18:43 -0700426 }
427 if (chand->lb_policy != NULL) {
428 grpc_pollset_set_del_pollset_set(exec_ctx,
429 chand->lb_policy->interested_parties,
430 chand->interested_parties);
431 GRPC_LB_POLICY_UNREF(exec_ctx, chand->lb_policy, "channel");
432 chand->lb_policy = NULL;
433 }
Craig Tillerb12d22a2016-04-23 12:50:21 -0700434 }
Craig Tiller1c51edc2016-05-07 16:18:43 -0700435 GRPC_ERROR_UNREF(op->disconnect_with_error);
Craig Tillera82950e2015-09-22 12:33:20 -0700436 }
Mark D. Rothff4df062016-08-22 15:02:49 -0700437 gpr_mu_unlock(&chand->mu);
Craig Tillerca3e9d32015-06-27 18:37:27 -0700438}
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800439
Mark D. Rothb2d24882016-10-27 15:44:07 -0700440static void cc_get_channel_info(grpc_exec_ctx *exec_ctx,
441 grpc_channel_element *elem,
Mark D. Rothf79ce7d2016-11-04 08:43:36 -0700442 const grpc_channel_info *info) {
Mark D. Rothb2d24882016-10-27 15:44:07 -0700443 channel_data *chand = elem->channel_data;
444 gpr_mu_lock(&chand->mu);
445 if (info->lb_policy_name != NULL) {
446 *info->lb_policy_name = chand->lb_policy_name == NULL
Mark D. Roth78afd772016-11-04 12:49:49 -0700447 ? NULL
448 : gpr_strdup(chand->lb_policy_name);
Mark D. Rothb2d24882016-10-27 15:44:07 -0700449 }
450 gpr_mu_unlock(&chand->mu);
451}
452
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700453/* Constructor for channel_data */
Mark D. Roth5e2566e2016-11-18 10:53:13 -0800454static grpc_error* cc_init_channel_elem(grpc_exec_ctx *exec_ctx,
455 grpc_channel_element *elem,
456 grpc_channel_element_args *args) {
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700457 channel_data *chand = elem->channel_data;
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700458 memset(chand, 0, sizeof(*chand));
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700459 GPR_ASSERT(args->is_last);
460 GPR_ASSERT(elem->filter == &grpc_client_channel_filter);
Mark D. Roth21d4b2d2016-11-18 09:53:41 -0800461 // Initialize data members.
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700462 gpr_mu_init(&chand->mu);
Mark D. Roth21d4b2d2016-11-18 09:53:41 -0800463 chand->owning_stack = args->channel_stack;
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700464 grpc_closure_init(&chand->on_resolver_result_changed,
465 on_resolver_result_changed, chand);
Mark D. Roth21d4b2d2016-11-18 09:53:41 -0800466 chand->interested_parties = grpc_pollset_set_create();
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700467 grpc_connectivity_state_init(&chand->state_tracker, GRPC_CHANNEL_IDLE,
468 "client_channel");
Mark D. Roth21d4b2d2016-11-18 09:53:41 -0800469 // Record client channel factory.
470 const grpc_arg *arg = grpc_channel_args_find(args->channel_args,
471 GRPC_ARG_CLIENT_CHANNEL_FACTORY);
472 GPR_ASSERT(arg != NULL);
473 GPR_ASSERT(arg->type == GRPC_ARG_POINTER);
474 grpc_client_channel_factory_ref(arg->value.pointer.p);
475 chand->client_channel_factory = arg->value.pointer.p;
476 // Instantiate resolver.
Mark D. Roth86e90592016-11-18 09:56:40 -0800477 arg = grpc_channel_args_find(args->channel_args, GRPC_ARG_SERVER_URI);
Mark D. Roth21d4b2d2016-11-18 09:53:41 -0800478 GPR_ASSERT(arg != NULL);
479 GPR_ASSERT(arg->type == GRPC_ARG_STRING);
480 chand->resolver = grpc_resolver_create(arg->value.string, args->channel_args);
Mark D. Roth5e2566e2016-11-18 10:53:13 -0800481 if (chand->resolver == NULL) {
482 return GRPC_ERROR_CREATE("resolver creation failed");
483 }
484 return GRPC_ERROR_NONE;
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700485}
486
487/* Destructor for channel_data */
488static void cc_destroy_channel_elem(grpc_exec_ctx *exec_ctx,
489 grpc_channel_element *elem) {
490 channel_data *chand = elem->channel_data;
491
492 if (chand->resolver != NULL) {
493 grpc_resolver_shutdown(exec_ctx, chand->resolver);
494 GRPC_RESOLVER_UNREF(exec_ctx, chand->resolver, "channel");
495 }
Mark D. Roth0e48a9a2016-09-08 14:14:39 -0700496 if (chand->client_channel_factory != NULL) {
497 grpc_client_channel_factory_unref(exec_ctx, chand->client_channel_factory);
498 }
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700499 if (chand->lb_policy != NULL) {
500 grpc_pollset_set_del_pollset_set(exec_ctx,
501 chand->lb_policy->interested_parties,
502 chand->interested_parties);
503 GRPC_LB_POLICY_UNREF(exec_ctx, chand->lb_policy, "channel");
504 }
Mark D. Rothb2d24882016-10-27 15:44:07 -0700505 gpr_free(chand->lb_policy_name);
Mark D. Roth9d480942016-10-19 14:18:05 -0700506 if (chand->method_params_table != NULL) {
507 grpc_mdstr_hash_table_unref(chand->method_params_table);
Mark D. Roth9fe284e2016-09-12 11:22:27 -0700508 }
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700509 grpc_connectivity_state_destroy(exec_ctx, &chand->state_tracker);
510 grpc_pollset_set_destroy(chand->interested_parties);
511 gpr_mu_destroy(&chand->mu);
512}
513
514/*************************************************************************
515 * PER-CALL FUNCTIONS
516 */
517
518#define GET_CALL(call_data) \
519 ((grpc_subchannel_call *)(gpr_atm_acq_load(&(call_data)->subchannel_call)))
520
521#define CANCELLED_CALL ((grpc_subchannel_call *)1)
522
523typedef enum {
524 GRPC_SUBCHANNEL_CALL_HOLDER_NOT_CREATING,
525 GRPC_SUBCHANNEL_CALL_HOLDER_PICKING_SUBCHANNEL
526} subchannel_creation_phase;
527
528/** Call data. Holds a pointer to grpc_subchannel_call and the
529 associated machinery to create such a pointer.
530 Handles queueing of stream ops until a call object is ready, waiting
531 for initial metadata before trying to create a call object,
532 and handling cancellation gracefully. */
533typedef struct client_channel_call_data {
Mark D. Roth72f6da82016-09-02 13:42:38 -0700534 // State for handling deadlines.
535 // The code in deadline_filter.c requires this to be the first field.
Mark D. Roth72f6da82016-09-02 13:42:38 -0700536 // TODO(roth): This is slightly sub-optimal in that grpc_deadline_state
537 // and this struct both independently store a pointer to the call
538 // stack and each has its own mutex. If/when we have time, find a way
Mark D. Roth6ad99172016-09-09 07:52:48 -0700539 // to avoid this without breaking the grpc_deadline_state abstraction.
Mark D. Roth72f6da82016-09-02 13:42:38 -0700540 grpc_deadline_state deadline_state;
Mark D. Rothf28763c2016-09-14 15:18:40 -0700541
Mark D. Rothe40dd292016-10-05 14:58:37 -0700542 grpc_mdstr *path; // Request path.
543 gpr_timespec call_start_time;
544 gpr_timespec deadline;
Mark D. Roth9d480942016-10-19 14:18:05 -0700545 wait_for_ready_value wait_for_ready_from_service_config;
Mark D. Rothe40dd292016-10-05 14:58:37 -0700546 grpc_closure read_service_config;
Mark D. Rothaa850a72016-09-26 13:38:02 -0700547
Mark D. Rothf28763c2016-09-14 15:18:40 -0700548 grpc_error *cancel_error;
Mark D. Roth72f6da82016-09-02 13:42:38 -0700549
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700550 /** either 0 for no call, 1 for cancelled, or a pointer to a
551 grpc_subchannel_call */
552 gpr_atm subchannel_call;
553
554 gpr_mu mu;
555
556 subchannel_creation_phase creation_phase;
557 grpc_connected_subchannel *connected_subchannel;
558 grpc_polling_entity *pollent;
559
Craig Tiller57726ca2016-09-12 11:59:45 -0700560 grpc_transport_stream_op **waiting_ops;
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700561 size_t waiting_ops_count;
562 size_t waiting_ops_capacity;
563
564 grpc_closure next_step;
565
566 grpc_call_stack *owning_call;
David Garcia Quintasd1a47f12016-09-02 12:46:44 +0200567
568 grpc_linked_mdelem lb_token_mdelem;
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700569} call_data;
570
571static void add_waiting_locked(call_data *calld, grpc_transport_stream_op *op) {
572 GPR_TIMER_BEGIN("add_waiting_locked", 0);
573 if (calld->waiting_ops_count == calld->waiting_ops_capacity) {
574 calld->waiting_ops_capacity = GPR_MAX(3, 2 * calld->waiting_ops_capacity);
575 calld->waiting_ops =
576 gpr_realloc(calld->waiting_ops,
577 calld->waiting_ops_capacity * sizeof(*calld->waiting_ops));
578 }
Craig Tiller57726ca2016-09-12 11:59:45 -0700579 calld->waiting_ops[calld->waiting_ops_count++] = op;
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700580 GPR_TIMER_END("add_waiting_locked", 0);
581}
582
583static void fail_locked(grpc_exec_ctx *exec_ctx, call_data *calld,
584 grpc_error *error) {
585 size_t i;
586 for (i = 0; i < calld->waiting_ops_count; i++) {
587 grpc_transport_stream_op_finish_with_failure(
Craig Tiller57726ca2016-09-12 11:59:45 -0700588 exec_ctx, calld->waiting_ops[i], GRPC_ERROR_REF(error));
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700589 }
590 calld->waiting_ops_count = 0;
591 GRPC_ERROR_UNREF(error);
592}
593
594typedef struct {
Craig Tiller57726ca2016-09-12 11:59:45 -0700595 grpc_transport_stream_op **ops;
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700596 size_t nops;
597 grpc_subchannel_call *call;
598} retry_ops_args;
599
600static void retry_ops(grpc_exec_ctx *exec_ctx, void *args, grpc_error *error) {
601 retry_ops_args *a = args;
602 size_t i;
603 for (i = 0; i < a->nops; i++) {
Craig Tiller57726ca2016-09-12 11:59:45 -0700604 grpc_subchannel_call_process_op(exec_ctx, a->call, a->ops[i]);
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700605 }
606 GRPC_SUBCHANNEL_CALL_UNREF(exec_ctx, a->call, "retry_ops");
607 gpr_free(a->ops);
608 gpr_free(a);
609}
610
611static void retry_waiting_locked(grpc_exec_ctx *exec_ctx, call_data *calld) {
Craig Tiller57726ca2016-09-12 11:59:45 -0700612 if (calld->waiting_ops_count == 0) {
613 return;
614 }
615
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700616 retry_ops_args *a = gpr_malloc(sizeof(*a));
617 a->ops = calld->waiting_ops;
618 a->nops = calld->waiting_ops_count;
619 a->call = GET_CALL(calld);
620 if (a->call == CANCELLED_CALL) {
621 gpr_free(a);
622 fail_locked(exec_ctx, calld, GRPC_ERROR_CANCELLED);
623 return;
624 }
625 calld->waiting_ops = NULL;
626 calld->waiting_ops_count = 0;
627 calld->waiting_ops_capacity = 0;
628 GRPC_SUBCHANNEL_CALL_REF(a->call, "retry_ops");
629 grpc_exec_ctx_sched(exec_ctx, grpc_closure_create(retry_ops, a),
630 GRPC_ERROR_NONE, NULL);
631}
632
633static void subchannel_ready(grpc_exec_ctx *exec_ctx, void *arg,
634 grpc_error *error) {
Yuchen Zeng19656b12016-09-01 18:00:45 -0700635 grpc_call_element *elem = arg;
636 call_data *calld = elem->call_data;
637 channel_data *chand = elem->channel_data;
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700638 gpr_mu_lock(&calld->mu);
639 GPR_ASSERT(calld->creation_phase ==
640 GRPC_SUBCHANNEL_CALL_HOLDER_PICKING_SUBCHANNEL);
Yuchen Zeng19656b12016-09-01 18:00:45 -0700641 grpc_polling_entity_del_from_pollset_set(exec_ctx, calld->pollent,
642 chand->interested_parties);
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700643 calld->creation_phase = GRPC_SUBCHANNEL_CALL_HOLDER_NOT_CREATING;
644 if (calld->connected_subchannel == NULL) {
645 gpr_atm_no_barrier_store(&calld->subchannel_call, 1);
646 fail_locked(exec_ctx, calld, GRPC_ERROR_CREATE_REFERENCING(
647 "Failed to create subchannel", &error, 1));
Mark D. Roth72f6da82016-09-02 13:42:38 -0700648 } else if (GET_CALL(calld) == CANCELLED_CALL) {
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700649 /* already cancelled before subchannel became ready */
650 fail_locked(exec_ctx, calld,
651 GRPC_ERROR_CREATE_REFERENCING(
652 "Cancelled before creating subchannel", &error, 1));
653 } else {
Mark D. Roth9fe284e2016-09-12 11:22:27 -0700654 /* Create call on subchannel. */
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700655 grpc_subchannel_call *subchannel_call = NULL;
656 grpc_error *new_error = grpc_connected_subchannel_create_call(
Mark D. Rothaa850a72016-09-26 13:38:02 -0700657 exec_ctx, calld->connected_subchannel, calld->pollent, calld->path,
Mark D. Roth3d883412016-11-07 13:42:54 -0800658 calld->call_start_time, calld->deadline, &subchannel_call);
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700659 if (new_error != GRPC_ERROR_NONE) {
660 new_error = grpc_error_add_child(new_error, error);
661 subchannel_call = CANCELLED_CALL;
662 fail_locked(exec_ctx, calld, new_error);
663 }
664 gpr_atm_rel_store(&calld->subchannel_call,
665 (gpr_atm)(uintptr_t)subchannel_call);
666 retry_waiting_locked(exec_ctx, calld);
667 }
668 gpr_mu_unlock(&calld->mu);
669 GRPC_CALL_STACK_UNREF(exec_ctx, calld->owning_call, "pick_subchannel");
670}
671
672static char *cc_get_peer(grpc_exec_ctx *exec_ctx, grpc_call_element *elem) {
673 call_data *calld = elem->call_data;
674 grpc_subchannel_call *subchannel_call = GET_CALL(calld);
675 if (subchannel_call == NULL || subchannel_call == CANCELLED_CALL) {
676 return NULL;
677 } else {
678 return grpc_subchannel_call_get_peer(exec_ctx, subchannel_call);
679 }
680}
681
Craig Tiller577c9b22015-11-02 14:11:15 -0800682typedef struct {
683 grpc_metadata_batch *initial_metadata;
Craig Tiller8c0d96f2016-03-11 14:27:52 -0800684 uint32_t initial_metadata_flags;
Craig Tillerb5585d42015-11-17 07:18:31 -0800685 grpc_connected_subchannel **connected_subchannel;
Craig Tiller577c9b22015-11-02 14:11:15 -0800686 grpc_closure *on_ready;
687 grpc_call_element *elem;
688 grpc_closure closure;
689} continue_picking_args;
690
Yuchen Zeng144ce652016-09-01 18:19:34 -0700691/** Return true if subchannel is available immediately (in which case on_ready
692 should not be called), or false otherwise (in which case on_ready should be
693 called when the subchannel is available). */
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700694static bool pick_subchannel(grpc_exec_ctx *exec_ctx, grpc_call_element *elem,
695 grpc_metadata_batch *initial_metadata,
696 uint32_t initial_metadata_flags,
697 grpc_connected_subchannel **connected_subchannel,
Mark D. Roth72f6da82016-09-02 13:42:38 -0700698 grpc_closure *on_ready, grpc_error *error);
Craig Tiller577c9b22015-11-02 14:11:15 -0800699
Craig Tiller804ff712016-05-05 16:25:40 -0700700static void continue_picking(grpc_exec_ctx *exec_ctx, void *arg,
701 grpc_error *error) {
Craig Tiller577c9b22015-11-02 14:11:15 -0800702 continue_picking_args *cpa = arg;
Craig Tiller0ede5452016-04-23 12:21:45 -0700703 if (cpa->connected_subchannel == NULL) {
Craig Tiller577c9b22015-11-02 14:11:15 -0800704 /* cancelled, do nothing */
Craig Tiller804ff712016-05-05 16:25:40 -0700705 } else if (error != GRPC_ERROR_NONE) {
Craig Tiller332f1b32016-05-24 13:21:21 -0700706 grpc_exec_ctx_sched(exec_ctx, cpa->on_ready, GRPC_ERROR_REF(error), NULL);
Mark D. Roth9dab7d52016-10-07 07:48:03 -0700707 } else {
708 call_data *calld = cpa->elem->call_data;
709 gpr_mu_lock(&calld->mu);
710 if (pick_subchannel(exec_ctx, cpa->elem, cpa->initial_metadata,
Mark D. Rothfd2ddd22016-10-07 10:11:10 -0700711 cpa->initial_metadata_flags, cpa->connected_subchannel,
712 cpa->on_ready, GRPC_ERROR_NONE)) {
Mark D. Roth9dab7d52016-10-07 07:48:03 -0700713 grpc_exec_ctx_sched(exec_ctx, cpa->on_ready, GRPC_ERROR_NONE, NULL);
714 }
715 gpr_mu_unlock(&calld->mu);
Craig Tiller577c9b22015-11-02 14:11:15 -0800716 }
717 gpr_free(cpa);
718}
719
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700720static bool pick_subchannel(grpc_exec_ctx *exec_ctx, grpc_call_element *elem,
721 grpc_metadata_batch *initial_metadata,
722 uint32_t initial_metadata_flags,
723 grpc_connected_subchannel **connected_subchannel,
Mark D. Roth72f6da82016-09-02 13:42:38 -0700724 grpc_closure *on_ready, grpc_error *error) {
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700725 GPR_TIMER_BEGIN("pick_subchannel", 0);
Craig Tillerbfc9adc2016-06-27 13:16:22 -0700726
Craig Tiller577c9b22015-11-02 14:11:15 -0800727 channel_data *chand = elem->channel_data;
728 call_data *calld = elem->call_data;
729 continue_picking_args *cpa;
730 grpc_closure *closure;
731
Craig Tillerb5585d42015-11-17 07:18:31 -0800732 GPR_ASSERT(connected_subchannel);
Craig Tiller577c9b22015-11-02 14:11:15 -0800733
Mark D. Rothff4df062016-08-22 15:02:49 -0700734 gpr_mu_lock(&chand->mu);
Craig Tiller577c9b22015-11-02 14:11:15 -0800735 if (initial_metadata == NULL) {
736 if (chand->lb_policy != NULL) {
Craig Tillerab33b482015-11-21 08:11:04 -0800737 grpc_lb_policy_cancel_pick(exec_ctx, chand->lb_policy,
Mark D. Roth5f844002016-09-08 08:20:53 -0700738 connected_subchannel, GRPC_ERROR_REF(error));
Craig Tiller577c9b22015-11-02 14:11:15 -0800739 }
740 for (closure = chand->waiting_for_config_closures.head; closure != NULL;
Craig Tiller804ff712016-05-05 16:25:40 -0700741 closure = closure->next_data.next) {
Craig Tiller577c9b22015-11-02 14:11:15 -0800742 cpa = closure->cb_arg;
Craig Tillerb5585d42015-11-17 07:18:31 -0800743 if (cpa->connected_subchannel == connected_subchannel) {
744 cpa->connected_subchannel = NULL;
Mark D. Roth932b10c2016-09-09 08:44:30 -0700745 grpc_exec_ctx_sched(
746 exec_ctx, cpa->on_ready,
747 GRPC_ERROR_CREATE_REFERENCING("Pick cancelled", &error, 1), NULL);
Craig Tiller577c9b22015-11-02 14:11:15 -0800748 }
749 }
Mark D. Rothff4df062016-08-22 15:02:49 -0700750 gpr_mu_unlock(&chand->mu);
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700751 GPR_TIMER_END("pick_subchannel", 0);
Mark D. Roth697a1f62016-09-07 13:35:07 -0700752 GRPC_ERROR_UNREF(error);
Mark D. Roth4c0fe492016-08-31 13:51:55 -0700753 return true;
Craig Tiller577c9b22015-11-02 14:11:15 -0800754 }
Mark D. Roth697a1f62016-09-07 13:35:07 -0700755 GPR_ASSERT(error == GRPC_ERROR_NONE);
Craig Tiller577c9b22015-11-02 14:11:15 -0800756 if (chand->lb_policy != NULL) {
Craig Tiller86c0f8a2015-12-01 20:05:40 -0800757 grpc_lb_policy *lb_policy = chand->lb_policy;
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700758 GRPC_LB_POLICY_REF(lb_policy, "pick_subchannel");
Mark D. Rothff4df062016-08-22 15:02:49 -0700759 gpr_mu_unlock(&chand->mu);
Mark D. Rothe40dd292016-10-05 14:58:37 -0700760 // If the application explicitly set wait_for_ready, use that.
761 // Otherwise, if the service config specified a value for this
762 // method, use that.
Mark D. Rothc1c38582016-10-11 11:03:27 -0700763 const bool wait_for_ready_set_from_api =
764 initial_metadata_flags &
765 GRPC_INITIAL_METADATA_WAIT_FOR_READY_EXPLICITLY_SET;
766 const bool wait_for_ready_set_from_service_config =
767 calld->wait_for_ready_from_service_config != WAIT_FOR_READY_UNSET;
768 if (!wait_for_ready_set_from_api &&
769 wait_for_ready_set_from_service_config) {
Mark D. Rothe40dd292016-10-05 14:58:37 -0700770 if (calld->wait_for_ready_from_service_config == WAIT_FOR_READY_TRUE) {
771 initial_metadata_flags |= GRPC_INITIAL_METADATA_WAIT_FOR_READY;
772 } else {
773 initial_metadata_flags &= ~GRPC_INITIAL_METADATA_WAIT_FOR_READY;
774 }
775 }
David Garcia Quintas61c58012016-10-03 14:44:20 -0700776 // TODO(dgq): make this deadline configurable somehow.
David Garcia Quintas92eb6b92016-09-30 14:07:39 -0700777 const grpc_lb_policy_pick_args inputs = {
Yuchen Zengac8bc422016-10-05 14:00:02 -0700778 initial_metadata, initial_metadata_flags, &calld->lb_token_mdelem,
779 gpr_inf_future(GPR_CLOCK_MONOTONIC)};
Mark D. Roth55f25b62016-10-12 14:55:20 -0700780 const bool result = grpc_lb_policy_pick(
781 exec_ctx, lb_policy, &inputs, connected_subchannel, NULL, on_ready);
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700782 GRPC_LB_POLICY_UNREF(exec_ctx, lb_policy, "pick_subchannel");
783 GPR_TIMER_END("pick_subchannel", 0);
Mark D. Roth9dab7d52016-10-07 07:48:03 -0700784 return result;
Craig Tiller577c9b22015-11-02 14:11:15 -0800785 }
786 if (chand->resolver != NULL && !chand->started_resolving) {
Mark D. Roth4c0fe492016-08-31 13:51:55 -0700787 chand->started_resolving = true;
Craig Tiller906e3bc2015-11-24 07:31:31 -0800788 GRPC_CHANNEL_STACK_REF(chand->owning_stack, "resolver");
Mark D. Roth046cf762016-09-26 11:13:51 -0700789 grpc_resolver_next(exec_ctx, chand->resolver, &chand->resolver_result,
Mark D. Rothff4df062016-08-22 15:02:49 -0700790 &chand->on_resolver_result_changed);
Craig Tiller577c9b22015-11-02 14:11:15 -0800791 }
Craig Tiller0eab6972016-04-23 12:59:57 -0700792 if (chand->resolver != NULL) {
793 cpa = gpr_malloc(sizeof(*cpa));
794 cpa->initial_metadata = initial_metadata;
795 cpa->initial_metadata_flags = initial_metadata_flags;
796 cpa->connected_subchannel = connected_subchannel;
797 cpa->on_ready = on_ready;
798 cpa->elem = elem;
799 grpc_closure_init(&cpa->closure, continue_picking, cpa);
Craig Tiller804ff712016-05-05 16:25:40 -0700800 grpc_closure_list_append(&chand->waiting_for_config_closures, &cpa->closure,
801 GRPC_ERROR_NONE);
Craig Tiller0eab6972016-04-23 12:59:57 -0700802 } else {
Craig Tiller332f1b32016-05-24 13:21:21 -0700803 grpc_exec_ctx_sched(exec_ctx, on_ready, GRPC_ERROR_CREATE("Disconnected"),
804 NULL);
Craig Tiller0eab6972016-04-23 12:59:57 -0700805 }
Mark D. Rothff4df062016-08-22 15:02:49 -0700806 gpr_mu_unlock(&chand->mu);
Craig Tillerbfc9adc2016-06-27 13:16:22 -0700807
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700808 GPR_TIMER_END("pick_subchannel", 0);
Mark D. Roth4c0fe492016-08-31 13:51:55 -0700809 return false;
Craig Tiller577c9b22015-11-02 14:11:15 -0800810}
811
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700812// The logic here is fairly complicated, due to (a) the fact that we
813// need to handle the case where we receive the send op before the
814// initial metadata op, and (b) the need for efficiency, especially in
815// the streaming case.
816// TODO(ctiller): Explain this more thoroughly.
817static void cc_start_transport_stream_op(grpc_exec_ctx *exec_ctx,
818 grpc_call_element *elem,
819 grpc_transport_stream_op *op) {
820 call_data *calld = elem->call_data;
Yuchen Zeng19656b12016-09-01 18:00:45 -0700821 channel_data *chand = elem->channel_data;
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700822 GRPC_CALL_LOG_OP(GPR_INFO, elem, op);
Mark D. Roth72f6da82016-09-02 13:42:38 -0700823 grpc_deadline_state_client_start_transport_stream_op(exec_ctx, elem, op);
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700824 /* try to (atomically) get the call */
825 grpc_subchannel_call *call = GET_CALL(calld);
826 GPR_TIMER_BEGIN("cc_start_transport_stream_op", 0);
827 if (call == CANCELLED_CALL) {
Mark D. Rothf28763c2016-09-14 15:18:40 -0700828 grpc_transport_stream_op_finish_with_failure(
829 exec_ctx, op, GRPC_ERROR_REF(calld->cancel_error));
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700830 GPR_TIMER_END("cc_start_transport_stream_op", 0);
831 return;
832 }
833 if (call != NULL) {
834 grpc_subchannel_call_process_op(exec_ctx, call, op);
835 GPR_TIMER_END("cc_start_transport_stream_op", 0);
836 return;
837 }
838 /* we failed; lock and figure out what to do */
839 gpr_mu_lock(&calld->mu);
840retry:
841 /* need to recheck that another thread hasn't set the call */
842 call = GET_CALL(calld);
843 if (call == CANCELLED_CALL) {
844 gpr_mu_unlock(&calld->mu);
Mark D. Rothf28763c2016-09-14 15:18:40 -0700845 grpc_transport_stream_op_finish_with_failure(
846 exec_ctx, op, GRPC_ERROR_REF(calld->cancel_error));
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700847 GPR_TIMER_END("cc_start_transport_stream_op", 0);
848 return;
849 }
850 if (call != NULL) {
851 gpr_mu_unlock(&calld->mu);
852 grpc_subchannel_call_process_op(exec_ctx, call, op);
853 GPR_TIMER_END("cc_start_transport_stream_op", 0);
854 return;
855 }
856 /* if this is a cancellation, then we can raise our cancelled flag */
857 if (op->cancel_error != GRPC_ERROR_NONE) {
858 if (!gpr_atm_rel_cas(&calld->subchannel_call, 0,
859 (gpr_atm)(uintptr_t)CANCELLED_CALL)) {
860 goto retry;
861 } else {
Mark D. Rothf28763c2016-09-14 15:18:40 -0700862 // Stash a copy of cancel_error in our call data, so that we can use
863 // it for subsequent operations. This ensures that if the call is
864 // cancelled before any ops are passed down (e.g., if the deadline
865 // is in the past when the call starts), we can return the right
866 // error to the caller when the first op does get passed down.
867 calld->cancel_error = GRPC_ERROR_REF(op->cancel_error);
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700868 switch (calld->creation_phase) {
869 case GRPC_SUBCHANNEL_CALL_HOLDER_NOT_CREATING:
870 fail_locked(exec_ctx, calld, GRPC_ERROR_REF(op->cancel_error));
871 break;
872 case GRPC_SUBCHANNEL_CALL_HOLDER_PICKING_SUBCHANNEL:
Mark D. Rothd4c0f552016-09-01 09:25:32 -0700873 pick_subchannel(exec_ctx, elem, NULL, 0, &calld->connected_subchannel,
Mark D. Roth72f6da82016-09-02 13:42:38 -0700874 NULL, GRPC_ERROR_REF(op->cancel_error));
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700875 break;
876 }
877 gpr_mu_unlock(&calld->mu);
Mark D. Roth72f6da82016-09-02 13:42:38 -0700878 grpc_transport_stream_op_finish_with_failure(
879 exec_ctx, op, GRPC_ERROR_REF(op->cancel_error));
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700880 GPR_TIMER_END("cc_start_transport_stream_op", 0);
881 return;
882 }
883 }
884 /* if we don't have a subchannel, try to get one */
885 if (calld->creation_phase == GRPC_SUBCHANNEL_CALL_HOLDER_NOT_CREATING &&
886 calld->connected_subchannel == NULL &&
887 op->send_initial_metadata != NULL) {
888 calld->creation_phase = GRPC_SUBCHANNEL_CALL_HOLDER_PICKING_SUBCHANNEL;
Yuchen Zeng19656b12016-09-01 18:00:45 -0700889 grpc_closure_init(&calld->next_step, subchannel_ready, elem);
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700890 GRPC_CALL_STACK_REF(calld->owning_call, "pick_subchannel");
Yuchen Zeng144ce652016-09-01 18:19:34 -0700891 /* If a subchannel is not available immediately, the polling entity from
892 call_data should be provided to channel_data's interested_parties, so
893 that IO of the lb_policy and resolver could be done under it. */
Mark D. Rothd4c0f552016-09-01 09:25:32 -0700894 if (pick_subchannel(exec_ctx, elem, op->send_initial_metadata,
Mark D. Rothe40dd292016-10-05 14:58:37 -0700895 op->send_initial_metadata_flags,
896 &calld->connected_subchannel, &calld->next_step,
897 GRPC_ERROR_NONE)) {
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700898 calld->creation_phase = GRPC_SUBCHANNEL_CALL_HOLDER_NOT_CREATING;
899 GRPC_CALL_STACK_UNREF(exec_ctx, calld->owning_call, "pick_subchannel");
Yuchen Zeng19656b12016-09-01 18:00:45 -0700900 } else {
Yuchen Zeng19656b12016-09-01 18:00:45 -0700901 grpc_polling_entity_add_to_pollset_set(exec_ctx, calld->pollent,
902 chand->interested_parties);
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700903 }
904 }
905 /* if we've got a subchannel, then let's ask it to create a call */
906 if (calld->creation_phase == GRPC_SUBCHANNEL_CALL_HOLDER_NOT_CREATING &&
907 calld->connected_subchannel != NULL) {
908 grpc_subchannel_call *subchannel_call = NULL;
909 grpc_error *error = grpc_connected_subchannel_create_call(
Mark D. Rothaa850a72016-09-26 13:38:02 -0700910 exec_ctx, calld->connected_subchannel, calld->pollent, calld->path,
Mark D. Roth3d883412016-11-07 13:42:54 -0800911 calld->call_start_time, calld->deadline, &subchannel_call);
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700912 if (error != GRPC_ERROR_NONE) {
913 subchannel_call = CANCELLED_CALL;
914 fail_locked(exec_ctx, calld, GRPC_ERROR_REF(error));
915 grpc_transport_stream_op_finish_with_failure(exec_ctx, op, error);
916 }
917 gpr_atm_rel_store(&calld->subchannel_call,
918 (gpr_atm)(uintptr_t)subchannel_call);
919 retry_waiting_locked(exec_ctx, calld);
920 goto retry;
921 }
922 /* nothing to be done but wait */
923 add_waiting_locked(calld, op);
924 gpr_mu_unlock(&calld->mu);
925 GPR_TIMER_END("cc_start_transport_stream_op", 0);
926}
927
Mark D. Rothe40dd292016-10-05 14:58:37 -0700928// Gets data from the service config. Invoked when the resolver returns
929// its initial result.
930static void read_service_config(grpc_exec_ctx *exec_ctx, void *arg,
931 grpc_error *error) {
932 grpc_call_element *elem = arg;
933 channel_data *chand = elem->channel_data;
934 call_data *calld = elem->call_data;
935 // If this is an error, there's no point in looking at the service config.
Mark D. Roth196387a2016-10-12 14:53:36 -0700936 if (error == GRPC_ERROR_NONE) {
937 // Get the method config table from channel data.
938 gpr_mu_lock(&chand->mu);
Mark D. Roth9d480942016-10-19 14:18:05 -0700939 grpc_mdstr_hash_table *method_params_table = NULL;
940 if (chand->method_params_table != NULL) {
941 method_params_table =
942 grpc_mdstr_hash_table_ref(chand->method_params_table);
Mark D. Rothe40dd292016-10-05 14:58:37 -0700943 }
Mark D. Roth196387a2016-10-12 14:53:36 -0700944 gpr_mu_unlock(&chand->mu);
945 // If the method config table was present, use it.
Mark D. Roth9d480942016-10-19 14:18:05 -0700946 if (method_params_table != NULL) {
947 const method_parameters *method_params =
948 grpc_method_config_table_get(method_params_table, calld->path);
949 if (method_params != NULL) {
950 const bool have_method_timeout =
951 gpr_time_cmp(method_params->timeout, gpr_time_0(GPR_TIMESPAN)) != 0;
952 if (have_method_timeout ||
953 method_params->wait_for_ready != WAIT_FOR_READY_UNSET) {
Mark D. Roth196387a2016-10-12 14:53:36 -0700954 gpr_mu_lock(&calld->mu);
Mark D. Roth9d480942016-10-19 14:18:05 -0700955 if (have_method_timeout) {
956 const gpr_timespec per_method_deadline =
957 gpr_time_add(calld->call_start_time, method_params->timeout);
Mark D. Roth196387a2016-10-12 14:53:36 -0700958 if (gpr_time_cmp(per_method_deadline, calld->deadline) < 0) {
959 calld->deadline = per_method_deadline;
960 // Reset deadline timer.
961 grpc_deadline_state_reset(exec_ctx, elem, calld->deadline);
962 }
963 }
Mark D. Roth9d480942016-10-19 14:18:05 -0700964 if (method_params->wait_for_ready != WAIT_FOR_READY_UNSET) {
Mark D. Roth196387a2016-10-12 14:53:36 -0700965 calld->wait_for_ready_from_service_config =
Mark D. Roth9d480942016-10-19 14:18:05 -0700966 method_params->wait_for_ready;
Mark D. Roth196387a2016-10-12 14:53:36 -0700967 }
968 gpr_mu_unlock(&calld->mu);
969 }
970 }
Mark D. Roth9d480942016-10-19 14:18:05 -0700971 grpc_mdstr_hash_table_unref(method_params_table);
Mark D. Roth196387a2016-10-12 14:53:36 -0700972 }
Mark D. Rothe40dd292016-10-05 14:58:37 -0700973 }
Mark D. Roth31292f22016-10-12 13:14:07 -0700974 GRPC_CALL_STACK_UNREF(exec_ctx, calld->owning_call, "read_service_config");
Mark D. Rothe40dd292016-10-05 14:58:37 -0700975}
976
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800977/* Constructor for call_data */
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700978static grpc_error *cc_init_call_elem(grpc_exec_ctx *exec_ctx,
979 grpc_call_element *elem,
980 grpc_call_element_args *args) {
Mark D. Rothaa850a72016-09-26 13:38:02 -0700981 channel_data *chand = elem->channel_data;
Mark D. Roth4c0fe492016-08-31 13:51:55 -0700982 call_data *calld = elem->call_data;
Mark D. Rothe40dd292016-10-05 14:58:37 -0700983 // Initialize data members.
984 grpc_deadline_state_init(exec_ctx, elem, args->call_stack);
Mark D. Rothaa850a72016-09-26 13:38:02 -0700985 calld->path = GRPC_MDSTR_REF(args->path);
Mark D. Rothff08f332016-10-14 13:01:01 -0700986 calld->call_start_time = args->start_time;
Mark D. Rothe40dd292016-10-05 14:58:37 -0700987 calld->deadline = gpr_convert_clock_type(args->deadline, GPR_CLOCK_MONOTONIC);
988 calld->wait_for_ready_from_service_config = WAIT_FOR_READY_UNSET;
Mark D. Rothf28763c2016-09-14 15:18:40 -0700989 calld->cancel_error = GRPC_ERROR_NONE;
Mark D. Roth4c0fe492016-08-31 13:51:55 -0700990 gpr_atm_rel_store(&calld->subchannel_call, 0);
Mark D. Roth4c0fe492016-08-31 13:51:55 -0700991 gpr_mu_init(&calld->mu);
992 calld->connected_subchannel = NULL;
993 calld->waiting_ops = NULL;
994 calld->waiting_ops_count = 0;
995 calld->waiting_ops_capacity = 0;
996 calld->creation_phase = GRPC_SUBCHANNEL_CALL_HOLDER_NOT_CREATING;
997 calld->owning_call = args->call_stack;
998 calld->pollent = NULL;
Mark D. Rothe40dd292016-10-05 14:58:37 -0700999 // If the resolver has already returned results, then we can access
1000 // the service config parameters immediately. Otherwise, we need to
1001 // defer that work until the resolver returns an initial result.
1002 // TODO(roth): This code is almost but not quite identical to the code
1003 // in read_service_config() above. It would be nice to find a way to
1004 // combine them, to avoid having to maintain it twice.
1005 gpr_mu_lock(&chand->mu);
1006 if (chand->lb_policy != NULL) {
1007 // We already have a resolver result, so check for service config.
Mark D. Roth9d480942016-10-19 14:18:05 -07001008 if (chand->method_params_table != NULL) {
1009 grpc_mdstr_hash_table *method_params_table =
1010 grpc_mdstr_hash_table_ref(chand->method_params_table);
Mark D. Rothe40dd292016-10-05 14:58:37 -07001011 gpr_mu_unlock(&chand->mu);
Mark D. Roth9d480942016-10-19 14:18:05 -07001012 method_parameters *method_params =
1013 grpc_method_config_table_get(method_params_table, args->path);
1014 if (method_params != NULL) {
1015 if (gpr_time_cmp(method_params->timeout,
1016 gpr_time_0(GPR_CLOCK_MONOTONIC)) != 0) {
Mark D. Rothe40dd292016-10-05 14:58:37 -07001017 gpr_timespec per_method_deadline =
Mark D. Roth9d480942016-10-19 14:18:05 -07001018 gpr_time_add(calld->call_start_time, method_params->timeout);
Mark D. Rothe40dd292016-10-05 14:58:37 -07001019 calld->deadline = gpr_time_min(calld->deadline, per_method_deadline);
1020 }
Mark D. Roth9d480942016-10-19 14:18:05 -07001021 if (method_params->wait_for_ready != WAIT_FOR_READY_UNSET) {
Mark D. Rothe40dd292016-10-05 14:58:37 -07001022 calld->wait_for_ready_from_service_config =
Mark D. Roth9d480942016-10-19 14:18:05 -07001023 method_params->wait_for_ready;
Mark D. Rothe40dd292016-10-05 14:58:37 -07001024 }
1025 }
Mark D. Roth9d480942016-10-19 14:18:05 -07001026 grpc_mdstr_hash_table_unref(method_params_table);
Mark D. Rothe40dd292016-10-05 14:58:37 -07001027 } else {
1028 gpr_mu_unlock(&chand->mu);
1029 }
1030 } else {
1031 // We don't yet have a resolver result, so register a callback to
1032 // get the service config data once the resolver returns.
Mark D. Roth31292f22016-10-12 13:14:07 -07001033 // Take a reference to the call stack to be owned by the callback.
1034 GRPC_CALL_STACK_REF(calld->owning_call, "read_service_config");
Mark D. Rothe40dd292016-10-05 14:58:37 -07001035 grpc_closure_init(&calld->read_service_config, read_service_config, elem);
1036 grpc_closure_list_append(&chand->waiting_for_config_closures,
1037 &calld->read_service_config, GRPC_ERROR_NONE);
1038 gpr_mu_unlock(&chand->mu);
1039 }
1040 // Start the deadline timer with the current deadline value. If we
1041 // do not yet have service config data, then the timer may be reset
1042 // later.
1043 grpc_deadline_state_start(exec_ctx, elem, calld->deadline);
Mark D. Roth0badbe82016-06-23 10:15:12 -07001044 return GRPC_ERROR_NONE;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001045}
1046
1047/* Destructor for call_data */
Mark D. Roth2a5959f2016-09-01 08:20:27 -07001048static void cc_destroy_call_elem(grpc_exec_ctx *exec_ctx,
1049 grpc_call_element *elem,
1050 const grpc_call_final_info *final_info,
1051 void *and_free_memory) {
Mark D. Roth4c0fe492016-08-31 13:51:55 -07001052 call_data *calld = elem->call_data;
Mark D. Rothf28763c2016-09-14 15:18:40 -07001053 grpc_deadline_state_destroy(exec_ctx, elem);
Mark D. Rothaa850a72016-09-26 13:38:02 -07001054 GRPC_MDSTR_UNREF(calld->path);
Mark D. Rothf28763c2016-09-14 15:18:40 -07001055 GRPC_ERROR_UNREF(calld->cancel_error);
Mark D. Roth4c0fe492016-08-31 13:51:55 -07001056 grpc_subchannel_call *call = GET_CALL(calld);
1057 if (call != NULL && call != CANCELLED_CALL) {
1058 GRPC_SUBCHANNEL_CALL_UNREF(exec_ctx, call, "client_channel_destroy_call");
1059 }
1060 GPR_ASSERT(calld->creation_phase == GRPC_SUBCHANNEL_CALL_HOLDER_NOT_CREATING);
1061 gpr_mu_destroy(&calld->mu);
1062 GPR_ASSERT(calld->waiting_ops_count == 0);
Craig Tiller693d3942016-10-27 16:51:25 -07001063 if (calld->connected_subchannel != NULL) {
1064 GRPC_CONNECTED_SUBCHANNEL_UNREF(exec_ctx, calld->connected_subchannel,
1065 "picked");
1066 }
Mark D. Roth4c0fe492016-08-31 13:51:55 -07001067 gpr_free(calld->waiting_ops);
Craig Tiller2c8063c2016-03-22 22:12:15 -07001068 gpr_free(and_free_memory);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001069}
1070
David Garcia Quintasf72eb972016-05-03 18:28:09 -07001071static void cc_set_pollset_or_pollset_set(grpc_exec_ctx *exec_ctx,
1072 grpc_call_element *elem,
David Garcia Quintas2a50dfe2016-05-31 15:09:12 -07001073 grpc_polling_entity *pollent) {
Craig Tiller577c9b22015-11-02 14:11:15 -08001074 call_data *calld = elem->call_data;
David Garcia Quintas2a50dfe2016-05-31 15:09:12 -07001075 calld->pollent = pollent;
Craig Tiller577c9b22015-11-02 14:11:15 -08001076}
1077
Mark D. Roth2a5959f2016-09-01 08:20:27 -07001078/*************************************************************************
1079 * EXPORTED SYMBOLS
1080 */
1081
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001082const grpc_channel_filter grpc_client_channel_filter = {
Craig Tillerf40df232016-03-25 13:38:14 -07001083 cc_start_transport_stream_op,
1084 cc_start_transport_op,
1085 sizeof(call_data),
Mark D. Roth2a5959f2016-09-01 08:20:27 -07001086 cc_init_call_elem,
David Garcia Quintas4afce7e2016-04-18 16:25:17 -07001087 cc_set_pollset_or_pollset_set,
Mark D. Roth2a5959f2016-09-01 08:20:27 -07001088 cc_destroy_call_elem,
Craig Tillerf40df232016-03-25 13:38:14 -07001089 sizeof(channel_data),
Mark D. Roth2a5959f2016-09-01 08:20:27 -07001090 cc_init_channel_elem,
1091 cc_destroy_channel_elem,
Craig Tillerf40df232016-03-25 13:38:14 -07001092 cc_get_peer,
Mark D. Rothb2d24882016-10-27 15:44:07 -07001093 cc_get_channel_info,
Craig Tillerf40df232016-03-25 13:38:14 -07001094 "client-channel",
Craig Tiller87d5b192015-04-16 14:37:57 -07001095};
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001096
Craig Tillera82950e2015-09-22 12:33:20 -07001097grpc_connectivity_state grpc_client_channel_check_connectivity_state(
1098 grpc_exec_ctx *exec_ctx, grpc_channel_element *elem, int try_to_connect) {
Craig Tiller48cb07c2015-07-15 16:16:15 -07001099 channel_data *chand = elem->channel_data;
1100 grpc_connectivity_state out;
Mark D. Rothff4df062016-08-22 15:02:49 -07001101 gpr_mu_lock(&chand->mu);
Craig Tiller804ff712016-05-05 16:25:40 -07001102 out = grpc_connectivity_state_check(&chand->state_tracker, NULL);
Craig Tillera82950e2015-09-22 12:33:20 -07001103 if (out == GRPC_CHANNEL_IDLE && try_to_connect) {
1104 if (chand->lb_policy != NULL) {
1105 grpc_lb_policy_exit_idle(exec_ctx, chand->lb_policy);
1106 } else {
Mark D. Roth4c0fe492016-08-31 13:51:55 -07001107 chand->exit_idle_when_lb_policy_arrives = true;
Craig Tillera82950e2015-09-22 12:33:20 -07001108 if (!chand->started_resolving && chand->resolver != NULL) {
Craig Tiller906e3bc2015-11-24 07:31:31 -08001109 GRPC_CHANNEL_STACK_REF(chand->owning_stack, "resolver");
Mark D. Roth4c0fe492016-08-31 13:51:55 -07001110 chand->started_resolving = true;
Mark D. Roth046cf762016-09-26 11:13:51 -07001111 grpc_resolver_next(exec_ctx, chand->resolver, &chand->resolver_result,
Mark D. Rothff4df062016-08-22 15:02:49 -07001112 &chand->on_resolver_result_changed);
Craig Tillera82950e2015-09-22 12:33:20 -07001113 }
Craig Tiller48cb07c2015-07-15 16:16:15 -07001114 }
Craig Tillera82950e2015-09-22 12:33:20 -07001115 }
Mark D. Rothff4df062016-08-22 15:02:49 -07001116 gpr_mu_unlock(&chand->mu);
Craig Tiller48cb07c2015-07-15 16:16:15 -07001117 return out;
1118}
1119
Craig Tiller86c99582015-11-25 15:22:26 -08001120typedef struct {
1121 channel_data *chand;
1122 grpc_pollset *pollset;
1123 grpc_closure *on_complete;
1124 grpc_closure my_closure;
1125} external_connectivity_watcher;
1126
Craig Tiller1d881fb2015-12-01 07:39:04 -08001127static void on_external_watch_complete(grpc_exec_ctx *exec_ctx, void *arg,
Craig Tiller804ff712016-05-05 16:25:40 -07001128 grpc_error *error) {
Craig Tiller86c99582015-11-25 15:22:26 -08001129 external_connectivity_watcher *w = arg;
1130 grpc_closure *follow_up = w->on_complete;
Craig Tiller69b093b2016-02-25 19:04:07 -08001131 grpc_pollset_set_del_pollset(exec_ctx, w->chand->interested_parties,
Craig Tiller1d881fb2015-12-01 07:39:04 -08001132 w->pollset);
1133 GRPC_CHANNEL_STACK_UNREF(exec_ctx, w->chand->owning_stack,
1134 "external_connectivity_watcher");
Craig Tiller86c99582015-11-25 15:22:26 -08001135 gpr_free(w);
Craig Tiller804ff712016-05-05 16:25:40 -07001136 follow_up->cb(exec_ctx, follow_up->cb_arg, error);
Craig Tiller86c99582015-11-25 15:22:26 -08001137}
1138
Craig Tillera82950e2015-09-22 12:33:20 -07001139void grpc_client_channel_watch_connectivity_state(
Craig Tiller906e3bc2015-11-24 07:31:31 -08001140 grpc_exec_ctx *exec_ctx, grpc_channel_element *elem, grpc_pollset *pollset,
Craig Tillera82950e2015-09-22 12:33:20 -07001141 grpc_connectivity_state *state, grpc_closure *on_complete) {
Craig Tiller48cb07c2015-07-15 16:16:15 -07001142 channel_data *chand = elem->channel_data;
Craig Tiller86c99582015-11-25 15:22:26 -08001143 external_connectivity_watcher *w = gpr_malloc(sizeof(*w));
1144 w->chand = chand;
1145 w->pollset = pollset;
1146 w->on_complete = on_complete;
Craig Tiller69b093b2016-02-25 19:04:07 -08001147 grpc_pollset_set_add_pollset(exec_ctx, chand->interested_parties, pollset);
Craig Tiller86c99582015-11-25 15:22:26 -08001148 grpc_closure_init(&w->my_closure, on_external_watch_complete, w);
Craig Tiller1d881fb2015-12-01 07:39:04 -08001149 GRPC_CHANNEL_STACK_REF(w->chand->owning_stack,
1150 "external_connectivity_watcher");
Mark D. Rothff4df062016-08-22 15:02:49 -07001151 gpr_mu_lock(&chand->mu);
Craig Tillera82950e2015-09-22 12:33:20 -07001152 grpc_connectivity_state_notify_on_state_change(
Craig Tiller86c99582015-11-25 15:22:26 -08001153 exec_ctx, &chand->state_tracker, state, &w->my_closure);
Mark D. Rothff4df062016-08-22 15:02:49 -07001154 gpr_mu_unlock(&chand->mu);
Craig Tiller48cb07c2015-07-15 16:16:15 -07001155}