blob: ff773ac33489d0c21cc26a3e6f6bd1c2c316322b [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>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080042#include <grpc/support/sync.h>
43#include <grpc/support/useful.h>
44
Mark D. Roth15195742016-10-07 09:02:28 -070045#include "src/core/ext/client_channel/lb_policy_registry.h"
Mark D. Roth2137cd82016-09-14 09:04:00 -070046#include "src/core/ext/client_channel/subchannel.h"
Craig Tiller9533d042016-03-25 17:11:06 -070047#include "src/core/lib/channel/channel_args.h"
48#include "src/core/lib/channel/connected_channel.h"
Mark D. Roth72f6da82016-09-02 13:42:38 -070049#include "src/core/lib/channel/deadline_filter.h"
Craig Tiller9533d042016-03-25 17:11:06 -070050#include "src/core/lib/iomgr/iomgr.h"
Mark D. Roth4c0fe492016-08-31 13:51:55 -070051#include "src/core/lib/iomgr/polling_entity.h"
Craig Tiller9533d042016-03-25 17:11:06 -070052#include "src/core/lib/profiling/timers.h"
53#include "src/core/lib/support/string.h"
54#include "src/core/lib/surface/channel.h"
55#include "src/core/lib/transport/connectivity_state.h"
Mark D. Roth9fe284e2016-09-12 11:22:27 -070056#include "src/core/lib/transport/metadata.h"
57#include "src/core/lib/transport/metadata_batch.h"
Mark D. Rothbfe56802016-10-26 12:45:20 -070058#include "src/core/lib/transport/method_config.h"
Mark D. Roth9fe284e2016-09-12 11:22:27 -070059#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. Roth26b7be42016-10-24 10:08:07 -070063/*************************************************************************
64 * METHOD-CONFIG TABLE
65 */
66
Mark D. Roth9d480942016-10-19 14:18:05 -070067typedef enum {
68 WAIT_FOR_READY_UNSET,
69 WAIT_FOR_READY_FALSE,
70 WAIT_FOR_READY_TRUE
71} wait_for_ready_value;
72
73typedef struct method_parameters {
74 gpr_timespec timeout;
75 wait_for_ready_value wait_for_ready;
76} method_parameters;
77
78static void *method_parameters_copy(void *value) {
79 void *new_value = gpr_malloc(sizeof(method_parameters));
80 memcpy(new_value, value, sizeof(method_parameters));
81 return new_value;
82}
83
84static int method_parameters_cmp(void *value1, void *value2) {
85 const method_parameters *v1 = value1;
86 const method_parameters *v2 = value2;
87 const int retval = gpr_time_cmp(v1->timeout, v2->timeout);
88 if (retval != 0) return retval;
89 if (v1->wait_for_ready > v2->wait_for_ready) return 1;
90 if (v1->wait_for_ready < v2->wait_for_ready) return -1;
91 return 0;
92}
93
94static const grpc_mdstr_hash_table_vtable method_parameters_vtable = {
95 gpr_free, method_parameters_copy, method_parameters_cmp};
96
97static void *method_config_convert_value(
98 const grpc_method_config *method_config) {
99 method_parameters *value = gpr_malloc(sizeof(method_parameters));
100 const gpr_timespec *timeout = grpc_method_config_get_timeout(method_config);
101 value->timeout = timeout != NULL ? *timeout : gpr_time_0(GPR_TIMESPAN);
102 const bool *wait_for_ready =
103 grpc_method_config_get_wait_for_ready(method_config);
104 value->wait_for_ready =
105 wait_for_ready == NULL
106 ? WAIT_FOR_READY_UNSET
107 : (wait_for_ready ? WAIT_FOR_READY_TRUE : WAIT_FOR_READY_FALSE);
108 return value;
109}
110
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700111/*************************************************************************
112 * CHANNEL-WIDE FUNCTIONS
113 */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800114
Craig Tiller800dacb2015-10-06 09:10:26 -0700115typedef struct client_channel_channel_data {
Craig Tillerf5f17122015-06-25 08:47:26 -0700116 /** resolver for this channel */
117 grpc_resolver *resolver;
Craig Tiller20a3c352015-08-05 08:39:50 -0700118 /** have we started resolving this channel */
Mark D. Roth4c0fe492016-08-31 13:51:55 -0700119 bool started_resolving;
Mark D. Roth0e48a9a2016-09-08 14:14:39 -0700120 /** client channel factory */
121 grpc_client_channel_factory *client_channel_factory;
Craig Tillerf5f17122015-06-25 08:47:26 -0700122
Mark D. Roth046cf762016-09-26 11:13:51 -0700123 /** mutex protecting all variables below in this data structure */
Mark D. Rothff4df062016-08-22 15:02:49 -0700124 gpr_mu mu;
Mark D. Roth046cf762016-09-26 11:13:51 -0700125 /** currently active load balancer */
Craig Tillerf5f17122015-06-25 08:47:26 -0700126 grpc_lb_policy *lb_policy;
Mark D. Roth9d480942016-10-19 14:18:05 -0700127 /** maps method names to method_parameters structs */
128 grpc_mdstr_hash_table *method_params_table;
Mark D. Roth046cf762016-09-26 11:13:51 -0700129 /** incoming resolver result - set by resolver.next() */
Mark D. Rothaf842452016-10-21 15:05:15 -0700130 grpc_channel_args *resolver_result;
Craig Tiller3f475422015-06-25 10:43:05 -0700131 /** a list of closures that are all waiting for config to come in */
Craig Tillerd9ccbbf2015-09-22 09:30:00 -0700132 grpc_closure_list waiting_for_config_closures;
Craig Tiller3f475422015-06-25 10:43:05 -0700133 /** resolver callback */
Mark D. Rothff4df062016-08-22 15:02:49 -0700134 grpc_closure on_resolver_result_changed;
Craig Tiller3f475422015-06-25 10:43:05 -0700135 /** connectivity state being tracked */
Craig Tillerca3e9d32015-06-27 18:37:27 -0700136 grpc_connectivity_state_tracker state_tracker;
Craig Tiller48cb07c2015-07-15 16:16:15 -0700137 /** when an lb_policy arrives, should we try to exit idle */
Mark D. Roth4c0fe492016-08-31 13:51:55 -0700138 bool exit_idle_when_lb_policy_arrives;
Craig Tiller906e3bc2015-11-24 07:31:31 -0800139 /** owning stack */
140 grpc_channel_stack *owning_stack;
Craig Tiller69b093b2016-02-25 19:04:07 -0800141 /** interested parties (owned) */
142 grpc_pollset_set *interested_parties;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800143} channel_data;
144
Craig Tillerd6c98df2015-08-18 09:33:44 -0700145/** We create one watcher for each new lb_policy that is returned from a
Mark D. Roth4c0fe492016-08-31 13:51:55 -0700146 resolver, to watch for state changes from the lb_policy. When a state
147 change is seen, we update the channel, and create a new watcher. */
Craig Tillera82950e2015-09-22 12:33:20 -0700148typedef struct {
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700149 channel_data *chand;
Craig Tiller33825112015-09-18 07:44:19 -0700150 grpc_closure on_changed;
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700151 grpc_connectivity_state state;
152 grpc_lb_policy *lb_policy;
153} lb_policy_connectivity_watcher;
154
Craig Tillera82950e2015-09-22 12:33:20 -0700155static void watch_lb_policy(grpc_exec_ctx *exec_ctx, channel_data *chand,
156 grpc_lb_policy *lb_policy,
157 grpc_connectivity_state current_state);
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700158
Craig Tiller8c0d96f2016-03-11 14:27:52 -0800159static void set_channel_connectivity_state_locked(grpc_exec_ctx *exec_ctx,
160 channel_data *chand,
161 grpc_connectivity_state state,
Craig Tiller804ff712016-05-05 16:25:40 -0700162 grpc_error *error,
Craig Tiller8c0d96f2016-03-11 14:27:52 -0800163 const char *reason) {
164 if ((state == GRPC_CHANNEL_TRANSIENT_FAILURE ||
Craig Tiller48ed92e2016-06-02 11:07:12 -0700165 state == GRPC_CHANNEL_SHUTDOWN) &&
Craig Tiller8c0d96f2016-03-11 14:27:52 -0800166 chand->lb_policy != NULL) {
Mark D. Roth59c9f902016-09-28 13:33:21 -0700167 /* cancel picks with wait_for_ready=false */
Craig Tiller8c0d96f2016-03-11 14:27:52 -0800168 grpc_lb_policy_cancel_picks(
169 exec_ctx, chand->lb_policy,
Mark D. Roth59c9f902016-09-28 13:33:21 -0700170 /* mask= */ GRPC_INITIAL_METADATA_WAIT_FOR_READY,
Mark D. Roth58f52b72016-09-09 13:55:18 -0700171 /* check= */ 0, GRPC_ERROR_REF(error));
Craig Tiller8c0d96f2016-03-11 14:27:52 -0800172 }
Craig Tiller9ccf5f12016-05-07 21:41:01 -0700173 grpc_connectivity_state_set(exec_ctx, &chand->state_tracker, state, error,
174 reason);
Craig Tiller8c0d96f2016-03-11 14:27:52 -0800175}
176
Craig Tiller804ff712016-05-05 16:25:40 -0700177static void on_lb_policy_state_changed_locked(grpc_exec_ctx *exec_ctx,
178 lb_policy_connectivity_watcher *w,
179 grpc_error *error) {
Craig Tillercb2609f2015-11-24 17:19:19 -0800180 grpc_connectivity_state publish_state = w->state;
Craig Tiller5795da72015-09-17 15:27:13 -0700181 /* check if the notification is for a stale policy */
Craig Tillera82950e2015-09-22 12:33:20 -0700182 if (w->lb_policy != w->chand->lb_policy) return;
Craig Tiller5795da72015-09-17 15:27:13 -0700183
Craig Tiller48ed92e2016-06-02 11:07:12 -0700184 if (publish_state == GRPC_CHANNEL_SHUTDOWN && w->chand->resolver != NULL) {
Craig Tillercb2609f2015-11-24 17:19:19 -0800185 publish_state = GRPC_CHANNEL_TRANSIENT_FAILURE;
186 grpc_resolver_channel_saw_error(exec_ctx, w->chand->resolver);
Craig Tillerf62c4d52015-12-04 07:43:07 -0800187 GRPC_LB_POLICY_UNREF(exec_ctx, w->chand->lb_policy, "channel");
188 w->chand->lb_policy = NULL;
Craig Tillercb2609f2015-11-24 17:19:19 -0800189 }
Craig Tiller8c0d96f2016-03-11 14:27:52 -0800190 set_channel_connectivity_state_locked(exec_ctx, w->chand, publish_state,
Craig Tillerfc353d62016-05-10 12:58:03 -0700191 GRPC_ERROR_REF(error), "lb_changed");
Craig Tiller48ed92e2016-06-02 11:07:12 -0700192 if (w->state != GRPC_CHANNEL_SHUTDOWN) {
Craig Tillera82950e2015-09-22 12:33:20 -0700193 watch_lb_policy(exec_ctx, w->chand, w->lb_policy, w->state);
194 }
Craig Tiller5795da72015-09-17 15:27:13 -0700195}
196
Craig Tillera82950e2015-09-22 12:33:20 -0700197static void on_lb_policy_state_changed(grpc_exec_ctx *exec_ctx, void *arg,
Craig Tiller804ff712016-05-05 16:25:40 -0700198 grpc_error *error) {
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700199 lb_policy_connectivity_watcher *w = arg;
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700200
Mark D. Rothff4df062016-08-22 15:02:49 -0700201 gpr_mu_lock(&w->chand->mu);
Craig Tiller804ff712016-05-05 16:25:40 -0700202 on_lb_policy_state_changed_locked(exec_ctx, w, error);
Mark D. Rothff4df062016-08-22 15:02:49 -0700203 gpr_mu_unlock(&w->chand->mu);
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700204
Craig Tiller906e3bc2015-11-24 07:31:31 -0800205 GRPC_CHANNEL_STACK_UNREF(exec_ctx, w->chand->owning_stack, "watch_lb_policy");
Craig Tillera82950e2015-09-22 12:33:20 -0700206 gpr_free(w);
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700207}
208
Craig Tillera82950e2015-09-22 12:33:20 -0700209static void watch_lb_policy(grpc_exec_ctx *exec_ctx, channel_data *chand,
210 grpc_lb_policy *lb_policy,
211 grpc_connectivity_state current_state) {
212 lb_policy_connectivity_watcher *w = gpr_malloc(sizeof(*w));
Craig Tiller906e3bc2015-11-24 07:31:31 -0800213 GRPC_CHANNEL_STACK_REF(chand->owning_stack, "watch_lb_policy");
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700214
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700215 w->chand = chand;
Craig Tillera82950e2015-09-22 12:33:20 -0700216 grpc_closure_init(&w->on_changed, on_lb_policy_state_changed, w);
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700217 w->state = current_state;
218 w->lb_policy = lb_policy;
Craig Tillera82950e2015-09-22 12:33:20 -0700219 grpc_lb_policy_notify_on_state_change(exec_ctx, lb_policy, &w->state,
220 &w->on_changed);
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700221}
222
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700223static void on_resolver_result_changed(grpc_exec_ctx *exec_ctx, void *arg,
224 grpc_error *error) {
Craig Tiller3f475422015-06-25 10:43:05 -0700225 channel_data *chand = arg;
226 grpc_lb_policy *lb_policy = NULL;
227 grpc_lb_policy *old_lb_policy;
Mark D. Roth9d480942016-10-19 14:18:05 -0700228 grpc_mdstr_hash_table *method_params_table = NULL;
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700229 grpc_connectivity_state state = GRPC_CHANNEL_TRANSIENT_FAILURE;
Mark D. Roth4c0fe492016-08-31 13:51:55 -0700230 bool exit_idle = false;
Craig Tiller804ff712016-05-05 16:25:40 -0700231 grpc_error *state_error = GRPC_ERROR_CREATE("No load balancing policy");
Craig Tiller3f475422015-06-25 10:43:05 -0700232
Mark D. Roth046cf762016-09-26 11:13:51 -0700233 if (chand->resolver_result != NULL) {
Mark D. Roth0e48a9a2016-09-08 14:14:39 -0700234 grpc_lb_policy_args lb_policy_args;
Mark D. Rothaf842452016-10-21 15:05:15 -0700235 lb_policy_args.args = chand->resolver_result;
Mark D. Roth0e48a9a2016-09-08 14:14:39 -0700236 lb_policy_args.client_channel_factory = chand->client_channel_factory;
Mark D. Roth88405f72016-10-03 08:24:52 -0700237
Mark D. Roth5bd7be02016-10-21 14:19:50 -0700238 // Find LB policy name.
239 const char *lb_policy_name = NULL;
Mark D. Rothaf842452016-10-21 15:05:15 -0700240 const grpc_arg *channel_arg =
Mark D. Roth5bd7be02016-10-21 14:19:50 -0700241 grpc_channel_args_find(lb_policy_args.args, GRPC_ARG_LB_POLICY_NAME);
Mark D. Rothaf842452016-10-21 15:05:15 -0700242 if (channel_arg != NULL) {
243 GPR_ASSERT(channel_arg->type == GRPC_ARG_STRING);
244 lb_policy_name = channel_arg->value.string;
Mark D. Roth5bd7be02016-10-21 14:19:50 -0700245 }
Mark D. Roth88405f72016-10-03 08:24:52 -0700246 // Special case: If all of the addresses are balancer addresses,
247 // assume that we should use the grpclb policy, regardless of what the
248 // resolver actually specified.
Mark D. Rothaf842452016-10-21 15:05:15 -0700249 channel_arg =
250 grpc_channel_args_find(lb_policy_args.args, GRPC_ARG_LB_ADDRESSES);
251 if (channel_arg != NULL) {
252 GPR_ASSERT(channel_arg->type == GRPC_ARG_POINTER);
Mark D. Roth557c9902016-10-24 11:12:05 -0700253 grpc_lb_addresses *addresses = channel_arg->value.pointer.p;
Mark D. Rothaf842452016-10-21 15:05:15 -0700254 bool found_backend_address = false;
255 for (size_t i = 0; i < addresses->num_addresses; ++i) {
256 if (!addresses->addresses[i].is_balancer) {
257 found_backend_address = true;
258 break;
259 }
Mark D. Roth88405f72016-10-03 08:24:52 -0700260 }
Mark D. Rothaf842452016-10-21 15:05:15 -0700261 if (!found_backend_address) {
262 if (lb_policy_name != NULL && strcmp(lb_policy_name, "grpclb") != 0) {
263 gpr_log(GPR_INFO,
264 "resolver requested LB policy %s but provided only balancer "
265 "addresses, no backend addresses -- forcing use of grpclb LB "
266 "policy",
Mark D. Roth5f40e5d2016-10-24 13:09:05 -0700267 lb_policy_name);
Mark D. Rothaf842452016-10-21 15:05:15 -0700268 }
269 lb_policy_name = "grpclb";
Mark D. Roth88405f72016-10-03 08:24:52 -0700270 }
Mark D. Roth88405f72016-10-03 08:24:52 -0700271 }
272 // Use pick_first if nothing was specified and we didn't select grpclb
273 // above.
274 if (lb_policy_name == NULL) lb_policy_name = "pick_first";
275
276 lb_policy =
277 grpc_lb_policy_create(exec_ctx, lb_policy_name, &lb_policy_args);
Craig Tillera82950e2015-09-22 12:33:20 -0700278 if (lb_policy != NULL) {
Craig Tillera82950e2015-09-22 12:33:20 -0700279 GRPC_LB_POLICY_REF(lb_policy, "config_change");
Craig Tillerf707d622016-05-06 14:26:12 -0700280 GRPC_ERROR_UNREF(state_error);
Craig Tiller804ff712016-05-05 16:25:40 -0700281 state =
282 grpc_lb_policy_check_connectivity(exec_ctx, lb_policy, &state_error);
Craig Tiller45724b32015-09-22 10:42:19 -0700283 }
Mark D. Rothaf842452016-10-21 15:05:15 -0700284 channel_arg =
285 grpc_channel_args_find(lb_policy_args.args, GRPC_ARG_SERVICE_CONFIG);
Mark D. Roth046cf762016-09-26 11:13:51 -0700286 if (channel_arg != NULL) {
287 GPR_ASSERT(channel_arg->type == GRPC_ARG_POINTER);
Mark D. Roth9d480942016-10-19 14:18:05 -0700288 method_params_table = grpc_method_config_table_convert(
289 (grpc_method_config_table *)channel_arg->value.pointer.p,
290 method_config_convert_value, &method_parameters_vtable);
Mark D. Roth9fe284e2016-09-12 11:22:27 -0700291 }
Mark D. Rothaf842452016-10-21 15:05:15 -0700292 grpc_channel_args_destroy(chand->resolver_result);
Mark D. Roth046cf762016-09-26 11:13:51 -0700293 chand->resolver_result = NULL;
Craig Tillera82950e2015-09-22 12:33:20 -0700294 }
295
Craig Tiller86c99582015-11-25 15:22:26 -0800296 if (lb_policy != NULL) {
Craig Tiller69b093b2016-02-25 19:04:07 -0800297 grpc_pollset_set_add_pollset_set(exec_ctx, lb_policy->interested_parties,
298 chand->interested_parties);
Craig Tiller86c99582015-11-25 15:22:26 -0800299 }
300
Mark D. Rothff4df062016-08-22 15:02:49 -0700301 gpr_mu_lock(&chand->mu);
Craig Tiller3f475422015-06-25 10:43:05 -0700302 old_lb_policy = chand->lb_policy;
303 chand->lb_policy = lb_policy;
Mark D. Roth9d480942016-10-19 14:18:05 -0700304 if (chand->method_params_table != NULL) {
305 grpc_mdstr_hash_table_unref(chand->method_params_table);
Mark D. Roth046cf762016-09-26 11:13:51 -0700306 }
Mark D. Roth9d480942016-10-19 14:18:05 -0700307 chand->method_params_table = method_params_table;
Craig Tiller0ede5452016-04-23 12:21:45 -0700308 if (lb_policy != NULL) {
309 grpc_exec_ctx_enqueue_list(exec_ctx, &chand->waiting_for_config_closures,
310 NULL);
311 } else if (chand->resolver == NULL /* disconnected */) {
Craig Tiller804ff712016-05-05 16:25:40 -0700312 grpc_closure_list_fail_all(
313 &chand->waiting_for_config_closures,
314 GRPC_ERROR_CREATE_REFERENCING("Channel disconnected", &error, 1));
Craig Tiller6c396862016-01-28 13:53:40 -0800315 grpc_exec_ctx_enqueue_list(exec_ctx, &chand->waiting_for_config_closures,
316 NULL);
Craig Tillera82950e2015-09-22 12:33:20 -0700317 }
318 if (lb_policy != NULL && chand->exit_idle_when_lb_policy_arrives) {
319 GRPC_LB_POLICY_REF(lb_policy, "exit_idle");
Mark D. Roth4c0fe492016-08-31 13:51:55 -0700320 exit_idle = true;
321 chand->exit_idle_when_lb_policy_arrives = false;
Craig Tillera82950e2015-09-22 12:33:20 -0700322 }
Craig Tiller98465032015-06-29 14:36:42 -0700323
Craig Tiller804ff712016-05-05 16:25:40 -0700324 if (error == GRPC_ERROR_NONE && chand->resolver) {
Craig Tiller9ccf5f12016-05-07 21:41:01 -0700325 set_channel_connectivity_state_locked(
326 exec_ctx, chand, state, GRPC_ERROR_REF(state_error), "new_lb+resolver");
Craig Tillera82950e2015-09-22 12:33:20 -0700327 if (lb_policy != NULL) {
328 watch_lb_policy(exec_ctx, chand, lb_policy, state);
Craig Tiller45724b32015-09-22 10:42:19 -0700329 }
Craig Tiller906e3bc2015-11-24 07:31:31 -0800330 GRPC_CHANNEL_STACK_REF(chand->owning_stack, "resolver");
Mark D. Roth046cf762016-09-26 11:13:51 -0700331 grpc_resolver_next(exec_ctx, chand->resolver, &chand->resolver_result,
Mark D. Rothff4df062016-08-22 15:02:49 -0700332 &chand->on_resolver_result_changed);
333 gpr_mu_unlock(&chand->mu);
Craig Tillera82950e2015-09-22 12:33:20 -0700334 } else {
Craig Tiller76a5c0e2016-03-09 09:05:30 -0800335 if (chand->resolver != NULL) {
336 grpc_resolver_shutdown(exec_ctx, chand->resolver);
337 GRPC_RESOLVER_UNREF(exec_ctx, chand->resolver, "channel");
338 chand->resolver = NULL;
339 }
Craig Tiller804ff712016-05-05 16:25:40 -0700340 grpc_error *refs[] = {error, state_error};
Craig Tiller8c0d96f2016-03-11 14:27:52 -0800341 set_channel_connectivity_state_locked(
Craig Tillerd925c932016-06-06 08:38:50 -0700342 exec_ctx, chand, GRPC_CHANNEL_SHUTDOWN,
Craig Tiller804ff712016-05-05 16:25:40 -0700343 GRPC_ERROR_CREATE_REFERENCING("Got config after disconnection", refs,
344 GPR_ARRAY_SIZE(refs)),
345 "resolver_gone");
Mark D. Rothff4df062016-08-22 15:02:49 -0700346 gpr_mu_unlock(&chand->mu);
Craig Tillera82950e2015-09-22 12:33:20 -0700347 }
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700348
Craig Tillera82950e2015-09-22 12:33:20 -0700349 if (exit_idle) {
350 grpc_lb_policy_exit_idle(exec_ctx, lb_policy);
351 GRPC_LB_POLICY_UNREF(exec_ctx, lb_policy, "exit_idle");
352 }
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700353
Craig Tillera82950e2015-09-22 12:33:20 -0700354 if (old_lb_policy != NULL) {
Craig Tiller69b093b2016-02-25 19:04:07 -0800355 grpc_pollset_set_del_pollset_set(
356 exec_ctx, old_lb_policy->interested_parties, chand->interested_parties);
Craig Tillera82950e2015-09-22 12:33:20 -0700357 GRPC_LB_POLICY_UNREF(exec_ctx, old_lb_policy, "channel");
358 }
Craig Tiller000cd8f2015-09-18 07:20:29 -0700359
Craig Tillera82950e2015-09-22 12:33:20 -0700360 if (lb_policy != NULL) {
361 GRPC_LB_POLICY_UNREF(exec_ctx, lb_policy, "config_change");
362 }
Craig Tiller45724b32015-09-22 10:42:19 -0700363
Craig Tiller906e3bc2015-11-24 07:31:31 -0800364 GRPC_CHANNEL_STACK_UNREF(exec_ctx, chand->owning_stack, "resolver");
Craig Tiller9ccf5f12016-05-07 21:41:01 -0700365 GRPC_ERROR_UNREF(state_error);
Craig Tiller3f475422015-06-25 10:43:05 -0700366}
367
Craig Tillera82950e2015-09-22 12:33:20 -0700368static void cc_start_transport_op(grpc_exec_ctx *exec_ctx,
369 grpc_channel_element *elem,
370 grpc_transport_op *op) {
Craig Tillerca3e9d32015-06-27 18:37:27 -0700371 channel_data *chand = elem->channel_data;
Craig Tiller000cd8f2015-09-18 07:20:29 -0700372
Craig Tiller332f1b32016-05-24 13:21:21 -0700373 grpc_exec_ctx_sched(exec_ctx, op->on_consumed, GRPC_ERROR_NONE, NULL);
Craig Tillerca3e9d32015-06-27 18:37:27 -0700374
Craig Tillerd7f12e32016-03-03 10:08:31 -0800375 GPR_ASSERT(op->set_accept_stream == false);
Craig Tiller28bf8912015-12-07 16:07:04 -0800376 if (op->bind_pollset != NULL) {
Craig Tiller69b093b2016-02-25 19:04:07 -0800377 grpc_pollset_set_add_pollset(exec_ctx, chand->interested_parties,
Craig Tillere2c62372015-12-07 16:11:03 -0800378 op->bind_pollset);
Craig Tiller28bf8912015-12-07 16:07:04 -0800379 }
Craig Tillerca3e9d32015-06-27 18:37:27 -0700380
Mark D. Rothff4df062016-08-22 15:02:49 -0700381 gpr_mu_lock(&chand->mu);
Craig Tillera82950e2015-09-22 12:33:20 -0700382 if (op->on_connectivity_state_change != NULL) {
383 grpc_connectivity_state_notify_on_state_change(
384 exec_ctx, &chand->state_tracker, op->connectivity_state,
385 op->on_connectivity_state_change);
386 op->on_connectivity_state_change = NULL;
387 op->connectivity_state = NULL;
388 }
389
Craig Tiller26dab312015-12-07 14:43:47 -0800390 if (op->send_ping != NULL) {
Craig Tiller87b71e22015-12-07 15:14:14 -0800391 if (chand->lb_policy == NULL) {
Craig Tiller332f1b32016-05-24 13:21:21 -0700392 grpc_exec_ctx_sched(exec_ctx, op->send_ping,
393 GRPC_ERROR_CREATE("Ping with no load balancing"),
394 NULL);
Craig Tiller26dab312015-12-07 14:43:47 -0800395 } else {
Craig Tiller28bf8912015-12-07 16:07:04 -0800396 grpc_lb_policy_ping_one(exec_ctx, chand->lb_policy, op->send_ping);
Craig Tiller26dab312015-12-07 14:43:47 -0800397 op->bind_pollset = NULL;
398 }
399 op->send_ping = NULL;
400 }
401
Craig Tiller1c51edc2016-05-07 16:18:43 -0700402 if (op->disconnect_with_error != GRPC_ERROR_NONE) {
403 if (chand->resolver != NULL) {
404 set_channel_connectivity_state_locked(
Craig Tillerd925c932016-06-06 08:38:50 -0700405 exec_ctx, chand, GRPC_CHANNEL_SHUTDOWN,
Craig Tiller1c51edc2016-05-07 16:18:43 -0700406 GRPC_ERROR_REF(op->disconnect_with_error), "disconnect");
407 grpc_resolver_shutdown(exec_ctx, chand->resolver);
408 GRPC_RESOLVER_UNREF(exec_ctx, chand->resolver, "channel");
409 chand->resolver = NULL;
410 if (!chand->started_resolving) {
411 grpc_closure_list_fail_all(&chand->waiting_for_config_closures,
412 GRPC_ERROR_REF(op->disconnect_with_error));
Craig Tiller9ccf5f12016-05-07 21:41:01 -0700413 grpc_exec_ctx_enqueue_list(exec_ctx,
414 &chand->waiting_for_config_closures, NULL);
Craig Tiller1c51edc2016-05-07 16:18:43 -0700415 }
416 if (chand->lb_policy != NULL) {
417 grpc_pollset_set_del_pollset_set(exec_ctx,
418 chand->lb_policy->interested_parties,
419 chand->interested_parties);
420 GRPC_LB_POLICY_UNREF(exec_ctx, chand->lb_policy, "channel");
421 chand->lb_policy = NULL;
422 }
Craig Tillerb12d22a2016-04-23 12:50:21 -0700423 }
Craig Tiller1c51edc2016-05-07 16:18:43 -0700424 GRPC_ERROR_UNREF(op->disconnect_with_error);
Craig Tillera82950e2015-09-22 12:33:20 -0700425 }
Mark D. Rothff4df062016-08-22 15:02:49 -0700426 gpr_mu_unlock(&chand->mu);
Craig Tillerca3e9d32015-06-27 18:37:27 -0700427}
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800428
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700429/* Constructor for channel_data */
430static void cc_init_channel_elem(grpc_exec_ctx *exec_ctx,
431 grpc_channel_element *elem,
432 grpc_channel_element_args *args) {
433 channel_data *chand = elem->channel_data;
434
435 memset(chand, 0, sizeof(*chand));
436
437 GPR_ASSERT(args->is_last);
438 GPR_ASSERT(elem->filter == &grpc_client_channel_filter);
439
440 gpr_mu_init(&chand->mu);
441 grpc_closure_init(&chand->on_resolver_result_changed,
442 on_resolver_result_changed, chand);
443 chand->owning_stack = args->channel_stack;
444
445 grpc_connectivity_state_init(&chand->state_tracker, GRPC_CHANNEL_IDLE,
446 "client_channel");
447 chand->interested_parties = grpc_pollset_set_create();
448}
449
450/* Destructor for channel_data */
451static void cc_destroy_channel_elem(grpc_exec_ctx *exec_ctx,
452 grpc_channel_element *elem) {
453 channel_data *chand = elem->channel_data;
454
455 if (chand->resolver != NULL) {
456 grpc_resolver_shutdown(exec_ctx, chand->resolver);
457 GRPC_RESOLVER_UNREF(exec_ctx, chand->resolver, "channel");
458 }
Mark D. Roth0e48a9a2016-09-08 14:14:39 -0700459 if (chand->client_channel_factory != NULL) {
460 grpc_client_channel_factory_unref(exec_ctx, chand->client_channel_factory);
461 }
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700462 if (chand->lb_policy != NULL) {
463 grpc_pollset_set_del_pollset_set(exec_ctx,
464 chand->lb_policy->interested_parties,
465 chand->interested_parties);
466 GRPC_LB_POLICY_UNREF(exec_ctx, chand->lb_policy, "channel");
467 }
Mark D. Roth9d480942016-10-19 14:18:05 -0700468 if (chand->method_params_table != NULL) {
469 grpc_mdstr_hash_table_unref(chand->method_params_table);
Mark D. Roth9fe284e2016-09-12 11:22:27 -0700470 }
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700471 grpc_connectivity_state_destroy(exec_ctx, &chand->state_tracker);
472 grpc_pollset_set_destroy(chand->interested_parties);
473 gpr_mu_destroy(&chand->mu);
474}
475
476/*************************************************************************
477 * PER-CALL FUNCTIONS
478 */
479
480#define GET_CALL(call_data) \
481 ((grpc_subchannel_call *)(gpr_atm_acq_load(&(call_data)->subchannel_call)))
482
483#define CANCELLED_CALL ((grpc_subchannel_call *)1)
484
485typedef enum {
486 GRPC_SUBCHANNEL_CALL_HOLDER_NOT_CREATING,
487 GRPC_SUBCHANNEL_CALL_HOLDER_PICKING_SUBCHANNEL
488} subchannel_creation_phase;
489
490/** Call data. Holds a pointer to grpc_subchannel_call and the
491 associated machinery to create such a pointer.
492 Handles queueing of stream ops until a call object is ready, waiting
493 for initial metadata before trying to create a call object,
494 and handling cancellation gracefully. */
495typedef struct client_channel_call_data {
Mark D. Roth72f6da82016-09-02 13:42:38 -0700496 // State for handling deadlines.
497 // The code in deadline_filter.c requires this to be the first field.
Mark D. Roth72f6da82016-09-02 13:42:38 -0700498 // TODO(roth): This is slightly sub-optimal in that grpc_deadline_state
499 // and this struct both independently store a pointer to the call
500 // stack and each has its own mutex. If/when we have time, find a way
Mark D. Roth6ad99172016-09-09 07:52:48 -0700501 // to avoid this without breaking the grpc_deadline_state abstraction.
Mark D. Roth72f6da82016-09-02 13:42:38 -0700502 grpc_deadline_state deadline_state;
Mark D. Rothf28763c2016-09-14 15:18:40 -0700503
Mark D. Rothe40dd292016-10-05 14:58:37 -0700504 grpc_mdstr *path; // Request path.
505 gpr_timespec call_start_time;
506 gpr_timespec deadline;
Mark D. Roth9d480942016-10-19 14:18:05 -0700507 wait_for_ready_value wait_for_ready_from_service_config;
Mark D. Rothe40dd292016-10-05 14:58:37 -0700508 grpc_closure read_service_config;
Mark D. Rothaa850a72016-09-26 13:38:02 -0700509
Mark D. Rothf28763c2016-09-14 15:18:40 -0700510 grpc_error *cancel_error;
Mark D. Roth72f6da82016-09-02 13:42:38 -0700511
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700512 /** either 0 for no call, 1 for cancelled, or a pointer to a
513 grpc_subchannel_call */
514 gpr_atm subchannel_call;
515
516 gpr_mu mu;
517
518 subchannel_creation_phase creation_phase;
519 grpc_connected_subchannel *connected_subchannel;
520 grpc_polling_entity *pollent;
521
Craig Tiller57726ca2016-09-12 11:59:45 -0700522 grpc_transport_stream_op **waiting_ops;
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700523 size_t waiting_ops_count;
524 size_t waiting_ops_capacity;
525
526 grpc_closure next_step;
527
528 grpc_call_stack *owning_call;
David Garcia Quintasd1a47f12016-09-02 12:46:44 +0200529
530 grpc_linked_mdelem lb_token_mdelem;
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700531} call_data;
532
533static void add_waiting_locked(call_data *calld, grpc_transport_stream_op *op) {
534 GPR_TIMER_BEGIN("add_waiting_locked", 0);
535 if (calld->waiting_ops_count == calld->waiting_ops_capacity) {
536 calld->waiting_ops_capacity = GPR_MAX(3, 2 * calld->waiting_ops_capacity);
537 calld->waiting_ops =
538 gpr_realloc(calld->waiting_ops,
539 calld->waiting_ops_capacity * sizeof(*calld->waiting_ops));
540 }
Craig Tiller57726ca2016-09-12 11:59:45 -0700541 calld->waiting_ops[calld->waiting_ops_count++] = op;
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700542 GPR_TIMER_END("add_waiting_locked", 0);
543}
544
545static void fail_locked(grpc_exec_ctx *exec_ctx, call_data *calld,
546 grpc_error *error) {
547 size_t i;
548 for (i = 0; i < calld->waiting_ops_count; i++) {
549 grpc_transport_stream_op_finish_with_failure(
Craig Tiller57726ca2016-09-12 11:59:45 -0700550 exec_ctx, calld->waiting_ops[i], GRPC_ERROR_REF(error));
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700551 }
552 calld->waiting_ops_count = 0;
553 GRPC_ERROR_UNREF(error);
554}
555
556typedef struct {
Craig Tiller57726ca2016-09-12 11:59:45 -0700557 grpc_transport_stream_op **ops;
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700558 size_t nops;
559 grpc_subchannel_call *call;
560} retry_ops_args;
561
562static void retry_ops(grpc_exec_ctx *exec_ctx, void *args, grpc_error *error) {
563 retry_ops_args *a = args;
564 size_t i;
565 for (i = 0; i < a->nops; i++) {
Craig Tiller57726ca2016-09-12 11:59:45 -0700566 grpc_subchannel_call_process_op(exec_ctx, a->call, a->ops[i]);
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700567 }
568 GRPC_SUBCHANNEL_CALL_UNREF(exec_ctx, a->call, "retry_ops");
569 gpr_free(a->ops);
570 gpr_free(a);
571}
572
573static void retry_waiting_locked(grpc_exec_ctx *exec_ctx, call_data *calld) {
Craig Tiller57726ca2016-09-12 11:59:45 -0700574 if (calld->waiting_ops_count == 0) {
575 return;
576 }
577
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700578 retry_ops_args *a = gpr_malloc(sizeof(*a));
579 a->ops = calld->waiting_ops;
580 a->nops = calld->waiting_ops_count;
581 a->call = GET_CALL(calld);
582 if (a->call == CANCELLED_CALL) {
583 gpr_free(a);
584 fail_locked(exec_ctx, calld, GRPC_ERROR_CANCELLED);
585 return;
586 }
587 calld->waiting_ops = NULL;
588 calld->waiting_ops_count = 0;
589 calld->waiting_ops_capacity = 0;
590 GRPC_SUBCHANNEL_CALL_REF(a->call, "retry_ops");
591 grpc_exec_ctx_sched(exec_ctx, grpc_closure_create(retry_ops, a),
592 GRPC_ERROR_NONE, NULL);
593}
594
595static void subchannel_ready(grpc_exec_ctx *exec_ctx, void *arg,
596 grpc_error *error) {
Yuchen Zeng19656b12016-09-01 18:00:45 -0700597 grpc_call_element *elem = arg;
598 call_data *calld = elem->call_data;
599 channel_data *chand = elem->channel_data;
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700600 gpr_mu_lock(&calld->mu);
601 GPR_ASSERT(calld->creation_phase ==
602 GRPC_SUBCHANNEL_CALL_HOLDER_PICKING_SUBCHANNEL);
Yuchen Zeng19656b12016-09-01 18:00:45 -0700603 grpc_polling_entity_del_from_pollset_set(exec_ctx, calld->pollent,
604 chand->interested_parties);
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700605 calld->creation_phase = GRPC_SUBCHANNEL_CALL_HOLDER_NOT_CREATING;
606 if (calld->connected_subchannel == NULL) {
607 gpr_atm_no_barrier_store(&calld->subchannel_call, 1);
608 fail_locked(exec_ctx, calld, GRPC_ERROR_CREATE_REFERENCING(
609 "Failed to create subchannel", &error, 1));
Mark D. Roth72f6da82016-09-02 13:42:38 -0700610 } else if (GET_CALL(calld) == CANCELLED_CALL) {
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700611 /* already cancelled before subchannel became ready */
612 fail_locked(exec_ctx, calld,
613 GRPC_ERROR_CREATE_REFERENCING(
614 "Cancelled before creating subchannel", &error, 1));
615 } else {
Mark D. Roth9fe284e2016-09-12 11:22:27 -0700616 /* Create call on subchannel. */
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700617 grpc_subchannel_call *subchannel_call = NULL;
618 grpc_error *new_error = grpc_connected_subchannel_create_call(
Mark D. Rothaa850a72016-09-26 13:38:02 -0700619 exec_ctx, calld->connected_subchannel, calld->pollent, calld->path,
620 calld->deadline, &subchannel_call);
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700621 if (new_error != GRPC_ERROR_NONE) {
622 new_error = grpc_error_add_child(new_error, error);
623 subchannel_call = CANCELLED_CALL;
624 fail_locked(exec_ctx, calld, new_error);
625 }
626 gpr_atm_rel_store(&calld->subchannel_call,
627 (gpr_atm)(uintptr_t)subchannel_call);
628 retry_waiting_locked(exec_ctx, calld);
629 }
630 gpr_mu_unlock(&calld->mu);
631 GRPC_CALL_STACK_UNREF(exec_ctx, calld->owning_call, "pick_subchannel");
632}
633
634static char *cc_get_peer(grpc_exec_ctx *exec_ctx, grpc_call_element *elem) {
635 call_data *calld = elem->call_data;
636 grpc_subchannel_call *subchannel_call = GET_CALL(calld);
637 if (subchannel_call == NULL || subchannel_call == CANCELLED_CALL) {
638 return NULL;
639 } else {
640 return grpc_subchannel_call_get_peer(exec_ctx, subchannel_call);
641 }
642}
643
Craig Tiller577c9b22015-11-02 14:11:15 -0800644typedef struct {
645 grpc_metadata_batch *initial_metadata;
Craig Tiller8c0d96f2016-03-11 14:27:52 -0800646 uint32_t initial_metadata_flags;
Craig Tillerb5585d42015-11-17 07:18:31 -0800647 grpc_connected_subchannel **connected_subchannel;
Craig Tiller577c9b22015-11-02 14:11:15 -0800648 grpc_closure *on_ready;
649 grpc_call_element *elem;
650 grpc_closure closure;
651} continue_picking_args;
652
Yuchen Zeng144ce652016-09-01 18:19:34 -0700653/** Return true if subchannel is available immediately (in which case on_ready
654 should not be called), or false otherwise (in which case on_ready should be
655 called when the subchannel is available). */
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700656static bool pick_subchannel(grpc_exec_ctx *exec_ctx, grpc_call_element *elem,
657 grpc_metadata_batch *initial_metadata,
658 uint32_t initial_metadata_flags,
659 grpc_connected_subchannel **connected_subchannel,
Mark D. Roth72f6da82016-09-02 13:42:38 -0700660 grpc_closure *on_ready, grpc_error *error);
Craig Tiller577c9b22015-11-02 14:11:15 -0800661
Craig Tiller804ff712016-05-05 16:25:40 -0700662static void continue_picking(grpc_exec_ctx *exec_ctx, void *arg,
663 grpc_error *error) {
Craig Tiller577c9b22015-11-02 14:11:15 -0800664 continue_picking_args *cpa = arg;
Craig Tiller0ede5452016-04-23 12:21:45 -0700665 if (cpa->connected_subchannel == NULL) {
Craig Tiller577c9b22015-11-02 14:11:15 -0800666 /* cancelled, do nothing */
Craig Tiller804ff712016-05-05 16:25:40 -0700667 } else if (error != GRPC_ERROR_NONE) {
Craig Tiller332f1b32016-05-24 13:21:21 -0700668 grpc_exec_ctx_sched(exec_ctx, cpa->on_ready, GRPC_ERROR_REF(error), NULL);
Mark D. Roth9dab7d52016-10-07 07:48:03 -0700669 } else {
670 call_data *calld = cpa->elem->call_data;
671 gpr_mu_lock(&calld->mu);
672 if (pick_subchannel(exec_ctx, cpa->elem, cpa->initial_metadata,
Mark D. Rothfd2ddd22016-10-07 10:11:10 -0700673 cpa->initial_metadata_flags, cpa->connected_subchannel,
674 cpa->on_ready, GRPC_ERROR_NONE)) {
Mark D. Roth9dab7d52016-10-07 07:48:03 -0700675 grpc_exec_ctx_sched(exec_ctx, cpa->on_ready, GRPC_ERROR_NONE, NULL);
676 }
677 gpr_mu_unlock(&calld->mu);
Craig Tiller577c9b22015-11-02 14:11:15 -0800678 }
679 gpr_free(cpa);
680}
681
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700682static bool pick_subchannel(grpc_exec_ctx *exec_ctx, grpc_call_element *elem,
683 grpc_metadata_batch *initial_metadata,
684 uint32_t initial_metadata_flags,
685 grpc_connected_subchannel **connected_subchannel,
Mark D. Roth72f6da82016-09-02 13:42:38 -0700686 grpc_closure *on_ready, grpc_error *error) {
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700687 GPR_TIMER_BEGIN("pick_subchannel", 0);
Craig Tillerbfc9adc2016-06-27 13:16:22 -0700688
Craig Tiller577c9b22015-11-02 14:11:15 -0800689 channel_data *chand = elem->channel_data;
690 call_data *calld = elem->call_data;
691 continue_picking_args *cpa;
692 grpc_closure *closure;
693
Craig Tillerb5585d42015-11-17 07:18:31 -0800694 GPR_ASSERT(connected_subchannel);
Craig Tiller577c9b22015-11-02 14:11:15 -0800695
Mark D. Rothff4df062016-08-22 15:02:49 -0700696 gpr_mu_lock(&chand->mu);
Craig Tiller577c9b22015-11-02 14:11:15 -0800697 if (initial_metadata == NULL) {
698 if (chand->lb_policy != NULL) {
Craig Tillerab33b482015-11-21 08:11:04 -0800699 grpc_lb_policy_cancel_pick(exec_ctx, chand->lb_policy,
Mark D. Roth5f844002016-09-08 08:20:53 -0700700 connected_subchannel, GRPC_ERROR_REF(error));
Craig Tiller577c9b22015-11-02 14:11:15 -0800701 }
702 for (closure = chand->waiting_for_config_closures.head; closure != NULL;
Craig Tiller804ff712016-05-05 16:25:40 -0700703 closure = closure->next_data.next) {
Craig Tiller577c9b22015-11-02 14:11:15 -0800704 cpa = closure->cb_arg;
Craig Tillerb5585d42015-11-17 07:18:31 -0800705 if (cpa->connected_subchannel == connected_subchannel) {
706 cpa->connected_subchannel = NULL;
Mark D. Roth932b10c2016-09-09 08:44:30 -0700707 grpc_exec_ctx_sched(
708 exec_ctx, cpa->on_ready,
709 GRPC_ERROR_CREATE_REFERENCING("Pick cancelled", &error, 1), NULL);
Craig Tiller577c9b22015-11-02 14:11:15 -0800710 }
711 }
Mark D. Rothff4df062016-08-22 15:02:49 -0700712 gpr_mu_unlock(&chand->mu);
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700713 GPR_TIMER_END("pick_subchannel", 0);
Mark D. Roth697a1f62016-09-07 13:35:07 -0700714 GRPC_ERROR_UNREF(error);
Mark D. Roth4c0fe492016-08-31 13:51:55 -0700715 return true;
Craig Tiller577c9b22015-11-02 14:11:15 -0800716 }
Mark D. Roth697a1f62016-09-07 13:35:07 -0700717 GPR_ASSERT(error == GRPC_ERROR_NONE);
Craig Tiller577c9b22015-11-02 14:11:15 -0800718 if (chand->lb_policy != NULL) {
Craig Tiller86c0f8a2015-12-01 20:05:40 -0800719 grpc_lb_policy *lb_policy = chand->lb_policy;
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700720 GRPC_LB_POLICY_REF(lb_policy, "pick_subchannel");
Mark D. Rothff4df062016-08-22 15:02:49 -0700721 gpr_mu_unlock(&chand->mu);
Mark D. Rothe40dd292016-10-05 14:58:37 -0700722 // If the application explicitly set wait_for_ready, use that.
723 // Otherwise, if the service config specified a value for this
724 // method, use that.
Mark D. Rothc1c38582016-10-11 11:03:27 -0700725 const bool wait_for_ready_set_from_api =
726 initial_metadata_flags &
727 GRPC_INITIAL_METADATA_WAIT_FOR_READY_EXPLICITLY_SET;
728 const bool wait_for_ready_set_from_service_config =
729 calld->wait_for_ready_from_service_config != WAIT_FOR_READY_UNSET;
730 if (!wait_for_ready_set_from_api &&
731 wait_for_ready_set_from_service_config) {
Mark D. Rothe40dd292016-10-05 14:58:37 -0700732 if (calld->wait_for_ready_from_service_config == WAIT_FOR_READY_TRUE) {
733 initial_metadata_flags |= GRPC_INITIAL_METADATA_WAIT_FOR_READY;
734 } else {
735 initial_metadata_flags &= ~GRPC_INITIAL_METADATA_WAIT_FOR_READY;
736 }
737 }
David Garcia Quintas61c58012016-10-03 14:44:20 -0700738 // TODO(dgq): make this deadline configurable somehow.
David Garcia Quintas92eb6b92016-09-30 14:07:39 -0700739 const grpc_lb_policy_pick_args inputs = {
Yuchen Zengac8bc422016-10-05 14:00:02 -0700740 initial_metadata, initial_metadata_flags, &calld->lb_token_mdelem,
741 gpr_inf_future(GPR_CLOCK_MONOTONIC)};
Mark D. Roth55f25b62016-10-12 14:55:20 -0700742 const bool result = grpc_lb_policy_pick(
743 exec_ctx, lb_policy, &inputs, connected_subchannel, NULL, on_ready);
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700744 GRPC_LB_POLICY_UNREF(exec_ctx, lb_policy, "pick_subchannel");
745 GPR_TIMER_END("pick_subchannel", 0);
Mark D. Roth9dab7d52016-10-07 07:48:03 -0700746 return result;
Craig Tiller577c9b22015-11-02 14:11:15 -0800747 }
748 if (chand->resolver != NULL && !chand->started_resolving) {
Mark D. Roth4c0fe492016-08-31 13:51:55 -0700749 chand->started_resolving = true;
Craig Tiller906e3bc2015-11-24 07:31:31 -0800750 GRPC_CHANNEL_STACK_REF(chand->owning_stack, "resolver");
Mark D. Roth046cf762016-09-26 11:13:51 -0700751 grpc_resolver_next(exec_ctx, chand->resolver, &chand->resolver_result,
Mark D. Rothff4df062016-08-22 15:02:49 -0700752 &chand->on_resolver_result_changed);
Craig Tiller577c9b22015-11-02 14:11:15 -0800753 }
Craig Tiller0eab6972016-04-23 12:59:57 -0700754 if (chand->resolver != NULL) {
755 cpa = gpr_malloc(sizeof(*cpa));
756 cpa->initial_metadata = initial_metadata;
757 cpa->initial_metadata_flags = initial_metadata_flags;
758 cpa->connected_subchannel = connected_subchannel;
759 cpa->on_ready = on_ready;
760 cpa->elem = elem;
761 grpc_closure_init(&cpa->closure, continue_picking, cpa);
Craig Tiller804ff712016-05-05 16:25:40 -0700762 grpc_closure_list_append(&chand->waiting_for_config_closures, &cpa->closure,
763 GRPC_ERROR_NONE);
Craig Tiller0eab6972016-04-23 12:59:57 -0700764 } else {
Craig Tiller332f1b32016-05-24 13:21:21 -0700765 grpc_exec_ctx_sched(exec_ctx, on_ready, GRPC_ERROR_CREATE("Disconnected"),
766 NULL);
Craig Tiller0eab6972016-04-23 12:59:57 -0700767 }
Mark D. Rothff4df062016-08-22 15:02:49 -0700768 gpr_mu_unlock(&chand->mu);
Craig Tillerbfc9adc2016-06-27 13:16:22 -0700769
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700770 GPR_TIMER_END("pick_subchannel", 0);
Mark D. Roth4c0fe492016-08-31 13:51:55 -0700771 return false;
Craig Tiller577c9b22015-11-02 14:11:15 -0800772}
773
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700774// The logic here is fairly complicated, due to (a) the fact that we
775// need to handle the case where we receive the send op before the
776// initial metadata op, and (b) the need for efficiency, especially in
777// the streaming case.
778// TODO(ctiller): Explain this more thoroughly.
779static void cc_start_transport_stream_op(grpc_exec_ctx *exec_ctx,
780 grpc_call_element *elem,
781 grpc_transport_stream_op *op) {
782 call_data *calld = elem->call_data;
Yuchen Zeng19656b12016-09-01 18:00:45 -0700783 channel_data *chand = elem->channel_data;
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700784 GRPC_CALL_LOG_OP(GPR_INFO, elem, op);
Mark D. Roth72f6da82016-09-02 13:42:38 -0700785 grpc_deadline_state_client_start_transport_stream_op(exec_ctx, elem, op);
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700786 /* try to (atomically) get the call */
787 grpc_subchannel_call *call = GET_CALL(calld);
788 GPR_TIMER_BEGIN("cc_start_transport_stream_op", 0);
789 if (call == CANCELLED_CALL) {
Mark D. Rothf28763c2016-09-14 15:18:40 -0700790 grpc_transport_stream_op_finish_with_failure(
791 exec_ctx, op, GRPC_ERROR_REF(calld->cancel_error));
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700792 GPR_TIMER_END("cc_start_transport_stream_op", 0);
793 return;
794 }
795 if (call != NULL) {
796 grpc_subchannel_call_process_op(exec_ctx, call, op);
797 GPR_TIMER_END("cc_start_transport_stream_op", 0);
798 return;
799 }
800 /* we failed; lock and figure out what to do */
801 gpr_mu_lock(&calld->mu);
802retry:
803 /* need to recheck that another thread hasn't set the call */
804 call = GET_CALL(calld);
805 if (call == CANCELLED_CALL) {
806 gpr_mu_unlock(&calld->mu);
Mark D. Rothf28763c2016-09-14 15:18:40 -0700807 grpc_transport_stream_op_finish_with_failure(
808 exec_ctx, op, GRPC_ERROR_REF(calld->cancel_error));
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700809 GPR_TIMER_END("cc_start_transport_stream_op", 0);
810 return;
811 }
812 if (call != NULL) {
813 gpr_mu_unlock(&calld->mu);
814 grpc_subchannel_call_process_op(exec_ctx, call, op);
815 GPR_TIMER_END("cc_start_transport_stream_op", 0);
816 return;
817 }
818 /* if this is a cancellation, then we can raise our cancelled flag */
819 if (op->cancel_error != GRPC_ERROR_NONE) {
820 if (!gpr_atm_rel_cas(&calld->subchannel_call, 0,
821 (gpr_atm)(uintptr_t)CANCELLED_CALL)) {
822 goto retry;
823 } else {
Mark D. Rothf28763c2016-09-14 15:18:40 -0700824 // Stash a copy of cancel_error in our call data, so that we can use
825 // it for subsequent operations. This ensures that if the call is
826 // cancelled before any ops are passed down (e.g., if the deadline
827 // is in the past when the call starts), we can return the right
828 // error to the caller when the first op does get passed down.
829 calld->cancel_error = GRPC_ERROR_REF(op->cancel_error);
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700830 switch (calld->creation_phase) {
831 case GRPC_SUBCHANNEL_CALL_HOLDER_NOT_CREATING:
832 fail_locked(exec_ctx, calld, GRPC_ERROR_REF(op->cancel_error));
833 break;
834 case GRPC_SUBCHANNEL_CALL_HOLDER_PICKING_SUBCHANNEL:
Mark D. Rothd4c0f552016-09-01 09:25:32 -0700835 pick_subchannel(exec_ctx, elem, NULL, 0, &calld->connected_subchannel,
Mark D. Roth72f6da82016-09-02 13:42:38 -0700836 NULL, GRPC_ERROR_REF(op->cancel_error));
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700837 break;
838 }
839 gpr_mu_unlock(&calld->mu);
Mark D. Roth72f6da82016-09-02 13:42:38 -0700840 grpc_transport_stream_op_finish_with_failure(
841 exec_ctx, op, GRPC_ERROR_REF(op->cancel_error));
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700842 GPR_TIMER_END("cc_start_transport_stream_op", 0);
843 return;
844 }
845 }
846 /* if we don't have a subchannel, try to get one */
847 if (calld->creation_phase == GRPC_SUBCHANNEL_CALL_HOLDER_NOT_CREATING &&
848 calld->connected_subchannel == NULL &&
849 op->send_initial_metadata != NULL) {
850 calld->creation_phase = GRPC_SUBCHANNEL_CALL_HOLDER_PICKING_SUBCHANNEL;
Yuchen Zeng19656b12016-09-01 18:00:45 -0700851 grpc_closure_init(&calld->next_step, subchannel_ready, elem);
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700852 GRPC_CALL_STACK_REF(calld->owning_call, "pick_subchannel");
Yuchen Zeng144ce652016-09-01 18:19:34 -0700853 /* If a subchannel is not available immediately, the polling entity from
854 call_data should be provided to channel_data's interested_parties, so
855 that IO of the lb_policy and resolver could be done under it. */
Mark D. Rothd4c0f552016-09-01 09:25:32 -0700856 if (pick_subchannel(exec_ctx, elem, op->send_initial_metadata,
Mark D. Rothe40dd292016-10-05 14:58:37 -0700857 op->send_initial_metadata_flags,
858 &calld->connected_subchannel, &calld->next_step,
859 GRPC_ERROR_NONE)) {
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700860 calld->creation_phase = GRPC_SUBCHANNEL_CALL_HOLDER_NOT_CREATING;
861 GRPC_CALL_STACK_UNREF(exec_ctx, calld->owning_call, "pick_subchannel");
Yuchen Zeng19656b12016-09-01 18:00:45 -0700862 } else {
Yuchen Zeng19656b12016-09-01 18:00:45 -0700863 grpc_polling_entity_add_to_pollset_set(exec_ctx, calld->pollent,
864 chand->interested_parties);
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700865 }
866 }
867 /* if we've got a subchannel, then let's ask it to create a call */
868 if (calld->creation_phase == GRPC_SUBCHANNEL_CALL_HOLDER_NOT_CREATING &&
869 calld->connected_subchannel != NULL) {
870 grpc_subchannel_call *subchannel_call = NULL;
871 grpc_error *error = grpc_connected_subchannel_create_call(
Mark D. Rothaa850a72016-09-26 13:38:02 -0700872 exec_ctx, calld->connected_subchannel, calld->pollent, calld->path,
873 calld->deadline, &subchannel_call);
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700874 if (error != GRPC_ERROR_NONE) {
875 subchannel_call = CANCELLED_CALL;
876 fail_locked(exec_ctx, calld, GRPC_ERROR_REF(error));
877 grpc_transport_stream_op_finish_with_failure(exec_ctx, op, error);
878 }
879 gpr_atm_rel_store(&calld->subchannel_call,
880 (gpr_atm)(uintptr_t)subchannel_call);
881 retry_waiting_locked(exec_ctx, calld);
882 goto retry;
883 }
884 /* nothing to be done but wait */
885 add_waiting_locked(calld, op);
886 gpr_mu_unlock(&calld->mu);
887 GPR_TIMER_END("cc_start_transport_stream_op", 0);
888}
889
Mark D. Rothe40dd292016-10-05 14:58:37 -0700890// Gets data from the service config. Invoked when the resolver returns
891// its initial result.
892static void read_service_config(grpc_exec_ctx *exec_ctx, void *arg,
893 grpc_error *error) {
894 grpc_call_element *elem = arg;
895 channel_data *chand = elem->channel_data;
896 call_data *calld = elem->call_data;
897 // If this is an error, there's no point in looking at the service config.
Mark D. Roth196387a2016-10-12 14:53:36 -0700898 if (error == GRPC_ERROR_NONE) {
899 // Get the method config table from channel data.
900 gpr_mu_lock(&chand->mu);
Mark D. Roth9d480942016-10-19 14:18:05 -0700901 grpc_mdstr_hash_table *method_params_table = NULL;
902 if (chand->method_params_table != NULL) {
903 method_params_table =
904 grpc_mdstr_hash_table_ref(chand->method_params_table);
Mark D. Rothe40dd292016-10-05 14:58:37 -0700905 }
Mark D. Roth196387a2016-10-12 14:53:36 -0700906 gpr_mu_unlock(&chand->mu);
907 // If the method config table was present, use it.
Mark D. Roth9d480942016-10-19 14:18:05 -0700908 if (method_params_table != NULL) {
909 const method_parameters *method_params =
910 grpc_method_config_table_get(method_params_table, calld->path);
911 if (method_params != NULL) {
912 const bool have_method_timeout =
913 gpr_time_cmp(method_params->timeout, gpr_time_0(GPR_TIMESPAN)) != 0;
914 if (have_method_timeout ||
915 method_params->wait_for_ready != WAIT_FOR_READY_UNSET) {
Mark D. Roth196387a2016-10-12 14:53:36 -0700916 gpr_mu_lock(&calld->mu);
Mark D. Roth9d480942016-10-19 14:18:05 -0700917 if (have_method_timeout) {
918 const gpr_timespec per_method_deadline =
919 gpr_time_add(calld->call_start_time, method_params->timeout);
Mark D. Roth196387a2016-10-12 14:53:36 -0700920 if (gpr_time_cmp(per_method_deadline, calld->deadline) < 0) {
921 calld->deadline = per_method_deadline;
922 // Reset deadline timer.
923 grpc_deadline_state_reset(exec_ctx, elem, calld->deadline);
924 }
925 }
Mark D. Roth9d480942016-10-19 14:18:05 -0700926 if (method_params->wait_for_ready != WAIT_FOR_READY_UNSET) {
Mark D. Roth196387a2016-10-12 14:53:36 -0700927 calld->wait_for_ready_from_service_config =
Mark D. Roth9d480942016-10-19 14:18:05 -0700928 method_params->wait_for_ready;
Mark D. Roth196387a2016-10-12 14:53:36 -0700929 }
930 gpr_mu_unlock(&calld->mu);
931 }
932 }
Mark D. Roth9d480942016-10-19 14:18:05 -0700933 grpc_mdstr_hash_table_unref(method_params_table);
Mark D. Roth196387a2016-10-12 14:53:36 -0700934 }
Mark D. Rothe40dd292016-10-05 14:58:37 -0700935 }
Mark D. Roth31292f22016-10-12 13:14:07 -0700936 GRPC_CALL_STACK_UNREF(exec_ctx, calld->owning_call, "read_service_config");
Mark D. Rothe40dd292016-10-05 14:58:37 -0700937}
938
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800939/* Constructor for call_data */
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700940static grpc_error *cc_init_call_elem(grpc_exec_ctx *exec_ctx,
941 grpc_call_element *elem,
942 grpc_call_element_args *args) {
Mark D. Rothaa850a72016-09-26 13:38:02 -0700943 channel_data *chand = elem->channel_data;
Mark D. Roth4c0fe492016-08-31 13:51:55 -0700944 call_data *calld = elem->call_data;
Mark D. Rothe40dd292016-10-05 14:58:37 -0700945 // Initialize data members.
946 grpc_deadline_state_init(exec_ctx, elem, args->call_stack);
Mark D. Rothaa850a72016-09-26 13:38:02 -0700947 calld->path = GRPC_MDSTR_REF(args->path);
Mark D. Rothff08f332016-10-14 13:01:01 -0700948 calld->call_start_time = args->start_time;
Mark D. Rothe40dd292016-10-05 14:58:37 -0700949 calld->deadline = gpr_convert_clock_type(args->deadline, GPR_CLOCK_MONOTONIC);
950 calld->wait_for_ready_from_service_config = WAIT_FOR_READY_UNSET;
Mark D. Rothf28763c2016-09-14 15:18:40 -0700951 calld->cancel_error = GRPC_ERROR_NONE;
Mark D. Roth4c0fe492016-08-31 13:51:55 -0700952 gpr_atm_rel_store(&calld->subchannel_call, 0);
Mark D. Roth4c0fe492016-08-31 13:51:55 -0700953 gpr_mu_init(&calld->mu);
954 calld->connected_subchannel = NULL;
955 calld->waiting_ops = NULL;
956 calld->waiting_ops_count = 0;
957 calld->waiting_ops_capacity = 0;
958 calld->creation_phase = GRPC_SUBCHANNEL_CALL_HOLDER_NOT_CREATING;
959 calld->owning_call = args->call_stack;
960 calld->pollent = NULL;
Mark D. Rothe40dd292016-10-05 14:58:37 -0700961 // If the resolver has already returned results, then we can access
962 // the service config parameters immediately. Otherwise, we need to
963 // defer that work until the resolver returns an initial result.
964 // TODO(roth): This code is almost but not quite identical to the code
965 // in read_service_config() above. It would be nice to find a way to
966 // combine them, to avoid having to maintain it twice.
967 gpr_mu_lock(&chand->mu);
968 if (chand->lb_policy != NULL) {
969 // We already have a resolver result, so check for service config.
Mark D. Roth9d480942016-10-19 14:18:05 -0700970 if (chand->method_params_table != NULL) {
971 grpc_mdstr_hash_table *method_params_table =
972 grpc_mdstr_hash_table_ref(chand->method_params_table);
Mark D. Rothe40dd292016-10-05 14:58:37 -0700973 gpr_mu_unlock(&chand->mu);
Mark D. Roth9d480942016-10-19 14:18:05 -0700974 method_parameters *method_params =
975 grpc_method_config_table_get(method_params_table, args->path);
976 if (method_params != NULL) {
977 if (gpr_time_cmp(method_params->timeout,
978 gpr_time_0(GPR_CLOCK_MONOTONIC)) != 0) {
Mark D. Rothe40dd292016-10-05 14:58:37 -0700979 gpr_timespec per_method_deadline =
Mark D. Roth9d480942016-10-19 14:18:05 -0700980 gpr_time_add(calld->call_start_time, method_params->timeout);
Mark D. Rothe40dd292016-10-05 14:58:37 -0700981 calld->deadline = gpr_time_min(calld->deadline, per_method_deadline);
982 }
Mark D. Roth9d480942016-10-19 14:18:05 -0700983 if (method_params->wait_for_ready != WAIT_FOR_READY_UNSET) {
Mark D. Rothe40dd292016-10-05 14:58:37 -0700984 calld->wait_for_ready_from_service_config =
Mark D. Roth9d480942016-10-19 14:18:05 -0700985 method_params->wait_for_ready;
Mark D. Rothe40dd292016-10-05 14:58:37 -0700986 }
987 }
Mark D. Roth9d480942016-10-19 14:18:05 -0700988 grpc_mdstr_hash_table_unref(method_params_table);
Mark D. Rothe40dd292016-10-05 14:58:37 -0700989 } else {
990 gpr_mu_unlock(&chand->mu);
991 }
992 } else {
993 // We don't yet have a resolver result, so register a callback to
994 // get the service config data once the resolver returns.
Mark D. Roth31292f22016-10-12 13:14:07 -0700995 // Take a reference to the call stack to be owned by the callback.
996 GRPC_CALL_STACK_REF(calld->owning_call, "read_service_config");
Mark D. Rothe40dd292016-10-05 14:58:37 -0700997 grpc_closure_init(&calld->read_service_config, read_service_config, elem);
998 grpc_closure_list_append(&chand->waiting_for_config_closures,
999 &calld->read_service_config, GRPC_ERROR_NONE);
1000 gpr_mu_unlock(&chand->mu);
1001 }
1002 // Start the deadline timer with the current deadline value. If we
1003 // do not yet have service config data, then the timer may be reset
1004 // later.
1005 grpc_deadline_state_start(exec_ctx, elem, calld->deadline);
Mark D. Roth0badbe82016-06-23 10:15:12 -07001006 return GRPC_ERROR_NONE;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001007}
1008
1009/* Destructor for call_data */
Mark D. Roth2a5959f2016-09-01 08:20:27 -07001010static void cc_destroy_call_elem(grpc_exec_ctx *exec_ctx,
1011 grpc_call_element *elem,
1012 const grpc_call_final_info *final_info,
1013 void *and_free_memory) {
Mark D. Roth4c0fe492016-08-31 13:51:55 -07001014 call_data *calld = elem->call_data;
Mark D. Rothf28763c2016-09-14 15:18:40 -07001015 grpc_deadline_state_destroy(exec_ctx, elem);
Mark D. Rothaa850a72016-09-26 13:38:02 -07001016 GRPC_MDSTR_UNREF(calld->path);
Mark D. Rothf28763c2016-09-14 15:18:40 -07001017 GRPC_ERROR_UNREF(calld->cancel_error);
Mark D. Roth4c0fe492016-08-31 13:51:55 -07001018 grpc_subchannel_call *call = GET_CALL(calld);
1019 if (call != NULL && call != CANCELLED_CALL) {
1020 GRPC_SUBCHANNEL_CALL_UNREF(exec_ctx, call, "client_channel_destroy_call");
1021 }
1022 GPR_ASSERT(calld->creation_phase == GRPC_SUBCHANNEL_CALL_HOLDER_NOT_CREATING);
1023 gpr_mu_destroy(&calld->mu);
1024 GPR_ASSERT(calld->waiting_ops_count == 0);
Craig Tiller693d3942016-10-27 16:51:25 -07001025 if (calld->connected_subchannel != NULL) {
1026 GRPC_CONNECTED_SUBCHANNEL_UNREF(exec_ctx, calld->connected_subchannel,
1027 "picked");
1028 }
Mark D. Roth4c0fe492016-08-31 13:51:55 -07001029 gpr_free(calld->waiting_ops);
Craig Tiller2c8063c2016-03-22 22:12:15 -07001030 gpr_free(and_free_memory);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001031}
1032
David Garcia Quintasf72eb972016-05-03 18:28:09 -07001033static void cc_set_pollset_or_pollset_set(grpc_exec_ctx *exec_ctx,
1034 grpc_call_element *elem,
David Garcia Quintas2a50dfe2016-05-31 15:09:12 -07001035 grpc_polling_entity *pollent) {
Craig Tiller577c9b22015-11-02 14:11:15 -08001036 call_data *calld = elem->call_data;
David Garcia Quintas2a50dfe2016-05-31 15:09:12 -07001037 calld->pollent = pollent;
Craig Tiller577c9b22015-11-02 14:11:15 -08001038}
1039
Mark D. Roth2a5959f2016-09-01 08:20:27 -07001040/*************************************************************************
1041 * EXPORTED SYMBOLS
1042 */
1043
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001044const grpc_channel_filter grpc_client_channel_filter = {
Craig Tillerf40df232016-03-25 13:38:14 -07001045 cc_start_transport_stream_op,
1046 cc_start_transport_op,
1047 sizeof(call_data),
Mark D. Roth2a5959f2016-09-01 08:20:27 -07001048 cc_init_call_elem,
David Garcia Quintas4afce7e2016-04-18 16:25:17 -07001049 cc_set_pollset_or_pollset_set,
Mark D. Roth2a5959f2016-09-01 08:20:27 -07001050 cc_destroy_call_elem,
Craig Tillerf40df232016-03-25 13:38:14 -07001051 sizeof(channel_data),
Mark D. Roth2a5959f2016-09-01 08:20:27 -07001052 cc_init_channel_elem,
1053 cc_destroy_channel_elem,
Craig Tillerf40df232016-03-25 13:38:14 -07001054 cc_get_peer,
1055 "client-channel",
Craig Tiller87d5b192015-04-16 14:37:57 -07001056};
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001057
Mark D. Roth60534972016-09-20 08:37:12 -07001058void grpc_client_channel_finish_initialization(
Mark D. Roth0e48a9a2016-09-08 14:14:39 -07001059 grpc_exec_ctx *exec_ctx, grpc_channel_stack *channel_stack,
1060 grpc_resolver *resolver,
1061 grpc_client_channel_factory *client_channel_factory) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001062 /* post construction initialization: set the transport setup pointer */
Mark D. Roth7f7d1652016-09-20 10:46:15 -07001063 GPR_ASSERT(client_channel_factory != NULL);
Craig Tillera82950e2015-09-22 12:33:20 -07001064 grpc_channel_element *elem = grpc_channel_stack_last_element(channel_stack);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001065 channel_data *chand = elem->channel_data;
Mark D. Rothff4df062016-08-22 15:02:49 -07001066 gpr_mu_lock(&chand->mu);
Craig Tillera82950e2015-09-22 12:33:20 -07001067 GPR_ASSERT(!chand->resolver);
Craig Tillerf5f17122015-06-25 08:47:26 -07001068 chand->resolver = resolver;
Craig Tillera82950e2015-09-22 12:33:20 -07001069 GRPC_RESOLVER_REF(resolver, "channel");
1070 if (!grpc_closure_list_empty(chand->waiting_for_config_closures) ||
1071 chand->exit_idle_when_lb_policy_arrives) {
Mark D. Roth4c0fe492016-08-31 13:51:55 -07001072 chand->started_resolving = true;
Craig Tiller906e3bc2015-11-24 07:31:31 -08001073 GRPC_CHANNEL_STACK_REF(chand->owning_stack, "resolver");
Mark D. Roth046cf762016-09-26 11:13:51 -07001074 grpc_resolver_next(exec_ctx, resolver, &chand->resolver_result,
Mark D. Rothff4df062016-08-22 15:02:49 -07001075 &chand->on_resolver_result_changed);
Craig Tillera82950e2015-09-22 12:33:20 -07001076 }
Mark D. Roth0e48a9a2016-09-08 14:14:39 -07001077 chand->client_channel_factory = client_channel_factory;
1078 grpc_client_channel_factory_ref(client_channel_factory);
Mark D. Rothff4df062016-08-22 15:02:49 -07001079 gpr_mu_unlock(&chand->mu);
Craig Tiller190d3602015-02-18 09:23:38 -08001080}
Craig Tiller48cb07c2015-07-15 16:16:15 -07001081
Craig Tillera82950e2015-09-22 12:33:20 -07001082grpc_connectivity_state grpc_client_channel_check_connectivity_state(
1083 grpc_exec_ctx *exec_ctx, grpc_channel_element *elem, int try_to_connect) {
Craig Tiller48cb07c2015-07-15 16:16:15 -07001084 channel_data *chand = elem->channel_data;
1085 grpc_connectivity_state out;
Mark D. Rothff4df062016-08-22 15:02:49 -07001086 gpr_mu_lock(&chand->mu);
Craig Tiller804ff712016-05-05 16:25:40 -07001087 out = grpc_connectivity_state_check(&chand->state_tracker, NULL);
Craig Tillera82950e2015-09-22 12:33:20 -07001088 if (out == GRPC_CHANNEL_IDLE && try_to_connect) {
1089 if (chand->lb_policy != NULL) {
1090 grpc_lb_policy_exit_idle(exec_ctx, chand->lb_policy);
1091 } else {
Mark D. Roth4c0fe492016-08-31 13:51:55 -07001092 chand->exit_idle_when_lb_policy_arrives = true;
Craig Tillera82950e2015-09-22 12:33:20 -07001093 if (!chand->started_resolving && chand->resolver != NULL) {
Craig Tiller906e3bc2015-11-24 07:31:31 -08001094 GRPC_CHANNEL_STACK_REF(chand->owning_stack, "resolver");
Mark D. Roth4c0fe492016-08-31 13:51:55 -07001095 chand->started_resolving = true;
Mark D. Roth046cf762016-09-26 11:13:51 -07001096 grpc_resolver_next(exec_ctx, chand->resolver, &chand->resolver_result,
Mark D. Rothff4df062016-08-22 15:02:49 -07001097 &chand->on_resolver_result_changed);
Craig Tillera82950e2015-09-22 12:33:20 -07001098 }
Craig Tiller48cb07c2015-07-15 16:16:15 -07001099 }
Craig Tillera82950e2015-09-22 12:33:20 -07001100 }
Mark D. Rothff4df062016-08-22 15:02:49 -07001101 gpr_mu_unlock(&chand->mu);
Craig Tiller48cb07c2015-07-15 16:16:15 -07001102 return out;
1103}
1104
Craig Tiller86c99582015-11-25 15:22:26 -08001105typedef struct {
1106 channel_data *chand;
1107 grpc_pollset *pollset;
1108 grpc_closure *on_complete;
1109 grpc_closure my_closure;
1110} external_connectivity_watcher;
1111
Craig Tiller1d881fb2015-12-01 07:39:04 -08001112static void on_external_watch_complete(grpc_exec_ctx *exec_ctx, void *arg,
Craig Tiller804ff712016-05-05 16:25:40 -07001113 grpc_error *error) {
Craig Tiller86c99582015-11-25 15:22:26 -08001114 external_connectivity_watcher *w = arg;
1115 grpc_closure *follow_up = w->on_complete;
Craig Tiller69b093b2016-02-25 19:04:07 -08001116 grpc_pollset_set_del_pollset(exec_ctx, w->chand->interested_parties,
Craig Tiller1d881fb2015-12-01 07:39:04 -08001117 w->pollset);
1118 GRPC_CHANNEL_STACK_UNREF(exec_ctx, w->chand->owning_stack,
1119 "external_connectivity_watcher");
Craig Tiller86c99582015-11-25 15:22:26 -08001120 gpr_free(w);
Craig Tiller804ff712016-05-05 16:25:40 -07001121 follow_up->cb(exec_ctx, follow_up->cb_arg, error);
Craig Tiller86c99582015-11-25 15:22:26 -08001122}
1123
Craig Tillera82950e2015-09-22 12:33:20 -07001124void grpc_client_channel_watch_connectivity_state(
Craig Tiller906e3bc2015-11-24 07:31:31 -08001125 grpc_exec_ctx *exec_ctx, grpc_channel_element *elem, grpc_pollset *pollset,
Craig Tillera82950e2015-09-22 12:33:20 -07001126 grpc_connectivity_state *state, grpc_closure *on_complete) {
Craig Tiller48cb07c2015-07-15 16:16:15 -07001127 channel_data *chand = elem->channel_data;
Craig Tiller86c99582015-11-25 15:22:26 -08001128 external_connectivity_watcher *w = gpr_malloc(sizeof(*w));
1129 w->chand = chand;
1130 w->pollset = pollset;
1131 w->on_complete = on_complete;
Craig Tiller69b093b2016-02-25 19:04:07 -08001132 grpc_pollset_set_add_pollset(exec_ctx, chand->interested_parties, pollset);
Craig Tiller86c99582015-11-25 15:22:26 -08001133 grpc_closure_init(&w->my_closure, on_external_watch_complete, w);
Craig Tiller1d881fb2015-12-01 07:39:04 -08001134 GRPC_CHANNEL_STACK_REF(w->chand->owning_stack,
1135 "external_connectivity_watcher");
Mark D. Rothff4df062016-08-22 15:02:49 -07001136 gpr_mu_lock(&chand->mu);
Craig Tillera82950e2015-09-22 12:33:20 -07001137 grpc_connectivity_state_notify_on_state_change(
Craig Tiller86c99582015-11-25 15:22:26 -08001138 exec_ctx, &chand->state_tracker, state, &w->my_closure);
Mark D. Rothff4df062016-08-22 15:02:49 -07001139 gpr_mu_unlock(&chand->mu);
Craig Tiller48cb07c2015-07-15 16:16:15 -07001140}