blob: e5f4a8a813c7944d8d71b4394b9d5bd898dc03e0 [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003 * Copyright 2015 gRPC authors.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08004 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02005 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08008 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009 * http://www.apache.org/licenses/LICENSE-2.0
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080010 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +020011 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080016 *
17 */
18
Craig Tiller9eb0fde2017-03-31 16:59:30 -070019#include "src/core/ext/filters/client_channel/client_channel.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080020
Mark D. Roth4c0fe492016-08-31 13:51:55 -070021#include <stdbool.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080022#include <stdio.h>
Craig Tillereb3b12e2015-06-26 14:42:49 -070023#include <string.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080024
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080025#include <grpc/support/alloc.h>
26#include <grpc/support/log.h>
Mark D. Rothb2d24882016-10-27 15:44:07 -070027#include <grpc/support/string_util.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080028#include <grpc/support/sync.h>
29#include <grpc/support/useful.h>
30
Craig Tiller9eb0fde2017-03-31 16:59:30 -070031#include "src/core/ext/filters/client_channel/http_connect_handshaker.h"
32#include "src/core/ext/filters/client_channel/lb_policy_registry.h"
33#include "src/core/ext/filters/client_channel/proxy_mapper_registry.h"
34#include "src/core/ext/filters/client_channel/resolver_registry.h"
35#include "src/core/ext/filters/client_channel/retry_throttle.h"
36#include "src/core/ext/filters/client_channel/subchannel.h"
Craig Tiller3be7dd02017-04-03 14:30:03 -070037#include "src/core/ext/filters/deadline/deadline_filter.h"
Craig Tiller9533d042016-03-25 17:11:06 -070038#include "src/core/lib/channel/channel_args.h"
39#include "src/core/lib/channel/connected_channel.h"
Craig Tillerbefafe62017-02-09 11:30:54 -080040#include "src/core/lib/iomgr/combiner.h"
Craig Tiller9533d042016-03-25 17:11:06 -070041#include "src/core/lib/iomgr/iomgr.h"
Mark D. Roth4c0fe492016-08-31 13:51:55 -070042#include "src/core/lib/iomgr/polling_entity.h"
Craig Tiller9533d042016-03-25 17:11:06 -070043#include "src/core/lib/profiling/timers.h"
Craig Tiller7c70b6c2017-01-23 07:48:42 -080044#include "src/core/lib/slice/slice_internal.h"
Craig Tiller9533d042016-03-25 17:11:06 -070045#include "src/core/lib/support/string.h"
46#include "src/core/lib/surface/channel.h"
47#include "src/core/lib/transport/connectivity_state.h"
Mark D. Roth9fe284e2016-09-12 11:22:27 -070048#include "src/core/lib/transport/metadata.h"
49#include "src/core/lib/transport/metadata_batch.h"
Mark D. Rothea846a02016-11-03 11:32:54 -070050#include "src/core/lib/transport/service_config.h"
Mark D. Roth9fe284e2016-09-12 11:22:27 -070051#include "src/core/lib/transport/static_metadata.h"
Craig Tiller8910ac62015-10-08 16:49:15 -070052
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080053/* Client channel implementation */
54
ncteisen7712c7c2017-07-12 23:11:27 -070055grpc_tracer_flag grpc_client_channel_trace =
56 GRPC_TRACER_INITIALIZER(false, "client_channel");
Mark D. Roth60751fe2017-07-07 12:50:33 -070057
Mark D. Roth26b7be42016-10-24 10:08:07 -070058/*************************************************************************
59 * METHOD-CONFIG TABLE
60 */
61
Mark D. Roth9d480942016-10-19 14:18:05 -070062typedef enum {
Craig Tiller7acc37e2017-02-28 10:01:37 -080063 /* zero so it can be default initialized */
64 WAIT_FOR_READY_UNSET = 0,
Mark D. Roth9d480942016-10-19 14:18:05 -070065 WAIT_FOR_READY_FALSE,
66 WAIT_FOR_READY_TRUE
67} wait_for_ready_value;
68
Mark D. Roth95b627b2017-02-24 11:02:58 -080069typedef struct {
70 gpr_refcount refs;
Mark D. Roth9d480942016-10-19 14:18:05 -070071 gpr_timespec timeout;
72 wait_for_ready_value wait_for_ready;
73} method_parameters;
74
Mark D. Roth722de8d2017-02-27 10:50:44 -080075static method_parameters *method_parameters_ref(
Mark D. Roth95b627b2017-02-24 11:02:58 -080076 method_parameters *method_params) {
77 gpr_ref(&method_params->refs);
78 return method_params;
Mark D. Roth9d480942016-10-19 14:18:05 -070079}
80
Mark D. Roth95b627b2017-02-24 11:02:58 -080081static void method_parameters_unref(method_parameters *method_params) {
82 if (gpr_unref(&method_params->refs)) {
83 gpr_free(method_params);
84 }
85}
86
Mark D. Roth95b627b2017-02-24 11:02:58 -080087static void method_parameters_free(grpc_exec_ctx *exec_ctx, void *value) {
88 method_parameters_unref(value);
Craig Tiller87a7e1f2016-11-09 09:42:19 -080089}
90
Mark D. Roth95b627b2017-02-24 11:02:58 -080091static bool parse_wait_for_ready(grpc_json *field,
92 wait_for_ready_value *wait_for_ready) {
93 if (field->type != GRPC_JSON_TRUE && field->type != GRPC_JSON_FALSE) {
94 return false;
95 }
96 *wait_for_ready = field->type == GRPC_JSON_TRUE ? WAIT_FOR_READY_TRUE
97 : WAIT_FOR_READY_FALSE;
98 return true;
99}
100
Mark D. Roth722de8d2017-02-27 10:50:44 -0800101static bool parse_timeout(grpc_json *field, gpr_timespec *timeout) {
Mark D. Roth95b627b2017-02-24 11:02:58 -0800102 if (field->type != GRPC_JSON_STRING) return false;
103 size_t len = strlen(field->value);
104 if (field->value[len - 1] != 's') return false;
105 char *buf = gpr_strdup(field->value);
106 buf[len - 1] = '\0'; // Remove trailing 's'.
107 char *decimal_point = strchr(buf, '.');
108 if (decimal_point != NULL) {
109 *decimal_point = '\0';
110 timeout->tv_nsec = gpr_parse_nonnegative_int(decimal_point + 1);
111 if (timeout->tv_nsec == -1) {
112 gpr_free(buf);
113 return false;
114 }
115 // There should always be exactly 3, 6, or 9 fractional digits.
116 int multiplier = 1;
117 switch (strlen(decimal_point + 1)) {
118 case 9:
119 break;
120 case 6:
121 multiplier *= 1000;
122 break;
123 case 3:
124 multiplier *= 1000000;
125 break;
126 default: // Unsupported number of digits.
127 gpr_free(buf);
128 return false;
129 }
130 timeout->tv_nsec *= multiplier;
131 }
132 timeout->tv_sec = gpr_parse_nonnegative_int(buf);
133 gpr_free(buf);
134 if (timeout->tv_sec == -1) return false;
135 return true;
136}
137
Mark D. Rothe30baeb2016-11-03 08:16:19 -0700138static void *method_parameters_create_from_json(const grpc_json *json) {
Mark D. Rothc968e602016-11-02 14:07:36 -0700139 wait_for_ready_value wait_for_ready = WAIT_FOR_READY_UNSET;
Mark D. Roth47f10842016-11-03 08:45:27 -0700140 gpr_timespec timeout = {0, 0, GPR_TIMESPAN};
141 for (grpc_json *field = json->child; field != NULL; field = field->next) {
Mark D. Rothc968e602016-11-02 14:07:36 -0700142 if (field->key == NULL) continue;
Mark D. Roth84c8a022016-11-10 09:39:34 -0800143 if (strcmp(field->key, "waitForReady") == 0) {
Mark D. Rothc968e602016-11-02 14:07:36 -0700144 if (wait_for_ready != WAIT_FOR_READY_UNSET) return NULL; // Duplicate.
Mark D. Roth95b627b2017-02-24 11:02:58 -0800145 if (!parse_wait_for_ready(field, &wait_for_ready)) return NULL;
Mark D. Rothc968e602016-11-02 14:07:36 -0700146 } else if (strcmp(field->key, "timeout") == 0) {
147 if (timeout.tv_sec > 0 || timeout.tv_nsec > 0) return NULL; // Duplicate.
Mark D. Roth95b627b2017-02-24 11:02:58 -0800148 if (!parse_timeout(field, &timeout)) return NULL;
Mark D. Rothc968e602016-11-02 14:07:36 -0700149 }
150 }
Yash Tibrewalca3c1c02017-09-07 22:47:16 -0700151 method_parameters *value =
152 (method_parameters *)gpr_malloc(sizeof(method_parameters));
Mark D. Roth95b627b2017-02-24 11:02:58 -0800153 gpr_ref_init(&value->refs, 1);
Mark D. Rothc968e602016-11-02 14:07:36 -0700154 value->timeout = timeout;
155 value->wait_for_ready = wait_for_ready;
Mark D. Roth9d480942016-10-19 14:18:05 -0700156 return value;
157}
158
Alexander Polcync3b1f182017-04-18 13:51:36 -0700159struct external_connectivity_watcher;
160
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700161/*************************************************************************
162 * CHANNEL-WIDE FUNCTIONS
163 */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800164
Craig Tiller800dacb2015-10-06 09:10:26 -0700165typedef struct client_channel_channel_data {
Craig Tillerf5f17122015-06-25 08:47:26 -0700166 /** resolver for this channel */
167 grpc_resolver *resolver;
Craig Tiller20a3c352015-08-05 08:39:50 -0700168 /** have we started resolving this channel */
Mark D. Roth4c0fe492016-08-31 13:51:55 -0700169 bool started_resolving;
Craig Tiller3be7dd02017-04-03 14:30:03 -0700170 /** is deadline checking enabled? */
171 bool deadline_checking_enabled;
Mark D. Roth0e48a9a2016-09-08 14:14:39 -0700172 /** client channel factory */
173 grpc_client_channel_factory *client_channel_factory;
Craig Tillerf5f17122015-06-25 08:47:26 -0700174
Craig Tillerbefafe62017-02-09 11:30:54 -0800175 /** combiner protecting all variables below in this data structure */
176 grpc_combiner *combiner;
Mark D. Roth046cf762016-09-26 11:13:51 -0700177 /** currently active load balancer */
Craig Tillerf5f17122015-06-25 08:47:26 -0700178 grpc_lb_policy *lb_policy;
Mark D. Rothd6d192d2017-02-23 08:58:42 -0800179 /** retry throttle data */
180 grpc_server_retry_throttle_data *retry_throttle_data;
Mark D. Roth9d480942016-10-19 14:18:05 -0700181 /** maps method names to method_parameters structs */
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800182 grpc_slice_hash_table *method_params_table;
Mark D. Roth046cf762016-09-26 11:13:51 -0700183 /** incoming resolver result - set by resolver.next() */
Mark D. Rothaf842452016-10-21 15:05:15 -0700184 grpc_channel_args *resolver_result;
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700185 /** a list of closures that are all waiting for resolver result to come in */
186 grpc_closure_list waiting_for_resolver_result_closures;
Craig Tiller3f475422015-06-25 10:43:05 -0700187 /** resolver callback */
Mark D. Rothff4df062016-08-22 15:02:49 -0700188 grpc_closure on_resolver_result_changed;
Craig Tiller3f475422015-06-25 10:43:05 -0700189 /** connectivity state being tracked */
Craig Tillerca3e9d32015-06-27 18:37:27 -0700190 grpc_connectivity_state_tracker state_tracker;
Craig Tiller48cb07c2015-07-15 16:16:15 -0700191 /** when an lb_policy arrives, should we try to exit idle */
Mark D. Roth4c0fe492016-08-31 13:51:55 -0700192 bool exit_idle_when_lb_policy_arrives;
Craig Tiller906e3bc2015-11-24 07:31:31 -0800193 /** owning stack */
194 grpc_channel_stack *owning_stack;
Craig Tiller69b093b2016-02-25 19:04:07 -0800195 /** interested parties (owned) */
196 grpc_pollset_set *interested_parties;
Craig Tiller613dafa2017-02-09 12:00:43 -0800197
Alexander Polcync3b1f182017-04-18 13:51:36 -0700198 /* external_connectivity_watcher_list head is guarded by its own mutex, since
199 * counts need to be grabbed immediately without polling on a cq */
200 gpr_mu external_connectivity_watcher_list_mu;
201 struct external_connectivity_watcher *external_connectivity_watcher_list_head;
202
Craig Tiller613dafa2017-02-09 12:00:43 -0800203 /* the following properties are guarded by a mutex since API's require them
Craig Tiller46dd7902017-02-23 09:42:16 -0800204 to be instantaneously available */
Craig Tiller613dafa2017-02-09 12:00:43 -0800205 gpr_mu info_mu;
206 char *info_lb_policy_name;
207 /** service config in JSON form */
208 char *info_service_config_json;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800209} channel_data;
210
Craig Tillerd6c98df2015-08-18 09:33:44 -0700211/** We create one watcher for each new lb_policy that is returned from a
Mark D. Roth4c0fe492016-08-31 13:51:55 -0700212 resolver, to watch for state changes from the lb_policy. When a state
213 change is seen, we update the channel, and create a new watcher. */
Craig Tillera82950e2015-09-22 12:33:20 -0700214typedef struct {
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700215 channel_data *chand;
Craig Tiller33825112015-09-18 07:44:19 -0700216 grpc_closure on_changed;
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700217 grpc_connectivity_state state;
218 grpc_lb_policy *lb_policy;
219} lb_policy_connectivity_watcher;
220
Craig Tiller2400bf52017-02-09 16:25:19 -0800221static void watch_lb_policy_locked(grpc_exec_ctx *exec_ctx, channel_data *chand,
222 grpc_lb_policy *lb_policy,
223 grpc_connectivity_state current_state);
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700224
Craig Tiller8c0d96f2016-03-11 14:27:52 -0800225static void set_channel_connectivity_state_locked(grpc_exec_ctx *exec_ctx,
226 channel_data *chand,
227 grpc_connectivity_state state,
Craig Tiller804ff712016-05-05 16:25:40 -0700228 grpc_error *error,
Craig Tiller8c0d96f2016-03-11 14:27:52 -0800229 const char *reason) {
David Garcia Quintas37251282017-04-14 13:46:03 -0700230 /* TODO: Improve failure handling:
231 * - Make it possible for policies to return GRPC_CHANNEL_TRANSIENT_FAILURE.
232 * - Hand over pending picks from old policies during the switch that happens
233 * when resolver provides an update. */
David Garcia Quintas956f7002017-04-13 15:40:06 -0700234 if (chand->lb_policy != NULL) {
235 if (state == GRPC_CHANNEL_TRANSIENT_FAILURE) {
236 /* cancel picks with wait_for_ready=false */
237 grpc_lb_policy_cancel_picks_locked(
238 exec_ctx, chand->lb_policy,
239 /* mask= */ GRPC_INITIAL_METADATA_WAIT_FOR_READY,
240 /* check= */ 0, GRPC_ERROR_REF(error));
241 } else if (state == GRPC_CHANNEL_SHUTDOWN) {
242 /* cancel all picks */
243 grpc_lb_policy_cancel_picks_locked(exec_ctx, chand->lb_policy,
244 /* mask= */ 0, /* check= */ 0,
245 GRPC_ERROR_REF(error));
246 }
Craig Tiller8c0d96f2016-03-11 14:27:52 -0800247 }
Mark D. Roth60751fe2017-07-07 12:50:33 -0700248 if (GRPC_TRACER_ON(grpc_client_channel_trace)) {
249 gpr_log(GPR_DEBUG, "chand=%p: setting connectivity state to %s", chand,
250 grpc_connectivity_state_name(state));
251 }
Craig Tiller9ccf5f12016-05-07 21:41:01 -0700252 grpc_connectivity_state_set(exec_ctx, &chand->state_tracker, state, error,
253 reason);
Craig Tiller8c0d96f2016-03-11 14:27:52 -0800254}
255
Craig Tiller804ff712016-05-05 16:25:40 -0700256static void on_lb_policy_state_changed_locked(grpc_exec_ctx *exec_ctx,
Craig Tillerbefafe62017-02-09 11:30:54 -0800257 void *arg, grpc_error *error) {
Yash Tibrewalca3c1c02017-09-07 22:47:16 -0700258 lb_policy_connectivity_watcher *w = (lb_policy_connectivity_watcher *)arg;
Craig Tillercb2609f2015-11-24 17:19:19 -0800259 grpc_connectivity_state publish_state = w->state;
Craig Tillerc5de8352017-02-09 14:08:05 -0800260 /* check if the notification is for the latest policy */
261 if (w->lb_policy == w->chand->lb_policy) {
Mark D. Roth60751fe2017-07-07 12:50:33 -0700262 if (GRPC_TRACER_ON(grpc_client_channel_trace)) {
263 gpr_log(GPR_DEBUG, "chand=%p: lb_policy=%p state changed to %s", w->chand,
264 w->lb_policy, grpc_connectivity_state_name(w->state));
265 }
Craig Tillerc5de8352017-02-09 14:08:05 -0800266 if (publish_state == GRPC_CHANNEL_SHUTDOWN && w->chand->resolver != NULL) {
267 publish_state = GRPC_CHANNEL_TRANSIENT_FAILURE;
Craig Tiller972470b2017-02-09 15:05:36 -0800268 grpc_resolver_channel_saw_error_locked(exec_ctx, w->chand->resolver);
Craig Tillerc5de8352017-02-09 14:08:05 -0800269 GRPC_LB_POLICY_UNREF(exec_ctx, w->chand->lb_policy, "channel");
270 w->chand->lb_policy = NULL;
271 }
272 set_channel_connectivity_state_locked(exec_ctx, w->chand, publish_state,
273 GRPC_ERROR_REF(error), "lb_changed");
274 if (w->state != GRPC_CHANNEL_SHUTDOWN) {
Craig Tiller2400bf52017-02-09 16:25:19 -0800275 watch_lb_policy_locked(exec_ctx, w->chand, w->lb_policy, w->state);
Craig Tillerc5de8352017-02-09 14:08:05 -0800276 }
Craig Tillera82950e2015-09-22 12:33:20 -0700277 }
Craig Tiller906e3bc2015-11-24 07:31:31 -0800278 GRPC_CHANNEL_STACK_UNREF(exec_ctx, w->chand->owning_stack, "watch_lb_policy");
Craig Tillera82950e2015-09-22 12:33:20 -0700279 gpr_free(w);
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700280}
281
Craig Tiller2400bf52017-02-09 16:25:19 -0800282static void watch_lb_policy_locked(grpc_exec_ctx *exec_ctx, channel_data *chand,
283 grpc_lb_policy *lb_policy,
284 grpc_connectivity_state current_state) {
Yash Tibrewalca3c1c02017-09-07 22:47:16 -0700285 lb_policy_connectivity_watcher *w =
286 (lb_policy_connectivity_watcher *)gpr_malloc(sizeof(*w));
Craig Tiller906e3bc2015-11-24 07:31:31 -0800287 GRPC_CHANNEL_STACK_REF(chand->owning_stack, "watch_lb_policy");
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700288 w->chand = chand;
ncteisen274bbbe2017-06-08 14:57:11 -0700289 GRPC_CLOSURE_INIT(&w->on_changed, on_lb_policy_state_changed_locked, w,
Craig Tilleree4b1452017-05-12 10:56:03 -0700290 grpc_combiner_scheduler(chand->combiner));
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700291 w->state = current_state;
292 w->lb_policy = lb_policy;
Craig Tiller2400bf52017-02-09 16:25:19 -0800293 grpc_lb_policy_notify_on_state_change_locked(exec_ctx, lb_policy, &w->state,
294 &w->on_changed);
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700295}
296
Mark D. Roth60751fe2017-07-07 12:50:33 -0700297static void start_resolving_locked(grpc_exec_ctx *exec_ctx,
298 channel_data *chand) {
299 if (GRPC_TRACER_ON(grpc_client_channel_trace)) {
300 gpr_log(GPR_DEBUG, "chand=%p: starting name resolution", chand);
301 }
302 GPR_ASSERT(!chand->started_resolving);
303 chand->started_resolving = true;
304 GRPC_CHANNEL_STACK_REF(chand->owning_stack, "resolver");
305 grpc_resolver_next_locked(exec_ctx, chand->resolver, &chand->resolver_result,
306 &chand->on_resolver_result_changed);
307}
308
Mark D. Rothd6d192d2017-02-23 08:58:42 -0800309typedef struct {
310 char *server_name;
311 grpc_server_retry_throttle_data *retry_throttle_data;
312} service_config_parsing_state;
313
314static void parse_retry_throttle_params(const grpc_json *field, void *arg) {
Yash Tibrewalca3c1c02017-09-07 22:47:16 -0700315 service_config_parsing_state *parsing_state =
316 (service_config_parsing_state *)arg;
Mark D. Rothd6d192d2017-02-23 08:58:42 -0800317 if (strcmp(field->key, "retryThrottling") == 0) {
318 if (parsing_state->retry_throttle_data != NULL) return; // Duplicate.
319 if (field->type != GRPC_JSON_OBJECT) return;
320 int max_milli_tokens = 0;
321 int milli_token_ratio = 0;
322 for (grpc_json *sub_field = field->child; sub_field != NULL;
323 sub_field = sub_field->next) {
Mark D. Rothb3322562017-02-23 14:38:02 -0800324 if (sub_field->key == NULL) return;
Mark D. Rothd6d192d2017-02-23 08:58:42 -0800325 if (strcmp(sub_field->key, "maxTokens") == 0) {
326 if (max_milli_tokens != 0) return; // Duplicate.
327 if (sub_field->type != GRPC_JSON_NUMBER) return;
328 max_milli_tokens = gpr_parse_nonnegative_int(sub_field->value);
329 if (max_milli_tokens == -1) return;
330 max_milli_tokens *= 1000;
331 } else if (strcmp(sub_field->key, "tokenRatio") == 0) {
332 if (milli_token_ratio != 0) return; // Duplicate.
333 if (sub_field->type != GRPC_JSON_NUMBER) return;
334 // We support up to 3 decimal digits.
335 size_t whole_len = strlen(sub_field->value);
336 uint32_t multiplier = 1;
337 uint32_t decimal_value = 0;
338 const char *decimal_point = strchr(sub_field->value, '.');
339 if (decimal_point != NULL) {
340 whole_len = (size_t)(decimal_point - sub_field->value);
341 multiplier = 1000;
342 size_t decimal_len = strlen(decimal_point + 1);
343 if (decimal_len > 3) decimal_len = 3;
344 if (!gpr_parse_bytes_to_uint32(decimal_point + 1, decimal_len,
345 &decimal_value)) {
346 return;
347 }
348 uint32_t decimal_multiplier = 1;
349 for (size_t i = 0; i < (3 - decimal_len); ++i) {
350 decimal_multiplier *= 10;
351 }
352 decimal_value *= decimal_multiplier;
353 }
354 uint32_t whole_value;
355 if (!gpr_parse_bytes_to_uint32(sub_field->value, whole_len,
356 &whole_value)) {
357 return;
358 }
359 milli_token_ratio = (int)((whole_value * multiplier) + decimal_value);
Mark D. Rothb3322562017-02-23 14:38:02 -0800360 if (milli_token_ratio <= 0) return;
Mark D. Rothd6d192d2017-02-23 08:58:42 -0800361 }
362 }
363 parsing_state->retry_throttle_data =
364 grpc_retry_throttle_map_get_data_for_server(
365 parsing_state->server_name, max_milli_tokens, milli_token_ratio);
366 }
367}
368
Craig Tillerbefafe62017-02-09 11:30:54 -0800369static void on_resolver_result_changed_locked(grpc_exec_ctx *exec_ctx,
370 void *arg, grpc_error *error) {
Yash Tibrewalca3c1c02017-09-07 22:47:16 -0700371 channel_data *chand = (channel_data *)arg;
Mark D. Roth60751fe2017-07-07 12:50:33 -0700372 if (GRPC_TRACER_ON(grpc_client_channel_trace)) {
373 gpr_log(GPR_DEBUG, "chand=%p: got resolver result: error=%s", chand,
374 grpc_error_string(error));
375 }
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700376 // Extract the following fields from the resolver result, if non-NULL.
Mark D. Roth15494b52017-07-12 15:26:55 -0700377 bool lb_policy_updated = false;
Mark D. Rothb2d24882016-10-27 15:44:07 -0700378 char *lb_policy_name = NULL;
Mark D. Roth60751fe2017-07-07 12:50:33 -0700379 bool lb_policy_name_changed = false;
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700380 grpc_lb_policy *new_lb_policy = NULL;
Mark D. Rothc625c7a2016-11-09 14:12:37 -0800381 char *service_config_json = NULL;
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700382 grpc_server_retry_throttle_data *retry_throttle_data = NULL;
383 grpc_slice_hash_table *method_params_table = NULL;
Mark D. Roth046cf762016-09-26 11:13:51 -0700384 if (chand->resolver_result != NULL) {
Mark D. Roth5bd7be02016-10-21 14:19:50 -0700385 // Find LB policy name.
Mark D. Rothaf842452016-10-21 15:05:15 -0700386 const grpc_arg *channel_arg =
Mark D. Roth41124992016-11-03 11:22:20 -0700387 grpc_channel_args_find(chand->resolver_result, GRPC_ARG_LB_POLICY_NAME);
Mark D. Rothaf842452016-10-21 15:05:15 -0700388 if (channel_arg != NULL) {
389 GPR_ASSERT(channel_arg->type == GRPC_ARG_STRING);
390 lb_policy_name = channel_arg->value.string;
Mark D. Roth5bd7be02016-10-21 14:19:50 -0700391 }
Mark D. Roth1eb96dc2017-03-22 12:19:03 -0700392 // Special case: If at least one balancer address is present, we use
393 // the grpclb policy, regardless of what the resolver actually specified.
Mark D. Rothaf842452016-10-21 15:05:15 -0700394 channel_arg =
Mark D. Roth41124992016-11-03 11:22:20 -0700395 grpc_channel_args_find(chand->resolver_result, GRPC_ARG_LB_ADDRESSES);
David Garcia Quintas143cb772017-03-31 13:39:27 -0700396 if (channel_arg != NULL && channel_arg->type == GRPC_ARG_POINTER) {
Yash Tibrewalca3c1c02017-09-07 22:47:16 -0700397 grpc_lb_addresses *addresses =
398 (grpc_lb_addresses *)channel_arg->value.pointer.p;
Mark D. Roth1eb96dc2017-03-22 12:19:03 -0700399 bool found_balancer_address = false;
Mark D. Rothaf842452016-10-21 15:05:15 -0700400 for (size_t i = 0; i < addresses->num_addresses; ++i) {
Mark D. Roth1eb96dc2017-03-22 12:19:03 -0700401 if (addresses->addresses[i].is_balancer) {
402 found_balancer_address = true;
Mark D. Rothaf842452016-10-21 15:05:15 -0700403 break;
404 }
Mark D. Roth88405f72016-10-03 08:24:52 -0700405 }
Mark D. Roth1eb96dc2017-03-22 12:19:03 -0700406 if (found_balancer_address) {
Mark D. Rothaf842452016-10-21 15:05:15 -0700407 if (lb_policy_name != NULL && strcmp(lb_policy_name, "grpclb") != 0) {
408 gpr_log(GPR_INFO,
Mark D. Roth1eb96dc2017-03-22 12:19:03 -0700409 "resolver requested LB policy %s but provided at least one "
410 "balancer address -- forcing use of grpclb LB policy",
Mark D. Roth5f40e5d2016-10-24 13:09:05 -0700411 lb_policy_name);
Mark D. Rothaf842452016-10-21 15:05:15 -0700412 }
413 lb_policy_name = "grpclb";
Mark D. Roth88405f72016-10-03 08:24:52 -0700414 }
Mark D. Roth88405f72016-10-03 08:24:52 -0700415 }
416 // Use pick_first if nothing was specified and we didn't select grpclb
417 // above.
418 if (lb_policy_name == NULL) lb_policy_name = "pick_first";
Mark D. Roth41124992016-11-03 11:22:20 -0700419 grpc_lb_policy_args lb_policy_args;
420 lb_policy_args.args = chand->resolver_result;
421 lb_policy_args.client_channel_factory = chand->client_channel_factory;
Craig Tiller2400bf52017-02-09 16:25:19 -0800422 lb_policy_args.combiner = chand->combiner;
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700423 // Check to see if we're already using the right LB policy.
424 // Note: It's safe to use chand->info_lb_policy_name here without
425 // taking a lock on chand->info_mu, because this function is the
426 // only thing that modifies its value, and it can only be invoked
427 // once at any given time.
Mark D. Roth60751fe2017-07-07 12:50:33 -0700428 lb_policy_name_changed =
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700429 chand->info_lb_policy_name == NULL ||
430 strcmp(chand->info_lb_policy_name, lb_policy_name) != 0;
Mark D. Roth60751fe2017-07-07 12:50:33 -0700431 if (chand->lb_policy != NULL && !lb_policy_name_changed) {
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700432 // Continue using the same LB policy. Update with new addresses.
Mark D. Roth15494b52017-07-12 15:26:55 -0700433 lb_policy_updated = true;
David Garcia Quintas87d5a312017-06-06 19:45:58 -0700434 grpc_lb_policy_update_locked(exec_ctx, chand->lb_policy, &lb_policy_args);
435 } else {
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700436 // Instantiate new LB policy.
437 new_lb_policy =
David Garcia Quintas87d5a312017-06-06 19:45:58 -0700438 grpc_lb_policy_create(exec_ctx, lb_policy_name, &lb_policy_args);
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700439 if (new_lb_policy == NULL) {
440 gpr_log(GPR_ERROR, "could not create LB policy \"%s\"", lb_policy_name);
David Garcia Quintas87d5a312017-06-06 19:45:58 -0700441 }
Craig Tiller45724b32015-09-22 10:42:19 -0700442 }
Mark D. Roth41124992016-11-03 11:22:20 -0700443 // Find service config.
Mark D. Rothaf842452016-10-21 15:05:15 -0700444 channel_arg =
Mark D. Roth41124992016-11-03 11:22:20 -0700445 grpc_channel_args_find(chand->resolver_result, GRPC_ARG_SERVICE_CONFIG);
Mark D. Roth046cf762016-09-26 11:13:51 -0700446 if (channel_arg != NULL) {
Mark D. Roth9ec28af2016-11-03 12:32:39 -0700447 GPR_ASSERT(channel_arg->type == GRPC_ARG_STRING);
Mark D. Rothc625c7a2016-11-09 14:12:37 -0800448 service_config_json = gpr_strdup(channel_arg->value.string);
Mark D. Roth70a1abd2016-11-04 09:26:37 -0700449 grpc_service_config *service_config =
Mark D. Rothc625c7a2016-11-09 14:12:37 -0800450 grpc_service_config_create(service_config_json);
Mark D. Rothbdc58b22016-11-04 09:25:57 -0700451 if (service_config != NULL) {
Mark D. Rothd6d192d2017-02-23 08:58:42 -0800452 channel_arg =
453 grpc_channel_args_find(chand->resolver_result, GRPC_ARG_SERVER_URI);
454 GPR_ASSERT(channel_arg != NULL);
455 GPR_ASSERT(channel_arg->type == GRPC_ARG_STRING);
Mark D. Roth9ccbc4d2017-03-15 08:30:04 -0700456 grpc_uri *uri =
457 grpc_uri_parse(exec_ctx, channel_arg->value.string, true);
Mark D. Rothd6d192d2017-02-23 08:58:42 -0800458 GPR_ASSERT(uri->path[0] != '\0');
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700459 service_config_parsing_state parsing_state;
460 memset(&parsing_state, 0, sizeof(parsing_state));
Mark D. Rothd6d192d2017-02-23 08:58:42 -0800461 parsing_state.server_name =
462 uri->path[0] == '/' ? uri->path + 1 : uri->path;
463 grpc_service_config_parse_global_params(
464 service_config, parse_retry_throttle_params, &parsing_state);
Mark D. Rothd6d192d2017-02-23 08:58:42 -0800465 grpc_uri_destroy(uri);
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700466 retry_throttle_data = parsing_state.retry_throttle_data;
Mark D. Rothbdc58b22016-11-04 09:25:57 -0700467 method_params_table = grpc_service_config_create_method_config_table(
Craig Tillerb28c7e82016-11-18 10:29:04 -0800468 exec_ctx, service_config, method_parameters_create_from_json,
Mark D. Rothe3006702017-04-19 07:43:56 -0700469 method_parameters_free);
Mark D. Rothbdc58b22016-11-04 09:25:57 -0700470 grpc_service_config_destroy(service_config);
471 }
Mark D. Roth9fe284e2016-09-12 11:22:27 -0700472 }
Mark D. Rothf79ce7d2016-11-04 08:43:36 -0700473 // Before we clean up, save a copy of lb_policy_name, since it might
474 // be pointing to data inside chand->resolver_result.
475 // The copy will be saved in chand->lb_policy_name below.
476 lb_policy_name = gpr_strdup(lb_policy_name);
Craig Tiller87a7e1f2016-11-09 09:42:19 -0800477 grpc_channel_args_destroy(exec_ctx, chand->resolver_result);
Mark D. Roth046cf762016-09-26 11:13:51 -0700478 chand->resolver_result = NULL;
Craig Tillera82950e2015-09-22 12:33:20 -0700479 }
Mark D. Roth60751fe2017-07-07 12:50:33 -0700480 if (GRPC_TRACER_ON(grpc_client_channel_trace)) {
481 gpr_log(GPR_DEBUG,
482 "chand=%p: resolver result: lb_policy_name=\"%s\"%s, "
483 "service_config=\"%s\"",
484 chand, lb_policy_name, lb_policy_name_changed ? " (changed)" : "",
485 service_config_json);
486 }
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700487 // Now swap out fields in chand. Note that the new values may still
488 // be NULL if (e.g.) the resolver failed to return results or the
489 // results did not contain the necessary data.
490 //
491 // First, swap out the data used by cc_get_channel_info().
Craig Tiller613dafa2017-02-09 12:00:43 -0800492 gpr_mu_lock(&chand->info_mu);
Mark D. Rothb2d24882016-10-27 15:44:07 -0700493 if (lb_policy_name != NULL) {
Craig Tiller613dafa2017-02-09 12:00:43 -0800494 gpr_free(chand->info_lb_policy_name);
495 chand->info_lb_policy_name = lb_policy_name;
Mark D. Rothb2d24882016-10-27 15:44:07 -0700496 }
Mark D. Rothc625c7a2016-11-09 14:12:37 -0800497 if (service_config_json != NULL) {
Craig Tiller613dafa2017-02-09 12:00:43 -0800498 gpr_free(chand->info_service_config_json);
499 chand->info_service_config_json = service_config_json;
Mark D. Rothc625c7a2016-11-09 14:12:37 -0800500 }
Craig Tiller613dafa2017-02-09 12:00:43 -0800501 gpr_mu_unlock(&chand->info_mu);
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700502 // Swap out the retry throttle data.
Mark D. Rothd6d192d2017-02-23 08:58:42 -0800503 if (chand->retry_throttle_data != NULL) {
504 grpc_server_retry_throttle_data_unref(chand->retry_throttle_data);
505 }
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700506 chand->retry_throttle_data = retry_throttle_data;
507 // Swap out the method params table.
Mark D. Roth9d480942016-10-19 14:18:05 -0700508 if (chand->method_params_table != NULL) {
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800509 grpc_slice_hash_table_unref(exec_ctx, chand->method_params_table);
Mark D. Roth046cf762016-09-26 11:13:51 -0700510 }
Mark D. Roth9d480942016-10-19 14:18:05 -0700511 chand->method_params_table = method_params_table;
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700512 // If we have a new LB policy or are shutting down (in which case
513 // new_lb_policy will be NULL), swap out the LB policy, unreffing the
514 // old one and removing its fds from chand->interested_parties.
515 // Note that we do NOT do this if either (a) we updated the existing
516 // LB policy above or (b) we failed to create the new LB policy (in
517 // which case we want to continue using the most recent one we had).
518 if (new_lb_policy != NULL || error != GRPC_ERROR_NONE ||
519 chand->resolver == NULL) {
520 if (chand->lb_policy != NULL) {
Mark D. Roth60751fe2017-07-07 12:50:33 -0700521 if (GRPC_TRACER_ON(grpc_client_channel_trace)) {
522 gpr_log(GPR_DEBUG, "chand=%p: unreffing lb_policy=%p", chand,
523 chand->lb_policy);
524 }
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700525 grpc_pollset_set_del_pollset_set(exec_ctx,
526 chand->lb_policy->interested_parties,
527 chand->interested_parties);
528 GRPC_LB_POLICY_UNREF(exec_ctx, chand->lb_policy, "channel");
Craig Tiller45724b32015-09-22 10:42:19 -0700529 }
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700530 chand->lb_policy = new_lb_policy;
531 }
532 // Now that we've swapped out the relevant fields of chand, check for
533 // error or shutdown.
534 if (error != GRPC_ERROR_NONE || chand->resolver == NULL) {
Mark D. Roth60751fe2017-07-07 12:50:33 -0700535 if (GRPC_TRACER_ON(grpc_client_channel_trace)) {
536 gpr_log(GPR_DEBUG, "chand=%p: shutting down", chand);
537 }
Craig Tiller76a5c0e2016-03-09 09:05:30 -0800538 if (chand->resolver != NULL) {
Mark D. Roth60751fe2017-07-07 12:50:33 -0700539 if (GRPC_TRACER_ON(grpc_client_channel_trace)) {
540 gpr_log(GPR_DEBUG, "chand=%p: shutting down resolver", chand);
541 }
Craig Tiller972470b2017-02-09 15:05:36 -0800542 grpc_resolver_shutdown_locked(exec_ctx, chand->resolver);
Craig Tiller76a5c0e2016-03-09 09:05:30 -0800543 GRPC_RESOLVER_UNREF(exec_ctx, chand->resolver, "channel");
544 chand->resolver = NULL;
545 }
Craig Tiller8c0d96f2016-03-11 14:27:52 -0800546 set_channel_connectivity_state_locked(
Craig Tillerd925c932016-06-06 08:38:50 -0700547 exec_ctx, chand, GRPC_CHANNEL_SHUTDOWN,
ncteisen4b36a3d2017-03-13 19:08:06 -0700548 GRPC_ERROR_CREATE_REFERENCING_FROM_STATIC_STRING(
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700549 "Got resolver result after disconnection", &error, 1),
Craig Tiller804ff712016-05-05 16:25:40 -0700550 "resolver_gone");
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700551 GRPC_CHANNEL_STACK_UNREF(exec_ctx, chand->owning_stack, "resolver");
552 grpc_closure_list_fail_all(&chand->waiting_for_resolver_result_closures,
553 GRPC_ERROR_CREATE_REFERENCING_FROM_STATIC_STRING(
554 "Channel disconnected", &error, 1));
555 GRPC_CLOSURE_LIST_SCHED(exec_ctx,
556 &chand->waiting_for_resolver_result_closures);
557 } else { // Not shutting down.
558 grpc_connectivity_state state = GRPC_CHANNEL_TRANSIENT_FAILURE;
559 grpc_error *state_error =
560 GRPC_ERROR_CREATE_FROM_STATIC_STRING("No load balancing policy");
561 if (new_lb_policy != NULL) {
Mark D. Roth60751fe2017-07-07 12:50:33 -0700562 if (GRPC_TRACER_ON(grpc_client_channel_trace)) {
563 gpr_log(GPR_DEBUG, "chand=%p: initializing new LB policy", chand);
564 }
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700565 GRPC_ERROR_UNREF(state_error);
566 state = grpc_lb_policy_check_connectivity_locked(exec_ctx, new_lb_policy,
567 &state_error);
568 grpc_pollset_set_add_pollset_set(exec_ctx,
569 new_lb_policy->interested_parties,
570 chand->interested_parties);
571 GRPC_CLOSURE_LIST_SCHED(exec_ctx,
572 &chand->waiting_for_resolver_result_closures);
573 if (chand->exit_idle_when_lb_policy_arrives) {
574 grpc_lb_policy_exit_idle_locked(exec_ctx, new_lb_policy);
575 chand->exit_idle_when_lb_policy_arrives = false;
576 }
577 watch_lb_policy_locked(exec_ctx, chand, new_lb_policy, state);
578 }
Mark D. Roth15494b52017-07-12 15:26:55 -0700579 if (!lb_policy_updated) {
580 set_channel_connectivity_state_locked(exec_ctx, chand, state,
581 GRPC_ERROR_REF(state_error),
582 "new_lb+resolver");
583 }
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700584 grpc_resolver_next_locked(exec_ctx, chand->resolver,
585 &chand->resolver_result,
586 &chand->on_resolver_result_changed);
587 GRPC_ERROR_UNREF(state_error);
Craig Tillera82950e2015-09-22 12:33:20 -0700588 }
Craig Tiller3f475422015-06-25 10:43:05 -0700589}
590
Craig Tillera8610c02017-02-14 10:05:11 -0800591static void start_transport_op_locked(grpc_exec_ctx *exec_ctx, void *arg,
592 grpc_error *error_ignored) {
Yash Tibrewalca3c1c02017-09-07 22:47:16 -0700593 grpc_transport_op *op = (grpc_transport_op *)arg;
594 grpc_channel_element *elem =
595 (grpc_channel_element *)op->handler_private.extra_arg;
596 channel_data *chand = (channel_data *)elem->channel_data;
Craig Tiller000cd8f2015-09-18 07:20:29 -0700597
Craig Tillera82950e2015-09-22 12:33:20 -0700598 if (op->on_connectivity_state_change != NULL) {
599 grpc_connectivity_state_notify_on_state_change(
600 exec_ctx, &chand->state_tracker, op->connectivity_state,
601 op->on_connectivity_state_change);
602 op->on_connectivity_state_change = NULL;
603 op->connectivity_state = NULL;
604 }
605
Craig Tiller26dab312015-12-07 14:43:47 -0800606 if (op->send_ping != NULL) {
Craig Tiller87b71e22015-12-07 15:14:14 -0800607 if (chand->lb_policy == NULL) {
ncteisen274bbbe2017-06-08 14:57:11 -0700608 GRPC_CLOSURE_SCHED(
ncteisen4b36a3d2017-03-13 19:08:06 -0700609 exec_ctx, op->send_ping,
610 GRPC_ERROR_CREATE_FROM_STATIC_STRING("Ping with no load balancing"));
Craig Tiller26dab312015-12-07 14:43:47 -0800611 } else {
Craig Tiller2400bf52017-02-09 16:25:19 -0800612 grpc_lb_policy_ping_one_locked(exec_ctx, chand->lb_policy, op->send_ping);
Craig Tiller26dab312015-12-07 14:43:47 -0800613 op->bind_pollset = NULL;
614 }
615 op->send_ping = NULL;
616 }
617
Craig Tiller1c51edc2016-05-07 16:18:43 -0700618 if (op->disconnect_with_error != GRPC_ERROR_NONE) {
619 if (chand->resolver != NULL) {
620 set_channel_connectivity_state_locked(
Craig Tillerd925c932016-06-06 08:38:50 -0700621 exec_ctx, chand, GRPC_CHANNEL_SHUTDOWN,
Craig Tiller1c51edc2016-05-07 16:18:43 -0700622 GRPC_ERROR_REF(op->disconnect_with_error), "disconnect");
Craig Tiller972470b2017-02-09 15:05:36 -0800623 grpc_resolver_shutdown_locked(exec_ctx, chand->resolver);
Craig Tiller1c51edc2016-05-07 16:18:43 -0700624 GRPC_RESOLVER_UNREF(exec_ctx, chand->resolver, "channel");
625 chand->resolver = NULL;
626 if (!chand->started_resolving) {
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700627 grpc_closure_list_fail_all(&chand->waiting_for_resolver_result_closures,
Craig Tiller1c51edc2016-05-07 16:18:43 -0700628 GRPC_ERROR_REF(op->disconnect_with_error));
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700629 GRPC_CLOSURE_LIST_SCHED(exec_ctx,
630 &chand->waiting_for_resolver_result_closures);
Craig Tiller1c51edc2016-05-07 16:18:43 -0700631 }
632 if (chand->lb_policy != NULL) {
633 grpc_pollset_set_del_pollset_set(exec_ctx,
634 chand->lb_policy->interested_parties,
635 chand->interested_parties);
636 GRPC_LB_POLICY_UNREF(exec_ctx, chand->lb_policy, "channel");
637 chand->lb_policy = NULL;
638 }
Craig Tillerb12d22a2016-04-23 12:50:21 -0700639 }
Craig Tiller1c51edc2016-05-07 16:18:43 -0700640 GRPC_ERROR_UNREF(op->disconnect_with_error);
Craig Tillera82950e2015-09-22 12:33:20 -0700641 }
Craig Tillerd2e5cfc2017-02-09 13:02:20 -0800642 GRPC_CHANNEL_STACK_UNREF(exec_ctx, chand->owning_stack, "start_transport_op");
643
ncteisen274bbbe2017-06-08 14:57:11 -0700644 GRPC_CLOSURE_SCHED(exec_ctx, op->on_consumed, GRPC_ERROR_NONE);
Craig Tillerbefafe62017-02-09 11:30:54 -0800645}
646
647static void cc_start_transport_op(grpc_exec_ctx *exec_ctx,
648 grpc_channel_element *elem,
649 grpc_transport_op *op) {
Yash Tibrewalca3c1c02017-09-07 22:47:16 -0700650 channel_data *chand = (channel_data *)elem->channel_data;
Craig Tillerbefafe62017-02-09 11:30:54 -0800651
Craig Tillerbefafe62017-02-09 11:30:54 -0800652 GPR_ASSERT(op->set_accept_stream == false);
653 if (op->bind_pollset != NULL) {
654 grpc_pollset_set_add_pollset(exec_ctx, chand->interested_parties,
655 op->bind_pollset);
656 }
657
Craig Tillerc55c1022017-03-10 10:26:42 -0800658 op->handler_private.extra_arg = elem;
Craig Tillerd2e5cfc2017-02-09 13:02:20 -0800659 GRPC_CHANNEL_STACK_REF(chand->owning_stack, "start_transport_op");
ncteisen274bbbe2017-06-08 14:57:11 -0700660 GRPC_CLOSURE_SCHED(
Craig Tillerc55c1022017-03-10 10:26:42 -0800661 exec_ctx,
ncteisen274bbbe2017-06-08 14:57:11 -0700662 GRPC_CLOSURE_INIT(&op->handler_private.closure, start_transport_op_locked,
Craig Tilleree4b1452017-05-12 10:56:03 -0700663 op, grpc_combiner_scheduler(chand->combiner)),
Craig Tillerbefafe62017-02-09 11:30:54 -0800664 GRPC_ERROR_NONE);
Craig Tillerca3e9d32015-06-27 18:37:27 -0700665}
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800666
Mark D. Rothb2d24882016-10-27 15:44:07 -0700667static void cc_get_channel_info(grpc_exec_ctx *exec_ctx,
668 grpc_channel_element *elem,
Mark D. Rothf79ce7d2016-11-04 08:43:36 -0700669 const grpc_channel_info *info) {
Yash Tibrewalca3c1c02017-09-07 22:47:16 -0700670 channel_data *chand = (channel_data *)elem->channel_data;
Craig Tiller613dafa2017-02-09 12:00:43 -0800671 gpr_mu_lock(&chand->info_mu);
Mark D. Rothb2d24882016-10-27 15:44:07 -0700672 if (info->lb_policy_name != NULL) {
Craig Tiller613dafa2017-02-09 12:00:43 -0800673 *info->lb_policy_name = chand->info_lb_policy_name == NULL
Mark D. Roth78afd772016-11-04 12:49:49 -0700674 ? NULL
Craig Tiller613dafa2017-02-09 12:00:43 -0800675 : gpr_strdup(chand->info_lb_policy_name);
Mark D. Rothb2d24882016-10-27 15:44:07 -0700676 }
Mark D. Rothc625c7a2016-11-09 14:12:37 -0800677 if (info->service_config_json != NULL) {
Craig Tiller613dafa2017-02-09 12:00:43 -0800678 *info->service_config_json =
679 chand->info_service_config_json == NULL
680 ? NULL
681 : gpr_strdup(chand->info_service_config_json);
Mark D. Rothc625c7a2016-11-09 14:12:37 -0800682 }
Craig Tiller613dafa2017-02-09 12:00:43 -0800683 gpr_mu_unlock(&chand->info_mu);
Mark D. Rothb2d24882016-10-27 15:44:07 -0700684}
685
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700686/* Constructor for channel_data */
Mark D. Rothc1087882016-11-18 10:54:45 -0800687static grpc_error *cc_init_channel_elem(grpc_exec_ctx *exec_ctx,
Mark D. Roth5e2566e2016-11-18 10:53:13 -0800688 grpc_channel_element *elem,
689 grpc_channel_element_args *args) {
Yash Tibrewalca3c1c02017-09-07 22:47:16 -0700690 channel_data *chand = (channel_data *)elem->channel_data;
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700691 GPR_ASSERT(args->is_last);
692 GPR_ASSERT(elem->filter == &grpc_client_channel_filter);
Mark D. Roth21d4b2d2016-11-18 09:53:41 -0800693 // Initialize data members.
Craig Tilleree4b1452017-05-12 10:56:03 -0700694 chand->combiner = grpc_combiner_create();
Craig Tillerd85477512017-02-09 12:02:39 -0800695 gpr_mu_init(&chand->info_mu);
Alexander Polcync3b1f182017-04-18 13:51:36 -0700696 gpr_mu_init(&chand->external_connectivity_watcher_list_mu);
697
698 gpr_mu_lock(&chand->external_connectivity_watcher_list_mu);
699 chand->external_connectivity_watcher_list_head = NULL;
700 gpr_mu_unlock(&chand->external_connectivity_watcher_list_mu);
701
Mark D. Roth21d4b2d2016-11-18 09:53:41 -0800702 chand->owning_stack = args->channel_stack;
ncteisen274bbbe2017-06-08 14:57:11 -0700703 GRPC_CLOSURE_INIT(&chand->on_resolver_result_changed,
Craig Tillerbefafe62017-02-09 11:30:54 -0800704 on_resolver_result_changed_locked, chand,
Craig Tilleree4b1452017-05-12 10:56:03 -0700705 grpc_combiner_scheduler(chand->combiner));
Mark D. Roth21d4b2d2016-11-18 09:53:41 -0800706 chand->interested_parties = grpc_pollset_set_create();
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700707 grpc_connectivity_state_init(&chand->state_tracker, GRPC_CHANNEL_IDLE,
708 "client_channel");
Mark D. Roth21d4b2d2016-11-18 09:53:41 -0800709 // Record client channel factory.
710 const grpc_arg *arg = grpc_channel_args_find(args->channel_args,
711 GRPC_ARG_CLIENT_CHANNEL_FACTORY);
David Garcia Quintas228a5142017-03-30 19:43:00 -0700712 if (arg == NULL) {
713 return GRPC_ERROR_CREATE_FROM_STATIC_STRING(
714 "Missing client channel factory in args for client channel filter");
715 }
716 if (arg->type != GRPC_ARG_POINTER) {
717 return GRPC_ERROR_CREATE_FROM_STATIC_STRING(
718 "client channel factory arg must be a pointer");
719 }
Mark D. Roth21d4b2d2016-11-18 09:53:41 -0800720 grpc_client_channel_factory_ref(arg->value.pointer.p);
Yash Tibrewalca3c1c02017-09-07 22:47:16 -0700721 chand->client_channel_factory =
722 (grpc_client_channel_factory *)arg->value.pointer.p;
Mark D. Rothdc9bee72017-02-07 12:29:14 -0800723 // Get server name to resolve, using proxy mapper if needed.
Mark D. Roth86e90592016-11-18 09:56:40 -0800724 arg = grpc_channel_args_find(args->channel_args, GRPC_ARG_SERVER_URI);
David Garcia Quintas228a5142017-03-30 19:43:00 -0700725 if (arg == NULL) {
726 return GRPC_ERROR_CREATE_FROM_STATIC_STRING(
727 "Missing server uri in args for client channel filter");
728 }
729 if (arg->type != GRPC_ARG_STRING) {
730 return GRPC_ERROR_CREATE_FROM_STATIC_STRING(
731 "server uri arg must be a string");
732 }
Mark D. Rothdc9bee72017-02-07 12:29:14 -0800733 char *proxy_name = NULL;
734 grpc_channel_args *new_args = NULL;
735 grpc_proxy_mappers_map_name(exec_ctx, arg->value.string, args->channel_args,
736 &proxy_name, &new_args);
737 // Instantiate resolver.
Mark D. Roth45ccec52017-01-18 14:04:01 -0800738 chand->resolver = grpc_resolver_create(
Mark D. Rothdc9bee72017-02-07 12:29:14 -0800739 exec_ctx, proxy_name != NULL ? proxy_name : arg->value.string,
740 new_args != NULL ? new_args : args->channel_args,
Craig Tiller972470b2017-02-09 15:05:36 -0800741 chand->interested_parties, chand->combiner);
Mark D. Rothdc9bee72017-02-07 12:29:14 -0800742 if (proxy_name != NULL) gpr_free(proxy_name);
743 if (new_args != NULL) grpc_channel_args_destroy(exec_ctx, new_args);
Mark D. Roth5e2566e2016-11-18 10:53:13 -0800744 if (chand->resolver == NULL) {
ncteisen4b36a3d2017-03-13 19:08:06 -0700745 return GRPC_ERROR_CREATE_FROM_STATIC_STRING("resolver creation failed");
Mark D. Roth5e2566e2016-11-18 10:53:13 -0800746 }
Craig Tiller3be7dd02017-04-03 14:30:03 -0700747 chand->deadline_checking_enabled =
748 grpc_deadline_checking_enabled(args->channel_args);
Mark D. Roth5e2566e2016-11-18 10:53:13 -0800749 return GRPC_ERROR_NONE;
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700750}
751
Craig Tiller972470b2017-02-09 15:05:36 -0800752static void shutdown_resolver_locked(grpc_exec_ctx *exec_ctx, void *arg,
753 grpc_error *error) {
Yash Tibrewalca3c1c02017-09-07 22:47:16 -0700754 grpc_resolver *resolver = (grpc_resolver *)arg;
Craig Tiller972470b2017-02-09 15:05:36 -0800755 grpc_resolver_shutdown_locked(exec_ctx, resolver);
756 GRPC_RESOLVER_UNREF(exec_ctx, resolver, "channel");
757}
758
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700759/* Destructor for channel_data */
760static void cc_destroy_channel_elem(grpc_exec_ctx *exec_ctx,
761 grpc_channel_element *elem) {
Yash Tibrewalca3c1c02017-09-07 22:47:16 -0700762 channel_data *chand = (channel_data *)elem->channel_data;
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700763 if (chand->resolver != NULL) {
ncteisen274bbbe2017-06-08 14:57:11 -0700764 GRPC_CLOSURE_SCHED(
765 exec_ctx, GRPC_CLOSURE_CREATE(shutdown_resolver_locked, chand->resolver,
Craig Tilleree4b1452017-05-12 10:56:03 -0700766 grpc_combiner_scheduler(chand->combiner)),
Craig Tiller972470b2017-02-09 15:05:36 -0800767 GRPC_ERROR_NONE);
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700768 }
Mark D. Roth0e48a9a2016-09-08 14:14:39 -0700769 if (chand->client_channel_factory != NULL) {
770 grpc_client_channel_factory_unref(exec_ctx, chand->client_channel_factory);
771 }
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700772 if (chand->lb_policy != NULL) {
773 grpc_pollset_set_del_pollset_set(exec_ctx,
774 chand->lb_policy->interested_parties,
775 chand->interested_parties);
776 GRPC_LB_POLICY_UNREF(exec_ctx, chand->lb_policy, "channel");
777 }
Craig Tiller613dafa2017-02-09 12:00:43 -0800778 gpr_free(chand->info_lb_policy_name);
779 gpr_free(chand->info_service_config_json);
Mark D. Rothd6d192d2017-02-23 08:58:42 -0800780 if (chand->retry_throttle_data != NULL) {
781 grpc_server_retry_throttle_data_unref(chand->retry_throttle_data);
782 }
Mark D. Roth9d480942016-10-19 14:18:05 -0700783 if (chand->method_params_table != NULL) {
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800784 grpc_slice_hash_table_unref(exec_ctx, chand->method_params_table);
Mark D. Roth9fe284e2016-09-12 11:22:27 -0700785 }
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700786 grpc_connectivity_state_destroy(exec_ctx, &chand->state_tracker);
Craig Tiller9e5ac1b2017-02-14 22:25:50 -0800787 grpc_pollset_set_destroy(exec_ctx, chand->interested_parties);
Craig Tillerf1021672017-02-09 21:29:50 -0800788 GRPC_COMBINER_UNREF(exec_ctx, chand->combiner, "client_channel");
Craig Tillerd85477512017-02-09 12:02:39 -0800789 gpr_mu_destroy(&chand->info_mu);
Alexander Polcync3b1f182017-04-18 13:51:36 -0700790 gpr_mu_destroy(&chand->external_connectivity_watcher_list_mu);
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700791}
792
793/*************************************************************************
794 * PER-CALL FUNCTIONS
795 */
796
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700797// Max number of batches that can be pending on a call at any given
798// time. This includes:
799// recv_initial_metadata
800// send_initial_metadata
801// recv_message
802// send_message
803// recv_trailing_metadata
804// send_trailing_metadata
Mark D. Roth764cf042017-09-01 09:00:06 -0700805// We also add room for a single cancel_stream batch.
806#define MAX_WAITING_BATCHES 7
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700807
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700808/** Call data. Holds a pointer to grpc_subchannel_call and the
809 associated machinery to create such a pointer.
810 Handles queueing of stream ops until a call object is ready, waiting
811 for initial metadata before trying to create a call object,
812 and handling cancellation gracefully. */
813typedef struct client_channel_call_data {
Mark D. Roth72f6da82016-09-02 13:42:38 -0700814 // State for handling deadlines.
815 // The code in deadline_filter.c requires this to be the first field.
Mark D. Roth72f6da82016-09-02 13:42:38 -0700816 // TODO(roth): This is slightly sub-optimal in that grpc_deadline_state
Mark D. Roth66f3d2b2017-09-01 09:02:17 -0700817 // and this struct both independently store pointers to the call stack
818 // and call combiner. If/when we have time, find a way to avoid this
819 // without breaking the grpc_deadline_state abstraction.
Mark D. Roth72f6da82016-09-02 13:42:38 -0700820 grpc_deadline_state deadline_state;
Mark D. Rothf28763c2016-09-14 15:18:40 -0700821
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800822 grpc_slice path; // Request path.
Mark D. Rothe40dd292016-10-05 14:58:37 -0700823 gpr_timespec call_start_time;
824 gpr_timespec deadline;
Mark D. Roth764cf042017-09-01 09:00:06 -0700825 gpr_arena *arena;
Mark D. Roth66f3d2b2017-09-01 09:02:17 -0700826 grpc_call_stack *owning_call;
Mark D. Roth764cf042017-09-01 09:00:06 -0700827 grpc_call_combiner *call_combiner;
828
Mark D. Roth9ccbc4d2017-03-15 08:30:04 -0700829 grpc_server_retry_throttle_data *retry_throttle_data;
Mark D. Roth95b627b2017-02-24 11:02:58 -0800830 method_parameters *method_params;
Mark D. Rothaa850a72016-09-26 13:38:02 -0700831
Mark D. Roth764cf042017-09-01 09:00:06 -0700832 grpc_subchannel_call *subchannel_call;
833 grpc_error *error;
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700834
Mark D. Roth60751fe2017-07-07 12:50:33 -0700835 grpc_lb_policy *lb_policy; // Holds ref while LB pick is pending.
836 grpc_closure lb_pick_closure;
Mark D. Roth66f3d2b2017-09-01 09:02:17 -0700837 grpc_closure lb_pick_cancel_closure;
Mark D. Roth60751fe2017-07-07 12:50:33 -0700838
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700839 grpc_connected_subchannel *connected_subchannel;
Mark D. Roth09e458c2017-05-02 08:13:26 -0700840 grpc_call_context_element subchannel_call_context[GRPC_CONTEXT_COUNT];
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700841 grpc_polling_entity *pollent;
842
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700843 grpc_transport_stream_op_batch *waiting_for_pick_batches[MAX_WAITING_BATCHES];
844 size_t waiting_for_pick_batches_count;
Mark D. Roth764cf042017-09-01 09:00:06 -0700845 grpc_closure handle_pending_batch_in_call_combiner[MAX_WAITING_BATCHES];
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700846
Mark D. Roth764cf042017-09-01 09:00:06 -0700847 grpc_transport_stream_op_batch *initial_metadata_batch;
David Garcia Quintasd1a47f12016-09-02 12:46:44 +0200848
849 grpc_linked_mdelem lb_token_mdelem;
Mark D. Rothd6d192d2017-02-23 08:58:42 -0800850
851 grpc_closure on_complete;
852 grpc_closure *original_on_complete;
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700853} call_data;
854
Mark D. Rothbf199612017-08-29 16:59:07 -0700855grpc_subchannel_call *grpc_client_channel_get_subchannel_call(
Mark D. Roth764cf042017-09-01 09:00:06 -0700856 grpc_call_element *elem) {
Yash Tibrewalca3c1c02017-09-07 22:47:16 -0700857 call_data *calld = (call_data *)elem->call_data;
Mark D. Roth764cf042017-09-01 09:00:06 -0700858 return calld->subchannel_call;
Mark D. Roth76e264b2017-08-25 09:03:33 -0700859}
860
Mark D. Roth764cf042017-09-01 09:00:06 -0700861// This is called via the call combiner, so access to calld is synchronized.
862static void waiting_for_pick_batches_add(
Mark D. Rothbf199612017-08-29 16:59:07 -0700863 call_data *calld, grpc_transport_stream_op_batch *batch) {
Mark D. Roth764cf042017-09-01 09:00:06 -0700864 if (batch->send_initial_metadata) {
865 GPR_ASSERT(calld->initial_metadata_batch == NULL);
866 calld->initial_metadata_batch = batch;
867 } else {
868 GPR_ASSERT(calld->waiting_for_pick_batches_count < MAX_WAITING_BATCHES);
869 calld->waiting_for_pick_batches[calld->waiting_for_pick_batches_count++] =
870 batch;
871 }
Mark D. Rothbf199612017-08-29 16:59:07 -0700872}
873
Mark D. Roth764cf042017-09-01 09:00:06 -0700874// This is called via the call combiner, so access to calld is synchronized.
875static void fail_pending_batch_in_call_combiner(grpc_exec_ctx *exec_ctx,
876 void *arg, grpc_error *error) {
Yash Tibrewalca3c1c02017-09-07 22:47:16 -0700877 call_data *calld = (call_data *)arg;
Mark D. Roth764cf042017-09-01 09:00:06 -0700878 if (calld->waiting_for_pick_batches_count > 0) {
879 --calld->waiting_for_pick_batches_count;
880 grpc_transport_stream_op_batch_finish_with_failure(
881 exec_ctx,
882 calld->waiting_for_pick_batches[calld->waiting_for_pick_batches_count],
883 GRPC_ERROR_REF(error), calld->call_combiner);
884 }
885}
886
887// This is called via the call combiner, so access to calld is synchronized.
888static void waiting_for_pick_batches_fail(grpc_exec_ctx *exec_ctx,
889 grpc_call_element *elem,
890 grpc_error *error) {
Yash Tibrewalca3c1c02017-09-07 22:47:16 -0700891 call_data *calld = (call_data *)elem->call_data;
Mark D. Roth60751fe2017-07-07 12:50:33 -0700892 if (GRPC_TRACER_ON(grpc_client_channel_trace)) {
893 gpr_log(GPR_DEBUG,
894 "chand=%p calld=%p: failing %" PRIdPTR " pending batches: %s",
895 elem->channel_data, calld, calld->waiting_for_pick_batches_count,
896 grpc_error_string(error));
897 }
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700898 for (size_t i = 0; i < calld->waiting_for_pick_batches_count; ++i) {
Mark D. Roth764cf042017-09-01 09:00:06 -0700899 GRPC_CLOSURE_INIT(&calld->handle_pending_batch_in_call_combiner[i],
900 fail_pending_batch_in_call_combiner, calld,
901 grpc_schedule_on_exec_ctx);
902 GRPC_CALL_COMBINER_START(exec_ctx, calld->call_combiner,
903 &calld->handle_pending_batch_in_call_combiner[i],
904 GRPC_ERROR_REF(error),
905 "waiting_for_pick_batches_fail");
Mark D. Roth76e264b2017-08-25 09:03:33 -0700906 }
Mark D. Roth764cf042017-09-01 09:00:06 -0700907 if (calld->initial_metadata_batch != NULL) {
908 grpc_transport_stream_op_batch_finish_with_failure(
909 exec_ctx, calld->initial_metadata_batch, GRPC_ERROR_REF(error),
910 calld->call_combiner);
911 } else {
912 GRPC_CALL_COMBINER_STOP(exec_ctx, calld->call_combiner,
913 "waiting_for_pick_batches_fail");
914 }
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700915 GRPC_ERROR_UNREF(error);
916}
917
Mark D. Roth764cf042017-09-01 09:00:06 -0700918// This is called via the call combiner, so access to calld is synchronized.
919static void run_pending_batch_in_call_combiner(grpc_exec_ctx *exec_ctx,
920 void *arg, grpc_error *ignored) {
Yash Tibrewalca3c1c02017-09-07 22:47:16 -0700921 call_data *calld = (call_data *)arg;
Mark D. Roth764cf042017-09-01 09:00:06 -0700922 if (calld->waiting_for_pick_batches_count > 0) {
923 --calld->waiting_for_pick_batches_count;
924 grpc_subchannel_call_process_op(
925 exec_ctx, calld->subchannel_call,
926 calld->waiting_for_pick_batches[calld->waiting_for_pick_batches_count]);
Mark D. Rothbf199612017-08-29 16:59:07 -0700927 }
Mark D. Roth764cf042017-09-01 09:00:06 -0700928}
929
930// This is called via the call combiner, so access to calld is synchronized.
931static void waiting_for_pick_batches_resume(grpc_exec_ctx *exec_ctx,
932 grpc_call_element *elem) {
Yash Tibrewalca3c1c02017-09-07 22:47:16 -0700933 channel_data *chand = (channel_data *)elem->channel_data;
934 call_data *calld = (call_data *)elem->call_data;
Mark D. Roth60751fe2017-07-07 12:50:33 -0700935 if (GRPC_TRACER_ON(grpc_client_channel_trace)) {
936 gpr_log(GPR_DEBUG, "chand=%p calld=%p: sending %" PRIdPTR
937 " pending batches to subchannel_call=%p",
Mark D. Roth764cf042017-09-01 09:00:06 -0700938 chand, calld, calld->waiting_for_pick_batches_count,
939 calld->subchannel_call);
Mark D. Roth60751fe2017-07-07 12:50:33 -0700940 }
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700941 for (size_t i = 0; i < calld->waiting_for_pick_batches_count; ++i) {
Mark D. Roth764cf042017-09-01 09:00:06 -0700942 GRPC_CLOSURE_INIT(&calld->handle_pending_batch_in_call_combiner[i],
943 run_pending_batch_in_call_combiner, calld,
944 grpc_schedule_on_exec_ctx);
945 GRPC_CALL_COMBINER_START(exec_ctx, calld->call_combiner,
946 &calld->handle_pending_batch_in_call_combiner[i],
947 GRPC_ERROR_NONE,
948 "waiting_for_pick_batches_resume");
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700949 }
Mark D. Roth764cf042017-09-01 09:00:06 -0700950 GPR_ASSERT(calld->initial_metadata_batch != NULL);
951 grpc_subchannel_call_process_op(exec_ctx, calld->subchannel_call,
952 calld->initial_metadata_batch);
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700953}
954
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700955// Applies service config to the call. Must be invoked once we know
956// that the resolver has returned results to the channel.
957static void apply_service_config_to_call_locked(grpc_exec_ctx *exec_ctx,
958 grpc_call_element *elem) {
Yash Tibrewalca3c1c02017-09-07 22:47:16 -0700959 channel_data *chand = (channel_data *)elem->channel_data;
960 call_data *calld = (call_data *)elem->call_data;
Mark D. Roth60751fe2017-07-07 12:50:33 -0700961 if (GRPC_TRACER_ON(grpc_client_channel_trace)) {
962 gpr_log(GPR_DEBUG, "chand=%p calld=%p: applying service config to call",
963 chand, calld);
964 }
Mark D. Roth9ccbc4d2017-03-15 08:30:04 -0700965 if (chand->retry_throttle_data != NULL) {
966 calld->retry_throttle_data =
967 grpc_server_retry_throttle_data_ref(chand->retry_throttle_data);
968 }
Craig Tiller11c17d42017-03-13 13:36:34 -0700969 if (chand->method_params_table != NULL) {
Yash Tibrewalca3c1c02017-09-07 22:47:16 -0700970 calld->method_params = (method_parameters *)grpc_method_config_table_get(
Craig Tiller11c17d42017-03-13 13:36:34 -0700971 exec_ctx, chand->method_params_table, calld->path);
972 if (calld->method_params != NULL) {
973 method_parameters_ref(calld->method_params);
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700974 // If the deadline from the service config is shorter than the one
975 // from the client API, reset the deadline timer.
976 if (chand->deadline_checking_enabled &&
977 gpr_time_cmp(calld->method_params->timeout,
Craig Tiller11c17d42017-03-13 13:36:34 -0700978 gpr_time_0(GPR_TIMESPAN)) != 0) {
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700979 const gpr_timespec per_method_deadline =
Craig Tiller11c17d42017-03-13 13:36:34 -0700980 gpr_time_add(calld->call_start_time, calld->method_params->timeout);
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700981 if (gpr_time_cmp(per_method_deadline, calld->deadline) < 0) {
982 calld->deadline = per_method_deadline;
983 grpc_deadline_state_reset(exec_ctx, elem, calld->deadline);
984 }
Craig Tiller11c17d42017-03-13 13:36:34 -0700985 }
986 }
987 }
Craig Tiller11c17d42017-03-13 13:36:34 -0700988}
Craig Tillerea4a4f12017-03-13 13:36:52 -0700989
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700990static void create_subchannel_call_locked(grpc_exec_ctx *exec_ctx,
Mark D. Roth60751fe2017-07-07 12:50:33 -0700991 grpc_call_element *elem,
992 grpc_error *error) {
Yash Tibrewalca3c1c02017-09-07 22:47:16 -0700993 channel_data *chand = (channel_data *)elem->channel_data;
994 call_data *calld = (call_data *)elem->call_data;
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700995 const grpc_connected_subchannel_call_args call_args = {
996 .pollent = calld->pollent,
997 .path = calld->path,
998 .start_time = calld->call_start_time,
999 .deadline = calld->deadline,
1000 .arena = calld->arena,
Mark D. Roth764cf042017-09-01 09:00:06 -07001001 .context = calld->subchannel_call_context,
1002 .call_combiner = calld->call_combiner};
Mark D. Roth0ca0be82017-06-20 07:49:33 -07001003 grpc_error *new_error = grpc_connected_subchannel_create_call(
Mark D. Roth764cf042017-09-01 09:00:06 -07001004 exec_ctx, calld->connected_subchannel, &call_args,
1005 &calld->subchannel_call);
Mark D. Roth60751fe2017-07-07 12:50:33 -07001006 if (GRPC_TRACER_ON(grpc_client_channel_trace)) {
1007 gpr_log(GPR_DEBUG, "chand=%p calld=%p: create subchannel_call=%p: error=%s",
Mark D. Roth764cf042017-09-01 09:00:06 -07001008 chand, calld, calld->subchannel_call, grpc_error_string(new_error));
Mark D. Roth60751fe2017-07-07 12:50:33 -07001009 }
Mark D. Roth0ca0be82017-06-20 07:49:33 -07001010 if (new_error != GRPC_ERROR_NONE) {
1011 new_error = grpc_error_add_child(new_error, error);
Mark D. Roth764cf042017-09-01 09:00:06 -07001012 waiting_for_pick_batches_fail(exec_ctx, elem, new_error);
Mark D. Roth0ca0be82017-06-20 07:49:33 -07001013 } else {
Mark D. Roth764cf042017-09-01 09:00:06 -07001014 waiting_for_pick_batches_resume(exec_ctx, elem);
Craig Tiller11c17d42017-03-13 13:36:34 -07001015 }
Mark D. Roth0ca0be82017-06-20 07:49:33 -07001016 GRPC_ERROR_UNREF(error);
Craig Tiller11c17d42017-03-13 13:36:34 -07001017}
1018
Mark D. Rothb2929602017-09-11 09:31:11 -07001019// Invoked when a pick is completed, on both success or failure.
1020static void pick_done_locked(grpc_exec_ctx *exec_ctx, grpc_call_element *elem,
1021 grpc_error *error) {
Yash Tibrewalca3c1c02017-09-07 22:47:16 -07001022 call_data *calld = (call_data *)elem->call_data;
1023 channel_data *chand = (channel_data *)elem->channel_data;
Mark D. Roth2a5959f2016-09-01 08:20:27 -07001024 if (calld->connected_subchannel == NULL) {
Mark D. Roth0ca0be82017-06-20 07:49:33 -07001025 // Failed to create subchannel.
Mark D. Roth764cf042017-09-01 09:00:06 -07001026 GRPC_ERROR_UNREF(calld->error);
1027 calld->error = error == GRPC_ERROR_NONE
1028 ? GRPC_ERROR_CREATE_FROM_STATIC_STRING(
1029 "Call dropped by load balancing policy")
1030 : GRPC_ERROR_CREATE_REFERENCING_FROM_STATIC_STRING(
1031 "Failed to create subchannel", &error, 1);
Mark D. Roth60751fe2017-07-07 12:50:33 -07001032 if (GRPC_TRACER_ON(grpc_client_channel_trace)) {
1033 gpr_log(GPR_DEBUG,
1034 "chand=%p calld=%p: failed to create subchannel: error=%s", chand,
Mark D. Roth764cf042017-09-01 09:00:06 -07001035 calld, grpc_error_string(calld->error));
Mark D. Roth60751fe2017-07-07 12:50:33 -07001036 }
Mark D. Roth764cf042017-09-01 09:00:06 -07001037 waiting_for_pick_batches_fail(exec_ctx, elem, GRPC_ERROR_REF(calld->error));
Mark D. Roth2a5959f2016-09-01 08:20:27 -07001038 } else {
Mark D. Roth9fe284e2016-09-12 11:22:27 -07001039 /* Create call on subchannel. */
Mark D. Roth60751fe2017-07-07 12:50:33 -07001040 create_subchannel_call_locked(exec_ctx, elem, GRPC_ERROR_REF(error));
Mark D. Roth2a5959f2016-09-01 08:20:27 -07001041 }
Mark D. Roth0ca0be82017-06-20 07:49:33 -07001042 GRPC_ERROR_UNREF(error);
Mark D. Roth2a5959f2016-09-01 08:20:27 -07001043}
1044
Mark D. Rothb2929602017-09-11 09:31:11 -07001045// A wrapper around pick_done_locked() that is used in cases where
1046// either (a) the pick was deferred pending a resolver result or (b) the
1047// pick was done asynchronously. Removes the call's polling entity from
1048// chand->interested_parties before invoking pick_done_locked().
1049static void async_pick_done_locked(grpc_exec_ctx *exec_ctx,
1050 grpc_call_element *elem, grpc_error *error) {
1051 channel_data *chand = (channel_data *)elem->channel_data;
1052 call_data *calld = (call_data *)elem->call_data;
1053 grpc_polling_entity_del_from_pollset_set(exec_ctx, calld->pollent,
1054 chand->interested_parties);
1055 pick_done_locked(exec_ctx, elem, error);
1056}
1057
1058// Note: This runs under the client_channel combiner, but will NOT be
1059// holding the call combiner.
1060static void pick_callback_cancel_locked(grpc_exec_ctx *exec_ctx, void *arg,
1061 grpc_error *error) {
1062 grpc_call_element *elem = (grpc_call_element *)arg;
1063 channel_data *chand = (channel_data *)elem->channel_data;
1064 call_data *calld = (call_data *)elem->call_data;
1065 if (calld->lb_policy != NULL) {
1066 if (GRPC_TRACER_ON(grpc_client_channel_trace)) {
1067 gpr_log(GPR_DEBUG, "chand=%p calld=%p: cancelling pick from LB policy %p",
1068 chand, calld, calld->lb_policy);
1069 }
1070 grpc_lb_policy_cancel_pick_locked(exec_ctx, calld->lb_policy,
1071 &calld->connected_subchannel,
1072 GRPC_ERROR_REF(error));
1073 }
1074 GRPC_CALL_STACK_UNREF(exec_ctx, calld->owning_call, "pick_callback_cancel");
1075}
1076
1077// Callback invoked by grpc_lb_policy_pick_locked() for async picks.
1078// Unrefs the LB policy and invokes async_pick_done_locked().
1079static void pick_callback_done_locked(grpc_exec_ctx *exec_ctx, void *arg,
1080 grpc_error *error) {
1081 grpc_call_element *elem = (grpc_call_element *)arg;
1082 channel_data *chand = (channel_data *)elem->channel_data;
1083 call_data *calld = (call_data *)elem->call_data;
1084 if (GRPC_TRACER_ON(grpc_client_channel_trace)) {
1085 gpr_log(GPR_DEBUG, "chand=%p calld=%p: pick completed asynchronously",
1086 chand, calld);
1087 }
1088 GPR_ASSERT(calld->lb_policy != NULL);
1089 GRPC_LB_POLICY_UNREF(exec_ctx, calld->lb_policy, "pick_subchannel");
1090 calld->lb_policy = NULL;
1091 async_pick_done_locked(exec_ctx, elem, GRPC_ERROR_REF(error));
1092}
1093
1094// Takes a ref to chand->lb_policy and calls grpc_lb_policy_pick_locked().
1095// If the pick was completed synchronously, unrefs the LB policy and
1096// returns true.
1097static bool pick_callback_start_locked(grpc_exec_ctx *exec_ctx,
1098 grpc_call_element *elem) {
1099 channel_data *chand = (channel_data *)elem->channel_data;
1100 call_data *calld = (call_data *)elem->call_data;
1101 if (GRPC_TRACER_ON(grpc_client_channel_trace)) {
1102 gpr_log(GPR_DEBUG, "chand=%p calld=%p: starting pick on lb_policy=%p",
1103 chand, calld, chand->lb_policy);
1104 }
1105 apply_service_config_to_call_locked(exec_ctx, elem);
1106 // If the application explicitly set wait_for_ready, use that.
1107 // Otherwise, if the service config specified a value for this
1108 // method, use that.
1109 uint32_t initial_metadata_flags =
1110 calld->initial_metadata_batch->payload->send_initial_metadata
1111 .send_initial_metadata_flags;
1112 const bool wait_for_ready_set_from_api =
1113 initial_metadata_flags &
1114 GRPC_INITIAL_METADATA_WAIT_FOR_READY_EXPLICITLY_SET;
1115 const bool wait_for_ready_set_from_service_config =
1116 calld->method_params != NULL &&
1117 calld->method_params->wait_for_ready != WAIT_FOR_READY_UNSET;
1118 if (!wait_for_ready_set_from_api && wait_for_ready_set_from_service_config) {
1119 if (calld->method_params->wait_for_ready == WAIT_FOR_READY_TRUE) {
1120 initial_metadata_flags |= GRPC_INITIAL_METADATA_WAIT_FOR_READY;
1121 } else {
1122 initial_metadata_flags &= ~GRPC_INITIAL_METADATA_WAIT_FOR_READY;
1123 }
1124 }
1125 const grpc_lb_policy_pick_args inputs = {
1126 calld->initial_metadata_batch->payload->send_initial_metadata
1127 .send_initial_metadata,
1128 initial_metadata_flags, &calld->lb_token_mdelem};
1129 // Keep a ref to the LB policy in calld while the pick is pending.
1130 GRPC_LB_POLICY_REF(chand->lb_policy, "pick_subchannel");
1131 calld->lb_policy = chand->lb_policy;
1132 GRPC_CLOSURE_INIT(&calld->lb_pick_closure, pick_callback_done_locked, elem,
1133 grpc_combiner_scheduler(chand->combiner));
1134 const bool pick_done = grpc_lb_policy_pick_locked(
1135 exec_ctx, chand->lb_policy, &inputs, &calld->connected_subchannel,
1136 calld->subchannel_call_context, NULL, &calld->lb_pick_closure);
1137 if (pick_done) {
1138 /* synchronous grpc_lb_policy_pick call. Unref the LB policy. */
1139 if (GRPC_TRACER_ON(grpc_client_channel_trace)) {
1140 gpr_log(GPR_DEBUG, "chand=%p calld=%p: pick completed synchronously",
1141 chand, calld);
1142 }
1143 GRPC_LB_POLICY_UNREF(exec_ctx, calld->lb_policy, "pick_subchannel");
1144 calld->lb_policy = NULL;
1145 } else {
1146 GRPC_CALL_STACK_REF(calld->owning_call, "pick_callback_cancel");
1147 grpc_call_combiner_set_notify_on_cancel(
1148 exec_ctx, calld->call_combiner,
1149 GRPC_CLOSURE_INIT(&calld->lb_pick_cancel_closure,
1150 pick_callback_cancel_locked, elem,
1151 grpc_combiner_scheduler(chand->combiner)));
1152 }
1153 return pick_done;
1154}
Mark D. Roth0ca0be82017-06-20 07:49:33 -07001155
Craig Tiller577c9b22015-11-02 14:11:15 -08001156typedef struct {
Craig Tiller577c9b22015-11-02 14:11:15 -08001157 grpc_call_element *elem;
Mark D. Roth66f3d2b2017-09-01 09:02:17 -07001158 bool finished;
Craig Tiller577c9b22015-11-02 14:11:15 -08001159 grpc_closure closure;
Mark D. Roth66f3d2b2017-09-01 09:02:17 -07001160 grpc_closure cancel_closure;
Mark D. Roth0ca0be82017-06-20 07:49:33 -07001161} pick_after_resolver_result_args;
Craig Tiller577c9b22015-11-02 14:11:15 -08001162
Mark D. Roth764cf042017-09-01 09:00:06 -07001163// Note: This runs under the client_channel combiner, but will NOT be
1164// holding the call combiner.
1165static void pick_after_resolver_result_cancel_locked(grpc_exec_ctx *exec_ctx,
1166 void *arg,
1167 grpc_error *error) {
Yash Tibrewalca3c1c02017-09-07 22:47:16 -07001168 pick_after_resolver_result_args *args =
1169 (pick_after_resolver_result_args *)arg;
Mark D. Roth66f3d2b2017-09-01 09:02:17 -07001170 if (args->finished) {
1171 gpr_free(args);
Mark D. Rothb2b9a0f2017-09-01 09:06:47 -07001172 return;
Mark D. Roth764cf042017-09-01 09:00:06 -07001173 }
Mark D. Rothb2b9a0f2017-09-01 09:06:47 -07001174 // If we don't yet have a resolver result, then a closure for
1175 // pick_after_resolver_result_done_locked() will have been added to
1176 // chand->waiting_for_resolver_result_closures, and it may not be invoked
1177 // until after this call has been destroyed. We mark the operation as
1178 // finished, so that when pick_after_resolver_result_done_locked()
1179 // is called, it will be a no-op. We also immediately invoke
Mark D. Rothb2929602017-09-11 09:31:11 -07001180 // async_pick_done_locked() to propagate the error back to the caller.
1181 args->finished = true;
1182 grpc_call_element *elem = args->elem;
1183 channel_data *chand = (channel_data *)elem->channel_data;
1184 call_data *calld = (call_data *)elem->call_data;
Mark D. Rothb2b9a0f2017-09-01 09:06:47 -07001185 if (GRPC_TRACER_ON(grpc_client_channel_trace)) {
1186 gpr_log(GPR_DEBUG,
1187 "chand=%p calld=%p: cancelling pick waiting for resolver result",
1188 chand, calld);
1189 }
1190 // Note: Although we are not in the call combiner here, we are
1191 // basically stealing the call combiner from the pending pick, so
Mark D. Rothb2929602017-09-11 09:31:11 -07001192 // it's safe to call async_pick_done_locked() here -- we are
Mark D. Rothb2b9a0f2017-09-01 09:06:47 -07001193 // essentially calling it here instead of calling it in
1194 // pick_after_resolver_result_done_locked().
Mark D. Rothb2929602017-09-11 09:31:11 -07001195 async_pick_done_locked(exec_ctx, elem,
1196 GRPC_ERROR_CREATE_REFERENCING_FROM_STATIC_STRING(
1197 "Pick cancelled", &error, 1));
Mark D. Roth764cf042017-09-01 09:00:06 -07001198}
1199
Mark D. Roth60751fe2017-07-07 12:50:33 -07001200static void pick_after_resolver_result_done_locked(grpc_exec_ctx *exec_ctx,
1201 void *arg,
1202 grpc_error *error) {
Yash Tibrewalca3c1c02017-09-07 22:47:16 -07001203 pick_after_resolver_result_args *args =
1204 (pick_after_resolver_result_args *)arg;
Mark D. Roth66f3d2b2017-09-01 09:02:17 -07001205 if (args->finished) {
Craig Tiller577c9b22015-11-02 14:11:15 -08001206 /* cancelled, do nothing */
Mark D. Roth60751fe2017-07-07 12:50:33 -07001207 if (GRPC_TRACER_ON(grpc_client_channel_trace)) {
1208 gpr_log(GPR_DEBUG, "call cancelled before resolver result");
1209 }
Mark D. Roth66f3d2b2017-09-01 09:02:17 -07001210 gpr_free(args);
Mark D. Rothb2b9a0f2017-09-01 09:06:47 -07001211 return;
1212 }
1213 args->finished = true;
1214 grpc_call_element *elem = args->elem;
Yash Tibrewalca3c1c02017-09-07 22:47:16 -07001215 channel_data *chand = (channel_data *)elem->channel_data;
1216 call_data *calld = (call_data *)elem->call_data;
Mark D. Rothb2b9a0f2017-09-01 09:06:47 -07001217 if (error != GRPC_ERROR_NONE) {
1218 if (GRPC_TRACER_ON(grpc_client_channel_trace)) {
1219 gpr_log(GPR_DEBUG, "chand=%p calld=%p: resolver failed to return data",
1220 chand, calld);
1221 }
Mark D. Rothb2929602017-09-11 09:31:11 -07001222 async_pick_done_locked(exec_ctx, elem, GRPC_ERROR_REF(error));
Mark D. Roth9dab7d52016-10-07 07:48:03 -07001223 } else {
Mark D. Rothb2b9a0f2017-09-01 09:06:47 -07001224 if (GRPC_TRACER_ON(grpc_client_channel_trace)) {
1225 gpr_log(GPR_DEBUG, "chand=%p calld=%p: resolver returned, doing pick",
1226 chand, calld);
1227 }
Mark D. Rothb2929602017-09-11 09:31:11 -07001228 if (pick_callback_start_locked(exec_ctx, elem)) {
1229 // Even if the LB policy returns a result synchronously, we have
1230 // already added our polling entity to chand->interested_parties
1231 // in order to wait for the resolver result, so we need to
1232 // remove it here. Therefore, we call async_pick_done_locked()
1233 // instead of pick_done_locked().
1234 async_pick_done_locked(exec_ctx, elem, GRPC_ERROR_NONE);
Mark D. Roth9dab7d52016-10-07 07:48:03 -07001235 }
Craig Tiller577c9b22015-11-02 14:11:15 -08001236 }
Craig Tiller577c9b22015-11-02 14:11:15 -08001237}
1238
Mark D. Roth60751fe2017-07-07 12:50:33 -07001239static void pick_after_resolver_result_start_locked(grpc_exec_ctx *exec_ctx,
1240 grpc_call_element *elem) {
Yash Tibrewalca3c1c02017-09-07 22:47:16 -07001241 channel_data *chand = (channel_data *)elem->channel_data;
1242 call_data *calld = (call_data *)elem->call_data;
Mark D. Roth60751fe2017-07-07 12:50:33 -07001243 if (GRPC_TRACER_ON(grpc_client_channel_trace)) {
1244 gpr_log(GPR_DEBUG,
1245 "chand=%p calld=%p: deferring pick pending resolver result", chand,
1246 calld);
Mark D. Roth64a317c2017-05-02 08:27:08 -07001247 }
Mark D. Roth60751fe2017-07-07 12:50:33 -07001248 pick_after_resolver_result_args *args =
1249 (pick_after_resolver_result_args *)gpr_zalloc(sizeof(*args));
1250 args->elem = elem;
1251 GRPC_CLOSURE_INIT(&args->closure, pick_after_resolver_result_done_locked,
1252 args, grpc_combiner_scheduler(chand->combiner));
1253 grpc_closure_list_append(&chand->waiting_for_resolver_result_closures,
1254 &args->closure, GRPC_ERROR_NONE);
Mark D. Roth764cf042017-09-01 09:00:06 -07001255 grpc_call_combiner_set_notify_on_cancel(
1256 exec_ctx, calld->call_combiner,
Mark D. Roth66f3d2b2017-09-01 09:02:17 -07001257 GRPC_CLOSURE_INIT(&args->cancel_closure,
1258 pick_after_resolver_result_cancel_locked, args,
Mark D. Roth764cf042017-09-01 09:00:06 -07001259 grpc_combiner_scheduler(chand->combiner)));
Mark D. Roth60751fe2017-07-07 12:50:33 -07001260}
1261
Mark D. Roth764cf042017-09-01 09:00:06 -07001262static void start_pick_locked(grpc_exec_ctx *exec_ctx, void *arg,
Mark D. Rothb2929602017-09-11 09:31:11 -07001263 grpc_error *ignored) {
Mark D. Roth764cf042017-09-01 09:00:06 -07001264 grpc_call_element *elem = (grpc_call_element *)arg;
1265 call_data *calld = (call_data *)elem->call_data;
1266 channel_data *chand = (channel_data *)elem->channel_data;
1267 GPR_ASSERT(calld->connected_subchannel == NULL);
Mark D. Rothb2929602017-09-11 09:31:11 -07001268 if (chand->lb_policy != NULL) {
1269 // We already have an LB policy, so ask it for a pick.
1270 if (pick_callback_start_locked(exec_ctx, elem)) {
1271 // Pick completed synchronously.
1272 pick_done_locked(exec_ctx, elem, GRPC_ERROR_NONE);
1273 return;
Mark D. Rothbf199612017-08-29 16:59:07 -07001274 }
Mark D. Roth764cf042017-09-01 09:00:06 -07001275 } else {
Mark D. Rothb2929602017-09-11 09:31:11 -07001276 // We do not yet have an LB policy, so wait for a resolver result.
1277 if (chand->resolver == NULL) {
1278 pick_done_locked(exec_ctx, elem,
1279 GRPC_ERROR_CREATE_FROM_STATIC_STRING("Disconnected"));
1280 return;
1281 }
1282 if (!chand->started_resolving) {
1283 start_resolving_locked(exec_ctx, chand);
1284 }
1285 pick_after_resolver_result_start_locked(exec_ctx, elem);
Mark D. Rothbf199612017-08-29 16:59:07 -07001286 }
Mark D. Rothb2929602017-09-11 09:31:11 -07001287 // We need to wait for either a resolver result or for an async result
1288 // from the LB policy. Add the polling entity from call_data to the
1289 // channel_data's interested_parties, so that the I/O of the LB policy
1290 // and resolver can be done under it. The polling entity will be
1291 // removed in async_pick_done_locked().
1292 grpc_polling_entity_add_to_pollset_set(exec_ctx, calld->pollent,
1293 chand->interested_parties);
Craig Tillera11bfc82017-02-14 09:56:33 -08001294}
1295
Mark D. Rothde144102017-03-15 10:11:03 -07001296static void on_complete(grpc_exec_ctx *exec_ctx, void *arg, grpc_error *error) {
Yash Tibrewalca3c1c02017-09-07 22:47:16 -07001297 grpc_call_element *elem = (grpc_call_element *)arg;
1298 call_data *calld = (call_data *)elem->call_data;
Mark D. Roth9ccbc4d2017-03-15 08:30:04 -07001299 if (calld->retry_throttle_data != NULL) {
Mark D. Rothd6d192d2017-02-23 08:58:42 -08001300 if (error == GRPC_ERROR_NONE) {
1301 grpc_server_retry_throttle_data_record_success(
Mark D. Roth9ccbc4d2017-03-15 08:30:04 -07001302 calld->retry_throttle_data);
Mark D. Rothd6d192d2017-02-23 08:58:42 -08001303 } else {
1304 // TODO(roth): In a subsequent PR, check the return value here and
Mark D. Rothb3322562017-02-23 14:38:02 -08001305 // decide whether or not to retry. Note that we should only
1306 // record failures whose statuses match the configured retryable
1307 // or non-fatal status codes.
Mark D. Rothd6d192d2017-02-23 08:58:42 -08001308 grpc_server_retry_throttle_data_record_failure(
Mark D. Roth9ccbc4d2017-03-15 08:30:04 -07001309 calld->retry_throttle_data);
Mark D. Rothd6d192d2017-02-23 08:58:42 -08001310 }
1311 }
ncteisen274bbbe2017-06-08 14:57:11 -07001312 GRPC_CLOSURE_RUN(exec_ctx, calld->original_on_complete,
Mark D. Roth95039b52017-02-24 07:59:45 -08001313 GRPC_ERROR_REF(error));
Mark D. Rothd6d192d2017-02-23 08:58:42 -08001314}
1315
Craig Tillere1b51da2017-03-31 15:44:33 -07001316static void cc_start_transport_stream_op_batch(
1317 grpc_exec_ctx *exec_ctx, grpc_call_element *elem,
Mark D. Roth60751fe2017-07-07 12:50:33 -07001318 grpc_transport_stream_op_batch *batch) {
Yash Tibrewalca3c1c02017-09-07 22:47:16 -07001319 call_data *calld = (call_data *)elem->call_data;
1320 channel_data *chand = (channel_data *)elem->channel_data;
Craig Tiller3be7dd02017-04-03 14:30:03 -07001321 if (chand->deadline_checking_enabled) {
Craig Tiller29ebc572017-04-04 08:00:55 -07001322 grpc_deadline_state_client_start_transport_stream_op_batch(exec_ctx, elem,
Mark D. Roth60751fe2017-07-07 12:50:33 -07001323 batch);
Craig Tiller3be7dd02017-04-03 14:30:03 -07001324 }
Mark D. Roth764cf042017-09-01 09:00:06 -07001325 GPR_TIMER_BEGIN("cc_start_transport_stream_op_batch", 0);
1326 // If we've previously been cancelled, immediately fail any new batches.
1327 if (calld->error != GRPC_ERROR_NONE) {
1328 if (GRPC_TRACER_ON(grpc_client_channel_trace)) {
1329 gpr_log(GPR_DEBUG, "chand=%p calld=%p: failing batch with error: %s",
1330 chand, calld, grpc_error_string(calld->error));
1331 }
1332 grpc_transport_stream_op_batch_finish_with_failure(
1333 exec_ctx, batch, GRPC_ERROR_REF(calld->error), calld->call_combiner);
1334 goto done;
1335 }
1336 if (batch->cancel_stream) {
1337 // Stash a copy of cancel_error in our call data, so that we can use
1338 // it for subsequent operations. This ensures that if the call is
1339 // cancelled before any batches are passed down (e.g., if the deadline
1340 // is in the past when the call starts), we can return the right
1341 // error to the caller when the first batch does get passed down.
1342 GRPC_ERROR_UNREF(calld->error);
1343 calld->error = GRPC_ERROR_REF(batch->payload->cancel_stream.cancel_error);
1344 if (GRPC_TRACER_ON(grpc_client_channel_trace)) {
1345 gpr_log(GPR_DEBUG, "chand=%p calld=%p: recording cancel_error=%s", chand,
1346 calld, grpc_error_string(calld->error));
1347 }
1348 // If we have a subchannel call, send the cancellation batch down.
1349 // Otherwise, fail all pending batches.
1350 if (calld->subchannel_call != NULL) {
1351 grpc_subchannel_call_process_op(exec_ctx, calld->subchannel_call, batch);
1352 } else {
1353 waiting_for_pick_batches_add(calld, batch);
1354 waiting_for_pick_batches_fail(exec_ctx, elem,
1355 GRPC_ERROR_REF(calld->error));
1356 }
1357 goto done;
1358 }
Mark D. Roth0ca0be82017-06-20 07:49:33 -07001359 // Intercept on_complete for recv_trailing_metadata so that we can
1360 // check retry throttle status.
Mark D. Roth60751fe2017-07-07 12:50:33 -07001361 if (batch->recv_trailing_metadata) {
1362 GPR_ASSERT(batch->on_complete != NULL);
1363 calld->original_on_complete = batch->on_complete;
Mark D. Roth0ca0be82017-06-20 07:49:33 -07001364 GRPC_CLOSURE_INIT(&calld->on_complete, on_complete, elem,
1365 grpc_schedule_on_exec_ctx);
Mark D. Roth60751fe2017-07-07 12:50:33 -07001366 batch->on_complete = &calld->on_complete;
Mark D. Roth0ca0be82017-06-20 07:49:33 -07001367 }
Mark D. Roth764cf042017-09-01 09:00:06 -07001368 // Check if we've already gotten a subchannel call.
1369 // Note that once we have completed the pick, we do not need to enter
1370 // the channel combiner, which is more efficient (especially for
1371 // streaming calls).
1372 if (calld->subchannel_call != NULL) {
Mark D. Roth60751fe2017-07-07 12:50:33 -07001373 if (GRPC_TRACER_ON(grpc_client_channel_trace)) {
1374 gpr_log(GPR_DEBUG,
1375 "chand=%p calld=%p: sending batch to subchannel_call=%p", chand,
Mark D. Roth764cf042017-09-01 09:00:06 -07001376 calld, calld->subchannel_call);
Mark D. Roth60751fe2017-07-07 12:50:33 -07001377 }
Mark D. Roth764cf042017-09-01 09:00:06 -07001378 grpc_subchannel_call_process_op(exec_ctx, calld->subchannel_call, batch);
Mark D. Roth60751fe2017-07-07 12:50:33 -07001379 goto done;
Mark D. Roth2a5959f2016-09-01 08:20:27 -07001380 }
Mark D. Roth764cf042017-09-01 09:00:06 -07001381 // We do not yet have a subchannel call.
1382 // Add the batch to the waiting-for-pick list.
1383 waiting_for_pick_batches_add(calld, batch);
1384 // For batches containing a send_initial_metadata op, enter the channel
1385 // combiner to start a pick.
1386 if (batch->send_initial_metadata) {
1387 if (GRPC_TRACER_ON(grpc_client_channel_trace)) {
Mark D. Rothb2929602017-09-11 09:31:11 -07001388 gpr_log(GPR_DEBUG, "chand=%p calld=%p: entering client_channel combiner",
1389 chand, calld);
Mark D. Roth764cf042017-09-01 09:00:06 -07001390 }
1391 GRPC_CLOSURE_SCHED(
1392 exec_ctx,
1393 GRPC_CLOSURE_INIT(&batch->handler_private.closure, start_pick_locked,
1394 elem, grpc_combiner_scheduler(chand->combiner)),
1395 GRPC_ERROR_NONE);
1396 } else {
1397 // For all other batches, release the call combiner.
1398 if (GRPC_TRACER_ON(grpc_client_channel_trace)) {
1399 gpr_log(GPR_DEBUG,
1400 "chand=%p calld=%p: saved batch, yeilding call combiner", chand,
1401 calld);
1402 }
1403 GRPC_CALL_COMBINER_STOP(exec_ctx, calld->call_combiner,
1404 "batch does not include send_initial_metadata");
Mark D. Roth60751fe2017-07-07 12:50:33 -07001405 }
Mark D. Roth60751fe2017-07-07 12:50:33 -07001406done:
Craig Tillera0f3abd2017-03-31 15:42:16 -07001407 GPR_TIMER_END("cc_start_transport_stream_op_batch", 0);
Mark D. Roth2a5959f2016-09-01 08:20:27 -07001408}
1409
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001410/* Constructor for call_data */
Mark D. Roth2a5959f2016-09-01 08:20:27 -07001411static grpc_error *cc_init_call_elem(grpc_exec_ctx *exec_ctx,
1412 grpc_call_element *elem,
Craig Tillerc52ba3a2017-02-15 22:57:43 -08001413 const grpc_call_element_args *args) {
Yash Tibrewalca3c1c02017-09-07 22:47:16 -07001414 call_data *calld = (call_data *)elem->call_data;
1415 channel_data *chand = (channel_data *)elem->channel_data;
Mark D. Rothe40dd292016-10-05 14:58:37 -07001416 // Initialize data members.
Craig Tiller7c70b6c2017-01-23 07:48:42 -08001417 calld->path = grpc_slice_ref_internal(args->path);
Mark D. Rothff08f332016-10-14 13:01:01 -07001418 calld->call_start_time = args->start_time;
Mark D. Rothe40dd292016-10-05 14:58:37 -07001419 calld->deadline = gpr_convert_clock_type(args->deadline, GPR_CLOCK_MONOTONIC);
Craig Tillerd426cac2017-03-13 12:30:45 -07001420 calld->arena = args->arena;
Mark D. Roth66f3d2b2017-09-01 09:02:17 -07001421 calld->owning_call = args->call_stack;
Mark D. Roth764cf042017-09-01 09:00:06 -07001422 calld->call_combiner = args->call_combiner;
Craig Tiller3be7dd02017-04-03 14:30:03 -07001423 if (chand->deadline_checking_enabled) {
Mark D. Roth764cf042017-09-01 09:00:06 -07001424 grpc_deadline_state_init(exec_ctx, elem, args->call_stack,
1425 args->call_combiner, calld->deadline);
Craig Tiller3be7dd02017-04-03 14:30:03 -07001426 }
Mark D. Roth0badbe82016-06-23 10:15:12 -07001427 return GRPC_ERROR_NONE;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001428}
1429
1430/* Destructor for call_data */
Mark D. Roth2a5959f2016-09-01 08:20:27 -07001431static void cc_destroy_call_elem(grpc_exec_ctx *exec_ctx,
1432 grpc_call_element *elem,
1433 const grpc_call_final_info *final_info,
Craig Tillerd426cac2017-03-13 12:30:45 -07001434 grpc_closure *then_schedule_closure) {
Yash Tibrewalca3c1c02017-09-07 22:47:16 -07001435 call_data *calld = (call_data *)elem->call_data;
1436 channel_data *chand = (channel_data *)elem->channel_data;
Craig Tiller3be7dd02017-04-03 14:30:03 -07001437 if (chand->deadline_checking_enabled) {
1438 grpc_deadline_state_destroy(exec_ctx, elem);
1439 }
Craig Tiller7c70b6c2017-01-23 07:48:42 -08001440 grpc_slice_unref_internal(exec_ctx, calld->path);
Mark D. Roth95b627b2017-02-24 11:02:58 -08001441 if (calld->method_params != NULL) {
1442 method_parameters_unref(calld->method_params);
1443 }
Mark D. Roth764cf042017-09-01 09:00:06 -07001444 GRPC_ERROR_UNREF(calld->error);
1445 if (calld->subchannel_call != NULL) {
1446 grpc_subchannel_call_set_cleanup_closure(calld->subchannel_call,
Craig Tillerf7c8c9f2017-05-17 15:22:05 -07001447 then_schedule_closure);
Craig Tillerd426cac2017-03-13 12:30:45 -07001448 then_schedule_closure = NULL;
Mark D. Roth764cf042017-09-01 09:00:06 -07001449 GRPC_SUBCHANNEL_CALL_UNREF(exec_ctx, calld->subchannel_call,
Craig Tillerf7c8c9f2017-05-17 15:22:05 -07001450 "client_channel_destroy_call");
Mark D. Roth4c0fe492016-08-31 13:51:55 -07001451 }
Mark D. Roth60751fe2017-07-07 12:50:33 -07001452 GPR_ASSERT(calld->lb_policy == NULL);
Mark D. Roth0ca0be82017-06-20 07:49:33 -07001453 GPR_ASSERT(calld->waiting_for_pick_batches_count == 0);
Craig Tiller693d3942016-10-27 16:51:25 -07001454 if (calld->connected_subchannel != NULL) {
1455 GRPC_CONNECTED_SUBCHANNEL_UNREF(exec_ctx, calld->connected_subchannel,
1456 "picked");
1457 }
Mark D. Roth09e458c2017-05-02 08:13:26 -07001458 for (size_t i = 0; i < GRPC_CONTEXT_COUNT; ++i) {
1459 if (calld->subchannel_call_context[i].value != NULL) {
1460 calld->subchannel_call_context[i].destroy(
1461 calld->subchannel_call_context[i].value);
1462 }
1463 }
ncteisen274bbbe2017-06-08 14:57:11 -07001464 GRPC_CLOSURE_SCHED(exec_ctx, then_schedule_closure, GRPC_ERROR_NONE);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001465}
1466
David Garcia Quintasf72eb972016-05-03 18:28:09 -07001467static void cc_set_pollset_or_pollset_set(grpc_exec_ctx *exec_ctx,
1468 grpc_call_element *elem,
David Garcia Quintas2a50dfe2016-05-31 15:09:12 -07001469 grpc_polling_entity *pollent) {
Yash Tibrewalca3c1c02017-09-07 22:47:16 -07001470 call_data *calld = (call_data *)elem->call_data;
David Garcia Quintas2a50dfe2016-05-31 15:09:12 -07001471 calld->pollent = pollent;
Craig Tiller577c9b22015-11-02 14:11:15 -08001472}
1473
Mark D. Roth2a5959f2016-09-01 08:20:27 -07001474/*************************************************************************
1475 * EXPORTED SYMBOLS
1476 */
1477
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001478const grpc_channel_filter grpc_client_channel_filter = {
Craig Tillera0f3abd2017-03-31 15:42:16 -07001479 cc_start_transport_stream_op_batch,
Craig Tillerf40df232016-03-25 13:38:14 -07001480 cc_start_transport_op,
1481 sizeof(call_data),
Mark D. Roth2a5959f2016-09-01 08:20:27 -07001482 cc_init_call_elem,
David Garcia Quintas4afce7e2016-04-18 16:25:17 -07001483 cc_set_pollset_or_pollset_set,
Mark D. Roth2a5959f2016-09-01 08:20:27 -07001484 cc_destroy_call_elem,
Craig Tillerf40df232016-03-25 13:38:14 -07001485 sizeof(channel_data),
Mark D. Roth2a5959f2016-09-01 08:20:27 -07001486 cc_init_channel_elem,
1487 cc_destroy_channel_elem,
Mark D. Rothb2d24882016-10-27 15:44:07 -07001488 cc_get_channel_info,
Craig Tillerf40df232016-03-25 13:38:14 -07001489 "client-channel",
Craig Tiller87d5b192015-04-16 14:37:57 -07001490};
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001491
Craig Tiller613dafa2017-02-09 12:00:43 -08001492static void try_to_connect_locked(grpc_exec_ctx *exec_ctx, void *arg,
1493 grpc_error *error_ignored) {
Yash Tibrewalca3c1c02017-09-07 22:47:16 -07001494 channel_data *chand = (channel_data *)arg;
Craig Tiller613dafa2017-02-09 12:00:43 -08001495 if (chand->lb_policy != NULL) {
Craig Tiller2400bf52017-02-09 16:25:19 -08001496 grpc_lb_policy_exit_idle_locked(exec_ctx, chand->lb_policy);
Craig Tiller613dafa2017-02-09 12:00:43 -08001497 } else {
1498 chand->exit_idle_when_lb_policy_arrives = true;
1499 if (!chand->started_resolving && chand->resolver != NULL) {
Mark D. Roth60751fe2017-07-07 12:50:33 -07001500 start_resolving_locked(exec_ctx, chand);
Craig Tiller613dafa2017-02-09 12:00:43 -08001501 }
1502 }
Craig Tillerd2e5cfc2017-02-09 13:02:20 -08001503 GRPC_CHANNEL_STACK_UNREF(exec_ctx, chand->owning_stack, "try_to_connect");
Craig Tiller613dafa2017-02-09 12:00:43 -08001504}
1505
Craig Tillera82950e2015-09-22 12:33:20 -07001506grpc_connectivity_state grpc_client_channel_check_connectivity_state(
1507 grpc_exec_ctx *exec_ctx, grpc_channel_element *elem, int try_to_connect) {
Yash Tibrewalca3c1c02017-09-07 22:47:16 -07001508 channel_data *chand = (channel_data *)elem->channel_data;
Craig Tillera8610c02017-02-14 10:05:11 -08001509 grpc_connectivity_state out =
1510 grpc_connectivity_state_check(&chand->state_tracker);
Craig Tillera82950e2015-09-22 12:33:20 -07001511 if (out == GRPC_CHANNEL_IDLE && try_to_connect) {
Craig Tillerd2e5cfc2017-02-09 13:02:20 -08001512 GRPC_CHANNEL_STACK_REF(chand->owning_stack, "try_to_connect");
ncteisen274bbbe2017-06-08 14:57:11 -07001513 GRPC_CLOSURE_SCHED(
1514 exec_ctx, GRPC_CLOSURE_CREATE(try_to_connect_locked, chand,
Craig Tilleree4b1452017-05-12 10:56:03 -07001515 grpc_combiner_scheduler(chand->combiner)),
Craig Tiller613dafa2017-02-09 12:00:43 -08001516 GRPC_ERROR_NONE);
Craig Tillera82950e2015-09-22 12:33:20 -07001517 }
Craig Tiller48cb07c2015-07-15 16:16:15 -07001518 return out;
1519}
1520
Alexander Polcync3b1f182017-04-18 13:51:36 -07001521typedef struct external_connectivity_watcher {
Craig Tiller86c99582015-11-25 15:22:26 -08001522 channel_data *chand;
David Garcia Quintas87d5a312017-06-06 19:45:58 -07001523 grpc_polling_entity pollent;
Craig Tiller86c99582015-11-25 15:22:26 -08001524 grpc_closure *on_complete;
Alexander Polcync3b1f182017-04-18 13:51:36 -07001525 grpc_closure *watcher_timer_init;
Craig Tiller613dafa2017-02-09 12:00:43 -08001526 grpc_connectivity_state *state;
Craig Tiller86c99582015-11-25 15:22:26 -08001527 grpc_closure my_closure;
Alexander Polcync3b1f182017-04-18 13:51:36 -07001528 struct external_connectivity_watcher *next;
Craig Tiller86c99582015-11-25 15:22:26 -08001529} external_connectivity_watcher;
1530
Alexander Polcync3b1f182017-04-18 13:51:36 -07001531static external_connectivity_watcher *lookup_external_connectivity_watcher(
1532 channel_data *chand, grpc_closure *on_complete) {
1533 gpr_mu_lock(&chand->external_connectivity_watcher_list_mu);
1534 external_connectivity_watcher *w =
1535 chand->external_connectivity_watcher_list_head;
1536 while (w != NULL && w->on_complete != on_complete) {
1537 w = w->next;
1538 }
1539 gpr_mu_unlock(&chand->external_connectivity_watcher_list_mu);
1540 return w;
1541}
1542
1543static void external_connectivity_watcher_list_append(
1544 channel_data *chand, external_connectivity_watcher *w) {
1545 GPR_ASSERT(!lookup_external_connectivity_watcher(chand, w->on_complete));
1546
1547 gpr_mu_lock(&w->chand->external_connectivity_watcher_list_mu);
1548 GPR_ASSERT(!w->next);
1549 w->next = chand->external_connectivity_watcher_list_head;
1550 chand->external_connectivity_watcher_list_head = w;
1551 gpr_mu_unlock(&w->chand->external_connectivity_watcher_list_mu);
1552}
1553
1554static void external_connectivity_watcher_list_remove(
1555 channel_data *chand, external_connectivity_watcher *too_remove) {
1556 GPR_ASSERT(
1557 lookup_external_connectivity_watcher(chand, too_remove->on_complete));
1558 gpr_mu_lock(&chand->external_connectivity_watcher_list_mu);
1559 if (too_remove == chand->external_connectivity_watcher_list_head) {
1560 chand->external_connectivity_watcher_list_head = too_remove->next;
1561 gpr_mu_unlock(&chand->external_connectivity_watcher_list_mu);
1562 return;
1563 }
1564 external_connectivity_watcher *w =
1565 chand->external_connectivity_watcher_list_head;
1566 while (w != NULL) {
1567 if (w->next == too_remove) {
1568 w->next = w->next->next;
1569 gpr_mu_unlock(&chand->external_connectivity_watcher_list_mu);
1570 return;
1571 }
1572 w = w->next;
1573 }
1574 GPR_UNREACHABLE_CODE(return );
1575}
1576
1577int grpc_client_channel_num_external_connectivity_watchers(
1578 grpc_channel_element *elem) {
Yash Tibrewalca3c1c02017-09-07 22:47:16 -07001579 channel_data *chand = (channel_data *)elem->channel_data;
Alexander Polcync3b1f182017-04-18 13:51:36 -07001580 int count = 0;
1581
1582 gpr_mu_lock(&chand->external_connectivity_watcher_list_mu);
1583 external_connectivity_watcher *w =
1584 chand->external_connectivity_watcher_list_head;
1585 while (w != NULL) {
1586 count++;
1587 w = w->next;
1588 }
1589 gpr_mu_unlock(&chand->external_connectivity_watcher_list_mu);
1590
1591 return count;
1592}
1593
Craig Tiller1d881fb2015-12-01 07:39:04 -08001594static void on_external_watch_complete(grpc_exec_ctx *exec_ctx, void *arg,
Craig Tiller804ff712016-05-05 16:25:40 -07001595 grpc_error *error) {
Yash Tibrewalca3c1c02017-09-07 22:47:16 -07001596 external_connectivity_watcher *w = (external_connectivity_watcher *)arg;
Craig Tiller86c99582015-11-25 15:22:26 -08001597 grpc_closure *follow_up = w->on_complete;
David Garcia Quintas87d5a312017-06-06 19:45:58 -07001598 grpc_polling_entity_del_from_pollset_set(exec_ctx, &w->pollent,
1599 w->chand->interested_parties);
Craig Tiller1d881fb2015-12-01 07:39:04 -08001600 GRPC_CHANNEL_STACK_UNREF(exec_ctx, w->chand->owning_stack,
1601 "external_connectivity_watcher");
Alexander Polcync3b1f182017-04-18 13:51:36 -07001602 external_connectivity_watcher_list_remove(w->chand, w);
Craig Tiller86c99582015-11-25 15:22:26 -08001603 gpr_free(w);
ncteisen274bbbe2017-06-08 14:57:11 -07001604 GRPC_CLOSURE_RUN(exec_ctx, follow_up, GRPC_ERROR_REF(error));
Craig Tiller613dafa2017-02-09 12:00:43 -08001605}
1606
Craig Tillera8610c02017-02-14 10:05:11 -08001607static void watch_connectivity_state_locked(grpc_exec_ctx *exec_ctx, void *arg,
1608 grpc_error *error_ignored) {
Yash Tibrewalca3c1c02017-09-07 22:47:16 -07001609 external_connectivity_watcher *w = (external_connectivity_watcher *)arg;
Alexander Polcync3b1f182017-04-18 13:51:36 -07001610 external_connectivity_watcher *found = NULL;
1611 if (w->state != NULL) {
1612 external_connectivity_watcher_list_append(w->chand, w);
ncteisen274bbbe2017-06-08 14:57:11 -07001613 GRPC_CLOSURE_RUN(exec_ctx, w->watcher_timer_init, GRPC_ERROR_NONE);
1614 GRPC_CLOSURE_INIT(&w->my_closure, on_external_watch_complete, w,
Alexander Polcync3b1f182017-04-18 13:51:36 -07001615 grpc_schedule_on_exec_ctx);
1616 grpc_connectivity_state_notify_on_state_change(
1617 exec_ctx, &w->chand->state_tracker, w->state, &w->my_closure);
1618 } else {
1619 GPR_ASSERT(w->watcher_timer_init == NULL);
1620 found = lookup_external_connectivity_watcher(w->chand, w->on_complete);
1621 if (found) {
1622 GPR_ASSERT(found->on_complete == w->on_complete);
1623 grpc_connectivity_state_notify_on_state_change(
1624 exec_ctx, &found->chand->state_tracker, NULL, &found->my_closure);
1625 }
David Garcia Quintas87d5a312017-06-06 19:45:58 -07001626 grpc_polling_entity_del_from_pollset_set(exec_ctx, &w->pollent,
1627 w->chand->interested_parties);
Alexander Polcync3b1f182017-04-18 13:51:36 -07001628 GRPC_CHANNEL_STACK_UNREF(exec_ctx, w->chand->owning_stack,
1629 "external_connectivity_watcher");
1630 gpr_free(w);
1631 }
Craig Tiller86c99582015-11-25 15:22:26 -08001632}
1633
Craig Tillera82950e2015-09-22 12:33:20 -07001634void grpc_client_channel_watch_connectivity_state(
David Garcia Quintas87d5a312017-06-06 19:45:58 -07001635 grpc_exec_ctx *exec_ctx, grpc_channel_element *elem,
1636 grpc_polling_entity pollent, grpc_connectivity_state *state,
1637 grpc_closure *closure, grpc_closure *watcher_timer_init) {
Yash Tibrewalca3c1c02017-09-07 22:47:16 -07001638 channel_data *chand = (channel_data *)elem->channel_data;
1639 external_connectivity_watcher *w =
1640 (external_connectivity_watcher *)gpr_zalloc(sizeof(*w));
Craig Tiller86c99582015-11-25 15:22:26 -08001641 w->chand = chand;
David Garcia Quintas87d5a312017-06-06 19:45:58 -07001642 w->pollent = pollent;
Mark D. Roth92210832017-05-02 15:04:39 -07001643 w->on_complete = closure;
Craig Tiller613dafa2017-02-09 12:00:43 -08001644 w->state = state;
Alexander Polcync3b1f182017-04-18 13:51:36 -07001645 w->watcher_timer_init = watcher_timer_init;
David Garcia Quintas87d5a312017-06-06 19:45:58 -07001646 grpc_polling_entity_add_to_pollset_set(exec_ctx, &w->pollent,
1647 chand->interested_parties);
Craig Tiller1d881fb2015-12-01 07:39:04 -08001648 GRPC_CHANNEL_STACK_REF(w->chand->owning_stack,
1649 "external_connectivity_watcher");
ncteisen274bbbe2017-06-08 14:57:11 -07001650 GRPC_CLOSURE_SCHED(
Craig Tiller613dafa2017-02-09 12:00:43 -08001651 exec_ctx,
ncteisen274bbbe2017-06-08 14:57:11 -07001652 GRPC_CLOSURE_INIT(&w->my_closure, watch_connectivity_state_locked, w,
Craig Tilleree4b1452017-05-12 10:56:03 -07001653 grpc_combiner_scheduler(chand->combiner)),
Craig Tiller613dafa2017-02-09 12:00:43 -08001654 GRPC_ERROR_NONE);
Craig Tiller48cb07c2015-07-15 16:16:15 -07001655}