blob: d9161f47cddc03250e6f03a52983343dad677943 [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. Rothea846a02016-11-03 11:32:54 -070058#include "src/core/lib/transport/service_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
Mark D. Roth9d480942016-10-19 14:18:05 -070084static const grpc_mdstr_hash_table_vtable method_parameters_vtable = {
Mark D. Roth6ad80572016-11-03 08:33:17 -070085 gpr_free, method_parameters_copy};
Mark D. Roth9d480942016-10-19 14:18:05 -070086
Mark D. Rothe30baeb2016-11-03 08:16:19 -070087static void *method_parameters_create_from_json(const grpc_json *json) {
Mark D. Rothc968e602016-11-02 14:07:36 -070088 wait_for_ready_value wait_for_ready = WAIT_FOR_READY_UNSET;
Mark D. Roth47f10842016-11-03 08:45:27 -070089 gpr_timespec timeout = {0, 0, GPR_TIMESPAN};
90 for (grpc_json *field = json->child; field != NULL; field = field->next) {
Mark D. Rothc968e602016-11-02 14:07:36 -070091 if (field->key == NULL) continue;
92 if (strcmp(field->key, "wait_for_ready") == 0) {
93 if (wait_for_ready != WAIT_FOR_READY_UNSET) return NULL; // Duplicate.
94 if (field->type != GRPC_JSON_TRUE && field->type != GRPC_JSON_FALSE) {
95 return NULL;
96 }
Mark D. Roth47f10842016-11-03 08:45:27 -070097 wait_for_ready = field->type == GRPC_JSON_TRUE ? WAIT_FOR_READY_TRUE
98 : WAIT_FOR_READY_FALSE;
Mark D. Rothc968e602016-11-02 14:07:36 -070099 } else if (strcmp(field->key, "timeout") == 0) {
100 if (timeout.tv_sec > 0 || timeout.tv_nsec > 0) return NULL; // Duplicate.
101 if (field->type != GRPC_JSON_OBJECT) return NULL;
102 if (field->child == NULL) return NULL;
Mark D. Roth47f10842016-11-03 08:45:27 -0700103 for (grpc_json *subfield = field->child; subfield != NULL;
Mark D. Rothc968e602016-11-02 14:07:36 -0700104 subfield = subfield->next) {
105 if (subfield->key == NULL) return NULL;
106 if (strcmp(subfield->key, "seconds") == 0) {
107 if (timeout.tv_sec > 0) return NULL; // Duplicate.
108 if (subfield->type != GRPC_JSON_NUMBER) return NULL;
109 timeout.tv_sec = gpr_parse_nonnegative_number(subfield->value);
110 if (timeout.tv_sec == -1) return NULL;
111 } else if (strcmp(subfield->key, "nanos") == 0) {
112 if (timeout.tv_nsec > 0) return NULL; // Duplicate.
113 if (subfield->type != GRPC_JSON_NUMBER) return NULL;
114 timeout.tv_nsec = gpr_parse_nonnegative_number(subfield->value);
115 if (timeout.tv_nsec == -1) return NULL;
116 } else {
117 // Unknown key.
118 return NULL;
119 }
120 }
121 }
122 }
Mark D. Roth9d480942016-10-19 14:18:05 -0700123 method_parameters *value = gpr_malloc(sizeof(method_parameters));
Mark D. Rothc968e602016-11-02 14:07:36 -0700124 value->timeout = timeout;
125 value->wait_for_ready = wait_for_ready;
Mark D. Roth9d480942016-10-19 14:18:05 -0700126 return value;
127}
128
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700129/*************************************************************************
130 * CHANNEL-WIDE FUNCTIONS
131 */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800132
Craig Tiller800dacb2015-10-06 09:10:26 -0700133typedef struct client_channel_channel_data {
Craig Tillerf5f17122015-06-25 08:47:26 -0700134 /** resolver for this channel */
135 grpc_resolver *resolver;
Craig Tiller20a3c352015-08-05 08:39:50 -0700136 /** have we started resolving this channel */
Mark D. Roth4c0fe492016-08-31 13:51:55 -0700137 bool started_resolving;
Mark D. Roth0e48a9a2016-09-08 14:14:39 -0700138 /** client channel factory */
139 grpc_client_channel_factory *client_channel_factory;
Craig Tillerf5f17122015-06-25 08:47:26 -0700140
Mark D. Roth046cf762016-09-26 11:13:51 -0700141 /** mutex protecting all variables below in this data structure */
Mark D. Rothff4df062016-08-22 15:02:49 -0700142 gpr_mu mu;
Mark D. Roth046cf762016-09-26 11:13:51 -0700143 /** currently active load balancer */
Craig Tillerf5f17122015-06-25 08:47:26 -0700144 grpc_lb_policy *lb_policy;
Mark D. Roth9d480942016-10-19 14:18:05 -0700145 /** maps method names to method_parameters structs */
146 grpc_mdstr_hash_table *method_params_table;
Mark D. Roth046cf762016-09-26 11:13:51 -0700147 /** incoming resolver result - set by resolver.next() */
Mark D. Rothaf842452016-10-21 15:05:15 -0700148 grpc_channel_args *resolver_result;
Craig Tiller3f475422015-06-25 10:43:05 -0700149 /** a list of closures that are all waiting for config to come in */
Craig Tillerd9ccbbf2015-09-22 09:30:00 -0700150 grpc_closure_list waiting_for_config_closures;
Craig Tiller3f475422015-06-25 10:43:05 -0700151 /** resolver callback */
Mark D. Rothff4df062016-08-22 15:02:49 -0700152 grpc_closure on_resolver_result_changed;
Craig Tiller3f475422015-06-25 10:43:05 -0700153 /** connectivity state being tracked */
Craig Tillerca3e9d32015-06-27 18:37:27 -0700154 grpc_connectivity_state_tracker state_tracker;
Craig Tiller48cb07c2015-07-15 16:16:15 -0700155 /** when an lb_policy arrives, should we try to exit idle */
Mark D. Roth4c0fe492016-08-31 13:51:55 -0700156 bool exit_idle_when_lb_policy_arrives;
Craig Tiller906e3bc2015-11-24 07:31:31 -0800157 /** owning stack */
158 grpc_channel_stack *owning_stack;
Craig Tiller69b093b2016-02-25 19:04:07 -0800159 /** interested parties (owned) */
160 grpc_pollset_set *interested_parties;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800161} channel_data;
162
Craig Tillerd6c98df2015-08-18 09:33:44 -0700163/** We create one watcher for each new lb_policy that is returned from a
Mark D. Roth4c0fe492016-08-31 13:51:55 -0700164 resolver, to watch for state changes from the lb_policy. When a state
165 change is seen, we update the channel, and create a new watcher. */
Craig Tillera82950e2015-09-22 12:33:20 -0700166typedef struct {
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700167 channel_data *chand;
Craig Tiller33825112015-09-18 07:44:19 -0700168 grpc_closure on_changed;
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700169 grpc_connectivity_state state;
170 grpc_lb_policy *lb_policy;
171} lb_policy_connectivity_watcher;
172
Craig Tillera82950e2015-09-22 12:33:20 -0700173static void watch_lb_policy(grpc_exec_ctx *exec_ctx, channel_data *chand,
174 grpc_lb_policy *lb_policy,
175 grpc_connectivity_state current_state);
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700176
Craig Tiller8c0d96f2016-03-11 14:27:52 -0800177static void set_channel_connectivity_state_locked(grpc_exec_ctx *exec_ctx,
178 channel_data *chand,
179 grpc_connectivity_state state,
Craig Tiller804ff712016-05-05 16:25:40 -0700180 grpc_error *error,
Craig Tiller8c0d96f2016-03-11 14:27:52 -0800181 const char *reason) {
182 if ((state == GRPC_CHANNEL_TRANSIENT_FAILURE ||
Craig Tiller48ed92e2016-06-02 11:07:12 -0700183 state == GRPC_CHANNEL_SHUTDOWN) &&
Craig Tiller8c0d96f2016-03-11 14:27:52 -0800184 chand->lb_policy != NULL) {
Mark D. Roth59c9f902016-09-28 13:33:21 -0700185 /* cancel picks with wait_for_ready=false */
Craig Tiller8c0d96f2016-03-11 14:27:52 -0800186 grpc_lb_policy_cancel_picks(
187 exec_ctx, chand->lb_policy,
Mark D. Roth59c9f902016-09-28 13:33:21 -0700188 /* mask= */ GRPC_INITIAL_METADATA_WAIT_FOR_READY,
Mark D. Roth58f52b72016-09-09 13:55:18 -0700189 /* check= */ 0, GRPC_ERROR_REF(error));
Craig Tiller8c0d96f2016-03-11 14:27:52 -0800190 }
Craig Tiller9ccf5f12016-05-07 21:41:01 -0700191 grpc_connectivity_state_set(exec_ctx, &chand->state_tracker, state, error,
192 reason);
Craig Tiller8c0d96f2016-03-11 14:27:52 -0800193}
194
Craig Tiller804ff712016-05-05 16:25:40 -0700195static void on_lb_policy_state_changed_locked(grpc_exec_ctx *exec_ctx,
196 lb_policy_connectivity_watcher *w,
197 grpc_error *error) {
Craig Tillercb2609f2015-11-24 17:19:19 -0800198 grpc_connectivity_state publish_state = w->state;
Craig Tiller5795da72015-09-17 15:27:13 -0700199 /* check if the notification is for a stale policy */
Craig Tillera82950e2015-09-22 12:33:20 -0700200 if (w->lb_policy != w->chand->lb_policy) return;
Craig Tiller5795da72015-09-17 15:27:13 -0700201
Craig Tiller48ed92e2016-06-02 11:07:12 -0700202 if (publish_state == GRPC_CHANNEL_SHUTDOWN && w->chand->resolver != NULL) {
Craig Tillercb2609f2015-11-24 17:19:19 -0800203 publish_state = GRPC_CHANNEL_TRANSIENT_FAILURE;
204 grpc_resolver_channel_saw_error(exec_ctx, w->chand->resolver);
Craig Tillerf62c4d52015-12-04 07:43:07 -0800205 GRPC_LB_POLICY_UNREF(exec_ctx, w->chand->lb_policy, "channel");
206 w->chand->lb_policy = NULL;
Craig Tillercb2609f2015-11-24 17:19:19 -0800207 }
Craig Tiller8c0d96f2016-03-11 14:27:52 -0800208 set_channel_connectivity_state_locked(exec_ctx, w->chand, publish_state,
Craig Tillerfc353d62016-05-10 12:58:03 -0700209 GRPC_ERROR_REF(error), "lb_changed");
Craig Tiller48ed92e2016-06-02 11:07:12 -0700210 if (w->state != GRPC_CHANNEL_SHUTDOWN) {
Craig Tillera82950e2015-09-22 12:33:20 -0700211 watch_lb_policy(exec_ctx, w->chand, w->lb_policy, w->state);
212 }
Craig Tiller5795da72015-09-17 15:27:13 -0700213}
214
Craig Tillera82950e2015-09-22 12:33:20 -0700215static void on_lb_policy_state_changed(grpc_exec_ctx *exec_ctx, void *arg,
Craig Tiller804ff712016-05-05 16:25:40 -0700216 grpc_error *error) {
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700217 lb_policy_connectivity_watcher *w = arg;
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700218
Mark D. Rothff4df062016-08-22 15:02:49 -0700219 gpr_mu_lock(&w->chand->mu);
Craig Tiller804ff712016-05-05 16:25:40 -0700220 on_lb_policy_state_changed_locked(exec_ctx, w, error);
Mark D. Rothff4df062016-08-22 15:02:49 -0700221 gpr_mu_unlock(&w->chand->mu);
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700222
Craig Tiller906e3bc2015-11-24 07:31:31 -0800223 GRPC_CHANNEL_STACK_UNREF(exec_ctx, w->chand->owning_stack, "watch_lb_policy");
Craig Tillera82950e2015-09-22 12:33:20 -0700224 gpr_free(w);
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700225}
226
Craig Tillera82950e2015-09-22 12:33:20 -0700227static void watch_lb_policy(grpc_exec_ctx *exec_ctx, channel_data *chand,
228 grpc_lb_policy *lb_policy,
229 grpc_connectivity_state current_state) {
230 lb_policy_connectivity_watcher *w = gpr_malloc(sizeof(*w));
Craig Tiller906e3bc2015-11-24 07:31:31 -0800231 GRPC_CHANNEL_STACK_REF(chand->owning_stack, "watch_lb_policy");
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700232
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700233 w->chand = chand;
Craig Tillera82950e2015-09-22 12:33:20 -0700234 grpc_closure_init(&w->on_changed, on_lb_policy_state_changed, w);
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700235 w->state = current_state;
236 w->lb_policy = lb_policy;
Craig Tillera82950e2015-09-22 12:33:20 -0700237 grpc_lb_policy_notify_on_state_change(exec_ctx, lb_policy, &w->state,
238 &w->on_changed);
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700239}
240
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700241static void on_resolver_result_changed(grpc_exec_ctx *exec_ctx, void *arg,
242 grpc_error *error) {
Craig Tiller3f475422015-06-25 10:43:05 -0700243 channel_data *chand = arg;
244 grpc_lb_policy *lb_policy = NULL;
245 grpc_lb_policy *old_lb_policy;
Mark D. Roth9d480942016-10-19 14:18:05 -0700246 grpc_mdstr_hash_table *method_params_table = NULL;
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700247 grpc_connectivity_state state = GRPC_CHANNEL_TRANSIENT_FAILURE;
Mark D. Roth4c0fe492016-08-31 13:51:55 -0700248 bool exit_idle = false;
Craig Tiller804ff712016-05-05 16:25:40 -0700249 grpc_error *state_error = GRPC_ERROR_CREATE("No load balancing policy");
Craig Tiller3f475422015-06-25 10:43:05 -0700250
Mark D. Roth046cf762016-09-26 11:13:51 -0700251 if (chand->resolver_result != NULL) {
Mark D. Roth5bd7be02016-10-21 14:19:50 -0700252 // Find LB policy name.
253 const char *lb_policy_name = NULL;
Mark D. Rothaf842452016-10-21 15:05:15 -0700254 const grpc_arg *channel_arg =
Mark D. Roth41124992016-11-03 11:22:20 -0700255 grpc_channel_args_find(chand->resolver_result, GRPC_ARG_LB_POLICY_NAME);
Mark D. Rothaf842452016-10-21 15:05:15 -0700256 if (channel_arg != NULL) {
257 GPR_ASSERT(channel_arg->type == GRPC_ARG_STRING);
258 lb_policy_name = channel_arg->value.string;
Mark D. Roth5bd7be02016-10-21 14:19:50 -0700259 }
Mark D. Roth88405f72016-10-03 08:24:52 -0700260 // Special case: If all of the addresses are balancer addresses,
261 // assume that we should use the grpclb policy, regardless of what the
262 // resolver actually specified.
Mark D. Rothaf842452016-10-21 15:05:15 -0700263 channel_arg =
Mark D. Roth41124992016-11-03 11:22:20 -0700264 grpc_channel_args_find(chand->resolver_result, GRPC_ARG_LB_ADDRESSES);
Mark D. Rothaf842452016-10-21 15:05:15 -0700265 if (channel_arg != NULL) {
266 GPR_ASSERT(channel_arg->type == GRPC_ARG_POINTER);
Mark D. Roth557c9902016-10-24 11:12:05 -0700267 grpc_lb_addresses *addresses = channel_arg->value.pointer.p;
Mark D. Rothaf842452016-10-21 15:05:15 -0700268 bool found_backend_address = false;
269 for (size_t i = 0; i < addresses->num_addresses; ++i) {
270 if (!addresses->addresses[i].is_balancer) {
271 found_backend_address = true;
272 break;
273 }
Mark D. Roth88405f72016-10-03 08:24:52 -0700274 }
Mark D. Rothaf842452016-10-21 15:05:15 -0700275 if (!found_backend_address) {
276 if (lb_policy_name != NULL && strcmp(lb_policy_name, "grpclb") != 0) {
277 gpr_log(GPR_INFO,
278 "resolver requested LB policy %s but provided only balancer "
279 "addresses, no backend addresses -- forcing use of grpclb LB "
280 "policy",
Mark D. Roth5f40e5d2016-10-24 13:09:05 -0700281 lb_policy_name);
Mark D. Rothaf842452016-10-21 15:05:15 -0700282 }
283 lb_policy_name = "grpclb";
Mark D. Roth88405f72016-10-03 08:24:52 -0700284 }
Mark D. Roth88405f72016-10-03 08:24:52 -0700285 }
286 // Use pick_first if nothing was specified and we didn't select grpclb
287 // above.
288 if (lb_policy_name == NULL) lb_policy_name = "pick_first";
Mark D. Roth41124992016-11-03 11:22:20 -0700289 // Instantiate LB policy.
290 grpc_lb_policy_args lb_policy_args;
291 lb_policy_args.args = chand->resolver_result;
292 lb_policy_args.client_channel_factory = chand->client_channel_factory;
Mark D. Roth88405f72016-10-03 08:24:52 -0700293 lb_policy =
294 grpc_lb_policy_create(exec_ctx, lb_policy_name, &lb_policy_args);
Craig Tillera82950e2015-09-22 12:33:20 -0700295 if (lb_policy != NULL) {
Craig Tillera82950e2015-09-22 12:33:20 -0700296 GRPC_LB_POLICY_REF(lb_policy, "config_change");
Craig Tillerf707d622016-05-06 14:26:12 -0700297 GRPC_ERROR_UNREF(state_error);
Craig Tiller804ff712016-05-05 16:25:40 -0700298 state =
299 grpc_lb_policy_check_connectivity(exec_ctx, lb_policy, &state_error);
Craig Tiller45724b32015-09-22 10:42:19 -0700300 }
Mark D. Roth41124992016-11-03 11:22:20 -0700301 // Find service config.
Mark D. Rothaf842452016-10-21 15:05:15 -0700302 channel_arg =
Mark D. Roth41124992016-11-03 11:22:20 -0700303 grpc_channel_args_find(chand->resolver_result, GRPC_ARG_SERVICE_CONFIG);
Mark D. Roth046cf762016-09-26 11:13:51 -0700304 if (channel_arg != NULL) {
305 GPR_ASSERT(channel_arg->type == GRPC_ARG_POINTER);
Mark D. Roth41124992016-11-03 11:22:20 -0700306 grpc_json_tree *service_config_json = channel_arg->value.pointer.p;
Mark D. Rothc968e602016-11-02 14:07:36 -0700307 method_params_table = grpc_method_config_table_create_from_json(
Mark D. Roth41124992016-11-03 11:22:20 -0700308 service_config_json->root, method_parameters_create_from_json,
Mark D. Roth896d9252016-11-03 07:58:25 -0700309 &method_parameters_vtable);
Mark D. Roth9fe284e2016-09-12 11:22:27 -0700310 }
Mark D. Roth41124992016-11-03 11:22:20 -0700311 // Clean up.
Mark D. Rothaf842452016-10-21 15:05:15 -0700312 grpc_channel_args_destroy(chand->resolver_result);
Mark D. Roth046cf762016-09-26 11:13:51 -0700313 chand->resolver_result = NULL;
Craig Tillera82950e2015-09-22 12:33:20 -0700314 }
315
Craig Tiller86c99582015-11-25 15:22:26 -0800316 if (lb_policy != NULL) {
Craig Tiller69b093b2016-02-25 19:04:07 -0800317 grpc_pollset_set_add_pollset_set(exec_ctx, lb_policy->interested_parties,
318 chand->interested_parties);
Craig Tiller86c99582015-11-25 15:22:26 -0800319 }
320
Mark D. Rothff4df062016-08-22 15:02:49 -0700321 gpr_mu_lock(&chand->mu);
Craig Tiller3f475422015-06-25 10:43:05 -0700322 old_lb_policy = chand->lb_policy;
323 chand->lb_policy = lb_policy;
Mark D. Roth9d480942016-10-19 14:18:05 -0700324 if (chand->method_params_table != NULL) {
325 grpc_mdstr_hash_table_unref(chand->method_params_table);
Mark D. Roth046cf762016-09-26 11:13:51 -0700326 }
Mark D. Roth9d480942016-10-19 14:18:05 -0700327 chand->method_params_table = method_params_table;
Craig Tiller0ede5452016-04-23 12:21:45 -0700328 if (lb_policy != NULL) {
329 grpc_exec_ctx_enqueue_list(exec_ctx, &chand->waiting_for_config_closures,
330 NULL);
331 } else if (chand->resolver == NULL /* disconnected */) {
Craig Tiller804ff712016-05-05 16:25:40 -0700332 grpc_closure_list_fail_all(
333 &chand->waiting_for_config_closures,
334 GRPC_ERROR_CREATE_REFERENCING("Channel disconnected", &error, 1));
Craig Tiller6c396862016-01-28 13:53:40 -0800335 grpc_exec_ctx_enqueue_list(exec_ctx, &chand->waiting_for_config_closures,
336 NULL);
Craig Tillera82950e2015-09-22 12:33:20 -0700337 }
338 if (lb_policy != NULL && chand->exit_idle_when_lb_policy_arrives) {
339 GRPC_LB_POLICY_REF(lb_policy, "exit_idle");
Mark D. Roth4c0fe492016-08-31 13:51:55 -0700340 exit_idle = true;
341 chand->exit_idle_when_lb_policy_arrives = false;
Craig Tillera82950e2015-09-22 12:33:20 -0700342 }
Craig Tiller98465032015-06-29 14:36:42 -0700343
Craig Tiller804ff712016-05-05 16:25:40 -0700344 if (error == GRPC_ERROR_NONE && chand->resolver) {
Craig Tiller9ccf5f12016-05-07 21:41:01 -0700345 set_channel_connectivity_state_locked(
346 exec_ctx, chand, state, GRPC_ERROR_REF(state_error), "new_lb+resolver");
Craig Tillera82950e2015-09-22 12:33:20 -0700347 if (lb_policy != NULL) {
348 watch_lb_policy(exec_ctx, chand, lb_policy, state);
Craig Tiller45724b32015-09-22 10:42:19 -0700349 }
Craig Tiller906e3bc2015-11-24 07:31:31 -0800350 GRPC_CHANNEL_STACK_REF(chand->owning_stack, "resolver");
Mark D. Roth046cf762016-09-26 11:13:51 -0700351 grpc_resolver_next(exec_ctx, chand->resolver, &chand->resolver_result,
Mark D. Rothff4df062016-08-22 15:02:49 -0700352 &chand->on_resolver_result_changed);
353 gpr_mu_unlock(&chand->mu);
Craig Tillera82950e2015-09-22 12:33:20 -0700354 } else {
Craig Tiller76a5c0e2016-03-09 09:05:30 -0800355 if (chand->resolver != NULL) {
356 grpc_resolver_shutdown(exec_ctx, chand->resolver);
357 GRPC_RESOLVER_UNREF(exec_ctx, chand->resolver, "channel");
358 chand->resolver = NULL;
359 }
Craig Tiller804ff712016-05-05 16:25:40 -0700360 grpc_error *refs[] = {error, state_error};
Craig Tiller8c0d96f2016-03-11 14:27:52 -0800361 set_channel_connectivity_state_locked(
Craig Tillerd925c932016-06-06 08:38:50 -0700362 exec_ctx, chand, GRPC_CHANNEL_SHUTDOWN,
Craig Tiller804ff712016-05-05 16:25:40 -0700363 GRPC_ERROR_CREATE_REFERENCING("Got config after disconnection", refs,
364 GPR_ARRAY_SIZE(refs)),
365 "resolver_gone");
Mark D. Rothff4df062016-08-22 15:02:49 -0700366 gpr_mu_unlock(&chand->mu);
Craig Tillera82950e2015-09-22 12:33:20 -0700367 }
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700368
Craig Tillera82950e2015-09-22 12:33:20 -0700369 if (exit_idle) {
370 grpc_lb_policy_exit_idle(exec_ctx, lb_policy);
371 GRPC_LB_POLICY_UNREF(exec_ctx, lb_policy, "exit_idle");
372 }
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700373
Craig Tillera82950e2015-09-22 12:33:20 -0700374 if (old_lb_policy != NULL) {
Craig Tiller69b093b2016-02-25 19:04:07 -0800375 grpc_pollset_set_del_pollset_set(
376 exec_ctx, old_lb_policy->interested_parties, chand->interested_parties);
Craig Tillera82950e2015-09-22 12:33:20 -0700377 GRPC_LB_POLICY_UNREF(exec_ctx, old_lb_policy, "channel");
378 }
Craig Tiller000cd8f2015-09-18 07:20:29 -0700379
Craig Tillera82950e2015-09-22 12:33:20 -0700380 if (lb_policy != NULL) {
381 GRPC_LB_POLICY_UNREF(exec_ctx, lb_policy, "config_change");
382 }
Craig Tiller45724b32015-09-22 10:42:19 -0700383
Craig Tiller906e3bc2015-11-24 07:31:31 -0800384 GRPC_CHANNEL_STACK_UNREF(exec_ctx, chand->owning_stack, "resolver");
Craig Tiller9ccf5f12016-05-07 21:41:01 -0700385 GRPC_ERROR_UNREF(state_error);
Craig Tiller3f475422015-06-25 10:43:05 -0700386}
387
Craig Tillera82950e2015-09-22 12:33:20 -0700388static void cc_start_transport_op(grpc_exec_ctx *exec_ctx,
389 grpc_channel_element *elem,
390 grpc_transport_op *op) {
Craig Tillerca3e9d32015-06-27 18:37:27 -0700391 channel_data *chand = elem->channel_data;
Craig Tiller000cd8f2015-09-18 07:20:29 -0700392
Craig Tiller332f1b32016-05-24 13:21:21 -0700393 grpc_exec_ctx_sched(exec_ctx, op->on_consumed, GRPC_ERROR_NONE, NULL);
Craig Tillerca3e9d32015-06-27 18:37:27 -0700394
Craig Tillerd7f12e32016-03-03 10:08:31 -0800395 GPR_ASSERT(op->set_accept_stream == false);
Craig Tiller28bf8912015-12-07 16:07:04 -0800396 if (op->bind_pollset != NULL) {
Craig Tiller69b093b2016-02-25 19:04:07 -0800397 grpc_pollset_set_add_pollset(exec_ctx, chand->interested_parties,
Craig Tillere2c62372015-12-07 16:11:03 -0800398 op->bind_pollset);
Craig Tiller28bf8912015-12-07 16:07:04 -0800399 }
Craig Tillerca3e9d32015-06-27 18:37:27 -0700400
Mark D. Rothff4df062016-08-22 15:02:49 -0700401 gpr_mu_lock(&chand->mu);
Craig Tillera82950e2015-09-22 12:33:20 -0700402 if (op->on_connectivity_state_change != NULL) {
403 grpc_connectivity_state_notify_on_state_change(
404 exec_ctx, &chand->state_tracker, op->connectivity_state,
405 op->on_connectivity_state_change);
406 op->on_connectivity_state_change = NULL;
407 op->connectivity_state = NULL;
408 }
409
Craig Tiller26dab312015-12-07 14:43:47 -0800410 if (op->send_ping != NULL) {
Craig Tiller87b71e22015-12-07 15:14:14 -0800411 if (chand->lb_policy == NULL) {
Craig Tiller332f1b32016-05-24 13:21:21 -0700412 grpc_exec_ctx_sched(exec_ctx, op->send_ping,
413 GRPC_ERROR_CREATE("Ping with no load balancing"),
414 NULL);
Craig Tiller26dab312015-12-07 14:43:47 -0800415 } else {
Craig Tiller28bf8912015-12-07 16:07:04 -0800416 grpc_lb_policy_ping_one(exec_ctx, chand->lb_policy, op->send_ping);
Craig Tiller26dab312015-12-07 14:43:47 -0800417 op->bind_pollset = NULL;
418 }
419 op->send_ping = NULL;
420 }
421
Craig Tiller1c51edc2016-05-07 16:18:43 -0700422 if (op->disconnect_with_error != GRPC_ERROR_NONE) {
423 if (chand->resolver != NULL) {
424 set_channel_connectivity_state_locked(
Craig Tillerd925c932016-06-06 08:38:50 -0700425 exec_ctx, chand, GRPC_CHANNEL_SHUTDOWN,
Craig Tiller1c51edc2016-05-07 16:18:43 -0700426 GRPC_ERROR_REF(op->disconnect_with_error), "disconnect");
427 grpc_resolver_shutdown(exec_ctx, chand->resolver);
428 GRPC_RESOLVER_UNREF(exec_ctx, chand->resolver, "channel");
429 chand->resolver = NULL;
430 if (!chand->started_resolving) {
431 grpc_closure_list_fail_all(&chand->waiting_for_config_closures,
432 GRPC_ERROR_REF(op->disconnect_with_error));
Craig Tiller9ccf5f12016-05-07 21:41:01 -0700433 grpc_exec_ctx_enqueue_list(exec_ctx,
434 &chand->waiting_for_config_closures, NULL);
Craig Tiller1c51edc2016-05-07 16:18:43 -0700435 }
436 if (chand->lb_policy != NULL) {
437 grpc_pollset_set_del_pollset_set(exec_ctx,
438 chand->lb_policy->interested_parties,
439 chand->interested_parties);
440 GRPC_LB_POLICY_UNREF(exec_ctx, chand->lb_policy, "channel");
441 chand->lb_policy = NULL;
442 }
Craig Tillerb12d22a2016-04-23 12:50:21 -0700443 }
Craig Tiller1c51edc2016-05-07 16:18:43 -0700444 GRPC_ERROR_UNREF(op->disconnect_with_error);
Craig Tillera82950e2015-09-22 12:33:20 -0700445 }
Mark D. Rothff4df062016-08-22 15:02:49 -0700446 gpr_mu_unlock(&chand->mu);
Craig Tillerca3e9d32015-06-27 18:37:27 -0700447}
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800448
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700449/* Constructor for channel_data */
450static void cc_init_channel_elem(grpc_exec_ctx *exec_ctx,
451 grpc_channel_element *elem,
452 grpc_channel_element_args *args) {
453 channel_data *chand = elem->channel_data;
454
455 memset(chand, 0, sizeof(*chand));
456
457 GPR_ASSERT(args->is_last);
458 GPR_ASSERT(elem->filter == &grpc_client_channel_filter);
459
460 gpr_mu_init(&chand->mu);
461 grpc_closure_init(&chand->on_resolver_result_changed,
462 on_resolver_result_changed, chand);
463 chand->owning_stack = args->channel_stack;
464
465 grpc_connectivity_state_init(&chand->state_tracker, GRPC_CHANNEL_IDLE,
466 "client_channel");
467 chand->interested_parties = grpc_pollset_set_create();
468}
469
470/* Destructor for channel_data */
471static void cc_destroy_channel_elem(grpc_exec_ctx *exec_ctx,
472 grpc_channel_element *elem) {
473 channel_data *chand = elem->channel_data;
474
475 if (chand->resolver != NULL) {
476 grpc_resolver_shutdown(exec_ctx, chand->resolver);
477 GRPC_RESOLVER_UNREF(exec_ctx, chand->resolver, "channel");
478 }
Mark D. Roth0e48a9a2016-09-08 14:14:39 -0700479 if (chand->client_channel_factory != NULL) {
480 grpc_client_channel_factory_unref(exec_ctx, chand->client_channel_factory);
481 }
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700482 if (chand->lb_policy != NULL) {
483 grpc_pollset_set_del_pollset_set(exec_ctx,
484 chand->lb_policy->interested_parties,
485 chand->interested_parties);
486 GRPC_LB_POLICY_UNREF(exec_ctx, chand->lb_policy, "channel");
487 }
Mark D. Roth9d480942016-10-19 14:18:05 -0700488 if (chand->method_params_table != NULL) {
489 grpc_mdstr_hash_table_unref(chand->method_params_table);
Mark D. Roth9fe284e2016-09-12 11:22:27 -0700490 }
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700491 grpc_connectivity_state_destroy(exec_ctx, &chand->state_tracker);
492 grpc_pollset_set_destroy(chand->interested_parties);
493 gpr_mu_destroy(&chand->mu);
494}
495
496/*************************************************************************
497 * PER-CALL FUNCTIONS
498 */
499
500#define GET_CALL(call_data) \
501 ((grpc_subchannel_call *)(gpr_atm_acq_load(&(call_data)->subchannel_call)))
502
503#define CANCELLED_CALL ((grpc_subchannel_call *)1)
504
505typedef enum {
506 GRPC_SUBCHANNEL_CALL_HOLDER_NOT_CREATING,
507 GRPC_SUBCHANNEL_CALL_HOLDER_PICKING_SUBCHANNEL
508} subchannel_creation_phase;
509
510/** Call data. Holds a pointer to grpc_subchannel_call and the
511 associated machinery to create such a pointer.
512 Handles queueing of stream ops until a call object is ready, waiting
513 for initial metadata before trying to create a call object,
514 and handling cancellation gracefully. */
515typedef struct client_channel_call_data {
Mark D. Roth72f6da82016-09-02 13:42:38 -0700516 // State for handling deadlines.
517 // The code in deadline_filter.c requires this to be the first field.
Mark D. Roth72f6da82016-09-02 13:42:38 -0700518 // TODO(roth): This is slightly sub-optimal in that grpc_deadline_state
519 // and this struct both independently store a pointer to the call
520 // stack and each has its own mutex. If/when we have time, find a way
Mark D. Roth6ad99172016-09-09 07:52:48 -0700521 // to avoid this without breaking the grpc_deadline_state abstraction.
Mark D. Roth72f6da82016-09-02 13:42:38 -0700522 grpc_deadline_state deadline_state;
Mark D. Rothf28763c2016-09-14 15:18:40 -0700523
Mark D. Rothe40dd292016-10-05 14:58:37 -0700524 grpc_mdstr *path; // Request path.
525 gpr_timespec call_start_time;
526 gpr_timespec deadline;
Mark D. Roth9d480942016-10-19 14:18:05 -0700527 wait_for_ready_value wait_for_ready_from_service_config;
Mark D. Rothe40dd292016-10-05 14:58:37 -0700528 grpc_closure read_service_config;
Mark D. Rothaa850a72016-09-26 13:38:02 -0700529
Mark D. Rothf28763c2016-09-14 15:18:40 -0700530 grpc_error *cancel_error;
Mark D. Roth72f6da82016-09-02 13:42:38 -0700531
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700532 /** either 0 for no call, 1 for cancelled, or a pointer to a
533 grpc_subchannel_call */
534 gpr_atm subchannel_call;
535
536 gpr_mu mu;
537
538 subchannel_creation_phase creation_phase;
539 grpc_connected_subchannel *connected_subchannel;
540 grpc_polling_entity *pollent;
541
Craig Tiller57726ca2016-09-12 11:59:45 -0700542 grpc_transport_stream_op **waiting_ops;
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700543 size_t waiting_ops_count;
544 size_t waiting_ops_capacity;
545
546 grpc_closure next_step;
547
548 grpc_call_stack *owning_call;
David Garcia Quintasd1a47f12016-09-02 12:46:44 +0200549
550 grpc_linked_mdelem lb_token_mdelem;
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700551} call_data;
552
553static void add_waiting_locked(call_data *calld, grpc_transport_stream_op *op) {
554 GPR_TIMER_BEGIN("add_waiting_locked", 0);
555 if (calld->waiting_ops_count == calld->waiting_ops_capacity) {
556 calld->waiting_ops_capacity = GPR_MAX(3, 2 * calld->waiting_ops_capacity);
557 calld->waiting_ops =
558 gpr_realloc(calld->waiting_ops,
559 calld->waiting_ops_capacity * sizeof(*calld->waiting_ops));
560 }
Craig Tiller57726ca2016-09-12 11:59:45 -0700561 calld->waiting_ops[calld->waiting_ops_count++] = op;
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700562 GPR_TIMER_END("add_waiting_locked", 0);
563}
564
565static void fail_locked(grpc_exec_ctx *exec_ctx, call_data *calld,
566 grpc_error *error) {
567 size_t i;
568 for (i = 0; i < calld->waiting_ops_count; i++) {
569 grpc_transport_stream_op_finish_with_failure(
Craig Tiller57726ca2016-09-12 11:59:45 -0700570 exec_ctx, calld->waiting_ops[i], GRPC_ERROR_REF(error));
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700571 }
572 calld->waiting_ops_count = 0;
573 GRPC_ERROR_UNREF(error);
574}
575
576typedef struct {
Craig Tiller57726ca2016-09-12 11:59:45 -0700577 grpc_transport_stream_op **ops;
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700578 size_t nops;
579 grpc_subchannel_call *call;
580} retry_ops_args;
581
582static void retry_ops(grpc_exec_ctx *exec_ctx, void *args, grpc_error *error) {
583 retry_ops_args *a = args;
584 size_t i;
585 for (i = 0; i < a->nops; i++) {
Craig Tiller57726ca2016-09-12 11:59:45 -0700586 grpc_subchannel_call_process_op(exec_ctx, a->call, a->ops[i]);
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700587 }
588 GRPC_SUBCHANNEL_CALL_UNREF(exec_ctx, a->call, "retry_ops");
589 gpr_free(a->ops);
590 gpr_free(a);
591}
592
593static void retry_waiting_locked(grpc_exec_ctx *exec_ctx, call_data *calld) {
Craig Tiller57726ca2016-09-12 11:59:45 -0700594 if (calld->waiting_ops_count == 0) {
595 return;
596 }
597
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700598 retry_ops_args *a = gpr_malloc(sizeof(*a));
599 a->ops = calld->waiting_ops;
600 a->nops = calld->waiting_ops_count;
601 a->call = GET_CALL(calld);
602 if (a->call == CANCELLED_CALL) {
603 gpr_free(a);
604 fail_locked(exec_ctx, calld, GRPC_ERROR_CANCELLED);
605 return;
606 }
607 calld->waiting_ops = NULL;
608 calld->waiting_ops_count = 0;
609 calld->waiting_ops_capacity = 0;
610 GRPC_SUBCHANNEL_CALL_REF(a->call, "retry_ops");
611 grpc_exec_ctx_sched(exec_ctx, grpc_closure_create(retry_ops, a),
612 GRPC_ERROR_NONE, NULL);
613}
614
615static void subchannel_ready(grpc_exec_ctx *exec_ctx, void *arg,
616 grpc_error *error) {
Yuchen Zeng19656b12016-09-01 18:00:45 -0700617 grpc_call_element *elem = arg;
618 call_data *calld = elem->call_data;
619 channel_data *chand = elem->channel_data;
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700620 gpr_mu_lock(&calld->mu);
621 GPR_ASSERT(calld->creation_phase ==
622 GRPC_SUBCHANNEL_CALL_HOLDER_PICKING_SUBCHANNEL);
Yuchen Zeng19656b12016-09-01 18:00:45 -0700623 grpc_polling_entity_del_from_pollset_set(exec_ctx, calld->pollent,
624 chand->interested_parties);
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700625 calld->creation_phase = GRPC_SUBCHANNEL_CALL_HOLDER_NOT_CREATING;
626 if (calld->connected_subchannel == NULL) {
627 gpr_atm_no_barrier_store(&calld->subchannel_call, 1);
628 fail_locked(exec_ctx, calld, GRPC_ERROR_CREATE_REFERENCING(
629 "Failed to create subchannel", &error, 1));
Mark D. Roth72f6da82016-09-02 13:42:38 -0700630 } else if (GET_CALL(calld) == CANCELLED_CALL) {
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700631 /* already cancelled before subchannel became ready */
632 fail_locked(exec_ctx, calld,
633 GRPC_ERROR_CREATE_REFERENCING(
634 "Cancelled before creating subchannel", &error, 1));
635 } else {
Mark D. Roth9fe284e2016-09-12 11:22:27 -0700636 /* Create call on subchannel. */
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700637 grpc_subchannel_call *subchannel_call = NULL;
638 grpc_error *new_error = grpc_connected_subchannel_create_call(
Mark D. Rothaa850a72016-09-26 13:38:02 -0700639 exec_ctx, calld->connected_subchannel, calld->pollent, calld->path,
640 calld->deadline, &subchannel_call);
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700641 if (new_error != GRPC_ERROR_NONE) {
642 new_error = grpc_error_add_child(new_error, error);
643 subchannel_call = CANCELLED_CALL;
644 fail_locked(exec_ctx, calld, new_error);
645 }
646 gpr_atm_rel_store(&calld->subchannel_call,
647 (gpr_atm)(uintptr_t)subchannel_call);
648 retry_waiting_locked(exec_ctx, calld);
649 }
650 gpr_mu_unlock(&calld->mu);
651 GRPC_CALL_STACK_UNREF(exec_ctx, calld->owning_call, "pick_subchannel");
652}
653
654static char *cc_get_peer(grpc_exec_ctx *exec_ctx, grpc_call_element *elem) {
655 call_data *calld = elem->call_data;
656 grpc_subchannel_call *subchannel_call = GET_CALL(calld);
657 if (subchannel_call == NULL || subchannel_call == CANCELLED_CALL) {
658 return NULL;
659 } else {
660 return grpc_subchannel_call_get_peer(exec_ctx, subchannel_call);
661 }
662}
663
Craig Tiller577c9b22015-11-02 14:11:15 -0800664typedef struct {
665 grpc_metadata_batch *initial_metadata;
Craig Tiller8c0d96f2016-03-11 14:27:52 -0800666 uint32_t initial_metadata_flags;
Craig Tillerb5585d42015-11-17 07:18:31 -0800667 grpc_connected_subchannel **connected_subchannel;
Craig Tiller577c9b22015-11-02 14:11:15 -0800668 grpc_closure *on_ready;
669 grpc_call_element *elem;
670 grpc_closure closure;
671} continue_picking_args;
672
Yuchen Zeng144ce652016-09-01 18:19:34 -0700673/** Return true if subchannel is available immediately (in which case on_ready
674 should not be called), or false otherwise (in which case on_ready should be
675 called when the subchannel is available). */
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700676static bool pick_subchannel(grpc_exec_ctx *exec_ctx, grpc_call_element *elem,
677 grpc_metadata_batch *initial_metadata,
678 uint32_t initial_metadata_flags,
679 grpc_connected_subchannel **connected_subchannel,
Mark D. Roth72f6da82016-09-02 13:42:38 -0700680 grpc_closure *on_ready, grpc_error *error);
Craig Tiller577c9b22015-11-02 14:11:15 -0800681
Craig Tiller804ff712016-05-05 16:25:40 -0700682static void continue_picking(grpc_exec_ctx *exec_ctx, void *arg,
683 grpc_error *error) {
Craig Tiller577c9b22015-11-02 14:11:15 -0800684 continue_picking_args *cpa = arg;
Craig Tiller0ede5452016-04-23 12:21:45 -0700685 if (cpa->connected_subchannel == NULL) {
Craig Tiller577c9b22015-11-02 14:11:15 -0800686 /* cancelled, do nothing */
Craig Tiller804ff712016-05-05 16:25:40 -0700687 } else if (error != GRPC_ERROR_NONE) {
Craig Tiller332f1b32016-05-24 13:21:21 -0700688 grpc_exec_ctx_sched(exec_ctx, cpa->on_ready, GRPC_ERROR_REF(error), NULL);
Mark D. Roth9dab7d52016-10-07 07:48:03 -0700689 } else {
690 call_data *calld = cpa->elem->call_data;
691 gpr_mu_lock(&calld->mu);
692 if (pick_subchannel(exec_ctx, cpa->elem, cpa->initial_metadata,
Mark D. Rothfd2ddd22016-10-07 10:11:10 -0700693 cpa->initial_metadata_flags, cpa->connected_subchannel,
694 cpa->on_ready, GRPC_ERROR_NONE)) {
Mark D. Roth9dab7d52016-10-07 07:48:03 -0700695 grpc_exec_ctx_sched(exec_ctx, cpa->on_ready, GRPC_ERROR_NONE, NULL);
696 }
697 gpr_mu_unlock(&calld->mu);
Craig Tiller577c9b22015-11-02 14:11:15 -0800698 }
699 gpr_free(cpa);
700}
701
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700702static bool pick_subchannel(grpc_exec_ctx *exec_ctx, grpc_call_element *elem,
703 grpc_metadata_batch *initial_metadata,
704 uint32_t initial_metadata_flags,
705 grpc_connected_subchannel **connected_subchannel,
Mark D. Roth72f6da82016-09-02 13:42:38 -0700706 grpc_closure *on_ready, grpc_error *error) {
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700707 GPR_TIMER_BEGIN("pick_subchannel", 0);
Craig Tillerbfc9adc2016-06-27 13:16:22 -0700708
Craig Tiller577c9b22015-11-02 14:11:15 -0800709 channel_data *chand = elem->channel_data;
710 call_data *calld = elem->call_data;
711 continue_picking_args *cpa;
712 grpc_closure *closure;
713
Craig Tillerb5585d42015-11-17 07:18:31 -0800714 GPR_ASSERT(connected_subchannel);
Craig Tiller577c9b22015-11-02 14:11:15 -0800715
Mark D. Rothff4df062016-08-22 15:02:49 -0700716 gpr_mu_lock(&chand->mu);
Craig Tiller577c9b22015-11-02 14:11:15 -0800717 if (initial_metadata == NULL) {
718 if (chand->lb_policy != NULL) {
Craig Tillerab33b482015-11-21 08:11:04 -0800719 grpc_lb_policy_cancel_pick(exec_ctx, chand->lb_policy,
Mark D. Roth5f844002016-09-08 08:20:53 -0700720 connected_subchannel, GRPC_ERROR_REF(error));
Craig Tiller577c9b22015-11-02 14:11:15 -0800721 }
722 for (closure = chand->waiting_for_config_closures.head; closure != NULL;
Craig Tiller804ff712016-05-05 16:25:40 -0700723 closure = closure->next_data.next) {
Craig Tiller577c9b22015-11-02 14:11:15 -0800724 cpa = closure->cb_arg;
Craig Tillerb5585d42015-11-17 07:18:31 -0800725 if (cpa->connected_subchannel == connected_subchannel) {
726 cpa->connected_subchannel = NULL;
Mark D. Roth932b10c2016-09-09 08:44:30 -0700727 grpc_exec_ctx_sched(
728 exec_ctx, cpa->on_ready,
729 GRPC_ERROR_CREATE_REFERENCING("Pick cancelled", &error, 1), NULL);
Craig Tiller577c9b22015-11-02 14:11:15 -0800730 }
731 }
Mark D. Rothff4df062016-08-22 15:02:49 -0700732 gpr_mu_unlock(&chand->mu);
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700733 GPR_TIMER_END("pick_subchannel", 0);
Mark D. Roth697a1f62016-09-07 13:35:07 -0700734 GRPC_ERROR_UNREF(error);
Mark D. Roth4c0fe492016-08-31 13:51:55 -0700735 return true;
Craig Tiller577c9b22015-11-02 14:11:15 -0800736 }
Mark D. Roth697a1f62016-09-07 13:35:07 -0700737 GPR_ASSERT(error == GRPC_ERROR_NONE);
Craig Tiller577c9b22015-11-02 14:11:15 -0800738 if (chand->lb_policy != NULL) {
Craig Tiller86c0f8a2015-12-01 20:05:40 -0800739 grpc_lb_policy *lb_policy = chand->lb_policy;
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700740 GRPC_LB_POLICY_REF(lb_policy, "pick_subchannel");
Mark D. Rothff4df062016-08-22 15:02:49 -0700741 gpr_mu_unlock(&chand->mu);
Mark D. Rothe40dd292016-10-05 14:58:37 -0700742 // If the application explicitly set wait_for_ready, use that.
743 // Otherwise, if the service config specified a value for this
744 // method, use that.
Mark D. Rothc1c38582016-10-11 11:03:27 -0700745 const bool wait_for_ready_set_from_api =
746 initial_metadata_flags &
747 GRPC_INITIAL_METADATA_WAIT_FOR_READY_EXPLICITLY_SET;
748 const bool wait_for_ready_set_from_service_config =
749 calld->wait_for_ready_from_service_config != WAIT_FOR_READY_UNSET;
750 if (!wait_for_ready_set_from_api &&
751 wait_for_ready_set_from_service_config) {
Mark D. Rothe40dd292016-10-05 14:58:37 -0700752 if (calld->wait_for_ready_from_service_config == WAIT_FOR_READY_TRUE) {
753 initial_metadata_flags |= GRPC_INITIAL_METADATA_WAIT_FOR_READY;
754 } else {
755 initial_metadata_flags &= ~GRPC_INITIAL_METADATA_WAIT_FOR_READY;
756 }
757 }
David Garcia Quintas61c58012016-10-03 14:44:20 -0700758 // TODO(dgq): make this deadline configurable somehow.
David Garcia Quintas92eb6b92016-09-30 14:07:39 -0700759 const grpc_lb_policy_pick_args inputs = {
Yuchen Zengac8bc422016-10-05 14:00:02 -0700760 initial_metadata, initial_metadata_flags, &calld->lb_token_mdelem,
761 gpr_inf_future(GPR_CLOCK_MONOTONIC)};
Mark D. Roth55f25b62016-10-12 14:55:20 -0700762 const bool result = grpc_lb_policy_pick(
763 exec_ctx, lb_policy, &inputs, connected_subchannel, NULL, on_ready);
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700764 GRPC_LB_POLICY_UNREF(exec_ctx, lb_policy, "pick_subchannel");
765 GPR_TIMER_END("pick_subchannel", 0);
Mark D. Roth9dab7d52016-10-07 07:48:03 -0700766 return result;
Craig Tiller577c9b22015-11-02 14:11:15 -0800767 }
768 if (chand->resolver != NULL && !chand->started_resolving) {
Mark D. Roth4c0fe492016-08-31 13:51:55 -0700769 chand->started_resolving = true;
Craig Tiller906e3bc2015-11-24 07:31:31 -0800770 GRPC_CHANNEL_STACK_REF(chand->owning_stack, "resolver");
Mark D. Roth046cf762016-09-26 11:13:51 -0700771 grpc_resolver_next(exec_ctx, chand->resolver, &chand->resolver_result,
Mark D. Rothff4df062016-08-22 15:02:49 -0700772 &chand->on_resolver_result_changed);
Craig Tiller577c9b22015-11-02 14:11:15 -0800773 }
Craig Tiller0eab6972016-04-23 12:59:57 -0700774 if (chand->resolver != NULL) {
775 cpa = gpr_malloc(sizeof(*cpa));
776 cpa->initial_metadata = initial_metadata;
777 cpa->initial_metadata_flags = initial_metadata_flags;
778 cpa->connected_subchannel = connected_subchannel;
779 cpa->on_ready = on_ready;
780 cpa->elem = elem;
781 grpc_closure_init(&cpa->closure, continue_picking, cpa);
Craig Tiller804ff712016-05-05 16:25:40 -0700782 grpc_closure_list_append(&chand->waiting_for_config_closures, &cpa->closure,
783 GRPC_ERROR_NONE);
Craig Tiller0eab6972016-04-23 12:59:57 -0700784 } else {
Craig Tiller332f1b32016-05-24 13:21:21 -0700785 grpc_exec_ctx_sched(exec_ctx, on_ready, GRPC_ERROR_CREATE("Disconnected"),
786 NULL);
Craig Tiller0eab6972016-04-23 12:59:57 -0700787 }
Mark D. Rothff4df062016-08-22 15:02:49 -0700788 gpr_mu_unlock(&chand->mu);
Craig Tillerbfc9adc2016-06-27 13:16:22 -0700789
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700790 GPR_TIMER_END("pick_subchannel", 0);
Mark D. Roth4c0fe492016-08-31 13:51:55 -0700791 return false;
Craig Tiller577c9b22015-11-02 14:11:15 -0800792}
793
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700794// The logic here is fairly complicated, due to (a) the fact that we
795// need to handle the case where we receive the send op before the
796// initial metadata op, and (b) the need for efficiency, especially in
797// the streaming case.
798// TODO(ctiller): Explain this more thoroughly.
799static void cc_start_transport_stream_op(grpc_exec_ctx *exec_ctx,
800 grpc_call_element *elem,
801 grpc_transport_stream_op *op) {
802 call_data *calld = elem->call_data;
Yuchen Zeng19656b12016-09-01 18:00:45 -0700803 channel_data *chand = elem->channel_data;
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700804 GRPC_CALL_LOG_OP(GPR_INFO, elem, op);
Mark D. Roth72f6da82016-09-02 13:42:38 -0700805 grpc_deadline_state_client_start_transport_stream_op(exec_ctx, elem, op);
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700806 /* try to (atomically) get the call */
807 grpc_subchannel_call *call = GET_CALL(calld);
808 GPR_TIMER_BEGIN("cc_start_transport_stream_op", 0);
809 if (call == CANCELLED_CALL) {
Mark D. Rothf28763c2016-09-14 15:18:40 -0700810 grpc_transport_stream_op_finish_with_failure(
811 exec_ctx, op, GRPC_ERROR_REF(calld->cancel_error));
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700812 GPR_TIMER_END("cc_start_transport_stream_op", 0);
813 return;
814 }
815 if (call != NULL) {
816 grpc_subchannel_call_process_op(exec_ctx, call, op);
817 GPR_TIMER_END("cc_start_transport_stream_op", 0);
818 return;
819 }
820 /* we failed; lock and figure out what to do */
821 gpr_mu_lock(&calld->mu);
822retry:
823 /* need to recheck that another thread hasn't set the call */
824 call = GET_CALL(calld);
825 if (call == CANCELLED_CALL) {
826 gpr_mu_unlock(&calld->mu);
Mark D. Rothf28763c2016-09-14 15:18:40 -0700827 grpc_transport_stream_op_finish_with_failure(
828 exec_ctx, op, GRPC_ERROR_REF(calld->cancel_error));
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700829 GPR_TIMER_END("cc_start_transport_stream_op", 0);
830 return;
831 }
832 if (call != NULL) {
833 gpr_mu_unlock(&calld->mu);
834 grpc_subchannel_call_process_op(exec_ctx, call, op);
835 GPR_TIMER_END("cc_start_transport_stream_op", 0);
836 return;
837 }
838 /* if this is a cancellation, then we can raise our cancelled flag */
839 if (op->cancel_error != GRPC_ERROR_NONE) {
840 if (!gpr_atm_rel_cas(&calld->subchannel_call, 0,
841 (gpr_atm)(uintptr_t)CANCELLED_CALL)) {
842 goto retry;
843 } else {
Mark D. Rothf28763c2016-09-14 15:18:40 -0700844 // Stash a copy of cancel_error in our call data, so that we can use
845 // it for subsequent operations. This ensures that if the call is
846 // cancelled before any ops are passed down (e.g., if the deadline
847 // is in the past when the call starts), we can return the right
848 // error to the caller when the first op does get passed down.
849 calld->cancel_error = GRPC_ERROR_REF(op->cancel_error);
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700850 switch (calld->creation_phase) {
851 case GRPC_SUBCHANNEL_CALL_HOLDER_NOT_CREATING:
852 fail_locked(exec_ctx, calld, GRPC_ERROR_REF(op->cancel_error));
853 break;
854 case GRPC_SUBCHANNEL_CALL_HOLDER_PICKING_SUBCHANNEL:
Mark D. Rothd4c0f552016-09-01 09:25:32 -0700855 pick_subchannel(exec_ctx, elem, NULL, 0, &calld->connected_subchannel,
Mark D. Roth72f6da82016-09-02 13:42:38 -0700856 NULL, GRPC_ERROR_REF(op->cancel_error));
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700857 break;
858 }
859 gpr_mu_unlock(&calld->mu);
Mark D. Roth72f6da82016-09-02 13:42:38 -0700860 grpc_transport_stream_op_finish_with_failure(
861 exec_ctx, op, GRPC_ERROR_REF(op->cancel_error));
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700862 GPR_TIMER_END("cc_start_transport_stream_op", 0);
863 return;
864 }
865 }
866 /* if we don't have a subchannel, try to get one */
867 if (calld->creation_phase == GRPC_SUBCHANNEL_CALL_HOLDER_NOT_CREATING &&
868 calld->connected_subchannel == NULL &&
869 op->send_initial_metadata != NULL) {
870 calld->creation_phase = GRPC_SUBCHANNEL_CALL_HOLDER_PICKING_SUBCHANNEL;
Yuchen Zeng19656b12016-09-01 18:00:45 -0700871 grpc_closure_init(&calld->next_step, subchannel_ready, elem);
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700872 GRPC_CALL_STACK_REF(calld->owning_call, "pick_subchannel");
Yuchen Zeng144ce652016-09-01 18:19:34 -0700873 /* If a subchannel is not available immediately, the polling entity from
874 call_data should be provided to channel_data's interested_parties, so
875 that IO of the lb_policy and resolver could be done under it. */
Mark D. Rothd4c0f552016-09-01 09:25:32 -0700876 if (pick_subchannel(exec_ctx, elem, op->send_initial_metadata,
Mark D. Rothe40dd292016-10-05 14:58:37 -0700877 op->send_initial_metadata_flags,
878 &calld->connected_subchannel, &calld->next_step,
879 GRPC_ERROR_NONE)) {
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700880 calld->creation_phase = GRPC_SUBCHANNEL_CALL_HOLDER_NOT_CREATING;
881 GRPC_CALL_STACK_UNREF(exec_ctx, calld->owning_call, "pick_subchannel");
Yuchen Zeng19656b12016-09-01 18:00:45 -0700882 } else {
Yuchen Zeng19656b12016-09-01 18:00:45 -0700883 grpc_polling_entity_add_to_pollset_set(exec_ctx, calld->pollent,
884 chand->interested_parties);
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700885 }
886 }
887 /* if we've got a subchannel, then let's ask it to create a call */
888 if (calld->creation_phase == GRPC_SUBCHANNEL_CALL_HOLDER_NOT_CREATING &&
889 calld->connected_subchannel != NULL) {
890 grpc_subchannel_call *subchannel_call = NULL;
891 grpc_error *error = grpc_connected_subchannel_create_call(
Mark D. Rothaa850a72016-09-26 13:38:02 -0700892 exec_ctx, calld->connected_subchannel, calld->pollent, calld->path,
893 calld->deadline, &subchannel_call);
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700894 if (error != GRPC_ERROR_NONE) {
895 subchannel_call = CANCELLED_CALL;
896 fail_locked(exec_ctx, calld, GRPC_ERROR_REF(error));
897 grpc_transport_stream_op_finish_with_failure(exec_ctx, op, error);
898 }
899 gpr_atm_rel_store(&calld->subchannel_call,
900 (gpr_atm)(uintptr_t)subchannel_call);
901 retry_waiting_locked(exec_ctx, calld);
902 goto retry;
903 }
904 /* nothing to be done but wait */
905 add_waiting_locked(calld, op);
906 gpr_mu_unlock(&calld->mu);
907 GPR_TIMER_END("cc_start_transport_stream_op", 0);
908}
909
Mark D. Rothe40dd292016-10-05 14:58:37 -0700910// Gets data from the service config. Invoked when the resolver returns
911// its initial result.
912static void read_service_config(grpc_exec_ctx *exec_ctx, void *arg,
913 grpc_error *error) {
914 grpc_call_element *elem = arg;
915 channel_data *chand = elem->channel_data;
916 call_data *calld = elem->call_data;
917 // If this is an error, there's no point in looking at the service config.
Mark D. Roth196387a2016-10-12 14:53:36 -0700918 if (error == GRPC_ERROR_NONE) {
919 // Get the method config table from channel data.
920 gpr_mu_lock(&chand->mu);
Mark D. Roth9d480942016-10-19 14:18:05 -0700921 grpc_mdstr_hash_table *method_params_table = NULL;
922 if (chand->method_params_table != NULL) {
923 method_params_table =
924 grpc_mdstr_hash_table_ref(chand->method_params_table);
Mark D. Rothe40dd292016-10-05 14:58:37 -0700925 }
Mark D. Roth196387a2016-10-12 14:53:36 -0700926 gpr_mu_unlock(&chand->mu);
927 // If the method config table was present, use it.
Mark D. Roth9d480942016-10-19 14:18:05 -0700928 if (method_params_table != NULL) {
929 const method_parameters *method_params =
930 grpc_method_config_table_get(method_params_table, calld->path);
931 if (method_params != NULL) {
932 const bool have_method_timeout =
933 gpr_time_cmp(method_params->timeout, gpr_time_0(GPR_TIMESPAN)) != 0;
934 if (have_method_timeout ||
935 method_params->wait_for_ready != WAIT_FOR_READY_UNSET) {
Mark D. Roth196387a2016-10-12 14:53:36 -0700936 gpr_mu_lock(&calld->mu);
Mark D. Roth9d480942016-10-19 14:18:05 -0700937 if (have_method_timeout) {
938 const gpr_timespec per_method_deadline =
939 gpr_time_add(calld->call_start_time, method_params->timeout);
Mark D. Roth196387a2016-10-12 14:53:36 -0700940 if (gpr_time_cmp(per_method_deadline, calld->deadline) < 0) {
941 calld->deadline = per_method_deadline;
942 // Reset deadline timer.
943 grpc_deadline_state_reset(exec_ctx, elem, calld->deadline);
944 }
945 }
Mark D. Roth9d480942016-10-19 14:18:05 -0700946 if (method_params->wait_for_ready != WAIT_FOR_READY_UNSET) {
Mark D. Roth196387a2016-10-12 14:53:36 -0700947 calld->wait_for_ready_from_service_config =
Mark D. Roth9d480942016-10-19 14:18:05 -0700948 method_params->wait_for_ready;
Mark D. Roth196387a2016-10-12 14:53:36 -0700949 }
950 gpr_mu_unlock(&calld->mu);
951 }
952 }
Mark D. Roth9d480942016-10-19 14:18:05 -0700953 grpc_mdstr_hash_table_unref(method_params_table);
Mark D. Roth196387a2016-10-12 14:53:36 -0700954 }
Mark D. Rothe40dd292016-10-05 14:58:37 -0700955 }
Mark D. Roth31292f22016-10-12 13:14:07 -0700956 GRPC_CALL_STACK_UNREF(exec_ctx, calld->owning_call, "read_service_config");
Mark D. Rothe40dd292016-10-05 14:58:37 -0700957}
958
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800959/* Constructor for call_data */
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700960static grpc_error *cc_init_call_elem(grpc_exec_ctx *exec_ctx,
961 grpc_call_element *elem,
962 grpc_call_element_args *args) {
Mark D. Rothaa850a72016-09-26 13:38:02 -0700963 channel_data *chand = elem->channel_data;
Mark D. Roth4c0fe492016-08-31 13:51:55 -0700964 call_data *calld = elem->call_data;
Mark D. Rothe40dd292016-10-05 14:58:37 -0700965 // Initialize data members.
966 grpc_deadline_state_init(exec_ctx, elem, args->call_stack);
Mark D. Rothaa850a72016-09-26 13:38:02 -0700967 calld->path = GRPC_MDSTR_REF(args->path);
Mark D. Rothff08f332016-10-14 13:01:01 -0700968 calld->call_start_time = args->start_time;
Mark D. Rothe40dd292016-10-05 14:58:37 -0700969 calld->deadline = gpr_convert_clock_type(args->deadline, GPR_CLOCK_MONOTONIC);
970 calld->wait_for_ready_from_service_config = WAIT_FOR_READY_UNSET;
Mark D. Rothf28763c2016-09-14 15:18:40 -0700971 calld->cancel_error = GRPC_ERROR_NONE;
Mark D. Roth4c0fe492016-08-31 13:51:55 -0700972 gpr_atm_rel_store(&calld->subchannel_call, 0);
Mark D. Roth4c0fe492016-08-31 13:51:55 -0700973 gpr_mu_init(&calld->mu);
974 calld->connected_subchannel = NULL;
975 calld->waiting_ops = NULL;
976 calld->waiting_ops_count = 0;
977 calld->waiting_ops_capacity = 0;
978 calld->creation_phase = GRPC_SUBCHANNEL_CALL_HOLDER_NOT_CREATING;
979 calld->owning_call = args->call_stack;
980 calld->pollent = NULL;
Mark D. Rothe40dd292016-10-05 14:58:37 -0700981 // If the resolver has already returned results, then we can access
982 // the service config parameters immediately. Otherwise, we need to
983 // defer that work until the resolver returns an initial result.
984 // TODO(roth): This code is almost but not quite identical to the code
985 // in read_service_config() above. It would be nice to find a way to
986 // combine them, to avoid having to maintain it twice.
987 gpr_mu_lock(&chand->mu);
988 if (chand->lb_policy != NULL) {
989 // We already have a resolver result, so check for service config.
Mark D. Roth9d480942016-10-19 14:18:05 -0700990 if (chand->method_params_table != NULL) {
991 grpc_mdstr_hash_table *method_params_table =
992 grpc_mdstr_hash_table_ref(chand->method_params_table);
Mark D. Rothe40dd292016-10-05 14:58:37 -0700993 gpr_mu_unlock(&chand->mu);
Mark D. Roth9d480942016-10-19 14:18:05 -0700994 method_parameters *method_params =
995 grpc_method_config_table_get(method_params_table, args->path);
996 if (method_params != NULL) {
997 if (gpr_time_cmp(method_params->timeout,
998 gpr_time_0(GPR_CLOCK_MONOTONIC)) != 0) {
Mark D. Rothe40dd292016-10-05 14:58:37 -0700999 gpr_timespec per_method_deadline =
Mark D. Roth9d480942016-10-19 14:18:05 -07001000 gpr_time_add(calld->call_start_time, method_params->timeout);
Mark D. Rothe40dd292016-10-05 14:58:37 -07001001 calld->deadline = gpr_time_min(calld->deadline, per_method_deadline);
1002 }
Mark D. Roth9d480942016-10-19 14:18:05 -07001003 if (method_params->wait_for_ready != WAIT_FOR_READY_UNSET) {
Mark D. Rothe40dd292016-10-05 14:58:37 -07001004 calld->wait_for_ready_from_service_config =
Mark D. Roth9d480942016-10-19 14:18:05 -07001005 method_params->wait_for_ready;
Mark D. Rothe40dd292016-10-05 14:58:37 -07001006 }
1007 }
Mark D. Roth9d480942016-10-19 14:18:05 -07001008 grpc_mdstr_hash_table_unref(method_params_table);
Mark D. Rothe40dd292016-10-05 14:58:37 -07001009 } else {
1010 gpr_mu_unlock(&chand->mu);
1011 }
1012 } else {
1013 // We don't yet have a resolver result, so register a callback to
1014 // get the service config data once the resolver returns.
Mark D. Roth31292f22016-10-12 13:14:07 -07001015 // Take a reference to the call stack to be owned by the callback.
1016 GRPC_CALL_STACK_REF(calld->owning_call, "read_service_config");
Mark D. Rothe40dd292016-10-05 14:58:37 -07001017 grpc_closure_init(&calld->read_service_config, read_service_config, elem);
1018 grpc_closure_list_append(&chand->waiting_for_config_closures,
1019 &calld->read_service_config, GRPC_ERROR_NONE);
1020 gpr_mu_unlock(&chand->mu);
1021 }
1022 // Start the deadline timer with the current deadline value. If we
1023 // do not yet have service config data, then the timer may be reset
1024 // later.
1025 grpc_deadline_state_start(exec_ctx, elem, calld->deadline);
Mark D. Roth0badbe82016-06-23 10:15:12 -07001026 return GRPC_ERROR_NONE;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001027}
1028
1029/* Destructor for call_data */
Mark D. Roth2a5959f2016-09-01 08:20:27 -07001030static void cc_destroy_call_elem(grpc_exec_ctx *exec_ctx,
1031 grpc_call_element *elem,
1032 const grpc_call_final_info *final_info,
1033 void *and_free_memory) {
Mark D. Roth4c0fe492016-08-31 13:51:55 -07001034 call_data *calld = elem->call_data;
Mark D. Rothf28763c2016-09-14 15:18:40 -07001035 grpc_deadline_state_destroy(exec_ctx, elem);
Mark D. Rothaa850a72016-09-26 13:38:02 -07001036 GRPC_MDSTR_UNREF(calld->path);
Mark D. Rothf28763c2016-09-14 15:18:40 -07001037 GRPC_ERROR_UNREF(calld->cancel_error);
Mark D. Roth4c0fe492016-08-31 13:51:55 -07001038 grpc_subchannel_call *call = GET_CALL(calld);
1039 if (call != NULL && call != CANCELLED_CALL) {
1040 GRPC_SUBCHANNEL_CALL_UNREF(exec_ctx, call, "client_channel_destroy_call");
1041 }
1042 GPR_ASSERT(calld->creation_phase == GRPC_SUBCHANNEL_CALL_HOLDER_NOT_CREATING);
1043 gpr_mu_destroy(&calld->mu);
1044 GPR_ASSERT(calld->waiting_ops_count == 0);
Craig Tiller693d3942016-10-27 16:51:25 -07001045 if (calld->connected_subchannel != NULL) {
1046 GRPC_CONNECTED_SUBCHANNEL_UNREF(exec_ctx, calld->connected_subchannel,
1047 "picked");
1048 }
Mark D. Roth4c0fe492016-08-31 13:51:55 -07001049 gpr_free(calld->waiting_ops);
Craig Tiller2c8063c2016-03-22 22:12:15 -07001050 gpr_free(and_free_memory);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001051}
1052
David Garcia Quintasf72eb972016-05-03 18:28:09 -07001053static void cc_set_pollset_or_pollset_set(grpc_exec_ctx *exec_ctx,
1054 grpc_call_element *elem,
David Garcia Quintas2a50dfe2016-05-31 15:09:12 -07001055 grpc_polling_entity *pollent) {
Craig Tiller577c9b22015-11-02 14:11:15 -08001056 call_data *calld = elem->call_data;
David Garcia Quintas2a50dfe2016-05-31 15:09:12 -07001057 calld->pollent = pollent;
Craig Tiller577c9b22015-11-02 14:11:15 -08001058}
1059
Mark D. Roth2a5959f2016-09-01 08:20:27 -07001060/*************************************************************************
1061 * EXPORTED SYMBOLS
1062 */
1063
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001064const grpc_channel_filter grpc_client_channel_filter = {
Craig Tillerf40df232016-03-25 13:38:14 -07001065 cc_start_transport_stream_op,
1066 cc_start_transport_op,
1067 sizeof(call_data),
Mark D. Roth2a5959f2016-09-01 08:20:27 -07001068 cc_init_call_elem,
David Garcia Quintas4afce7e2016-04-18 16:25:17 -07001069 cc_set_pollset_or_pollset_set,
Mark D. Roth2a5959f2016-09-01 08:20:27 -07001070 cc_destroy_call_elem,
Craig Tillerf40df232016-03-25 13:38:14 -07001071 sizeof(channel_data),
Mark D. Roth2a5959f2016-09-01 08:20:27 -07001072 cc_init_channel_elem,
1073 cc_destroy_channel_elem,
Craig Tillerf40df232016-03-25 13:38:14 -07001074 cc_get_peer,
1075 "client-channel",
Craig Tiller87d5b192015-04-16 14:37:57 -07001076};
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001077
Mark D. Roth60534972016-09-20 08:37:12 -07001078void grpc_client_channel_finish_initialization(
Mark D. Roth0e48a9a2016-09-08 14:14:39 -07001079 grpc_exec_ctx *exec_ctx, grpc_channel_stack *channel_stack,
1080 grpc_resolver *resolver,
1081 grpc_client_channel_factory *client_channel_factory) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001082 /* post construction initialization: set the transport setup pointer */
Mark D. Roth7f7d1652016-09-20 10:46:15 -07001083 GPR_ASSERT(client_channel_factory != NULL);
Craig Tillera82950e2015-09-22 12:33:20 -07001084 grpc_channel_element *elem = grpc_channel_stack_last_element(channel_stack);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001085 channel_data *chand = elem->channel_data;
Mark D. Rothff4df062016-08-22 15:02:49 -07001086 gpr_mu_lock(&chand->mu);
Craig Tillera82950e2015-09-22 12:33:20 -07001087 GPR_ASSERT(!chand->resolver);
Craig Tillerf5f17122015-06-25 08:47:26 -07001088 chand->resolver = resolver;
Craig Tillera82950e2015-09-22 12:33:20 -07001089 GRPC_RESOLVER_REF(resolver, "channel");
1090 if (!grpc_closure_list_empty(chand->waiting_for_config_closures) ||
1091 chand->exit_idle_when_lb_policy_arrives) {
Mark D. Roth4c0fe492016-08-31 13:51:55 -07001092 chand->started_resolving = true;
Craig Tiller906e3bc2015-11-24 07:31:31 -08001093 GRPC_CHANNEL_STACK_REF(chand->owning_stack, "resolver");
Mark D. Roth046cf762016-09-26 11:13:51 -07001094 grpc_resolver_next(exec_ctx, resolver, &chand->resolver_result,
Mark D. Rothff4df062016-08-22 15:02:49 -07001095 &chand->on_resolver_result_changed);
Craig Tillera82950e2015-09-22 12:33:20 -07001096 }
Mark D. Roth0e48a9a2016-09-08 14:14:39 -07001097 chand->client_channel_factory = client_channel_factory;
1098 grpc_client_channel_factory_ref(client_channel_factory);
Mark D. Rothff4df062016-08-22 15:02:49 -07001099 gpr_mu_unlock(&chand->mu);
Craig Tiller190d3602015-02-18 09:23:38 -08001100}
Craig Tiller48cb07c2015-07-15 16:16:15 -07001101
Craig Tillera82950e2015-09-22 12:33:20 -07001102grpc_connectivity_state grpc_client_channel_check_connectivity_state(
1103 grpc_exec_ctx *exec_ctx, grpc_channel_element *elem, int try_to_connect) {
Craig Tiller48cb07c2015-07-15 16:16:15 -07001104 channel_data *chand = elem->channel_data;
1105 grpc_connectivity_state out;
Mark D. Rothff4df062016-08-22 15:02:49 -07001106 gpr_mu_lock(&chand->mu);
Craig Tiller804ff712016-05-05 16:25:40 -07001107 out = grpc_connectivity_state_check(&chand->state_tracker, NULL);
Craig Tillera82950e2015-09-22 12:33:20 -07001108 if (out == GRPC_CHANNEL_IDLE && try_to_connect) {
1109 if (chand->lb_policy != NULL) {
1110 grpc_lb_policy_exit_idle(exec_ctx, chand->lb_policy);
1111 } else {
Mark D. Roth4c0fe492016-08-31 13:51:55 -07001112 chand->exit_idle_when_lb_policy_arrives = true;
Craig Tillera82950e2015-09-22 12:33:20 -07001113 if (!chand->started_resolving && chand->resolver != NULL) {
Craig Tiller906e3bc2015-11-24 07:31:31 -08001114 GRPC_CHANNEL_STACK_REF(chand->owning_stack, "resolver");
Mark D. Roth4c0fe492016-08-31 13:51:55 -07001115 chand->started_resolving = true;
Mark D. Roth046cf762016-09-26 11:13:51 -07001116 grpc_resolver_next(exec_ctx, chand->resolver, &chand->resolver_result,
Mark D. Rothff4df062016-08-22 15:02:49 -07001117 &chand->on_resolver_result_changed);
Craig Tillera82950e2015-09-22 12:33:20 -07001118 }
Craig Tiller48cb07c2015-07-15 16:16:15 -07001119 }
Craig Tillera82950e2015-09-22 12:33:20 -07001120 }
Mark D. Rothff4df062016-08-22 15:02:49 -07001121 gpr_mu_unlock(&chand->mu);
Craig Tiller48cb07c2015-07-15 16:16:15 -07001122 return out;
1123}
1124
Craig Tiller86c99582015-11-25 15:22:26 -08001125typedef struct {
1126 channel_data *chand;
1127 grpc_pollset *pollset;
1128 grpc_closure *on_complete;
1129 grpc_closure my_closure;
1130} external_connectivity_watcher;
1131
Craig Tiller1d881fb2015-12-01 07:39:04 -08001132static void on_external_watch_complete(grpc_exec_ctx *exec_ctx, void *arg,
Craig Tiller804ff712016-05-05 16:25:40 -07001133 grpc_error *error) {
Craig Tiller86c99582015-11-25 15:22:26 -08001134 external_connectivity_watcher *w = arg;
1135 grpc_closure *follow_up = w->on_complete;
Craig Tiller69b093b2016-02-25 19:04:07 -08001136 grpc_pollset_set_del_pollset(exec_ctx, w->chand->interested_parties,
Craig Tiller1d881fb2015-12-01 07:39:04 -08001137 w->pollset);
1138 GRPC_CHANNEL_STACK_UNREF(exec_ctx, w->chand->owning_stack,
1139 "external_connectivity_watcher");
Craig Tiller86c99582015-11-25 15:22:26 -08001140 gpr_free(w);
Craig Tiller804ff712016-05-05 16:25:40 -07001141 follow_up->cb(exec_ctx, follow_up->cb_arg, error);
Craig Tiller86c99582015-11-25 15:22:26 -08001142}
1143
Craig Tillera82950e2015-09-22 12:33:20 -07001144void grpc_client_channel_watch_connectivity_state(
Craig Tiller906e3bc2015-11-24 07:31:31 -08001145 grpc_exec_ctx *exec_ctx, grpc_channel_element *elem, grpc_pollset *pollset,
Craig Tillera82950e2015-09-22 12:33:20 -07001146 grpc_connectivity_state *state, grpc_closure *on_complete) {
Craig Tiller48cb07c2015-07-15 16:16:15 -07001147 channel_data *chand = elem->channel_data;
Craig Tiller86c99582015-11-25 15:22:26 -08001148 external_connectivity_watcher *w = gpr_malloc(sizeof(*w));
1149 w->chand = chand;
1150 w->pollset = pollset;
1151 w->on_complete = on_complete;
Craig Tiller69b093b2016-02-25 19:04:07 -08001152 grpc_pollset_set_add_pollset(exec_ctx, chand->interested_parties, pollset);
Craig Tiller86c99582015-11-25 15:22:26 -08001153 grpc_closure_init(&w->my_closure, on_external_watch_complete, w);
Craig Tiller1d881fb2015-12-01 07:39:04 -08001154 GRPC_CHANNEL_STACK_REF(w->chand->owning_stack,
1155 "external_connectivity_watcher");
Mark D. Rothff4df062016-08-22 15:02:49 -07001156 gpr_mu_lock(&chand->mu);
Craig Tillera82950e2015-09-22 12:33:20 -07001157 grpc_connectivity_state_notify_on_state_change(
Craig Tiller86c99582015-11-25 15:22:26 -08001158 exec_ctx, &chand->state_tracker, state, &w->my_closure);
Mark D. Rothff4df062016-08-22 15:02:49 -07001159 gpr_mu_unlock(&chand->mu);
Craig Tiller48cb07c2015-07-15 16:16:15 -07001160}