blob: 139912e4bac122cd054832ddd698f540078190cc [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Craig Tiller6169d5f2016-03-31 07:46:18 -07003 * Copyright 2015, Google Inc.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08004 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
Mark D. Roth2137cd82016-09-14 09:04:00 -070034#include "src/core/ext/client_channel/client_channel.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080035
Mark D. Roth4c0fe492016-08-31 13:51:55 -070036#include <stdbool.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080037#include <stdio.h>
Craig Tillereb3b12e2015-06-26 14:42:49 -070038#include <string.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080039
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080040#include <grpc/support/alloc.h>
41#include <grpc/support/log.h>
Mark D. Rothb2d24882016-10-27 15:44:07 -070042#include <grpc/support/string_util.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080043#include <grpc/support/sync.h>
44#include <grpc/support/useful.h>
45
Mark D. Roth15195742016-10-07 09:02:28 -070046#include "src/core/ext/client_channel/lb_policy_registry.h"
Mark D. Roth2137cd82016-09-14 09:04:00 -070047#include "src/core/ext/client_channel/subchannel.h"
Craig Tiller9533d042016-03-25 17:11:06 -070048#include "src/core/lib/channel/channel_args.h"
49#include "src/core/lib/channel/connected_channel.h"
Mark D. Roth72f6da82016-09-02 13:42:38 -070050#include "src/core/lib/channel/deadline_filter.h"
Craig Tiller9533d042016-03-25 17:11:06 -070051#include "src/core/lib/iomgr/iomgr.h"
Mark D. Roth4c0fe492016-08-31 13:51:55 -070052#include "src/core/lib/iomgr/polling_entity.h"
Craig Tiller9533d042016-03-25 17:11:06 -070053#include "src/core/lib/profiling/timers.h"
54#include "src/core/lib/support/string.h"
55#include "src/core/lib/surface/channel.h"
56#include "src/core/lib/transport/connectivity_state.h"
Mark D. Roth9fe284e2016-09-12 11:22:27 -070057#include "src/core/lib/transport/metadata.h"
58#include "src/core/lib/transport/metadata_batch.h"
Mark D. Rothea846a02016-11-03 11:32:54 -070059#include "src/core/lib/transport/service_config.h"
Mark D. Roth9fe284e2016-09-12 11:22:27 -070060#include "src/core/lib/transport/static_metadata.h"
Craig Tiller8910ac62015-10-08 16:49:15 -070061
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080062/* Client channel implementation */
63
Mark D. Roth26b7be42016-10-24 10:08:07 -070064/*************************************************************************
65 * METHOD-CONFIG TABLE
66 */
67
Mark D. Roth9d480942016-10-19 14:18:05 -070068typedef enum {
69 WAIT_FOR_READY_UNSET,
70 WAIT_FOR_READY_FALSE,
71 WAIT_FOR_READY_TRUE
72} wait_for_ready_value;
73
74typedef struct method_parameters {
75 gpr_timespec timeout;
76 wait_for_ready_value wait_for_ready;
77} method_parameters;
78
79static void *method_parameters_copy(void *value) {
80 void *new_value = gpr_malloc(sizeof(method_parameters));
81 memcpy(new_value, value, sizeof(method_parameters));
82 return new_value;
83}
84
Mark D. Roth9d480942016-10-19 14:18:05 -070085static const grpc_mdstr_hash_table_vtable method_parameters_vtable = {
Mark D. Roth6ad80572016-11-03 08:33:17 -070086 gpr_free, method_parameters_copy};
Mark D. Roth9d480942016-10-19 14:18:05 -070087
Mark D. Rothe30baeb2016-11-03 08:16:19 -070088static void *method_parameters_create_from_json(const grpc_json *json) {
Mark D. Rothc968e602016-11-02 14:07:36 -070089 wait_for_ready_value wait_for_ready = WAIT_FOR_READY_UNSET;
Mark D. Roth47f10842016-11-03 08:45:27 -070090 gpr_timespec timeout = {0, 0, GPR_TIMESPAN};
91 for (grpc_json *field = json->child; field != NULL; field = field->next) {
Mark D. Rothc968e602016-11-02 14:07:36 -070092 if (field->key == NULL) continue;
93 if (strcmp(field->key, "wait_for_ready") == 0) {
94 if (wait_for_ready != WAIT_FOR_READY_UNSET) return NULL; // Duplicate.
95 if (field->type != GRPC_JSON_TRUE && field->type != GRPC_JSON_FALSE) {
96 return NULL;
97 }
Mark D. Roth47f10842016-11-03 08:45:27 -070098 wait_for_ready = field->type == GRPC_JSON_TRUE ? WAIT_FOR_READY_TRUE
99 : WAIT_FOR_READY_FALSE;
Mark D. Rothc968e602016-11-02 14:07:36 -0700100 } else if (strcmp(field->key, "timeout") == 0) {
101 if (timeout.tv_sec > 0 || timeout.tv_nsec > 0) return NULL; // Duplicate.
102 if (field->type != GRPC_JSON_OBJECT) return NULL;
103 if (field->child == NULL) return NULL;
Mark D. Roth47f10842016-11-03 08:45:27 -0700104 for (grpc_json *subfield = field->child; subfield != NULL;
Mark D. Rothc968e602016-11-02 14:07:36 -0700105 subfield = subfield->next) {
106 if (subfield->key == NULL) return NULL;
107 if (strcmp(subfield->key, "seconds") == 0) {
108 if (timeout.tv_sec > 0) return NULL; // Duplicate.
109 if (subfield->type != GRPC_JSON_NUMBER) return NULL;
110 timeout.tv_sec = gpr_parse_nonnegative_number(subfield->value);
111 if (timeout.tv_sec == -1) return NULL;
112 } else if (strcmp(subfield->key, "nanos") == 0) {
113 if (timeout.tv_nsec > 0) return NULL; // Duplicate.
114 if (subfield->type != GRPC_JSON_NUMBER) return NULL;
115 timeout.tv_nsec = gpr_parse_nonnegative_number(subfield->value);
116 if (timeout.tv_nsec == -1) return NULL;
117 } else {
118 // Unknown key.
119 return NULL;
120 }
121 }
122 }
123 }
Mark D. Roth9d480942016-10-19 14:18:05 -0700124 method_parameters *value = gpr_malloc(sizeof(method_parameters));
Mark D. Rothc968e602016-11-02 14:07:36 -0700125 value->timeout = timeout;
126 value->wait_for_ready = wait_for_ready;
Mark D. Roth9d480942016-10-19 14:18:05 -0700127 return value;
128}
129
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700130/*************************************************************************
131 * CHANNEL-WIDE FUNCTIONS
132 */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800133
Craig Tiller800dacb2015-10-06 09:10:26 -0700134typedef struct client_channel_channel_data {
Craig Tillerf5f17122015-06-25 08:47:26 -0700135 /** resolver for this channel */
136 grpc_resolver *resolver;
Craig Tiller20a3c352015-08-05 08:39:50 -0700137 /** have we started resolving this channel */
Mark D. Roth4c0fe492016-08-31 13:51:55 -0700138 bool started_resolving;
Mark D. Roth0e48a9a2016-09-08 14:14:39 -0700139 /** client channel factory */
140 grpc_client_channel_factory *client_channel_factory;
Craig Tillerf5f17122015-06-25 08:47:26 -0700141
Mark D. Roth046cf762016-09-26 11:13:51 -0700142 /** mutex protecting all variables below in this data structure */
Mark D. Rothff4df062016-08-22 15:02:49 -0700143 gpr_mu mu;
Mark D. Roth046cf762016-09-26 11:13:51 -0700144 /** currently active load balancer */
Mark D. Roth78afd772016-11-04 12:49:49 -0700145 char *lb_policy_name;
Craig Tillerf5f17122015-06-25 08:47:26 -0700146 grpc_lb_policy *lb_policy;
Mark D. Rothc625c7a2016-11-09 14:12:37 -0800147 /** service config in JSON form */
148 char *service_config_json;
Mark D. Roth9d480942016-10-19 14:18:05 -0700149 /** maps method names to method_parameters structs */
150 grpc_mdstr_hash_table *method_params_table;
Mark D. Roth046cf762016-09-26 11:13:51 -0700151 /** incoming resolver result - set by resolver.next() */
Mark D. Rothaf842452016-10-21 15:05:15 -0700152 grpc_channel_args *resolver_result;
Craig Tiller3f475422015-06-25 10:43:05 -0700153 /** a list of closures that are all waiting for config to come in */
Craig Tillerd9ccbbf2015-09-22 09:30:00 -0700154 grpc_closure_list waiting_for_config_closures;
Craig Tiller3f475422015-06-25 10:43:05 -0700155 /** resolver callback */
Mark D. Rothff4df062016-08-22 15:02:49 -0700156 grpc_closure on_resolver_result_changed;
Craig Tiller3f475422015-06-25 10:43:05 -0700157 /** connectivity state being tracked */
Craig Tillerca3e9d32015-06-27 18:37:27 -0700158 grpc_connectivity_state_tracker state_tracker;
Craig Tiller48cb07c2015-07-15 16:16:15 -0700159 /** when an lb_policy arrives, should we try to exit idle */
Mark D. Roth4c0fe492016-08-31 13:51:55 -0700160 bool exit_idle_when_lb_policy_arrives;
Craig Tiller906e3bc2015-11-24 07:31:31 -0800161 /** owning stack */
162 grpc_channel_stack *owning_stack;
Craig Tiller69b093b2016-02-25 19:04:07 -0800163 /** interested parties (owned) */
164 grpc_pollset_set *interested_parties;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800165} channel_data;
166
Craig Tillerd6c98df2015-08-18 09:33:44 -0700167/** We create one watcher for each new lb_policy that is returned from a
Mark D. Roth4c0fe492016-08-31 13:51:55 -0700168 resolver, to watch for state changes from the lb_policy. When a state
169 change is seen, we update the channel, and create a new watcher. */
Craig Tillera82950e2015-09-22 12:33:20 -0700170typedef struct {
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700171 channel_data *chand;
Craig Tiller33825112015-09-18 07:44:19 -0700172 grpc_closure on_changed;
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700173 grpc_connectivity_state state;
174 grpc_lb_policy *lb_policy;
175} lb_policy_connectivity_watcher;
176
Craig Tillera82950e2015-09-22 12:33:20 -0700177static void watch_lb_policy(grpc_exec_ctx *exec_ctx, channel_data *chand,
178 grpc_lb_policy *lb_policy,
179 grpc_connectivity_state current_state);
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700180
Craig Tiller8c0d96f2016-03-11 14:27:52 -0800181static void set_channel_connectivity_state_locked(grpc_exec_ctx *exec_ctx,
182 channel_data *chand,
183 grpc_connectivity_state state,
Craig Tiller804ff712016-05-05 16:25:40 -0700184 grpc_error *error,
Craig Tiller8c0d96f2016-03-11 14:27:52 -0800185 const char *reason) {
186 if ((state == GRPC_CHANNEL_TRANSIENT_FAILURE ||
Craig Tiller48ed92e2016-06-02 11:07:12 -0700187 state == GRPC_CHANNEL_SHUTDOWN) &&
Craig Tiller8c0d96f2016-03-11 14:27:52 -0800188 chand->lb_policy != NULL) {
Mark D. Roth59c9f902016-09-28 13:33:21 -0700189 /* cancel picks with wait_for_ready=false */
Craig Tiller8c0d96f2016-03-11 14:27:52 -0800190 grpc_lb_policy_cancel_picks(
191 exec_ctx, chand->lb_policy,
Mark D. Roth59c9f902016-09-28 13:33:21 -0700192 /* mask= */ GRPC_INITIAL_METADATA_WAIT_FOR_READY,
Mark D. Roth58f52b72016-09-09 13:55:18 -0700193 /* check= */ 0, GRPC_ERROR_REF(error));
Craig Tiller8c0d96f2016-03-11 14:27:52 -0800194 }
Craig Tiller9ccf5f12016-05-07 21:41:01 -0700195 grpc_connectivity_state_set(exec_ctx, &chand->state_tracker, state, error,
196 reason);
Craig Tiller8c0d96f2016-03-11 14:27:52 -0800197}
198
Craig Tiller804ff712016-05-05 16:25:40 -0700199static void on_lb_policy_state_changed_locked(grpc_exec_ctx *exec_ctx,
200 lb_policy_connectivity_watcher *w,
201 grpc_error *error) {
Craig Tillercb2609f2015-11-24 17:19:19 -0800202 grpc_connectivity_state publish_state = w->state;
Craig Tiller5795da72015-09-17 15:27:13 -0700203 /* check if the notification is for a stale policy */
Craig Tillera82950e2015-09-22 12:33:20 -0700204 if (w->lb_policy != w->chand->lb_policy) return;
Craig Tiller5795da72015-09-17 15:27:13 -0700205
Craig Tiller48ed92e2016-06-02 11:07:12 -0700206 if (publish_state == GRPC_CHANNEL_SHUTDOWN && w->chand->resolver != NULL) {
Craig Tillercb2609f2015-11-24 17:19:19 -0800207 publish_state = GRPC_CHANNEL_TRANSIENT_FAILURE;
208 grpc_resolver_channel_saw_error(exec_ctx, w->chand->resolver);
Craig Tillerf62c4d52015-12-04 07:43:07 -0800209 GRPC_LB_POLICY_UNREF(exec_ctx, w->chand->lb_policy, "channel");
210 w->chand->lb_policy = NULL;
Craig Tillercb2609f2015-11-24 17:19:19 -0800211 }
Craig Tiller8c0d96f2016-03-11 14:27:52 -0800212 set_channel_connectivity_state_locked(exec_ctx, w->chand, publish_state,
Craig Tillerfc353d62016-05-10 12:58:03 -0700213 GRPC_ERROR_REF(error), "lb_changed");
Craig Tiller48ed92e2016-06-02 11:07:12 -0700214 if (w->state != GRPC_CHANNEL_SHUTDOWN) {
Craig Tillera82950e2015-09-22 12:33:20 -0700215 watch_lb_policy(exec_ctx, w->chand, w->lb_policy, w->state);
216 }
Craig Tiller5795da72015-09-17 15:27:13 -0700217}
218
Craig Tillera82950e2015-09-22 12:33:20 -0700219static void on_lb_policy_state_changed(grpc_exec_ctx *exec_ctx, void *arg,
Craig Tiller804ff712016-05-05 16:25:40 -0700220 grpc_error *error) {
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700221 lb_policy_connectivity_watcher *w = arg;
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700222
Mark D. Rothff4df062016-08-22 15:02:49 -0700223 gpr_mu_lock(&w->chand->mu);
Craig Tiller804ff712016-05-05 16:25:40 -0700224 on_lb_policy_state_changed_locked(exec_ctx, w, error);
Mark D. Rothff4df062016-08-22 15:02:49 -0700225 gpr_mu_unlock(&w->chand->mu);
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700226
Craig Tiller906e3bc2015-11-24 07:31:31 -0800227 GRPC_CHANNEL_STACK_UNREF(exec_ctx, w->chand->owning_stack, "watch_lb_policy");
Craig Tillera82950e2015-09-22 12:33:20 -0700228 gpr_free(w);
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700229}
230
Craig Tillera82950e2015-09-22 12:33:20 -0700231static void watch_lb_policy(grpc_exec_ctx *exec_ctx, channel_data *chand,
232 grpc_lb_policy *lb_policy,
233 grpc_connectivity_state current_state) {
234 lb_policy_connectivity_watcher *w = gpr_malloc(sizeof(*w));
Craig Tiller906e3bc2015-11-24 07:31:31 -0800235 GRPC_CHANNEL_STACK_REF(chand->owning_stack, "watch_lb_policy");
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700236
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700237 w->chand = chand;
Craig Tillera82950e2015-09-22 12:33:20 -0700238 grpc_closure_init(&w->on_changed, on_lb_policy_state_changed, w);
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700239 w->state = current_state;
240 w->lb_policy = lb_policy;
Craig Tillera82950e2015-09-22 12:33:20 -0700241 grpc_lb_policy_notify_on_state_change(exec_ctx, lb_policy, &w->state,
242 &w->on_changed);
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700243}
244
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700245static void on_resolver_result_changed(grpc_exec_ctx *exec_ctx, void *arg,
246 grpc_error *error) {
Craig Tiller3f475422015-06-25 10:43:05 -0700247 channel_data *chand = arg;
Mark D. Rothb2d24882016-10-27 15:44:07 -0700248 char *lb_policy_name = NULL;
Craig Tiller3f475422015-06-25 10:43:05 -0700249 grpc_lb_policy *lb_policy = NULL;
250 grpc_lb_policy *old_lb_policy;
Mark D. Roth9d480942016-10-19 14:18:05 -0700251 grpc_mdstr_hash_table *method_params_table = NULL;
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700252 grpc_connectivity_state state = GRPC_CHANNEL_TRANSIENT_FAILURE;
Mark D. Roth4c0fe492016-08-31 13:51:55 -0700253 bool exit_idle = false;
Craig Tiller804ff712016-05-05 16:25:40 -0700254 grpc_error *state_error = GRPC_ERROR_CREATE("No load balancing policy");
Mark D. Rothc625c7a2016-11-09 14:12:37 -0800255 char *service_config_json = NULL;
Craig Tiller3f475422015-06-25 10:43:05 -0700256
Mark D. Roth046cf762016-09-26 11:13:51 -0700257 if (chand->resolver_result != NULL) {
Mark D. Roth5bd7be02016-10-21 14:19:50 -0700258 // Find LB policy name.
Mark D. Rothaf842452016-10-21 15:05:15 -0700259 const grpc_arg *channel_arg =
Mark D. Roth41124992016-11-03 11:22:20 -0700260 grpc_channel_args_find(chand->resolver_result, GRPC_ARG_LB_POLICY_NAME);
Mark D. Rothaf842452016-10-21 15:05:15 -0700261 if (channel_arg != NULL) {
262 GPR_ASSERT(channel_arg->type == GRPC_ARG_STRING);
263 lb_policy_name = channel_arg->value.string;
Mark D. Roth5bd7be02016-10-21 14:19:50 -0700264 }
Mark D. Roth88405f72016-10-03 08:24:52 -0700265 // Special case: If all of the addresses are balancer addresses,
266 // assume that we should use the grpclb policy, regardless of what the
267 // resolver actually specified.
Mark D. Rothaf842452016-10-21 15:05:15 -0700268 channel_arg =
Mark D. Roth41124992016-11-03 11:22:20 -0700269 grpc_channel_args_find(chand->resolver_result, GRPC_ARG_LB_ADDRESSES);
Mark D. Rothaf842452016-10-21 15:05:15 -0700270 if (channel_arg != NULL) {
271 GPR_ASSERT(channel_arg->type == GRPC_ARG_POINTER);
Mark D. Roth557c9902016-10-24 11:12:05 -0700272 grpc_lb_addresses *addresses = channel_arg->value.pointer.p;
Mark D. Rothaf842452016-10-21 15:05:15 -0700273 bool found_backend_address = false;
274 for (size_t i = 0; i < addresses->num_addresses; ++i) {
275 if (!addresses->addresses[i].is_balancer) {
276 found_backend_address = true;
277 break;
278 }
Mark D. Roth88405f72016-10-03 08:24:52 -0700279 }
Mark D. Rothaf842452016-10-21 15:05:15 -0700280 if (!found_backend_address) {
281 if (lb_policy_name != NULL && strcmp(lb_policy_name, "grpclb") != 0) {
282 gpr_log(GPR_INFO,
283 "resolver requested LB policy %s but provided only balancer "
284 "addresses, no backend addresses -- forcing use of grpclb LB "
285 "policy",
Mark D. Roth5f40e5d2016-10-24 13:09:05 -0700286 lb_policy_name);
Mark D. Rothaf842452016-10-21 15:05:15 -0700287 }
288 lb_policy_name = "grpclb";
Mark D. Roth88405f72016-10-03 08:24:52 -0700289 }
Mark D. Roth88405f72016-10-03 08:24:52 -0700290 }
291 // Use pick_first if nothing was specified and we didn't select grpclb
292 // above.
293 if (lb_policy_name == NULL) lb_policy_name = "pick_first";
Mark D. Roth41124992016-11-03 11:22:20 -0700294 // Instantiate LB policy.
295 grpc_lb_policy_args lb_policy_args;
296 lb_policy_args.args = chand->resolver_result;
297 lb_policy_args.client_channel_factory = chand->client_channel_factory;
Mark D. Roth88405f72016-10-03 08:24:52 -0700298 lb_policy =
299 grpc_lb_policy_create(exec_ctx, lb_policy_name, &lb_policy_args);
Craig Tillera82950e2015-09-22 12:33:20 -0700300 if (lb_policy != NULL) {
Craig Tillera82950e2015-09-22 12:33:20 -0700301 GRPC_LB_POLICY_REF(lb_policy, "config_change");
Craig Tillerf707d622016-05-06 14:26:12 -0700302 GRPC_ERROR_UNREF(state_error);
Craig Tiller804ff712016-05-05 16:25:40 -0700303 state =
304 grpc_lb_policy_check_connectivity(exec_ctx, lb_policy, &state_error);
Craig Tiller45724b32015-09-22 10:42:19 -0700305 }
Mark D. Roth41124992016-11-03 11:22:20 -0700306 // Find service config.
Mark D. Rothaf842452016-10-21 15:05:15 -0700307 channel_arg =
Mark D. Roth41124992016-11-03 11:22:20 -0700308 grpc_channel_args_find(chand->resolver_result, GRPC_ARG_SERVICE_CONFIG);
Mark D. Roth046cf762016-09-26 11:13:51 -0700309 if (channel_arg != NULL) {
Mark D. Roth9ec28af2016-11-03 12:32:39 -0700310 GPR_ASSERT(channel_arg->type == GRPC_ARG_STRING);
Mark D. Rothc625c7a2016-11-09 14:12:37 -0800311 service_config_json = gpr_strdup(channel_arg->value.string);
Mark D. Roth70a1abd2016-11-04 09:26:37 -0700312 grpc_service_config *service_config =
Mark D. Rothc625c7a2016-11-09 14:12:37 -0800313 grpc_service_config_create(service_config_json);
Mark D. Rothbdc58b22016-11-04 09:25:57 -0700314 if (service_config != NULL) {
315 method_params_table = grpc_service_config_create_method_config_table(
316 service_config, method_parameters_create_from_json,
317 &method_parameters_vtable);
318 grpc_service_config_destroy(service_config);
319 }
Mark D. Roth9fe284e2016-09-12 11:22:27 -0700320 }
Mark D. Rothf79ce7d2016-11-04 08:43:36 -0700321 // Before we clean up, save a copy of lb_policy_name, since it might
322 // be pointing to data inside chand->resolver_result.
323 // The copy will be saved in chand->lb_policy_name below.
324 lb_policy_name = gpr_strdup(lb_policy_name);
Mark D. Rothaf842452016-10-21 15:05:15 -0700325 grpc_channel_args_destroy(chand->resolver_result);
Mark D. Roth046cf762016-09-26 11:13:51 -0700326 chand->resolver_result = NULL;
Craig Tillera82950e2015-09-22 12:33:20 -0700327 }
328
Craig Tiller86c99582015-11-25 15:22:26 -0800329 if (lb_policy != NULL) {
Craig Tiller69b093b2016-02-25 19:04:07 -0800330 grpc_pollset_set_add_pollset_set(exec_ctx, lb_policy->interested_parties,
331 chand->interested_parties);
Craig Tiller86c99582015-11-25 15:22:26 -0800332 }
333
Mark D. Rothff4df062016-08-22 15:02:49 -0700334 gpr_mu_lock(&chand->mu);
Mark D. Rothb2d24882016-10-27 15:44:07 -0700335 if (lb_policy_name != NULL) {
336 gpr_free(chand->lb_policy_name);
337 chand->lb_policy_name = lb_policy_name;
338 }
Craig Tiller3f475422015-06-25 10:43:05 -0700339 old_lb_policy = chand->lb_policy;
340 chand->lb_policy = lb_policy;
Mark D. Rothc625c7a2016-11-09 14:12:37 -0800341 if (service_config_json != NULL) {
342 gpr_free(chand->service_config_json);
343 chand->service_config_json = service_config_json;
344 }
Mark D. Roth9d480942016-10-19 14:18:05 -0700345 if (chand->method_params_table != NULL) {
346 grpc_mdstr_hash_table_unref(chand->method_params_table);
Mark D. Roth046cf762016-09-26 11:13:51 -0700347 }
Mark D. Roth9d480942016-10-19 14:18:05 -0700348 chand->method_params_table = method_params_table;
Craig Tiller0ede5452016-04-23 12:21:45 -0700349 if (lb_policy != NULL) {
350 grpc_exec_ctx_enqueue_list(exec_ctx, &chand->waiting_for_config_closures,
351 NULL);
352 } else if (chand->resolver == NULL /* disconnected */) {
Craig Tiller804ff712016-05-05 16:25:40 -0700353 grpc_closure_list_fail_all(
354 &chand->waiting_for_config_closures,
355 GRPC_ERROR_CREATE_REFERENCING("Channel disconnected", &error, 1));
Craig Tiller6c396862016-01-28 13:53:40 -0800356 grpc_exec_ctx_enqueue_list(exec_ctx, &chand->waiting_for_config_closures,
357 NULL);
Craig Tillera82950e2015-09-22 12:33:20 -0700358 }
359 if (lb_policy != NULL && chand->exit_idle_when_lb_policy_arrives) {
360 GRPC_LB_POLICY_REF(lb_policy, "exit_idle");
Mark D. Roth4c0fe492016-08-31 13:51:55 -0700361 exit_idle = true;
362 chand->exit_idle_when_lb_policy_arrives = false;
Craig Tillera82950e2015-09-22 12:33:20 -0700363 }
Craig Tiller98465032015-06-29 14:36:42 -0700364
Craig Tiller804ff712016-05-05 16:25:40 -0700365 if (error == GRPC_ERROR_NONE && chand->resolver) {
Craig Tiller9ccf5f12016-05-07 21:41:01 -0700366 set_channel_connectivity_state_locked(
367 exec_ctx, chand, state, GRPC_ERROR_REF(state_error), "new_lb+resolver");
Craig Tillera82950e2015-09-22 12:33:20 -0700368 if (lb_policy != NULL) {
369 watch_lb_policy(exec_ctx, chand, lb_policy, state);
Craig Tiller45724b32015-09-22 10:42:19 -0700370 }
Craig Tiller906e3bc2015-11-24 07:31:31 -0800371 GRPC_CHANNEL_STACK_REF(chand->owning_stack, "resolver");
Mark D. Roth046cf762016-09-26 11:13:51 -0700372 grpc_resolver_next(exec_ctx, chand->resolver, &chand->resolver_result,
Mark D. Rothff4df062016-08-22 15:02:49 -0700373 &chand->on_resolver_result_changed);
374 gpr_mu_unlock(&chand->mu);
Craig Tillera82950e2015-09-22 12:33:20 -0700375 } else {
Craig Tiller76a5c0e2016-03-09 09:05:30 -0800376 if (chand->resolver != NULL) {
377 grpc_resolver_shutdown(exec_ctx, chand->resolver);
378 GRPC_RESOLVER_UNREF(exec_ctx, chand->resolver, "channel");
379 chand->resolver = NULL;
380 }
Craig Tiller804ff712016-05-05 16:25:40 -0700381 grpc_error *refs[] = {error, state_error};
Craig Tiller8c0d96f2016-03-11 14:27:52 -0800382 set_channel_connectivity_state_locked(
Craig Tillerd925c932016-06-06 08:38:50 -0700383 exec_ctx, chand, GRPC_CHANNEL_SHUTDOWN,
Craig Tiller804ff712016-05-05 16:25:40 -0700384 GRPC_ERROR_CREATE_REFERENCING("Got config after disconnection", refs,
385 GPR_ARRAY_SIZE(refs)),
386 "resolver_gone");
Mark D. Rothff4df062016-08-22 15:02:49 -0700387 gpr_mu_unlock(&chand->mu);
Craig Tillera82950e2015-09-22 12:33:20 -0700388 }
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700389
Craig Tillera82950e2015-09-22 12:33:20 -0700390 if (exit_idle) {
391 grpc_lb_policy_exit_idle(exec_ctx, lb_policy);
392 GRPC_LB_POLICY_UNREF(exec_ctx, lb_policy, "exit_idle");
393 }
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700394
Craig Tillera82950e2015-09-22 12:33:20 -0700395 if (old_lb_policy != NULL) {
Craig Tiller69b093b2016-02-25 19:04:07 -0800396 grpc_pollset_set_del_pollset_set(
397 exec_ctx, old_lb_policy->interested_parties, chand->interested_parties);
Craig Tillera82950e2015-09-22 12:33:20 -0700398 GRPC_LB_POLICY_UNREF(exec_ctx, old_lb_policy, "channel");
399 }
Craig Tiller000cd8f2015-09-18 07:20:29 -0700400
Craig Tillera82950e2015-09-22 12:33:20 -0700401 if (lb_policy != NULL) {
402 GRPC_LB_POLICY_UNREF(exec_ctx, lb_policy, "config_change");
403 }
Craig Tiller45724b32015-09-22 10:42:19 -0700404
Craig Tiller906e3bc2015-11-24 07:31:31 -0800405 GRPC_CHANNEL_STACK_UNREF(exec_ctx, chand->owning_stack, "resolver");
Craig Tiller9ccf5f12016-05-07 21:41:01 -0700406 GRPC_ERROR_UNREF(state_error);
Craig Tiller3f475422015-06-25 10:43:05 -0700407}
408
Craig Tillera82950e2015-09-22 12:33:20 -0700409static void cc_start_transport_op(grpc_exec_ctx *exec_ctx,
410 grpc_channel_element *elem,
411 grpc_transport_op *op) {
Craig Tillerca3e9d32015-06-27 18:37:27 -0700412 channel_data *chand = elem->channel_data;
Craig Tiller000cd8f2015-09-18 07:20:29 -0700413
Craig Tiller332f1b32016-05-24 13:21:21 -0700414 grpc_exec_ctx_sched(exec_ctx, op->on_consumed, GRPC_ERROR_NONE, NULL);
Craig Tillerca3e9d32015-06-27 18:37:27 -0700415
Craig Tillerd7f12e32016-03-03 10:08:31 -0800416 GPR_ASSERT(op->set_accept_stream == false);
Craig Tiller28bf8912015-12-07 16:07:04 -0800417 if (op->bind_pollset != NULL) {
Craig Tiller69b093b2016-02-25 19:04:07 -0800418 grpc_pollset_set_add_pollset(exec_ctx, chand->interested_parties,
Craig Tillere2c62372015-12-07 16:11:03 -0800419 op->bind_pollset);
Craig Tiller28bf8912015-12-07 16:07:04 -0800420 }
Craig Tillerca3e9d32015-06-27 18:37:27 -0700421
Mark D. Rothff4df062016-08-22 15:02:49 -0700422 gpr_mu_lock(&chand->mu);
Craig Tillera82950e2015-09-22 12:33:20 -0700423 if (op->on_connectivity_state_change != NULL) {
424 grpc_connectivity_state_notify_on_state_change(
425 exec_ctx, &chand->state_tracker, op->connectivity_state,
426 op->on_connectivity_state_change);
427 op->on_connectivity_state_change = NULL;
428 op->connectivity_state = NULL;
429 }
430
Craig Tiller26dab312015-12-07 14:43:47 -0800431 if (op->send_ping != NULL) {
Craig Tiller87b71e22015-12-07 15:14:14 -0800432 if (chand->lb_policy == NULL) {
Craig Tiller332f1b32016-05-24 13:21:21 -0700433 grpc_exec_ctx_sched(exec_ctx, op->send_ping,
434 GRPC_ERROR_CREATE("Ping with no load balancing"),
435 NULL);
Craig Tiller26dab312015-12-07 14:43:47 -0800436 } else {
Craig Tiller28bf8912015-12-07 16:07:04 -0800437 grpc_lb_policy_ping_one(exec_ctx, chand->lb_policy, op->send_ping);
Craig Tiller26dab312015-12-07 14:43:47 -0800438 op->bind_pollset = NULL;
439 }
440 op->send_ping = NULL;
441 }
442
Craig Tiller1c51edc2016-05-07 16:18:43 -0700443 if (op->disconnect_with_error != GRPC_ERROR_NONE) {
444 if (chand->resolver != NULL) {
445 set_channel_connectivity_state_locked(
Craig Tillerd925c932016-06-06 08:38:50 -0700446 exec_ctx, chand, GRPC_CHANNEL_SHUTDOWN,
Craig Tiller1c51edc2016-05-07 16:18:43 -0700447 GRPC_ERROR_REF(op->disconnect_with_error), "disconnect");
448 grpc_resolver_shutdown(exec_ctx, chand->resolver);
449 GRPC_RESOLVER_UNREF(exec_ctx, chand->resolver, "channel");
450 chand->resolver = NULL;
451 if (!chand->started_resolving) {
452 grpc_closure_list_fail_all(&chand->waiting_for_config_closures,
453 GRPC_ERROR_REF(op->disconnect_with_error));
Craig Tiller9ccf5f12016-05-07 21:41:01 -0700454 grpc_exec_ctx_enqueue_list(exec_ctx,
455 &chand->waiting_for_config_closures, NULL);
Craig Tiller1c51edc2016-05-07 16:18:43 -0700456 }
457 if (chand->lb_policy != NULL) {
458 grpc_pollset_set_del_pollset_set(exec_ctx,
459 chand->lb_policy->interested_parties,
460 chand->interested_parties);
461 GRPC_LB_POLICY_UNREF(exec_ctx, chand->lb_policy, "channel");
462 chand->lb_policy = NULL;
463 }
Craig Tillerb12d22a2016-04-23 12:50:21 -0700464 }
Craig Tiller1c51edc2016-05-07 16:18:43 -0700465 GRPC_ERROR_UNREF(op->disconnect_with_error);
Craig Tillera82950e2015-09-22 12:33:20 -0700466 }
Mark D. Rothff4df062016-08-22 15:02:49 -0700467 gpr_mu_unlock(&chand->mu);
Craig Tillerca3e9d32015-06-27 18:37:27 -0700468}
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800469
Mark D. Rothb2d24882016-10-27 15:44:07 -0700470static void cc_get_channel_info(grpc_exec_ctx *exec_ctx,
471 grpc_channel_element *elem,
Mark D. Rothf79ce7d2016-11-04 08:43:36 -0700472 const grpc_channel_info *info) {
Mark D. Rothb2d24882016-10-27 15:44:07 -0700473 channel_data *chand = elem->channel_data;
474 gpr_mu_lock(&chand->mu);
475 if (info->lb_policy_name != NULL) {
476 *info->lb_policy_name = chand->lb_policy_name == NULL
Mark D. Roth78afd772016-11-04 12:49:49 -0700477 ? NULL
478 : gpr_strdup(chand->lb_policy_name);
Mark D. Rothb2d24882016-10-27 15:44:07 -0700479 }
Mark D. Rothc625c7a2016-11-09 14:12:37 -0800480 if (info->service_config_json != NULL) {
481 *info->service_config_json = chand->service_config_json == NULL
482 ? NULL
483 : gpr_strdup(chand->service_config_json);
484 }
Mark D. Rothb2d24882016-10-27 15:44:07 -0700485 gpr_mu_unlock(&chand->mu);
486}
487
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700488/* Constructor for channel_data */
489static void cc_init_channel_elem(grpc_exec_ctx *exec_ctx,
490 grpc_channel_element *elem,
491 grpc_channel_element_args *args) {
492 channel_data *chand = elem->channel_data;
493
494 memset(chand, 0, sizeof(*chand));
495
496 GPR_ASSERT(args->is_last);
497 GPR_ASSERT(elem->filter == &grpc_client_channel_filter);
498
499 gpr_mu_init(&chand->mu);
500 grpc_closure_init(&chand->on_resolver_result_changed,
501 on_resolver_result_changed, chand);
502 chand->owning_stack = args->channel_stack;
503
504 grpc_connectivity_state_init(&chand->state_tracker, GRPC_CHANNEL_IDLE,
505 "client_channel");
506 chand->interested_parties = grpc_pollset_set_create();
507}
508
509/* Destructor for channel_data */
510static void cc_destroy_channel_elem(grpc_exec_ctx *exec_ctx,
511 grpc_channel_element *elem) {
512 channel_data *chand = elem->channel_data;
513
514 if (chand->resolver != NULL) {
515 grpc_resolver_shutdown(exec_ctx, chand->resolver);
516 GRPC_RESOLVER_UNREF(exec_ctx, chand->resolver, "channel");
517 }
Mark D. Roth0e48a9a2016-09-08 14:14:39 -0700518 if (chand->client_channel_factory != NULL) {
519 grpc_client_channel_factory_unref(exec_ctx, chand->client_channel_factory);
520 }
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700521 if (chand->lb_policy != NULL) {
522 grpc_pollset_set_del_pollset_set(exec_ctx,
523 chand->lb_policy->interested_parties,
524 chand->interested_parties);
525 GRPC_LB_POLICY_UNREF(exec_ctx, chand->lb_policy, "channel");
526 }
Mark D. Rothb2d24882016-10-27 15:44:07 -0700527 gpr_free(chand->lb_policy_name);
Mark D. Rothc625c7a2016-11-09 14:12:37 -0800528 gpr_free(chand->service_config_json);
Mark D. Roth9d480942016-10-19 14:18:05 -0700529 if (chand->method_params_table != NULL) {
530 grpc_mdstr_hash_table_unref(chand->method_params_table);
Mark D. Roth9fe284e2016-09-12 11:22:27 -0700531 }
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700532 grpc_connectivity_state_destroy(exec_ctx, &chand->state_tracker);
533 grpc_pollset_set_destroy(chand->interested_parties);
534 gpr_mu_destroy(&chand->mu);
535}
536
537/*************************************************************************
538 * PER-CALL FUNCTIONS
539 */
540
541#define GET_CALL(call_data) \
542 ((grpc_subchannel_call *)(gpr_atm_acq_load(&(call_data)->subchannel_call)))
543
544#define CANCELLED_CALL ((grpc_subchannel_call *)1)
545
546typedef enum {
547 GRPC_SUBCHANNEL_CALL_HOLDER_NOT_CREATING,
548 GRPC_SUBCHANNEL_CALL_HOLDER_PICKING_SUBCHANNEL
549} subchannel_creation_phase;
550
551/** Call data. Holds a pointer to grpc_subchannel_call and the
552 associated machinery to create such a pointer.
553 Handles queueing of stream ops until a call object is ready, waiting
554 for initial metadata before trying to create a call object,
555 and handling cancellation gracefully. */
556typedef struct client_channel_call_data {
Mark D. Roth72f6da82016-09-02 13:42:38 -0700557 // State for handling deadlines.
558 // The code in deadline_filter.c requires this to be the first field.
Mark D. Roth72f6da82016-09-02 13:42:38 -0700559 // TODO(roth): This is slightly sub-optimal in that grpc_deadline_state
560 // and this struct both independently store a pointer to the call
561 // stack and each has its own mutex. If/when we have time, find a way
Mark D. Roth6ad99172016-09-09 07:52:48 -0700562 // to avoid this without breaking the grpc_deadline_state abstraction.
Mark D. Roth72f6da82016-09-02 13:42:38 -0700563 grpc_deadline_state deadline_state;
Mark D. Rothf28763c2016-09-14 15:18:40 -0700564
Mark D. Rothe40dd292016-10-05 14:58:37 -0700565 grpc_mdstr *path; // Request path.
566 gpr_timespec call_start_time;
567 gpr_timespec deadline;
Mark D. Roth9d480942016-10-19 14:18:05 -0700568 wait_for_ready_value wait_for_ready_from_service_config;
Mark D. Rothe40dd292016-10-05 14:58:37 -0700569 grpc_closure read_service_config;
Mark D. Rothaa850a72016-09-26 13:38:02 -0700570
Mark D. Rothf28763c2016-09-14 15:18:40 -0700571 grpc_error *cancel_error;
Mark D. Roth72f6da82016-09-02 13:42:38 -0700572
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700573 /** either 0 for no call, 1 for cancelled, or a pointer to a
574 grpc_subchannel_call */
575 gpr_atm subchannel_call;
576
577 gpr_mu mu;
578
579 subchannel_creation_phase creation_phase;
580 grpc_connected_subchannel *connected_subchannel;
581 grpc_polling_entity *pollent;
582
Craig Tiller57726ca2016-09-12 11:59:45 -0700583 grpc_transport_stream_op **waiting_ops;
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700584 size_t waiting_ops_count;
585 size_t waiting_ops_capacity;
586
587 grpc_closure next_step;
588
589 grpc_call_stack *owning_call;
David Garcia Quintasd1a47f12016-09-02 12:46:44 +0200590
591 grpc_linked_mdelem lb_token_mdelem;
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700592} call_data;
593
594static void add_waiting_locked(call_data *calld, grpc_transport_stream_op *op) {
595 GPR_TIMER_BEGIN("add_waiting_locked", 0);
596 if (calld->waiting_ops_count == calld->waiting_ops_capacity) {
597 calld->waiting_ops_capacity = GPR_MAX(3, 2 * calld->waiting_ops_capacity);
598 calld->waiting_ops =
599 gpr_realloc(calld->waiting_ops,
600 calld->waiting_ops_capacity * sizeof(*calld->waiting_ops));
601 }
Craig Tiller57726ca2016-09-12 11:59:45 -0700602 calld->waiting_ops[calld->waiting_ops_count++] = op;
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700603 GPR_TIMER_END("add_waiting_locked", 0);
604}
605
606static void fail_locked(grpc_exec_ctx *exec_ctx, call_data *calld,
607 grpc_error *error) {
608 size_t i;
609 for (i = 0; i < calld->waiting_ops_count; i++) {
610 grpc_transport_stream_op_finish_with_failure(
Craig Tiller57726ca2016-09-12 11:59:45 -0700611 exec_ctx, calld->waiting_ops[i], GRPC_ERROR_REF(error));
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700612 }
613 calld->waiting_ops_count = 0;
614 GRPC_ERROR_UNREF(error);
615}
616
617typedef struct {
Craig Tiller57726ca2016-09-12 11:59:45 -0700618 grpc_transport_stream_op **ops;
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700619 size_t nops;
620 grpc_subchannel_call *call;
621} retry_ops_args;
622
623static void retry_ops(grpc_exec_ctx *exec_ctx, void *args, grpc_error *error) {
624 retry_ops_args *a = args;
625 size_t i;
626 for (i = 0; i < a->nops; i++) {
Craig Tiller57726ca2016-09-12 11:59:45 -0700627 grpc_subchannel_call_process_op(exec_ctx, a->call, a->ops[i]);
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700628 }
629 GRPC_SUBCHANNEL_CALL_UNREF(exec_ctx, a->call, "retry_ops");
630 gpr_free(a->ops);
631 gpr_free(a);
632}
633
634static void retry_waiting_locked(grpc_exec_ctx *exec_ctx, call_data *calld) {
Craig Tiller57726ca2016-09-12 11:59:45 -0700635 if (calld->waiting_ops_count == 0) {
636 return;
637 }
638
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700639 retry_ops_args *a = gpr_malloc(sizeof(*a));
640 a->ops = calld->waiting_ops;
641 a->nops = calld->waiting_ops_count;
642 a->call = GET_CALL(calld);
643 if (a->call == CANCELLED_CALL) {
644 gpr_free(a);
645 fail_locked(exec_ctx, calld, GRPC_ERROR_CANCELLED);
646 return;
647 }
648 calld->waiting_ops = NULL;
649 calld->waiting_ops_count = 0;
650 calld->waiting_ops_capacity = 0;
651 GRPC_SUBCHANNEL_CALL_REF(a->call, "retry_ops");
652 grpc_exec_ctx_sched(exec_ctx, grpc_closure_create(retry_ops, a),
653 GRPC_ERROR_NONE, NULL);
654}
655
656static void subchannel_ready(grpc_exec_ctx *exec_ctx, void *arg,
657 grpc_error *error) {
Yuchen Zeng19656b12016-09-01 18:00:45 -0700658 grpc_call_element *elem = arg;
659 call_data *calld = elem->call_data;
660 channel_data *chand = elem->channel_data;
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700661 gpr_mu_lock(&calld->mu);
662 GPR_ASSERT(calld->creation_phase ==
663 GRPC_SUBCHANNEL_CALL_HOLDER_PICKING_SUBCHANNEL);
Yuchen Zeng19656b12016-09-01 18:00:45 -0700664 grpc_polling_entity_del_from_pollset_set(exec_ctx, calld->pollent,
665 chand->interested_parties);
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700666 calld->creation_phase = GRPC_SUBCHANNEL_CALL_HOLDER_NOT_CREATING;
667 if (calld->connected_subchannel == NULL) {
668 gpr_atm_no_barrier_store(&calld->subchannel_call, 1);
669 fail_locked(exec_ctx, calld, GRPC_ERROR_CREATE_REFERENCING(
670 "Failed to create subchannel", &error, 1));
Mark D. Roth72f6da82016-09-02 13:42:38 -0700671 } else if (GET_CALL(calld) == CANCELLED_CALL) {
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700672 /* already cancelled before subchannel became ready */
673 fail_locked(exec_ctx, calld,
674 GRPC_ERROR_CREATE_REFERENCING(
675 "Cancelled before creating subchannel", &error, 1));
676 } else {
Mark D. Roth9fe284e2016-09-12 11:22:27 -0700677 /* Create call on subchannel. */
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700678 grpc_subchannel_call *subchannel_call = NULL;
679 grpc_error *new_error = grpc_connected_subchannel_create_call(
Mark D. Rothaa850a72016-09-26 13:38:02 -0700680 exec_ctx, calld->connected_subchannel, calld->pollent, calld->path,
681 calld->deadline, &subchannel_call);
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700682 if (new_error != GRPC_ERROR_NONE) {
683 new_error = grpc_error_add_child(new_error, error);
684 subchannel_call = CANCELLED_CALL;
685 fail_locked(exec_ctx, calld, new_error);
686 }
687 gpr_atm_rel_store(&calld->subchannel_call,
688 (gpr_atm)(uintptr_t)subchannel_call);
689 retry_waiting_locked(exec_ctx, calld);
690 }
691 gpr_mu_unlock(&calld->mu);
692 GRPC_CALL_STACK_UNREF(exec_ctx, calld->owning_call, "pick_subchannel");
693}
694
695static char *cc_get_peer(grpc_exec_ctx *exec_ctx, grpc_call_element *elem) {
696 call_data *calld = elem->call_data;
697 grpc_subchannel_call *subchannel_call = GET_CALL(calld);
698 if (subchannel_call == NULL || subchannel_call == CANCELLED_CALL) {
699 return NULL;
700 } else {
701 return grpc_subchannel_call_get_peer(exec_ctx, subchannel_call);
702 }
703}
704
Craig Tiller577c9b22015-11-02 14:11:15 -0800705typedef struct {
706 grpc_metadata_batch *initial_metadata;
Craig Tiller8c0d96f2016-03-11 14:27:52 -0800707 uint32_t initial_metadata_flags;
Craig Tillerb5585d42015-11-17 07:18:31 -0800708 grpc_connected_subchannel **connected_subchannel;
Craig Tiller577c9b22015-11-02 14:11:15 -0800709 grpc_closure *on_ready;
710 grpc_call_element *elem;
711 grpc_closure closure;
712} continue_picking_args;
713
Yuchen Zeng144ce652016-09-01 18:19:34 -0700714/** Return true if subchannel is available immediately (in which case on_ready
715 should not be called), or false otherwise (in which case on_ready should be
716 called when the subchannel is available). */
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700717static bool pick_subchannel(grpc_exec_ctx *exec_ctx, grpc_call_element *elem,
718 grpc_metadata_batch *initial_metadata,
719 uint32_t initial_metadata_flags,
720 grpc_connected_subchannel **connected_subchannel,
Mark D. Roth72f6da82016-09-02 13:42:38 -0700721 grpc_closure *on_ready, grpc_error *error);
Craig Tiller577c9b22015-11-02 14:11:15 -0800722
Craig Tiller804ff712016-05-05 16:25:40 -0700723static void continue_picking(grpc_exec_ctx *exec_ctx, void *arg,
724 grpc_error *error) {
Craig Tiller577c9b22015-11-02 14:11:15 -0800725 continue_picking_args *cpa = arg;
Craig Tiller0ede5452016-04-23 12:21:45 -0700726 if (cpa->connected_subchannel == NULL) {
Craig Tiller577c9b22015-11-02 14:11:15 -0800727 /* cancelled, do nothing */
Craig Tiller804ff712016-05-05 16:25:40 -0700728 } else if (error != GRPC_ERROR_NONE) {
Craig Tiller332f1b32016-05-24 13:21:21 -0700729 grpc_exec_ctx_sched(exec_ctx, cpa->on_ready, GRPC_ERROR_REF(error), NULL);
Mark D. Roth9dab7d52016-10-07 07:48:03 -0700730 } else {
731 call_data *calld = cpa->elem->call_data;
732 gpr_mu_lock(&calld->mu);
733 if (pick_subchannel(exec_ctx, cpa->elem, cpa->initial_metadata,
Mark D. Rothfd2ddd22016-10-07 10:11:10 -0700734 cpa->initial_metadata_flags, cpa->connected_subchannel,
735 cpa->on_ready, GRPC_ERROR_NONE)) {
Mark D. Roth9dab7d52016-10-07 07:48:03 -0700736 grpc_exec_ctx_sched(exec_ctx, cpa->on_ready, GRPC_ERROR_NONE, NULL);
737 }
738 gpr_mu_unlock(&calld->mu);
Craig Tiller577c9b22015-11-02 14:11:15 -0800739 }
740 gpr_free(cpa);
741}
742
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700743static bool pick_subchannel(grpc_exec_ctx *exec_ctx, grpc_call_element *elem,
744 grpc_metadata_batch *initial_metadata,
745 uint32_t initial_metadata_flags,
746 grpc_connected_subchannel **connected_subchannel,
Mark D. Roth72f6da82016-09-02 13:42:38 -0700747 grpc_closure *on_ready, grpc_error *error) {
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700748 GPR_TIMER_BEGIN("pick_subchannel", 0);
Craig Tillerbfc9adc2016-06-27 13:16:22 -0700749
Craig Tiller577c9b22015-11-02 14:11:15 -0800750 channel_data *chand = elem->channel_data;
751 call_data *calld = elem->call_data;
752 continue_picking_args *cpa;
753 grpc_closure *closure;
754
Craig Tillerb5585d42015-11-17 07:18:31 -0800755 GPR_ASSERT(connected_subchannel);
Craig Tiller577c9b22015-11-02 14:11:15 -0800756
Mark D. Rothff4df062016-08-22 15:02:49 -0700757 gpr_mu_lock(&chand->mu);
Craig Tiller577c9b22015-11-02 14:11:15 -0800758 if (initial_metadata == NULL) {
759 if (chand->lb_policy != NULL) {
Craig Tillerab33b482015-11-21 08:11:04 -0800760 grpc_lb_policy_cancel_pick(exec_ctx, chand->lb_policy,
Mark D. Roth5f844002016-09-08 08:20:53 -0700761 connected_subchannel, GRPC_ERROR_REF(error));
Craig Tiller577c9b22015-11-02 14:11:15 -0800762 }
763 for (closure = chand->waiting_for_config_closures.head; closure != NULL;
Craig Tiller804ff712016-05-05 16:25:40 -0700764 closure = closure->next_data.next) {
Craig Tiller577c9b22015-11-02 14:11:15 -0800765 cpa = closure->cb_arg;
Craig Tillerb5585d42015-11-17 07:18:31 -0800766 if (cpa->connected_subchannel == connected_subchannel) {
767 cpa->connected_subchannel = NULL;
Mark D. Roth932b10c2016-09-09 08:44:30 -0700768 grpc_exec_ctx_sched(
769 exec_ctx, cpa->on_ready,
770 GRPC_ERROR_CREATE_REFERENCING("Pick cancelled", &error, 1), NULL);
Craig Tiller577c9b22015-11-02 14:11:15 -0800771 }
772 }
Mark D. Rothff4df062016-08-22 15:02:49 -0700773 gpr_mu_unlock(&chand->mu);
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700774 GPR_TIMER_END("pick_subchannel", 0);
Mark D. Roth697a1f62016-09-07 13:35:07 -0700775 GRPC_ERROR_UNREF(error);
Mark D. Roth4c0fe492016-08-31 13:51:55 -0700776 return true;
Craig Tiller577c9b22015-11-02 14:11:15 -0800777 }
Mark D. Roth697a1f62016-09-07 13:35:07 -0700778 GPR_ASSERT(error == GRPC_ERROR_NONE);
Craig Tiller577c9b22015-11-02 14:11:15 -0800779 if (chand->lb_policy != NULL) {
Craig Tiller86c0f8a2015-12-01 20:05:40 -0800780 grpc_lb_policy *lb_policy = chand->lb_policy;
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700781 GRPC_LB_POLICY_REF(lb_policy, "pick_subchannel");
Mark D. Rothff4df062016-08-22 15:02:49 -0700782 gpr_mu_unlock(&chand->mu);
Mark D. Rothe40dd292016-10-05 14:58:37 -0700783 // If the application explicitly set wait_for_ready, use that.
784 // Otherwise, if the service config specified a value for this
785 // method, use that.
Mark D. Rothc1c38582016-10-11 11:03:27 -0700786 const bool wait_for_ready_set_from_api =
787 initial_metadata_flags &
788 GRPC_INITIAL_METADATA_WAIT_FOR_READY_EXPLICITLY_SET;
789 const bool wait_for_ready_set_from_service_config =
790 calld->wait_for_ready_from_service_config != WAIT_FOR_READY_UNSET;
791 if (!wait_for_ready_set_from_api &&
792 wait_for_ready_set_from_service_config) {
Mark D. Rothe40dd292016-10-05 14:58:37 -0700793 if (calld->wait_for_ready_from_service_config == WAIT_FOR_READY_TRUE) {
794 initial_metadata_flags |= GRPC_INITIAL_METADATA_WAIT_FOR_READY;
795 } else {
796 initial_metadata_flags &= ~GRPC_INITIAL_METADATA_WAIT_FOR_READY;
797 }
798 }
David Garcia Quintas61c58012016-10-03 14:44:20 -0700799 // TODO(dgq): make this deadline configurable somehow.
David Garcia Quintas92eb6b92016-09-30 14:07:39 -0700800 const grpc_lb_policy_pick_args inputs = {
Yuchen Zengac8bc422016-10-05 14:00:02 -0700801 initial_metadata, initial_metadata_flags, &calld->lb_token_mdelem,
802 gpr_inf_future(GPR_CLOCK_MONOTONIC)};
Mark D. Roth55f25b62016-10-12 14:55:20 -0700803 const bool result = grpc_lb_policy_pick(
804 exec_ctx, lb_policy, &inputs, connected_subchannel, NULL, on_ready);
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700805 GRPC_LB_POLICY_UNREF(exec_ctx, lb_policy, "pick_subchannel");
806 GPR_TIMER_END("pick_subchannel", 0);
Mark D. Roth9dab7d52016-10-07 07:48:03 -0700807 return result;
Craig Tiller577c9b22015-11-02 14:11:15 -0800808 }
809 if (chand->resolver != NULL && !chand->started_resolving) {
Mark D. Roth4c0fe492016-08-31 13:51:55 -0700810 chand->started_resolving = true;
Craig Tiller906e3bc2015-11-24 07:31:31 -0800811 GRPC_CHANNEL_STACK_REF(chand->owning_stack, "resolver");
Mark D. Roth046cf762016-09-26 11:13:51 -0700812 grpc_resolver_next(exec_ctx, chand->resolver, &chand->resolver_result,
Mark D. Rothff4df062016-08-22 15:02:49 -0700813 &chand->on_resolver_result_changed);
Craig Tiller577c9b22015-11-02 14:11:15 -0800814 }
Craig Tiller0eab6972016-04-23 12:59:57 -0700815 if (chand->resolver != NULL) {
816 cpa = gpr_malloc(sizeof(*cpa));
817 cpa->initial_metadata = initial_metadata;
818 cpa->initial_metadata_flags = initial_metadata_flags;
819 cpa->connected_subchannel = connected_subchannel;
820 cpa->on_ready = on_ready;
821 cpa->elem = elem;
822 grpc_closure_init(&cpa->closure, continue_picking, cpa);
Craig Tiller804ff712016-05-05 16:25:40 -0700823 grpc_closure_list_append(&chand->waiting_for_config_closures, &cpa->closure,
824 GRPC_ERROR_NONE);
Craig Tiller0eab6972016-04-23 12:59:57 -0700825 } else {
Craig Tiller332f1b32016-05-24 13:21:21 -0700826 grpc_exec_ctx_sched(exec_ctx, on_ready, GRPC_ERROR_CREATE("Disconnected"),
827 NULL);
Craig Tiller0eab6972016-04-23 12:59:57 -0700828 }
Mark D. Rothff4df062016-08-22 15:02:49 -0700829 gpr_mu_unlock(&chand->mu);
Craig Tillerbfc9adc2016-06-27 13:16:22 -0700830
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700831 GPR_TIMER_END("pick_subchannel", 0);
Mark D. Roth4c0fe492016-08-31 13:51:55 -0700832 return false;
Craig Tiller577c9b22015-11-02 14:11:15 -0800833}
834
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700835// The logic here is fairly complicated, due to (a) the fact that we
836// need to handle the case where we receive the send op before the
837// initial metadata op, and (b) the need for efficiency, especially in
838// the streaming case.
839// TODO(ctiller): Explain this more thoroughly.
840static void cc_start_transport_stream_op(grpc_exec_ctx *exec_ctx,
841 grpc_call_element *elem,
842 grpc_transport_stream_op *op) {
843 call_data *calld = elem->call_data;
Yuchen Zeng19656b12016-09-01 18:00:45 -0700844 channel_data *chand = elem->channel_data;
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700845 GRPC_CALL_LOG_OP(GPR_INFO, elem, op);
Mark D. Roth72f6da82016-09-02 13:42:38 -0700846 grpc_deadline_state_client_start_transport_stream_op(exec_ctx, elem, op);
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700847 /* try to (atomically) get the call */
848 grpc_subchannel_call *call = GET_CALL(calld);
849 GPR_TIMER_BEGIN("cc_start_transport_stream_op", 0);
850 if (call == CANCELLED_CALL) {
Mark D. Rothf28763c2016-09-14 15:18:40 -0700851 grpc_transport_stream_op_finish_with_failure(
852 exec_ctx, op, GRPC_ERROR_REF(calld->cancel_error));
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700853 GPR_TIMER_END("cc_start_transport_stream_op", 0);
854 return;
855 }
856 if (call != NULL) {
857 grpc_subchannel_call_process_op(exec_ctx, call, op);
858 GPR_TIMER_END("cc_start_transport_stream_op", 0);
859 return;
860 }
861 /* we failed; lock and figure out what to do */
862 gpr_mu_lock(&calld->mu);
863retry:
864 /* need to recheck that another thread hasn't set the call */
865 call = GET_CALL(calld);
866 if (call == CANCELLED_CALL) {
867 gpr_mu_unlock(&calld->mu);
Mark D. Rothf28763c2016-09-14 15:18:40 -0700868 grpc_transport_stream_op_finish_with_failure(
869 exec_ctx, op, GRPC_ERROR_REF(calld->cancel_error));
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700870 GPR_TIMER_END("cc_start_transport_stream_op", 0);
871 return;
872 }
873 if (call != NULL) {
874 gpr_mu_unlock(&calld->mu);
875 grpc_subchannel_call_process_op(exec_ctx, call, op);
876 GPR_TIMER_END("cc_start_transport_stream_op", 0);
877 return;
878 }
879 /* if this is a cancellation, then we can raise our cancelled flag */
880 if (op->cancel_error != GRPC_ERROR_NONE) {
881 if (!gpr_atm_rel_cas(&calld->subchannel_call, 0,
882 (gpr_atm)(uintptr_t)CANCELLED_CALL)) {
883 goto retry;
884 } else {
Mark D. Rothf28763c2016-09-14 15:18:40 -0700885 // Stash a copy of cancel_error in our call data, so that we can use
886 // it for subsequent operations. This ensures that if the call is
887 // cancelled before any ops are passed down (e.g., if the deadline
888 // is in the past when the call starts), we can return the right
889 // error to the caller when the first op does get passed down.
890 calld->cancel_error = GRPC_ERROR_REF(op->cancel_error);
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700891 switch (calld->creation_phase) {
892 case GRPC_SUBCHANNEL_CALL_HOLDER_NOT_CREATING:
893 fail_locked(exec_ctx, calld, GRPC_ERROR_REF(op->cancel_error));
894 break;
895 case GRPC_SUBCHANNEL_CALL_HOLDER_PICKING_SUBCHANNEL:
Mark D. Rothd4c0f552016-09-01 09:25:32 -0700896 pick_subchannel(exec_ctx, elem, NULL, 0, &calld->connected_subchannel,
Mark D. Roth72f6da82016-09-02 13:42:38 -0700897 NULL, GRPC_ERROR_REF(op->cancel_error));
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700898 break;
899 }
900 gpr_mu_unlock(&calld->mu);
Mark D. Roth72f6da82016-09-02 13:42:38 -0700901 grpc_transport_stream_op_finish_with_failure(
902 exec_ctx, op, GRPC_ERROR_REF(op->cancel_error));
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700903 GPR_TIMER_END("cc_start_transport_stream_op", 0);
904 return;
905 }
906 }
907 /* if we don't have a subchannel, try to get one */
908 if (calld->creation_phase == GRPC_SUBCHANNEL_CALL_HOLDER_NOT_CREATING &&
909 calld->connected_subchannel == NULL &&
910 op->send_initial_metadata != NULL) {
911 calld->creation_phase = GRPC_SUBCHANNEL_CALL_HOLDER_PICKING_SUBCHANNEL;
Yuchen Zeng19656b12016-09-01 18:00:45 -0700912 grpc_closure_init(&calld->next_step, subchannel_ready, elem);
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700913 GRPC_CALL_STACK_REF(calld->owning_call, "pick_subchannel");
Yuchen Zeng144ce652016-09-01 18:19:34 -0700914 /* If a subchannel is not available immediately, the polling entity from
915 call_data should be provided to channel_data's interested_parties, so
916 that IO of the lb_policy and resolver could be done under it. */
Mark D. Rothd4c0f552016-09-01 09:25:32 -0700917 if (pick_subchannel(exec_ctx, elem, op->send_initial_metadata,
Mark D. Rothe40dd292016-10-05 14:58:37 -0700918 op->send_initial_metadata_flags,
919 &calld->connected_subchannel, &calld->next_step,
920 GRPC_ERROR_NONE)) {
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700921 calld->creation_phase = GRPC_SUBCHANNEL_CALL_HOLDER_NOT_CREATING;
922 GRPC_CALL_STACK_UNREF(exec_ctx, calld->owning_call, "pick_subchannel");
Yuchen Zeng19656b12016-09-01 18:00:45 -0700923 } else {
Yuchen Zeng19656b12016-09-01 18:00:45 -0700924 grpc_polling_entity_add_to_pollset_set(exec_ctx, calld->pollent,
925 chand->interested_parties);
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700926 }
927 }
928 /* if we've got a subchannel, then let's ask it to create a call */
929 if (calld->creation_phase == GRPC_SUBCHANNEL_CALL_HOLDER_NOT_CREATING &&
930 calld->connected_subchannel != NULL) {
931 grpc_subchannel_call *subchannel_call = NULL;
932 grpc_error *error = grpc_connected_subchannel_create_call(
Mark D. Rothaa850a72016-09-26 13:38:02 -0700933 exec_ctx, calld->connected_subchannel, calld->pollent, calld->path,
934 calld->deadline, &subchannel_call);
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700935 if (error != GRPC_ERROR_NONE) {
936 subchannel_call = CANCELLED_CALL;
937 fail_locked(exec_ctx, calld, GRPC_ERROR_REF(error));
938 grpc_transport_stream_op_finish_with_failure(exec_ctx, op, error);
939 }
940 gpr_atm_rel_store(&calld->subchannel_call,
941 (gpr_atm)(uintptr_t)subchannel_call);
942 retry_waiting_locked(exec_ctx, calld);
943 goto retry;
944 }
945 /* nothing to be done but wait */
946 add_waiting_locked(calld, op);
947 gpr_mu_unlock(&calld->mu);
948 GPR_TIMER_END("cc_start_transport_stream_op", 0);
949}
950
Mark D. Rothe40dd292016-10-05 14:58:37 -0700951// Gets data from the service config. Invoked when the resolver returns
952// its initial result.
953static void read_service_config(grpc_exec_ctx *exec_ctx, void *arg,
954 grpc_error *error) {
955 grpc_call_element *elem = arg;
956 channel_data *chand = elem->channel_data;
957 call_data *calld = elem->call_data;
958 // If this is an error, there's no point in looking at the service config.
Mark D. Roth196387a2016-10-12 14:53:36 -0700959 if (error == GRPC_ERROR_NONE) {
960 // Get the method config table from channel data.
961 gpr_mu_lock(&chand->mu);
Mark D. Roth9d480942016-10-19 14:18:05 -0700962 grpc_mdstr_hash_table *method_params_table = NULL;
963 if (chand->method_params_table != NULL) {
964 method_params_table =
965 grpc_mdstr_hash_table_ref(chand->method_params_table);
Mark D. Rothe40dd292016-10-05 14:58:37 -0700966 }
Mark D. Roth196387a2016-10-12 14:53:36 -0700967 gpr_mu_unlock(&chand->mu);
968 // If the method config table was present, use it.
Mark D. Roth9d480942016-10-19 14:18:05 -0700969 if (method_params_table != NULL) {
970 const method_parameters *method_params =
971 grpc_method_config_table_get(method_params_table, calld->path);
972 if (method_params != NULL) {
973 const bool have_method_timeout =
974 gpr_time_cmp(method_params->timeout, gpr_time_0(GPR_TIMESPAN)) != 0;
975 if (have_method_timeout ||
976 method_params->wait_for_ready != WAIT_FOR_READY_UNSET) {
Mark D. Roth196387a2016-10-12 14:53:36 -0700977 gpr_mu_lock(&calld->mu);
Mark D. Roth9d480942016-10-19 14:18:05 -0700978 if (have_method_timeout) {
979 const gpr_timespec per_method_deadline =
980 gpr_time_add(calld->call_start_time, method_params->timeout);
Mark D. Roth196387a2016-10-12 14:53:36 -0700981 if (gpr_time_cmp(per_method_deadline, calld->deadline) < 0) {
982 calld->deadline = per_method_deadline;
983 // Reset deadline timer.
984 grpc_deadline_state_reset(exec_ctx, elem, calld->deadline);
985 }
986 }
Mark D. Roth9d480942016-10-19 14:18:05 -0700987 if (method_params->wait_for_ready != WAIT_FOR_READY_UNSET) {
Mark D. Roth196387a2016-10-12 14:53:36 -0700988 calld->wait_for_ready_from_service_config =
Mark D. Roth9d480942016-10-19 14:18:05 -0700989 method_params->wait_for_ready;
Mark D. Roth196387a2016-10-12 14:53:36 -0700990 }
991 gpr_mu_unlock(&calld->mu);
992 }
993 }
Mark D. Roth9d480942016-10-19 14:18:05 -0700994 grpc_mdstr_hash_table_unref(method_params_table);
Mark D. Roth196387a2016-10-12 14:53:36 -0700995 }
Mark D. Rothe40dd292016-10-05 14:58:37 -0700996 }
Mark D. Roth31292f22016-10-12 13:14:07 -0700997 GRPC_CALL_STACK_UNREF(exec_ctx, calld->owning_call, "read_service_config");
Mark D. Rothe40dd292016-10-05 14:58:37 -0700998}
999
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001000/* Constructor for call_data */
Mark D. Roth2a5959f2016-09-01 08:20:27 -07001001static grpc_error *cc_init_call_elem(grpc_exec_ctx *exec_ctx,
1002 grpc_call_element *elem,
1003 grpc_call_element_args *args) {
Mark D. Rothaa850a72016-09-26 13:38:02 -07001004 channel_data *chand = elem->channel_data;
Mark D. Roth4c0fe492016-08-31 13:51:55 -07001005 call_data *calld = elem->call_data;
Mark D. Rothe40dd292016-10-05 14:58:37 -07001006 // Initialize data members.
1007 grpc_deadline_state_init(exec_ctx, elem, args->call_stack);
Mark D. Rothaa850a72016-09-26 13:38:02 -07001008 calld->path = GRPC_MDSTR_REF(args->path);
Mark D. Rothff08f332016-10-14 13:01:01 -07001009 calld->call_start_time = args->start_time;
Mark D. Rothe40dd292016-10-05 14:58:37 -07001010 calld->deadline = gpr_convert_clock_type(args->deadline, GPR_CLOCK_MONOTONIC);
1011 calld->wait_for_ready_from_service_config = WAIT_FOR_READY_UNSET;
Mark D. Rothf28763c2016-09-14 15:18:40 -07001012 calld->cancel_error = GRPC_ERROR_NONE;
Mark D. Roth4c0fe492016-08-31 13:51:55 -07001013 gpr_atm_rel_store(&calld->subchannel_call, 0);
Mark D. Roth4c0fe492016-08-31 13:51:55 -07001014 gpr_mu_init(&calld->mu);
1015 calld->connected_subchannel = NULL;
1016 calld->waiting_ops = NULL;
1017 calld->waiting_ops_count = 0;
1018 calld->waiting_ops_capacity = 0;
1019 calld->creation_phase = GRPC_SUBCHANNEL_CALL_HOLDER_NOT_CREATING;
1020 calld->owning_call = args->call_stack;
1021 calld->pollent = NULL;
Mark D. Rothe40dd292016-10-05 14:58:37 -07001022 // If the resolver has already returned results, then we can access
1023 // the service config parameters immediately. Otherwise, we need to
1024 // defer that work until the resolver returns an initial result.
1025 // TODO(roth): This code is almost but not quite identical to the code
1026 // in read_service_config() above. It would be nice to find a way to
1027 // combine them, to avoid having to maintain it twice.
1028 gpr_mu_lock(&chand->mu);
1029 if (chand->lb_policy != NULL) {
1030 // We already have a resolver result, so check for service config.
Mark D. Roth9d480942016-10-19 14:18:05 -07001031 if (chand->method_params_table != NULL) {
1032 grpc_mdstr_hash_table *method_params_table =
1033 grpc_mdstr_hash_table_ref(chand->method_params_table);
Mark D. Rothe40dd292016-10-05 14:58:37 -07001034 gpr_mu_unlock(&chand->mu);
Mark D. Roth9d480942016-10-19 14:18:05 -07001035 method_parameters *method_params =
1036 grpc_method_config_table_get(method_params_table, args->path);
1037 if (method_params != NULL) {
1038 if (gpr_time_cmp(method_params->timeout,
1039 gpr_time_0(GPR_CLOCK_MONOTONIC)) != 0) {
Mark D. Rothe40dd292016-10-05 14:58:37 -07001040 gpr_timespec per_method_deadline =
Mark D. Roth9d480942016-10-19 14:18:05 -07001041 gpr_time_add(calld->call_start_time, method_params->timeout);
Mark D. Rothe40dd292016-10-05 14:58:37 -07001042 calld->deadline = gpr_time_min(calld->deadline, per_method_deadline);
1043 }
Mark D. Roth9d480942016-10-19 14:18:05 -07001044 if (method_params->wait_for_ready != WAIT_FOR_READY_UNSET) {
Mark D. Rothe40dd292016-10-05 14:58:37 -07001045 calld->wait_for_ready_from_service_config =
Mark D. Roth9d480942016-10-19 14:18:05 -07001046 method_params->wait_for_ready;
Mark D. Rothe40dd292016-10-05 14:58:37 -07001047 }
1048 }
Mark D. Roth9d480942016-10-19 14:18:05 -07001049 grpc_mdstr_hash_table_unref(method_params_table);
Mark D. Rothe40dd292016-10-05 14:58:37 -07001050 } else {
1051 gpr_mu_unlock(&chand->mu);
1052 }
1053 } else {
1054 // We don't yet have a resolver result, so register a callback to
1055 // get the service config data once the resolver returns.
Mark D. Roth31292f22016-10-12 13:14:07 -07001056 // Take a reference to the call stack to be owned by the callback.
1057 GRPC_CALL_STACK_REF(calld->owning_call, "read_service_config");
Mark D. Rothe40dd292016-10-05 14:58:37 -07001058 grpc_closure_init(&calld->read_service_config, read_service_config, elem);
1059 grpc_closure_list_append(&chand->waiting_for_config_closures,
1060 &calld->read_service_config, GRPC_ERROR_NONE);
1061 gpr_mu_unlock(&chand->mu);
1062 }
1063 // Start the deadline timer with the current deadline value. If we
1064 // do not yet have service config data, then the timer may be reset
1065 // later.
1066 grpc_deadline_state_start(exec_ctx, elem, calld->deadline);
Mark D. Roth0badbe82016-06-23 10:15:12 -07001067 return GRPC_ERROR_NONE;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001068}
1069
1070/* Destructor for call_data */
Mark D. Roth2a5959f2016-09-01 08:20:27 -07001071static void cc_destroy_call_elem(grpc_exec_ctx *exec_ctx,
1072 grpc_call_element *elem,
1073 const grpc_call_final_info *final_info,
1074 void *and_free_memory) {
Mark D. Roth4c0fe492016-08-31 13:51:55 -07001075 call_data *calld = elem->call_data;
Mark D. Rothf28763c2016-09-14 15:18:40 -07001076 grpc_deadline_state_destroy(exec_ctx, elem);
Mark D. Rothaa850a72016-09-26 13:38:02 -07001077 GRPC_MDSTR_UNREF(calld->path);
Mark D. Rothf28763c2016-09-14 15:18:40 -07001078 GRPC_ERROR_UNREF(calld->cancel_error);
Mark D. Roth4c0fe492016-08-31 13:51:55 -07001079 grpc_subchannel_call *call = GET_CALL(calld);
1080 if (call != NULL && call != CANCELLED_CALL) {
1081 GRPC_SUBCHANNEL_CALL_UNREF(exec_ctx, call, "client_channel_destroy_call");
1082 }
1083 GPR_ASSERT(calld->creation_phase == GRPC_SUBCHANNEL_CALL_HOLDER_NOT_CREATING);
1084 gpr_mu_destroy(&calld->mu);
1085 GPR_ASSERT(calld->waiting_ops_count == 0);
Craig Tiller693d3942016-10-27 16:51:25 -07001086 if (calld->connected_subchannel != NULL) {
1087 GRPC_CONNECTED_SUBCHANNEL_UNREF(exec_ctx, calld->connected_subchannel,
1088 "picked");
1089 }
Mark D. Roth4c0fe492016-08-31 13:51:55 -07001090 gpr_free(calld->waiting_ops);
Craig Tiller2c8063c2016-03-22 22:12:15 -07001091 gpr_free(and_free_memory);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001092}
1093
David Garcia Quintasf72eb972016-05-03 18:28:09 -07001094static void cc_set_pollset_or_pollset_set(grpc_exec_ctx *exec_ctx,
1095 grpc_call_element *elem,
David Garcia Quintas2a50dfe2016-05-31 15:09:12 -07001096 grpc_polling_entity *pollent) {
Craig Tiller577c9b22015-11-02 14:11:15 -08001097 call_data *calld = elem->call_data;
David Garcia Quintas2a50dfe2016-05-31 15:09:12 -07001098 calld->pollent = pollent;
Craig Tiller577c9b22015-11-02 14:11:15 -08001099}
1100
Mark D. Roth2a5959f2016-09-01 08:20:27 -07001101/*************************************************************************
1102 * EXPORTED SYMBOLS
1103 */
1104
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001105const grpc_channel_filter grpc_client_channel_filter = {
Craig Tillerf40df232016-03-25 13:38:14 -07001106 cc_start_transport_stream_op,
1107 cc_start_transport_op,
1108 sizeof(call_data),
Mark D. Roth2a5959f2016-09-01 08:20:27 -07001109 cc_init_call_elem,
David Garcia Quintas4afce7e2016-04-18 16:25:17 -07001110 cc_set_pollset_or_pollset_set,
Mark D. Roth2a5959f2016-09-01 08:20:27 -07001111 cc_destroy_call_elem,
Craig Tillerf40df232016-03-25 13:38:14 -07001112 sizeof(channel_data),
Mark D. Roth2a5959f2016-09-01 08:20:27 -07001113 cc_init_channel_elem,
1114 cc_destroy_channel_elem,
Craig Tillerf40df232016-03-25 13:38:14 -07001115 cc_get_peer,
Mark D. Rothb2d24882016-10-27 15:44:07 -07001116 cc_get_channel_info,
Craig Tillerf40df232016-03-25 13:38:14 -07001117 "client-channel",
Craig Tiller87d5b192015-04-16 14:37:57 -07001118};
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001119
Mark D. Roth60534972016-09-20 08:37:12 -07001120void grpc_client_channel_finish_initialization(
Mark D. Roth0e48a9a2016-09-08 14:14:39 -07001121 grpc_exec_ctx *exec_ctx, grpc_channel_stack *channel_stack,
1122 grpc_resolver *resolver,
1123 grpc_client_channel_factory *client_channel_factory) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001124 /* post construction initialization: set the transport setup pointer */
Mark D. Roth7f7d1652016-09-20 10:46:15 -07001125 GPR_ASSERT(client_channel_factory != NULL);
Craig Tillera82950e2015-09-22 12:33:20 -07001126 grpc_channel_element *elem = grpc_channel_stack_last_element(channel_stack);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001127 channel_data *chand = elem->channel_data;
Mark D. Rothff4df062016-08-22 15:02:49 -07001128 gpr_mu_lock(&chand->mu);
Craig Tillera82950e2015-09-22 12:33:20 -07001129 GPR_ASSERT(!chand->resolver);
Craig Tillerf5f17122015-06-25 08:47:26 -07001130 chand->resolver = resolver;
Craig Tillera82950e2015-09-22 12:33:20 -07001131 GRPC_RESOLVER_REF(resolver, "channel");
1132 if (!grpc_closure_list_empty(chand->waiting_for_config_closures) ||
1133 chand->exit_idle_when_lb_policy_arrives) {
Mark D. Roth4c0fe492016-08-31 13:51:55 -07001134 chand->started_resolving = true;
Craig Tiller906e3bc2015-11-24 07:31:31 -08001135 GRPC_CHANNEL_STACK_REF(chand->owning_stack, "resolver");
Mark D. Roth046cf762016-09-26 11:13:51 -07001136 grpc_resolver_next(exec_ctx, resolver, &chand->resolver_result,
Mark D. Rothff4df062016-08-22 15:02:49 -07001137 &chand->on_resolver_result_changed);
Craig Tillera82950e2015-09-22 12:33:20 -07001138 }
Mark D. Roth0e48a9a2016-09-08 14:14:39 -07001139 chand->client_channel_factory = client_channel_factory;
1140 grpc_client_channel_factory_ref(client_channel_factory);
Mark D. Rothff4df062016-08-22 15:02:49 -07001141 gpr_mu_unlock(&chand->mu);
Craig Tiller190d3602015-02-18 09:23:38 -08001142}
Craig Tiller48cb07c2015-07-15 16:16:15 -07001143
Craig Tillera82950e2015-09-22 12:33:20 -07001144grpc_connectivity_state grpc_client_channel_check_connectivity_state(
1145 grpc_exec_ctx *exec_ctx, grpc_channel_element *elem, int try_to_connect) {
Craig Tiller48cb07c2015-07-15 16:16:15 -07001146 channel_data *chand = elem->channel_data;
1147 grpc_connectivity_state out;
Mark D. Rothff4df062016-08-22 15:02:49 -07001148 gpr_mu_lock(&chand->mu);
Craig Tiller804ff712016-05-05 16:25:40 -07001149 out = grpc_connectivity_state_check(&chand->state_tracker, NULL);
Craig Tillera82950e2015-09-22 12:33:20 -07001150 if (out == GRPC_CHANNEL_IDLE && try_to_connect) {
1151 if (chand->lb_policy != NULL) {
1152 grpc_lb_policy_exit_idle(exec_ctx, chand->lb_policy);
1153 } else {
Mark D. Roth4c0fe492016-08-31 13:51:55 -07001154 chand->exit_idle_when_lb_policy_arrives = true;
Craig Tillera82950e2015-09-22 12:33:20 -07001155 if (!chand->started_resolving && chand->resolver != NULL) {
Craig Tiller906e3bc2015-11-24 07:31:31 -08001156 GRPC_CHANNEL_STACK_REF(chand->owning_stack, "resolver");
Mark D. Roth4c0fe492016-08-31 13:51:55 -07001157 chand->started_resolving = true;
Mark D. Roth046cf762016-09-26 11:13:51 -07001158 grpc_resolver_next(exec_ctx, chand->resolver, &chand->resolver_result,
Mark D. Rothff4df062016-08-22 15:02:49 -07001159 &chand->on_resolver_result_changed);
Craig Tillera82950e2015-09-22 12:33:20 -07001160 }
Craig Tiller48cb07c2015-07-15 16:16:15 -07001161 }
Craig Tillera82950e2015-09-22 12:33:20 -07001162 }
Mark D. Rothff4df062016-08-22 15:02:49 -07001163 gpr_mu_unlock(&chand->mu);
Craig Tiller48cb07c2015-07-15 16:16:15 -07001164 return out;
1165}
1166
Craig Tiller86c99582015-11-25 15:22:26 -08001167typedef struct {
1168 channel_data *chand;
1169 grpc_pollset *pollset;
1170 grpc_closure *on_complete;
1171 grpc_closure my_closure;
1172} external_connectivity_watcher;
1173
Craig Tiller1d881fb2015-12-01 07:39:04 -08001174static void on_external_watch_complete(grpc_exec_ctx *exec_ctx, void *arg,
Craig Tiller804ff712016-05-05 16:25:40 -07001175 grpc_error *error) {
Craig Tiller86c99582015-11-25 15:22:26 -08001176 external_connectivity_watcher *w = arg;
1177 grpc_closure *follow_up = w->on_complete;
Craig Tiller69b093b2016-02-25 19:04:07 -08001178 grpc_pollset_set_del_pollset(exec_ctx, w->chand->interested_parties,
Craig Tiller1d881fb2015-12-01 07:39:04 -08001179 w->pollset);
1180 GRPC_CHANNEL_STACK_UNREF(exec_ctx, w->chand->owning_stack,
1181 "external_connectivity_watcher");
Craig Tiller86c99582015-11-25 15:22:26 -08001182 gpr_free(w);
Craig Tiller804ff712016-05-05 16:25:40 -07001183 follow_up->cb(exec_ctx, follow_up->cb_arg, error);
Craig Tiller86c99582015-11-25 15:22:26 -08001184}
1185
Craig Tillera82950e2015-09-22 12:33:20 -07001186void grpc_client_channel_watch_connectivity_state(
Craig Tiller906e3bc2015-11-24 07:31:31 -08001187 grpc_exec_ctx *exec_ctx, grpc_channel_element *elem, grpc_pollset *pollset,
Craig Tillera82950e2015-09-22 12:33:20 -07001188 grpc_connectivity_state *state, grpc_closure *on_complete) {
Craig Tiller48cb07c2015-07-15 16:16:15 -07001189 channel_data *chand = elem->channel_data;
Craig Tiller86c99582015-11-25 15:22:26 -08001190 external_connectivity_watcher *w = gpr_malloc(sizeof(*w));
1191 w->chand = chand;
1192 w->pollset = pollset;
1193 w->on_complete = on_complete;
Craig Tiller69b093b2016-02-25 19:04:07 -08001194 grpc_pollset_set_add_pollset(exec_ctx, chand->interested_parties, pollset);
Craig Tiller86c99582015-11-25 15:22:26 -08001195 grpc_closure_init(&w->my_closure, on_external_watch_complete, w);
Craig Tiller1d881fb2015-12-01 07:39:04 -08001196 GRPC_CHANNEL_STACK_REF(w->chand->owning_stack,
1197 "external_connectivity_watcher");
Mark D. Rothff4df062016-08-22 15:02:49 -07001198 gpr_mu_lock(&chand->mu);
Craig Tillera82950e2015-09-22 12:33:20 -07001199 grpc_connectivity_state_notify_on_state_change(
Craig Tiller86c99582015-11-25 15:22:26 -08001200 exec_ctx, &chand->state_tracker, state, &w->my_closure);
Mark D. Rothff4df062016-08-22 15:02:49 -07001201 gpr_mu_unlock(&chand->mu);
Craig Tiller48cb07c2015-07-15 16:16:15 -07001202}