blob: 8b8d512432ffcd97aec3a32647c01f37edfab89f [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
Yash Tibrewal37fdb732017-09-25 16:45:02 -070019#include <grpc/support/port_platform.h>
20
Craig Tiller9eb0fde2017-03-31 16:59:30 -070021#include "src/core/ext/filters/client_channel/client_channel.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080022
Yash Tibrewalfcd26bc2017-09-25 15:08:28 -070023#include <inttypes.h>
Mark D. Roth4c0fe492016-08-31 13:51:55 -070024#include <stdbool.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080025#include <stdio.h>
Craig Tillereb3b12e2015-06-26 14:42:49 -070026#include <string.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080027
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080028#include <grpc/support/alloc.h>
29#include <grpc/support/log.h>
Mark D. Rothb2d24882016-10-27 15:44:07 -070030#include <grpc/support/string_util.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080031#include <grpc/support/sync.h>
32#include <grpc/support/useful.h>
33
Yuchen Zeng0bad30a2017-10-05 21:47:39 -070034#include "src/core/ext/filters/client_channel/backup_poller.h"
Craig Tiller9eb0fde2017-03-31 16:59:30 -070035#include "src/core/ext/filters/client_channel/http_connect_handshaker.h"
36#include "src/core/ext/filters/client_channel/lb_policy_registry.h"
37#include "src/core/ext/filters/client_channel/proxy_mapper_registry.h"
38#include "src/core/ext/filters/client_channel/resolver_registry.h"
39#include "src/core/ext/filters/client_channel/retry_throttle.h"
40#include "src/core/ext/filters/client_channel/subchannel.h"
Craig Tiller3be7dd02017-04-03 14:30:03 -070041#include "src/core/ext/filters/deadline/deadline_filter.h"
Craig Tiller9533d042016-03-25 17:11:06 -070042#include "src/core/lib/channel/channel_args.h"
43#include "src/core/lib/channel/connected_channel.h"
Craig Tillerbefafe62017-02-09 11:30:54 -080044#include "src/core/lib/iomgr/combiner.h"
Craig Tiller9533d042016-03-25 17:11:06 -070045#include "src/core/lib/iomgr/iomgr.h"
Mark D. Roth4c0fe492016-08-31 13:51:55 -070046#include "src/core/lib/iomgr/polling_entity.h"
Craig Tiller9533d042016-03-25 17:11:06 -070047#include "src/core/lib/profiling/timers.h"
Craig Tiller7c70b6c2017-01-23 07:48:42 -080048#include "src/core/lib/slice/slice_internal.h"
Craig Tiller9533d042016-03-25 17:11:06 -070049#include "src/core/lib/support/string.h"
50#include "src/core/lib/surface/channel.h"
51#include "src/core/lib/transport/connectivity_state.h"
Mark D. Roth9fe284e2016-09-12 11:22:27 -070052#include "src/core/lib/transport/metadata.h"
53#include "src/core/lib/transport/metadata_batch.h"
Mark D. Rothea846a02016-11-03 11:32:54 -070054#include "src/core/lib/transport/service_config.h"
Mark D. Roth9fe284e2016-09-12 11:22:27 -070055#include "src/core/lib/transport/static_metadata.h"
Craig Tiller8910ac62015-10-08 16:49:15 -070056
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080057/* Client channel implementation */
58
ncteisen7712c7c2017-07-12 23:11:27 -070059grpc_tracer_flag grpc_client_channel_trace =
60 GRPC_TRACER_INITIALIZER(false, "client_channel");
Mark D. Roth60751fe2017-07-07 12:50:33 -070061
Mark D. Roth26b7be42016-10-24 10:08:07 -070062/*************************************************************************
63 * METHOD-CONFIG TABLE
64 */
65
Mark D. Roth9d480942016-10-19 14:18:05 -070066typedef enum {
Craig Tiller7acc37e2017-02-28 10:01:37 -080067 /* zero so it can be default initialized */
68 WAIT_FOR_READY_UNSET = 0,
Mark D. Roth9d480942016-10-19 14:18:05 -070069 WAIT_FOR_READY_FALSE,
70 WAIT_FOR_READY_TRUE
71} wait_for_ready_value;
72
Mark D. Roth95b627b2017-02-24 11:02:58 -080073typedef struct {
74 gpr_refcount refs;
Craig Tiller89c14282017-07-19 15:32:27 -070075 grpc_millis timeout;
Mark D. Roth9d480942016-10-19 14:18:05 -070076 wait_for_ready_value wait_for_ready;
77} method_parameters;
78
Craig Tillerbaa14a92017-11-03 09:09:36 -070079static method_parameters* method_parameters_ref(
80 method_parameters* method_params) {
Mark D. Roth95b627b2017-02-24 11:02:58 -080081 gpr_ref(&method_params->refs);
82 return method_params;
Mark D. Roth9d480942016-10-19 14:18:05 -070083}
84
Craig Tillerbaa14a92017-11-03 09:09:36 -070085static void method_parameters_unref(method_parameters* method_params) {
Mark D. Roth95b627b2017-02-24 11:02:58 -080086 if (gpr_unref(&method_params->refs)) {
87 gpr_free(method_params);
88 }
89}
90
Mark D. Roth76d0ec42017-10-26 11:08:14 -070091// Wrappers to pass to grpc_service_config_create_method_config_table().
Craig Tillera64b2b12017-11-03 15:23:13 -070092static void* method_parameters_ref_wrapper(void* value) {
93 return method_parameters_ref((method_parameters*)value);
Mark D. Roth76d0ec42017-10-26 11:08:14 -070094}
Craig Tillera64b2b12017-11-03 15:23:13 -070095static void method_parameters_unref_wrapper(grpc_exec_ctx* exec_ctx,
96 void* value) {
Craig Tillerbaa14a92017-11-03 09:09:36 -070097 method_parameters_unref((method_parameters*)value);
Craig Tiller87a7e1f2016-11-09 09:42:19 -080098}
99
Craig Tillerbaa14a92017-11-03 09:09:36 -0700100static bool parse_wait_for_ready(grpc_json* field,
101 wait_for_ready_value* wait_for_ready) {
Mark D. Roth95b627b2017-02-24 11:02:58 -0800102 if (field->type != GRPC_JSON_TRUE && field->type != GRPC_JSON_FALSE) {
103 return false;
104 }
105 *wait_for_ready = field->type == GRPC_JSON_TRUE ? WAIT_FOR_READY_TRUE
106 : WAIT_FOR_READY_FALSE;
107 return true;
108}
109
Craig Tillerbaa14a92017-11-03 09:09:36 -0700110static bool parse_timeout(grpc_json* field, grpc_millis* timeout) {
Mark D. Roth95b627b2017-02-24 11:02:58 -0800111 if (field->type != GRPC_JSON_STRING) return false;
112 size_t len = strlen(field->value);
113 if (field->value[len - 1] != 's') return false;
Craig Tillerbaa14a92017-11-03 09:09:36 -0700114 char* buf = gpr_strdup(field->value);
Mark D. Roth95b627b2017-02-24 11:02:58 -0800115 buf[len - 1] = '\0'; // Remove trailing 's'.
Craig Tillerbaa14a92017-11-03 09:09:36 -0700116 char* decimal_point = strchr(buf, '.');
Craig Tiller89c14282017-07-19 15:32:27 -0700117 int nanos = 0;
Craig Tiller4782d922017-11-10 09:53:21 -0800118 if (decimal_point != nullptr) {
Mark D. Roth95b627b2017-02-24 11:02:58 -0800119 *decimal_point = '\0';
Craig Tiller89c14282017-07-19 15:32:27 -0700120 nanos = gpr_parse_nonnegative_int(decimal_point + 1);
121 if (nanos == -1) {
Mark D. Roth95b627b2017-02-24 11:02:58 -0800122 gpr_free(buf);
123 return false;
124 }
Mark D. Rotha2821462017-10-26 11:31:58 -0700125 int num_digits = (int)strlen(decimal_point + 1);
126 if (num_digits > 9) { // We don't accept greater precision than nanos.
127 gpr_free(buf);
128 return false;
Mark D. Roth95b627b2017-02-24 11:02:58 -0800129 }
Mark D. Rotha2821462017-10-26 11:31:58 -0700130 for (int i = 0; i < (9 - num_digits); ++i) {
131 nanos *= 10;
132 }
Mark D. Roth95b627b2017-02-24 11:02:58 -0800133 }
Mark D. Rotha2821462017-10-26 11:31:58 -0700134 int seconds = decimal_point == buf ? 0 : gpr_parse_nonnegative_int(buf);
Mark D. Roth95b627b2017-02-24 11:02:58 -0800135 gpr_free(buf);
Craig Tiller89c14282017-07-19 15:32:27 -0700136 if (seconds == -1) return false;
137 *timeout = seconds * GPR_MS_PER_SEC + nanos / GPR_NS_PER_MS;
Mark D. Roth95b627b2017-02-24 11:02:58 -0800138 return true;
139}
140
Craig Tillerbaa14a92017-11-03 09:09:36 -0700141static void* method_parameters_create_from_json(const grpc_json* json) {
Mark D. Rothc968e602016-11-02 14:07:36 -0700142 wait_for_ready_value wait_for_ready = WAIT_FOR_READY_UNSET;
Craig Tiller89c14282017-07-19 15:32:27 -0700143 grpc_millis timeout = 0;
Craig Tiller4782d922017-11-10 09:53:21 -0800144 for (grpc_json* field = json->child; field != nullptr; field = field->next) {
145 if (field->key == nullptr) continue;
Mark D. Roth84c8a022016-11-10 09:39:34 -0800146 if (strcmp(field->key, "waitForReady") == 0) {
Craig Tiller4782d922017-11-10 09:53:21 -0800147 if (wait_for_ready != WAIT_FOR_READY_UNSET) return nullptr; // Duplicate.
148 if (!parse_wait_for_ready(field, &wait_for_ready)) return nullptr;
Mark D. Rothc968e602016-11-02 14:07:36 -0700149 } else if (strcmp(field->key, "timeout") == 0) {
Craig Tiller4782d922017-11-10 09:53:21 -0800150 if (timeout > 0) return nullptr; // Duplicate.
151 if (!parse_timeout(field, &timeout)) return nullptr;
Mark D. Rothc968e602016-11-02 14:07:36 -0700152 }
153 }
Craig Tillerbaa14a92017-11-03 09:09:36 -0700154 method_parameters* value =
155 (method_parameters*)gpr_malloc(sizeof(method_parameters));
Mark D. Roth95b627b2017-02-24 11:02:58 -0800156 gpr_ref_init(&value->refs, 1);
Mark D. Rothc968e602016-11-02 14:07:36 -0700157 value->timeout = timeout;
158 value->wait_for_ready = wait_for_ready;
Mark D. Roth9d480942016-10-19 14:18:05 -0700159 return value;
160}
161
Alexander Polcync3b1f182017-04-18 13:51:36 -0700162struct external_connectivity_watcher;
163
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700164/*************************************************************************
165 * CHANNEL-WIDE FUNCTIONS
166 */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800167
Craig Tiller800dacb2015-10-06 09:10:26 -0700168typedef struct client_channel_channel_data {
Craig Tillerf5f17122015-06-25 08:47:26 -0700169 /** resolver for this channel */
Craig Tillerbaa14a92017-11-03 09:09:36 -0700170 grpc_resolver* resolver;
Craig Tiller20a3c352015-08-05 08:39:50 -0700171 /** have we started resolving this channel */
Mark D. Roth4c0fe492016-08-31 13:51:55 -0700172 bool started_resolving;
Craig Tiller3be7dd02017-04-03 14:30:03 -0700173 /** is deadline checking enabled? */
174 bool deadline_checking_enabled;
Mark D. Roth0e48a9a2016-09-08 14:14:39 -0700175 /** client channel factory */
Craig Tillerbaa14a92017-11-03 09:09:36 -0700176 grpc_client_channel_factory* client_channel_factory;
Craig Tillerf5f17122015-06-25 08:47:26 -0700177
Craig Tillerbefafe62017-02-09 11:30:54 -0800178 /** combiner protecting all variables below in this data structure */
Craig Tillerbaa14a92017-11-03 09:09:36 -0700179 grpc_combiner* combiner;
Mark D. Roth046cf762016-09-26 11:13:51 -0700180 /** currently active load balancer */
Craig Tillerbaa14a92017-11-03 09:09:36 -0700181 grpc_lb_policy* lb_policy;
Mark D. Rothd6d192d2017-02-23 08:58:42 -0800182 /** retry throttle data */
Craig Tillerbaa14a92017-11-03 09:09:36 -0700183 grpc_server_retry_throttle_data* retry_throttle_data;
Mark D. Roth9d480942016-10-19 14:18:05 -0700184 /** maps method names to method_parameters structs */
Craig Tillerbaa14a92017-11-03 09:09:36 -0700185 grpc_slice_hash_table* method_params_table;
Mark D. Roth046cf762016-09-26 11:13:51 -0700186 /** incoming resolver result - set by resolver.next() */
Craig Tillerbaa14a92017-11-03 09:09:36 -0700187 grpc_channel_args* resolver_result;
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700188 /** a list of closures that are all waiting for resolver result to come in */
189 grpc_closure_list waiting_for_resolver_result_closures;
Craig Tiller3f475422015-06-25 10:43:05 -0700190 /** resolver callback */
Mark D. Rothff4df062016-08-22 15:02:49 -0700191 grpc_closure on_resolver_result_changed;
Craig Tiller3f475422015-06-25 10:43:05 -0700192 /** connectivity state being tracked */
Craig Tillerca3e9d32015-06-27 18:37:27 -0700193 grpc_connectivity_state_tracker state_tracker;
Craig Tiller48cb07c2015-07-15 16:16:15 -0700194 /** when an lb_policy arrives, should we try to exit idle */
Mark D. Roth4c0fe492016-08-31 13:51:55 -0700195 bool exit_idle_when_lb_policy_arrives;
Craig Tiller906e3bc2015-11-24 07:31:31 -0800196 /** owning stack */
Craig Tillerbaa14a92017-11-03 09:09:36 -0700197 grpc_channel_stack* owning_stack;
Craig Tiller69b093b2016-02-25 19:04:07 -0800198 /** interested parties (owned) */
Craig Tillerbaa14a92017-11-03 09:09:36 -0700199 grpc_pollset_set* interested_parties;
Craig Tiller613dafa2017-02-09 12:00:43 -0800200
Alexander Polcync3b1f182017-04-18 13:51:36 -0700201 /* external_connectivity_watcher_list head is guarded by its own mutex, since
202 * counts need to be grabbed immediately without polling on a cq */
203 gpr_mu external_connectivity_watcher_list_mu;
Craig Tillerbaa14a92017-11-03 09:09:36 -0700204 struct external_connectivity_watcher* external_connectivity_watcher_list_head;
Alexander Polcync3b1f182017-04-18 13:51:36 -0700205
Craig Tiller613dafa2017-02-09 12:00:43 -0800206 /* the following properties are guarded by a mutex since API's require them
Craig Tiller46dd7902017-02-23 09:42:16 -0800207 to be instantaneously available */
Craig Tiller613dafa2017-02-09 12:00:43 -0800208 gpr_mu info_mu;
Craig Tillerbaa14a92017-11-03 09:09:36 -0700209 char* info_lb_policy_name;
Craig Tiller613dafa2017-02-09 12:00:43 -0800210 /** service config in JSON form */
Craig Tillerbaa14a92017-11-03 09:09:36 -0700211 char* info_service_config_json;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800212} channel_data;
213
Craig Tillerd6c98df2015-08-18 09:33:44 -0700214/** We create one watcher for each new lb_policy that is returned from a
Mark D. Roth4c0fe492016-08-31 13:51:55 -0700215 resolver, to watch for state changes from the lb_policy. When a state
216 change is seen, we update the channel, and create a new watcher. */
Craig Tillera82950e2015-09-22 12:33:20 -0700217typedef struct {
Craig Tillerbaa14a92017-11-03 09:09:36 -0700218 channel_data* chand;
Craig Tiller33825112015-09-18 07:44:19 -0700219 grpc_closure on_changed;
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700220 grpc_connectivity_state state;
Craig Tillerbaa14a92017-11-03 09:09:36 -0700221 grpc_lb_policy* lb_policy;
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700222} lb_policy_connectivity_watcher;
223
Craig Tillerbaa14a92017-11-03 09:09:36 -0700224static void watch_lb_policy_locked(grpc_exec_ctx* exec_ctx, channel_data* chand,
225 grpc_lb_policy* lb_policy,
Craig Tiller2400bf52017-02-09 16:25:19 -0800226 grpc_connectivity_state current_state);
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700227
Craig Tillerbaa14a92017-11-03 09:09:36 -0700228static void set_channel_connectivity_state_locked(grpc_exec_ctx* exec_ctx,
229 channel_data* chand,
Craig Tiller8c0d96f2016-03-11 14:27:52 -0800230 grpc_connectivity_state state,
Craig Tillerbaa14a92017-11-03 09:09:36 -0700231 grpc_error* error,
232 const char* reason) {
David Garcia Quintas37251282017-04-14 13:46:03 -0700233 /* TODO: Improve failure handling:
234 * - Make it possible for policies to return GRPC_CHANNEL_TRANSIENT_FAILURE.
235 * - Hand over pending picks from old policies during the switch that happens
236 * when resolver provides an update. */
Craig Tiller4782d922017-11-10 09:53:21 -0800237 if (chand->lb_policy != nullptr) {
David Garcia Quintas956f7002017-04-13 15:40:06 -0700238 if (state == GRPC_CHANNEL_TRANSIENT_FAILURE) {
239 /* cancel picks with wait_for_ready=false */
240 grpc_lb_policy_cancel_picks_locked(
241 exec_ctx, chand->lb_policy,
242 /* mask= */ GRPC_INITIAL_METADATA_WAIT_FOR_READY,
243 /* check= */ 0, GRPC_ERROR_REF(error));
244 } else if (state == GRPC_CHANNEL_SHUTDOWN) {
245 /* cancel all picks */
246 grpc_lb_policy_cancel_picks_locked(exec_ctx, chand->lb_policy,
247 /* mask= */ 0, /* check= */ 0,
248 GRPC_ERROR_REF(error));
249 }
Craig Tiller8c0d96f2016-03-11 14:27:52 -0800250 }
Mark D. Roth60751fe2017-07-07 12:50:33 -0700251 if (GRPC_TRACER_ON(grpc_client_channel_trace)) {
252 gpr_log(GPR_DEBUG, "chand=%p: setting connectivity state to %s", chand,
253 grpc_connectivity_state_name(state));
254 }
Craig Tiller9ccf5f12016-05-07 21:41:01 -0700255 grpc_connectivity_state_set(exec_ctx, &chand->state_tracker, state, error,
256 reason);
Craig Tiller8c0d96f2016-03-11 14:27:52 -0800257}
258
Craig Tillerbaa14a92017-11-03 09:09:36 -0700259static void on_lb_policy_state_changed_locked(grpc_exec_ctx* exec_ctx,
260 void* arg, grpc_error* error) {
261 lb_policy_connectivity_watcher* w = (lb_policy_connectivity_watcher*)arg;
Craig Tillercb2609f2015-11-24 17:19:19 -0800262 grpc_connectivity_state publish_state = w->state;
Craig Tillerc5de8352017-02-09 14:08:05 -0800263 /* check if the notification is for the latest policy */
264 if (w->lb_policy == w->chand->lb_policy) {
Mark D. Roth60751fe2017-07-07 12:50:33 -0700265 if (GRPC_TRACER_ON(grpc_client_channel_trace)) {
266 gpr_log(GPR_DEBUG, "chand=%p: lb_policy=%p state changed to %s", w->chand,
267 w->lb_policy, grpc_connectivity_state_name(w->state));
268 }
Craig Tillerbe98d242017-11-10 15:26:57 -0800269 if (publish_state == GRPC_CHANNEL_SHUTDOWN &&
270 w->chand->resolver != nullptr) {
Craig Tillerc5de8352017-02-09 14:08:05 -0800271 publish_state = GRPC_CHANNEL_TRANSIENT_FAILURE;
Craig Tiller972470b2017-02-09 15:05:36 -0800272 grpc_resolver_channel_saw_error_locked(exec_ctx, w->chand->resolver);
Craig Tillerc5de8352017-02-09 14:08:05 -0800273 GRPC_LB_POLICY_UNREF(exec_ctx, w->chand->lb_policy, "channel");
Craig Tiller4782d922017-11-10 09:53:21 -0800274 w->chand->lb_policy = nullptr;
Craig Tillerc5de8352017-02-09 14:08:05 -0800275 }
276 set_channel_connectivity_state_locked(exec_ctx, w->chand, publish_state,
277 GRPC_ERROR_REF(error), "lb_changed");
278 if (w->state != GRPC_CHANNEL_SHUTDOWN) {
Craig Tiller2400bf52017-02-09 16:25:19 -0800279 watch_lb_policy_locked(exec_ctx, w->chand, w->lb_policy, w->state);
Craig Tillerc5de8352017-02-09 14:08:05 -0800280 }
Craig Tillera82950e2015-09-22 12:33:20 -0700281 }
Craig Tiller906e3bc2015-11-24 07:31:31 -0800282 GRPC_CHANNEL_STACK_UNREF(exec_ctx, w->chand->owning_stack, "watch_lb_policy");
Craig Tillera82950e2015-09-22 12:33:20 -0700283 gpr_free(w);
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700284}
285
Craig Tillerbaa14a92017-11-03 09:09:36 -0700286static void watch_lb_policy_locked(grpc_exec_ctx* exec_ctx, channel_data* chand,
287 grpc_lb_policy* lb_policy,
Craig Tiller2400bf52017-02-09 16:25:19 -0800288 grpc_connectivity_state current_state) {
Craig Tillerbaa14a92017-11-03 09:09:36 -0700289 lb_policy_connectivity_watcher* w =
290 (lb_policy_connectivity_watcher*)gpr_malloc(sizeof(*w));
Craig Tiller906e3bc2015-11-24 07:31:31 -0800291 GRPC_CHANNEL_STACK_REF(chand->owning_stack, "watch_lb_policy");
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700292 w->chand = chand;
ncteisen274bbbe2017-06-08 14:57:11 -0700293 GRPC_CLOSURE_INIT(&w->on_changed, on_lb_policy_state_changed_locked, w,
Craig Tilleree4b1452017-05-12 10:56:03 -0700294 grpc_combiner_scheduler(chand->combiner));
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700295 w->state = current_state;
296 w->lb_policy = lb_policy;
Craig Tiller2400bf52017-02-09 16:25:19 -0800297 grpc_lb_policy_notify_on_state_change_locked(exec_ctx, lb_policy, &w->state,
298 &w->on_changed);
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700299}
300
Craig Tillerbaa14a92017-11-03 09:09:36 -0700301static void start_resolving_locked(grpc_exec_ctx* exec_ctx,
302 channel_data* chand) {
Mark D. Roth60751fe2017-07-07 12:50:33 -0700303 if (GRPC_TRACER_ON(grpc_client_channel_trace)) {
304 gpr_log(GPR_DEBUG, "chand=%p: starting name resolution", chand);
305 }
306 GPR_ASSERT(!chand->started_resolving);
307 chand->started_resolving = true;
308 GRPC_CHANNEL_STACK_REF(chand->owning_stack, "resolver");
309 grpc_resolver_next_locked(exec_ctx, chand->resolver, &chand->resolver_result,
310 &chand->on_resolver_result_changed);
311}
312
Mark D. Rothd6d192d2017-02-23 08:58:42 -0800313typedef struct {
Craig Tillerbaa14a92017-11-03 09:09:36 -0700314 char* server_name;
315 grpc_server_retry_throttle_data* retry_throttle_data;
Mark D. Rothd6d192d2017-02-23 08:58:42 -0800316} service_config_parsing_state;
317
Craig Tillerbaa14a92017-11-03 09:09:36 -0700318static void parse_retry_throttle_params(const grpc_json* field, void* arg) {
319 service_config_parsing_state* parsing_state =
320 (service_config_parsing_state*)arg;
Mark D. Rothd6d192d2017-02-23 08:58:42 -0800321 if (strcmp(field->key, "retryThrottling") == 0) {
Craig Tiller4782d922017-11-10 09:53:21 -0800322 if (parsing_state->retry_throttle_data != nullptr) return; // Duplicate.
Mark D. Rothd6d192d2017-02-23 08:58:42 -0800323 if (field->type != GRPC_JSON_OBJECT) return;
324 int max_milli_tokens = 0;
325 int milli_token_ratio = 0;
Craig Tiller4782d922017-11-10 09:53:21 -0800326 for (grpc_json* sub_field = field->child; sub_field != nullptr;
Mark D. Rothd6d192d2017-02-23 08:58:42 -0800327 sub_field = sub_field->next) {
Craig Tiller4782d922017-11-10 09:53:21 -0800328 if (sub_field->key == nullptr) return;
Mark D. Rothd6d192d2017-02-23 08:58:42 -0800329 if (strcmp(sub_field->key, "maxTokens") == 0) {
330 if (max_milli_tokens != 0) return; // Duplicate.
331 if (sub_field->type != GRPC_JSON_NUMBER) return;
332 max_milli_tokens = gpr_parse_nonnegative_int(sub_field->value);
333 if (max_milli_tokens == -1) return;
334 max_milli_tokens *= 1000;
335 } else if (strcmp(sub_field->key, "tokenRatio") == 0) {
336 if (milli_token_ratio != 0) return; // Duplicate.
337 if (sub_field->type != GRPC_JSON_NUMBER) return;
338 // We support up to 3 decimal digits.
339 size_t whole_len = strlen(sub_field->value);
340 uint32_t multiplier = 1;
341 uint32_t decimal_value = 0;
Craig Tillerbaa14a92017-11-03 09:09:36 -0700342 const char* decimal_point = strchr(sub_field->value, '.');
Craig Tiller4782d922017-11-10 09:53:21 -0800343 if (decimal_point != nullptr) {
Mark D. Rothd6d192d2017-02-23 08:58:42 -0800344 whole_len = (size_t)(decimal_point - sub_field->value);
345 multiplier = 1000;
346 size_t decimal_len = strlen(decimal_point + 1);
347 if (decimal_len > 3) decimal_len = 3;
348 if (!gpr_parse_bytes_to_uint32(decimal_point + 1, decimal_len,
349 &decimal_value)) {
350 return;
351 }
352 uint32_t decimal_multiplier = 1;
353 for (size_t i = 0; i < (3 - decimal_len); ++i) {
354 decimal_multiplier *= 10;
355 }
356 decimal_value *= decimal_multiplier;
357 }
358 uint32_t whole_value;
359 if (!gpr_parse_bytes_to_uint32(sub_field->value, whole_len,
360 &whole_value)) {
361 return;
362 }
363 milli_token_ratio = (int)((whole_value * multiplier) + decimal_value);
Mark D. Rothb3322562017-02-23 14:38:02 -0800364 if (milli_token_ratio <= 0) return;
Mark D. Rothd6d192d2017-02-23 08:58:42 -0800365 }
366 }
367 parsing_state->retry_throttle_data =
368 grpc_retry_throttle_map_get_data_for_server(
369 parsing_state->server_name, max_milli_tokens, milli_token_ratio);
370 }
371}
372
Craig Tillerbaa14a92017-11-03 09:09:36 -0700373static void on_resolver_result_changed_locked(grpc_exec_ctx* exec_ctx,
374 void* arg, grpc_error* error) {
375 channel_data* chand = (channel_data*)arg;
Mark D. Roth60751fe2017-07-07 12:50:33 -0700376 if (GRPC_TRACER_ON(grpc_client_channel_trace)) {
377 gpr_log(GPR_DEBUG, "chand=%p: got resolver result: error=%s", chand,
378 grpc_error_string(error));
379 }
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700380 // Extract the following fields from the resolver result, if non-NULL.
Mark D. Roth15494b52017-07-12 15:26:55 -0700381 bool lb_policy_updated = false;
Craig Tiller4782d922017-11-10 09:53:21 -0800382 char* lb_policy_name_dup = nullptr;
Mark D. Roth60751fe2017-07-07 12:50:33 -0700383 bool lb_policy_name_changed = false;
Craig Tiller4782d922017-11-10 09:53:21 -0800384 grpc_lb_policy* new_lb_policy = nullptr;
385 char* service_config_json = nullptr;
386 grpc_server_retry_throttle_data* retry_throttle_data = nullptr;
387 grpc_slice_hash_table* method_params_table = nullptr;
388 if (chand->resolver_result != nullptr) {
Mark D. Roth5bd7be02016-10-21 14:19:50 -0700389 // Find LB policy name.
Craig Tiller4782d922017-11-10 09:53:21 -0800390 const char* lb_policy_name = nullptr;
Craig Tillerbaa14a92017-11-03 09:09:36 -0700391 const grpc_arg* channel_arg =
Mark D. Roth41124992016-11-03 11:22:20 -0700392 grpc_channel_args_find(chand->resolver_result, GRPC_ARG_LB_POLICY_NAME);
Craig Tiller4782d922017-11-10 09:53:21 -0800393 if (channel_arg != nullptr) {
Mark D. Rothaf842452016-10-21 15:05:15 -0700394 GPR_ASSERT(channel_arg->type == GRPC_ARG_STRING);
395 lb_policy_name = channel_arg->value.string;
Mark D. Roth5bd7be02016-10-21 14:19:50 -0700396 }
Mark D. Roth1eb96dc2017-03-22 12:19:03 -0700397 // Special case: If at least one balancer address is present, we use
398 // the grpclb policy, regardless of what the resolver actually specified.
Mark D. Rothaf842452016-10-21 15:05:15 -0700399 channel_arg =
Mark D. Roth41124992016-11-03 11:22:20 -0700400 grpc_channel_args_find(chand->resolver_result, GRPC_ARG_LB_ADDRESSES);
Craig Tiller4782d922017-11-10 09:53:21 -0800401 if (channel_arg != nullptr && channel_arg->type == GRPC_ARG_POINTER) {
Craig Tillerbaa14a92017-11-03 09:09:36 -0700402 grpc_lb_addresses* addresses =
403 (grpc_lb_addresses*)channel_arg->value.pointer.p;
Mark D. Roth1eb96dc2017-03-22 12:19:03 -0700404 bool found_balancer_address = false;
Mark D. Rothaf842452016-10-21 15:05:15 -0700405 for (size_t i = 0; i < addresses->num_addresses; ++i) {
Mark D. Roth1eb96dc2017-03-22 12:19:03 -0700406 if (addresses->addresses[i].is_balancer) {
407 found_balancer_address = true;
Mark D. Rothaf842452016-10-21 15:05:15 -0700408 break;
409 }
Mark D. Roth88405f72016-10-03 08:24:52 -0700410 }
Mark D. Roth1eb96dc2017-03-22 12:19:03 -0700411 if (found_balancer_address) {
Craig Tillerbe98d242017-11-10 15:26:57 -0800412 if (lb_policy_name != nullptr &&
413 strcmp(lb_policy_name, "grpclb") != 0) {
Mark D. Rothaf842452016-10-21 15:05:15 -0700414 gpr_log(GPR_INFO,
Mark D. Roth1eb96dc2017-03-22 12:19:03 -0700415 "resolver requested LB policy %s but provided at least one "
416 "balancer address -- forcing use of grpclb LB policy",
Mark D. Roth5f40e5d2016-10-24 13:09:05 -0700417 lb_policy_name);
Mark D. Rothaf842452016-10-21 15:05:15 -0700418 }
419 lb_policy_name = "grpclb";
Mark D. Roth88405f72016-10-03 08:24:52 -0700420 }
Mark D. Roth88405f72016-10-03 08:24:52 -0700421 }
422 // Use pick_first if nothing was specified and we didn't select grpclb
423 // above.
Craig Tiller4782d922017-11-10 09:53:21 -0800424 if (lb_policy_name == nullptr) lb_policy_name = "pick_first";
Mark D. Roth41124992016-11-03 11:22:20 -0700425 grpc_lb_policy_args lb_policy_args;
426 lb_policy_args.args = chand->resolver_result;
427 lb_policy_args.client_channel_factory = chand->client_channel_factory;
Craig Tiller2400bf52017-02-09 16:25:19 -0800428 lb_policy_args.combiner = chand->combiner;
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700429 // Check to see if we're already using the right LB policy.
430 // Note: It's safe to use chand->info_lb_policy_name here without
431 // taking a lock on chand->info_mu, because this function is the
432 // only thing that modifies its value, and it can only be invoked
433 // once at any given time.
Mark D. Roth60751fe2017-07-07 12:50:33 -0700434 lb_policy_name_changed =
Craig Tiller4782d922017-11-10 09:53:21 -0800435 chand->info_lb_policy_name == nullptr ||
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700436 strcmp(chand->info_lb_policy_name, lb_policy_name) != 0;
Craig Tiller4782d922017-11-10 09:53:21 -0800437 if (chand->lb_policy != nullptr && !lb_policy_name_changed) {
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700438 // Continue using the same LB policy. Update with new addresses.
Mark D. Roth15494b52017-07-12 15:26:55 -0700439 lb_policy_updated = true;
David Garcia Quintas87d5a312017-06-06 19:45:58 -0700440 grpc_lb_policy_update_locked(exec_ctx, chand->lb_policy, &lb_policy_args);
441 } else {
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700442 // Instantiate new LB policy.
443 new_lb_policy =
David Garcia Quintas87d5a312017-06-06 19:45:58 -0700444 grpc_lb_policy_create(exec_ctx, lb_policy_name, &lb_policy_args);
Craig Tiller4782d922017-11-10 09:53:21 -0800445 if (new_lb_policy == nullptr) {
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700446 gpr_log(GPR_ERROR, "could not create LB policy \"%s\"", lb_policy_name);
David Garcia Quintas87d5a312017-06-06 19:45:58 -0700447 }
Craig Tiller45724b32015-09-22 10:42:19 -0700448 }
Mark D. Roth41124992016-11-03 11:22:20 -0700449 // Find service config.
Mark D. Rothaf842452016-10-21 15:05:15 -0700450 channel_arg =
Mark D. Roth41124992016-11-03 11:22:20 -0700451 grpc_channel_args_find(chand->resolver_result, GRPC_ARG_SERVICE_CONFIG);
Craig Tiller4782d922017-11-10 09:53:21 -0800452 if (channel_arg != nullptr) {
Mark D. Roth9ec28af2016-11-03 12:32:39 -0700453 GPR_ASSERT(channel_arg->type == GRPC_ARG_STRING);
Mark D. Rothc625c7a2016-11-09 14:12:37 -0800454 service_config_json = gpr_strdup(channel_arg->value.string);
Craig Tillerbaa14a92017-11-03 09:09:36 -0700455 grpc_service_config* service_config =
Mark D. Rothc625c7a2016-11-09 14:12:37 -0800456 grpc_service_config_create(service_config_json);
Craig Tiller4782d922017-11-10 09:53:21 -0800457 if (service_config != nullptr) {
Mark D. Rothd6d192d2017-02-23 08:58:42 -0800458 channel_arg =
459 grpc_channel_args_find(chand->resolver_result, GRPC_ARG_SERVER_URI);
Craig Tiller4782d922017-11-10 09:53:21 -0800460 GPR_ASSERT(channel_arg != nullptr);
Mark D. Rothd6d192d2017-02-23 08:58:42 -0800461 GPR_ASSERT(channel_arg->type == GRPC_ARG_STRING);
Craig Tillerbaa14a92017-11-03 09:09:36 -0700462 grpc_uri* uri =
Mark D. Roth9ccbc4d2017-03-15 08:30:04 -0700463 grpc_uri_parse(exec_ctx, channel_arg->value.string, true);
Mark D. Rothd6d192d2017-02-23 08:58:42 -0800464 GPR_ASSERT(uri->path[0] != '\0');
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700465 service_config_parsing_state parsing_state;
466 memset(&parsing_state, 0, sizeof(parsing_state));
Mark D. Rothd6d192d2017-02-23 08:58:42 -0800467 parsing_state.server_name =
468 uri->path[0] == '/' ? uri->path + 1 : uri->path;
469 grpc_service_config_parse_global_params(
470 service_config, parse_retry_throttle_params, &parsing_state);
Mark D. Rothd6d192d2017-02-23 08:58:42 -0800471 grpc_uri_destroy(uri);
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700472 retry_throttle_data = parsing_state.retry_throttle_data;
Mark D. Rothbdc58b22016-11-04 09:25:57 -0700473 method_params_table = grpc_service_config_create_method_config_table(
Craig Tillerb28c7e82016-11-18 10:29:04 -0800474 exec_ctx, service_config, method_parameters_create_from_json,
Mark D. Roth76d0ec42017-10-26 11:08:14 -0700475 method_parameters_ref_wrapper, method_parameters_unref_wrapper);
Mark D. Rothbdc58b22016-11-04 09:25:57 -0700476 grpc_service_config_destroy(service_config);
477 }
Mark D. Roth9fe284e2016-09-12 11:22:27 -0700478 }
Mark D. Rothf79ce7d2016-11-04 08:43:36 -0700479 // Before we clean up, save a copy of lb_policy_name, since it might
480 // be pointing to data inside chand->resolver_result.
481 // The copy will be saved in chand->lb_policy_name below.
Yash Tibrewal9eb86722017-09-17 23:43:30 -0700482 lb_policy_name_dup = gpr_strdup(lb_policy_name);
Craig Tiller87a7e1f2016-11-09 09:42:19 -0800483 grpc_channel_args_destroy(exec_ctx, chand->resolver_result);
Craig Tiller4782d922017-11-10 09:53:21 -0800484 chand->resolver_result = nullptr;
Craig Tillera82950e2015-09-22 12:33:20 -0700485 }
Mark D. Roth60751fe2017-07-07 12:50:33 -0700486 if (GRPC_TRACER_ON(grpc_client_channel_trace)) {
487 gpr_log(GPR_DEBUG,
488 "chand=%p: resolver result: lb_policy_name=\"%s\"%s, "
489 "service_config=\"%s\"",
Yash Tibrewal9eb86722017-09-17 23:43:30 -0700490 chand, lb_policy_name_dup,
491 lb_policy_name_changed ? " (changed)" : "", service_config_json);
Mark D. Roth60751fe2017-07-07 12:50:33 -0700492 }
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700493 // Now swap out fields in chand. Note that the new values may still
494 // be NULL if (e.g.) the resolver failed to return results or the
495 // results did not contain the necessary data.
496 //
497 // First, swap out the data used by cc_get_channel_info().
Craig Tiller613dafa2017-02-09 12:00:43 -0800498 gpr_mu_lock(&chand->info_mu);
Craig Tiller4782d922017-11-10 09:53:21 -0800499 if (lb_policy_name_dup != nullptr) {
Craig Tiller613dafa2017-02-09 12:00:43 -0800500 gpr_free(chand->info_lb_policy_name);
Yash Tibrewal9eb86722017-09-17 23:43:30 -0700501 chand->info_lb_policy_name = lb_policy_name_dup;
Mark D. Rothb2d24882016-10-27 15:44:07 -0700502 }
Craig Tiller4782d922017-11-10 09:53:21 -0800503 if (service_config_json != nullptr) {
Craig Tiller613dafa2017-02-09 12:00:43 -0800504 gpr_free(chand->info_service_config_json);
505 chand->info_service_config_json = service_config_json;
Mark D. Rothc625c7a2016-11-09 14:12:37 -0800506 }
Craig Tiller613dafa2017-02-09 12:00:43 -0800507 gpr_mu_unlock(&chand->info_mu);
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700508 // Swap out the retry throttle data.
Craig Tiller4782d922017-11-10 09:53:21 -0800509 if (chand->retry_throttle_data != nullptr) {
Mark D. Rothd6d192d2017-02-23 08:58:42 -0800510 grpc_server_retry_throttle_data_unref(chand->retry_throttle_data);
511 }
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700512 chand->retry_throttle_data = retry_throttle_data;
513 // Swap out the method params table.
Craig Tiller4782d922017-11-10 09:53:21 -0800514 if (chand->method_params_table != nullptr) {
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800515 grpc_slice_hash_table_unref(exec_ctx, chand->method_params_table);
Mark D. Roth046cf762016-09-26 11:13:51 -0700516 }
Mark D. Roth9d480942016-10-19 14:18:05 -0700517 chand->method_params_table = method_params_table;
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700518 // If we have a new LB policy or are shutting down (in which case
519 // new_lb_policy will be NULL), swap out the LB policy, unreffing the
520 // old one and removing its fds from chand->interested_parties.
521 // Note that we do NOT do this if either (a) we updated the existing
522 // LB policy above or (b) we failed to create the new LB policy (in
523 // which case we want to continue using the most recent one we had).
Craig Tiller4782d922017-11-10 09:53:21 -0800524 if (new_lb_policy != nullptr || error != GRPC_ERROR_NONE ||
525 chand->resolver == nullptr) {
526 if (chand->lb_policy != nullptr) {
Mark D. Roth60751fe2017-07-07 12:50:33 -0700527 if (GRPC_TRACER_ON(grpc_client_channel_trace)) {
528 gpr_log(GPR_DEBUG, "chand=%p: unreffing lb_policy=%p", chand,
529 chand->lb_policy);
530 }
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700531 grpc_pollset_set_del_pollset_set(exec_ctx,
532 chand->lb_policy->interested_parties,
533 chand->interested_parties);
534 GRPC_LB_POLICY_UNREF(exec_ctx, chand->lb_policy, "channel");
Craig Tiller45724b32015-09-22 10:42:19 -0700535 }
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700536 chand->lb_policy = new_lb_policy;
537 }
538 // Now that we've swapped out the relevant fields of chand, check for
539 // error or shutdown.
Craig Tiller4782d922017-11-10 09:53:21 -0800540 if (error != GRPC_ERROR_NONE || chand->resolver == nullptr) {
Mark D. Roth60751fe2017-07-07 12:50:33 -0700541 if (GRPC_TRACER_ON(grpc_client_channel_trace)) {
542 gpr_log(GPR_DEBUG, "chand=%p: shutting down", chand);
543 }
Craig Tiller4782d922017-11-10 09:53:21 -0800544 if (chand->resolver != nullptr) {
Mark D. Roth60751fe2017-07-07 12:50:33 -0700545 if (GRPC_TRACER_ON(grpc_client_channel_trace)) {
546 gpr_log(GPR_DEBUG, "chand=%p: shutting down resolver", chand);
547 }
Craig Tiller972470b2017-02-09 15:05:36 -0800548 grpc_resolver_shutdown_locked(exec_ctx, chand->resolver);
Craig Tiller76a5c0e2016-03-09 09:05:30 -0800549 GRPC_RESOLVER_UNREF(exec_ctx, chand->resolver, "channel");
Craig Tiller4782d922017-11-10 09:53:21 -0800550 chand->resolver = nullptr;
Craig Tiller76a5c0e2016-03-09 09:05:30 -0800551 }
Craig Tiller8c0d96f2016-03-11 14:27:52 -0800552 set_channel_connectivity_state_locked(
Craig Tillerd925c932016-06-06 08:38:50 -0700553 exec_ctx, chand, GRPC_CHANNEL_SHUTDOWN,
ncteisen4b36a3d2017-03-13 19:08:06 -0700554 GRPC_ERROR_CREATE_REFERENCING_FROM_STATIC_STRING(
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700555 "Got resolver result after disconnection", &error, 1),
Craig Tiller804ff712016-05-05 16:25:40 -0700556 "resolver_gone");
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700557 GRPC_CHANNEL_STACK_UNREF(exec_ctx, chand->owning_stack, "resolver");
558 grpc_closure_list_fail_all(&chand->waiting_for_resolver_result_closures,
559 GRPC_ERROR_CREATE_REFERENCING_FROM_STATIC_STRING(
560 "Channel disconnected", &error, 1));
561 GRPC_CLOSURE_LIST_SCHED(exec_ctx,
562 &chand->waiting_for_resolver_result_closures);
563 } else { // Not shutting down.
564 grpc_connectivity_state state = GRPC_CHANNEL_TRANSIENT_FAILURE;
Craig Tillerbaa14a92017-11-03 09:09:36 -0700565 grpc_error* state_error =
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700566 GRPC_ERROR_CREATE_FROM_STATIC_STRING("No load balancing policy");
Craig Tiller4782d922017-11-10 09:53:21 -0800567 if (new_lb_policy != nullptr) {
Mark D. Roth60751fe2017-07-07 12:50:33 -0700568 if (GRPC_TRACER_ON(grpc_client_channel_trace)) {
569 gpr_log(GPR_DEBUG, "chand=%p: initializing new LB policy", chand);
570 }
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700571 GRPC_ERROR_UNREF(state_error);
572 state = grpc_lb_policy_check_connectivity_locked(exec_ctx, new_lb_policy,
573 &state_error);
574 grpc_pollset_set_add_pollset_set(exec_ctx,
575 new_lb_policy->interested_parties,
576 chand->interested_parties);
577 GRPC_CLOSURE_LIST_SCHED(exec_ctx,
578 &chand->waiting_for_resolver_result_closures);
579 if (chand->exit_idle_when_lb_policy_arrives) {
580 grpc_lb_policy_exit_idle_locked(exec_ctx, new_lb_policy);
581 chand->exit_idle_when_lb_policy_arrives = false;
582 }
583 watch_lb_policy_locked(exec_ctx, chand, new_lb_policy, state);
584 }
Mark D. Roth15494b52017-07-12 15:26:55 -0700585 if (!lb_policy_updated) {
586 set_channel_connectivity_state_locked(exec_ctx, chand, state,
587 GRPC_ERROR_REF(state_error),
588 "new_lb+resolver");
589 }
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700590 grpc_resolver_next_locked(exec_ctx, chand->resolver,
591 &chand->resolver_result,
592 &chand->on_resolver_result_changed);
593 GRPC_ERROR_UNREF(state_error);
Craig Tillera82950e2015-09-22 12:33:20 -0700594 }
Craig Tiller3f475422015-06-25 10:43:05 -0700595}
596
Craig Tillerbaa14a92017-11-03 09:09:36 -0700597static void start_transport_op_locked(grpc_exec_ctx* exec_ctx, void* arg,
598 grpc_error* error_ignored) {
599 grpc_transport_op* op = (grpc_transport_op*)arg;
600 grpc_channel_element* elem =
601 (grpc_channel_element*)op->handler_private.extra_arg;
602 channel_data* chand = (channel_data*)elem->channel_data;
Craig Tiller000cd8f2015-09-18 07:20:29 -0700603
Craig Tiller4782d922017-11-10 09:53:21 -0800604 if (op->on_connectivity_state_change != nullptr) {
Craig Tillera82950e2015-09-22 12:33:20 -0700605 grpc_connectivity_state_notify_on_state_change(
606 exec_ctx, &chand->state_tracker, op->connectivity_state,
607 op->on_connectivity_state_change);
Craig Tiller4782d922017-11-10 09:53:21 -0800608 op->on_connectivity_state_change = nullptr;
609 op->connectivity_state = nullptr;
Craig Tillera82950e2015-09-22 12:33:20 -0700610 }
611
Craig Tiller4782d922017-11-10 09:53:21 -0800612 if (op->send_ping != nullptr) {
613 if (chand->lb_policy == nullptr) {
ncteisen274bbbe2017-06-08 14:57:11 -0700614 GRPC_CLOSURE_SCHED(
ncteisen4b36a3d2017-03-13 19:08:06 -0700615 exec_ctx, op->send_ping,
616 GRPC_ERROR_CREATE_FROM_STATIC_STRING("Ping with no load balancing"));
Craig Tiller26dab312015-12-07 14:43:47 -0800617 } else {
Craig Tiller2400bf52017-02-09 16:25:19 -0800618 grpc_lb_policy_ping_one_locked(exec_ctx, chand->lb_policy, op->send_ping);
Craig Tiller4782d922017-11-10 09:53:21 -0800619 op->bind_pollset = nullptr;
Craig Tiller26dab312015-12-07 14:43:47 -0800620 }
Craig Tiller4782d922017-11-10 09:53:21 -0800621 op->send_ping = nullptr;
Craig Tiller26dab312015-12-07 14:43:47 -0800622 }
623
Craig Tiller1c51edc2016-05-07 16:18:43 -0700624 if (op->disconnect_with_error != GRPC_ERROR_NONE) {
Craig Tiller4782d922017-11-10 09:53:21 -0800625 if (chand->resolver != nullptr) {
Craig Tiller1c51edc2016-05-07 16:18:43 -0700626 set_channel_connectivity_state_locked(
Craig Tillerd925c932016-06-06 08:38:50 -0700627 exec_ctx, chand, GRPC_CHANNEL_SHUTDOWN,
Craig Tiller1c51edc2016-05-07 16:18:43 -0700628 GRPC_ERROR_REF(op->disconnect_with_error), "disconnect");
Craig Tiller972470b2017-02-09 15:05:36 -0800629 grpc_resolver_shutdown_locked(exec_ctx, chand->resolver);
Craig Tiller1c51edc2016-05-07 16:18:43 -0700630 GRPC_RESOLVER_UNREF(exec_ctx, chand->resolver, "channel");
Craig Tiller4782d922017-11-10 09:53:21 -0800631 chand->resolver = nullptr;
Craig Tiller1c51edc2016-05-07 16:18:43 -0700632 if (!chand->started_resolving) {
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700633 grpc_closure_list_fail_all(&chand->waiting_for_resolver_result_closures,
Craig Tiller1c51edc2016-05-07 16:18:43 -0700634 GRPC_ERROR_REF(op->disconnect_with_error));
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700635 GRPC_CLOSURE_LIST_SCHED(exec_ctx,
636 &chand->waiting_for_resolver_result_closures);
Craig Tiller1c51edc2016-05-07 16:18:43 -0700637 }
Craig Tiller4782d922017-11-10 09:53:21 -0800638 if (chand->lb_policy != nullptr) {
Craig Tiller1c51edc2016-05-07 16:18:43 -0700639 grpc_pollset_set_del_pollset_set(exec_ctx,
640 chand->lb_policy->interested_parties,
641 chand->interested_parties);
642 GRPC_LB_POLICY_UNREF(exec_ctx, chand->lb_policy, "channel");
Craig Tiller4782d922017-11-10 09:53:21 -0800643 chand->lb_policy = nullptr;
Craig Tiller1c51edc2016-05-07 16:18:43 -0700644 }
Craig Tillerb12d22a2016-04-23 12:50:21 -0700645 }
Craig Tiller1c51edc2016-05-07 16:18:43 -0700646 GRPC_ERROR_UNREF(op->disconnect_with_error);
Craig Tillera82950e2015-09-22 12:33:20 -0700647 }
Craig Tillerd2e5cfc2017-02-09 13:02:20 -0800648 GRPC_CHANNEL_STACK_UNREF(exec_ctx, chand->owning_stack, "start_transport_op");
649
ncteisen274bbbe2017-06-08 14:57:11 -0700650 GRPC_CLOSURE_SCHED(exec_ctx, op->on_consumed, GRPC_ERROR_NONE);
Craig Tillerbefafe62017-02-09 11:30:54 -0800651}
652
Craig Tillerbaa14a92017-11-03 09:09:36 -0700653static void cc_start_transport_op(grpc_exec_ctx* exec_ctx,
654 grpc_channel_element* elem,
655 grpc_transport_op* op) {
656 channel_data* chand = (channel_data*)elem->channel_data;
Craig Tillerbefafe62017-02-09 11:30:54 -0800657
Craig Tillerbefafe62017-02-09 11:30:54 -0800658 GPR_ASSERT(op->set_accept_stream == false);
Craig Tiller4782d922017-11-10 09:53:21 -0800659 if (op->bind_pollset != nullptr) {
Craig Tillerbefafe62017-02-09 11:30:54 -0800660 grpc_pollset_set_add_pollset(exec_ctx, chand->interested_parties,
661 op->bind_pollset);
662 }
663
Craig Tillerc55c1022017-03-10 10:26:42 -0800664 op->handler_private.extra_arg = elem;
Craig Tillerd2e5cfc2017-02-09 13:02:20 -0800665 GRPC_CHANNEL_STACK_REF(chand->owning_stack, "start_transport_op");
ncteisen274bbbe2017-06-08 14:57:11 -0700666 GRPC_CLOSURE_SCHED(
Craig Tillerc55c1022017-03-10 10:26:42 -0800667 exec_ctx,
ncteisen274bbbe2017-06-08 14:57:11 -0700668 GRPC_CLOSURE_INIT(&op->handler_private.closure, start_transport_op_locked,
Craig Tilleree4b1452017-05-12 10:56:03 -0700669 op, grpc_combiner_scheduler(chand->combiner)),
Craig Tillerbefafe62017-02-09 11:30:54 -0800670 GRPC_ERROR_NONE);
Craig Tillerca3e9d32015-06-27 18:37:27 -0700671}
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800672
Craig Tillerbaa14a92017-11-03 09:09:36 -0700673static void cc_get_channel_info(grpc_exec_ctx* exec_ctx,
674 grpc_channel_element* elem,
675 const grpc_channel_info* info) {
676 channel_data* chand = (channel_data*)elem->channel_data;
Craig Tiller613dafa2017-02-09 12:00:43 -0800677 gpr_mu_lock(&chand->info_mu);
Craig Tiller4782d922017-11-10 09:53:21 -0800678 if (info->lb_policy_name != nullptr) {
679 *info->lb_policy_name = chand->info_lb_policy_name == nullptr
680 ? nullptr
Craig Tiller613dafa2017-02-09 12:00:43 -0800681 : gpr_strdup(chand->info_lb_policy_name);
Mark D. Rothb2d24882016-10-27 15:44:07 -0700682 }
Craig Tiller4782d922017-11-10 09:53:21 -0800683 if (info->service_config_json != nullptr) {
Craig Tiller613dafa2017-02-09 12:00:43 -0800684 *info->service_config_json =
Craig Tiller4782d922017-11-10 09:53:21 -0800685 chand->info_service_config_json == nullptr
686 ? nullptr
Craig Tiller613dafa2017-02-09 12:00:43 -0800687 : gpr_strdup(chand->info_service_config_json);
Mark D. Rothc625c7a2016-11-09 14:12:37 -0800688 }
Craig Tiller613dafa2017-02-09 12:00:43 -0800689 gpr_mu_unlock(&chand->info_mu);
Mark D. Rothb2d24882016-10-27 15:44:07 -0700690}
691
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700692/* Constructor for channel_data */
Craig Tillerbaa14a92017-11-03 09:09:36 -0700693static grpc_error* cc_init_channel_elem(grpc_exec_ctx* exec_ctx,
694 grpc_channel_element* elem,
695 grpc_channel_element_args* args) {
696 channel_data* chand = (channel_data*)elem->channel_data;
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700697 GPR_ASSERT(args->is_last);
698 GPR_ASSERT(elem->filter == &grpc_client_channel_filter);
Mark D. Roth21d4b2d2016-11-18 09:53:41 -0800699 // Initialize data members.
Craig Tilleree4b1452017-05-12 10:56:03 -0700700 chand->combiner = grpc_combiner_create();
Craig Tillerd85477512017-02-09 12:02:39 -0800701 gpr_mu_init(&chand->info_mu);
Alexander Polcync3b1f182017-04-18 13:51:36 -0700702 gpr_mu_init(&chand->external_connectivity_watcher_list_mu);
703
704 gpr_mu_lock(&chand->external_connectivity_watcher_list_mu);
Craig Tiller4782d922017-11-10 09:53:21 -0800705 chand->external_connectivity_watcher_list_head = nullptr;
Alexander Polcync3b1f182017-04-18 13:51:36 -0700706 gpr_mu_unlock(&chand->external_connectivity_watcher_list_mu);
707
Mark D. Roth21d4b2d2016-11-18 09:53:41 -0800708 chand->owning_stack = args->channel_stack;
ncteisen274bbbe2017-06-08 14:57:11 -0700709 GRPC_CLOSURE_INIT(&chand->on_resolver_result_changed,
Craig Tillerbefafe62017-02-09 11:30:54 -0800710 on_resolver_result_changed_locked, chand,
Craig Tilleree4b1452017-05-12 10:56:03 -0700711 grpc_combiner_scheduler(chand->combiner));
Mark D. Roth21d4b2d2016-11-18 09:53:41 -0800712 chand->interested_parties = grpc_pollset_set_create();
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700713 grpc_connectivity_state_init(&chand->state_tracker, GRPC_CHANNEL_IDLE,
714 "client_channel");
Yuchen Zeng6f9985e2017-10-10 19:00:01 -0700715 grpc_client_channel_start_backup_polling(exec_ctx, chand->interested_parties);
Mark D. Roth21d4b2d2016-11-18 09:53:41 -0800716 // Record client channel factory.
Craig Tillerbaa14a92017-11-03 09:09:36 -0700717 const grpc_arg* arg = grpc_channel_args_find(args->channel_args,
Mark D. Roth21d4b2d2016-11-18 09:53:41 -0800718 GRPC_ARG_CLIENT_CHANNEL_FACTORY);
Craig Tiller4782d922017-11-10 09:53:21 -0800719 if (arg == nullptr) {
David Garcia Quintas228a5142017-03-30 19:43:00 -0700720 return GRPC_ERROR_CREATE_FROM_STATIC_STRING(
721 "Missing client channel factory in args for client channel filter");
722 }
723 if (arg->type != GRPC_ARG_POINTER) {
724 return GRPC_ERROR_CREATE_FROM_STATIC_STRING(
725 "client channel factory arg must be a pointer");
726 }
Yash Tibrewalbc130da2017-09-12 22:44:08 -0700727 grpc_client_channel_factory_ref(
Craig Tillerbaa14a92017-11-03 09:09:36 -0700728 (grpc_client_channel_factory*)arg->value.pointer.p);
Yash Tibrewalca3c1c02017-09-07 22:47:16 -0700729 chand->client_channel_factory =
Craig Tillerbaa14a92017-11-03 09:09:36 -0700730 (grpc_client_channel_factory*)arg->value.pointer.p;
Mark D. Rothdc9bee72017-02-07 12:29:14 -0800731 // Get server name to resolve, using proxy mapper if needed.
Mark D. Roth86e90592016-11-18 09:56:40 -0800732 arg = grpc_channel_args_find(args->channel_args, GRPC_ARG_SERVER_URI);
Craig Tiller4782d922017-11-10 09:53:21 -0800733 if (arg == nullptr) {
David Garcia Quintas228a5142017-03-30 19:43:00 -0700734 return GRPC_ERROR_CREATE_FROM_STATIC_STRING(
735 "Missing server uri in args for client channel filter");
736 }
737 if (arg->type != GRPC_ARG_STRING) {
738 return GRPC_ERROR_CREATE_FROM_STATIC_STRING(
739 "server uri arg must be a string");
740 }
Craig Tiller4782d922017-11-10 09:53:21 -0800741 char* proxy_name = nullptr;
742 grpc_channel_args* new_args = nullptr;
Mark D. Rothdc9bee72017-02-07 12:29:14 -0800743 grpc_proxy_mappers_map_name(exec_ctx, arg->value.string, args->channel_args,
744 &proxy_name, &new_args);
745 // Instantiate resolver.
Mark D. Roth45ccec52017-01-18 14:04:01 -0800746 chand->resolver = grpc_resolver_create(
Craig Tiller4782d922017-11-10 09:53:21 -0800747 exec_ctx, proxy_name != nullptr ? proxy_name : arg->value.string,
748 new_args != nullptr ? new_args : args->channel_args,
Craig Tiller972470b2017-02-09 15:05:36 -0800749 chand->interested_parties, chand->combiner);
Craig Tiller4782d922017-11-10 09:53:21 -0800750 if (proxy_name != nullptr) gpr_free(proxy_name);
751 if (new_args != nullptr) grpc_channel_args_destroy(exec_ctx, new_args);
752 if (chand->resolver == nullptr) {
ncteisen4b36a3d2017-03-13 19:08:06 -0700753 return GRPC_ERROR_CREATE_FROM_STATIC_STRING("resolver creation failed");
Mark D. Roth5e2566e2016-11-18 10:53:13 -0800754 }
Craig Tiller3be7dd02017-04-03 14:30:03 -0700755 chand->deadline_checking_enabled =
756 grpc_deadline_checking_enabled(args->channel_args);
Mark D. Roth5e2566e2016-11-18 10:53:13 -0800757 return GRPC_ERROR_NONE;
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700758}
759
Craig Tillerbaa14a92017-11-03 09:09:36 -0700760static void shutdown_resolver_locked(grpc_exec_ctx* exec_ctx, void* arg,
761 grpc_error* error) {
762 grpc_resolver* resolver = (grpc_resolver*)arg;
Craig Tiller972470b2017-02-09 15:05:36 -0800763 grpc_resolver_shutdown_locked(exec_ctx, resolver);
764 GRPC_RESOLVER_UNREF(exec_ctx, resolver, "channel");
765}
766
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700767/* Destructor for channel_data */
Craig Tillerbaa14a92017-11-03 09:09:36 -0700768static void cc_destroy_channel_elem(grpc_exec_ctx* exec_ctx,
769 grpc_channel_element* elem) {
770 channel_data* chand = (channel_data*)elem->channel_data;
Craig Tiller4782d922017-11-10 09:53:21 -0800771 if (chand->resolver != nullptr) {
ncteisen274bbbe2017-06-08 14:57:11 -0700772 GRPC_CLOSURE_SCHED(
Craig Tillerbaa14a92017-11-03 09:09:36 -0700773 exec_ctx,
774 GRPC_CLOSURE_CREATE(shutdown_resolver_locked, chand->resolver,
775 grpc_combiner_scheduler(chand->combiner)),
Craig Tiller972470b2017-02-09 15:05:36 -0800776 GRPC_ERROR_NONE);
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700777 }
Craig Tiller4782d922017-11-10 09:53:21 -0800778 if (chand->client_channel_factory != nullptr) {
Mark D. Roth0e48a9a2016-09-08 14:14:39 -0700779 grpc_client_channel_factory_unref(exec_ctx, chand->client_channel_factory);
780 }
Craig Tiller4782d922017-11-10 09:53:21 -0800781 if (chand->lb_policy != nullptr) {
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700782 grpc_pollset_set_del_pollset_set(exec_ctx,
783 chand->lb_policy->interested_parties,
784 chand->interested_parties);
785 GRPC_LB_POLICY_UNREF(exec_ctx, chand->lb_policy, "channel");
786 }
Craig Tiller613dafa2017-02-09 12:00:43 -0800787 gpr_free(chand->info_lb_policy_name);
788 gpr_free(chand->info_service_config_json);
Craig Tiller4782d922017-11-10 09:53:21 -0800789 if (chand->retry_throttle_data != nullptr) {
Mark D. Rothd6d192d2017-02-23 08:58:42 -0800790 grpc_server_retry_throttle_data_unref(chand->retry_throttle_data);
791 }
Craig Tiller4782d922017-11-10 09:53:21 -0800792 if (chand->method_params_table != nullptr) {
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800793 grpc_slice_hash_table_unref(exec_ctx, chand->method_params_table);
Mark D. Roth9fe284e2016-09-12 11:22:27 -0700794 }
Yuchen Zeng0bad30a2017-10-05 21:47:39 -0700795 grpc_client_channel_stop_backup_polling(exec_ctx, chand->interested_parties);
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700796 grpc_connectivity_state_destroy(exec_ctx, &chand->state_tracker);
Craig Tiller9e5ac1b2017-02-14 22:25:50 -0800797 grpc_pollset_set_destroy(exec_ctx, chand->interested_parties);
Craig Tillerf1021672017-02-09 21:29:50 -0800798 GRPC_COMBINER_UNREF(exec_ctx, chand->combiner, "client_channel");
Craig Tillerd85477512017-02-09 12:02:39 -0800799 gpr_mu_destroy(&chand->info_mu);
Alexander Polcync3b1f182017-04-18 13:51:36 -0700800 gpr_mu_destroy(&chand->external_connectivity_watcher_list_mu);
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700801}
802
803/*************************************************************************
804 * PER-CALL FUNCTIONS
805 */
806
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700807// Max number of batches that can be pending on a call at any given
808// time. This includes:
809// recv_initial_metadata
810// send_initial_metadata
811// recv_message
812// send_message
813// recv_trailing_metadata
814// send_trailing_metadata
Mark D. Roth76e264b2017-08-25 09:03:33 -0700815// We also add room for a single cancel_stream batch.
816#define MAX_WAITING_BATCHES 7
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700817
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700818/** Call data. Holds a pointer to grpc_subchannel_call and the
819 associated machinery to create such a pointer.
820 Handles queueing of stream ops until a call object is ready, waiting
821 for initial metadata before trying to create a call object,
822 and handling cancellation gracefully. */
823typedef struct client_channel_call_data {
Mark D. Roth72f6da82016-09-02 13:42:38 -0700824 // State for handling deadlines.
825 // The code in deadline_filter.c requires this to be the first field.
Mark D. Roth72f6da82016-09-02 13:42:38 -0700826 // TODO(roth): This is slightly sub-optimal in that grpc_deadline_state
Mark D. Roth66f3d2b2017-09-01 09:02:17 -0700827 // and this struct both independently store pointers to the call stack
828 // and call combiner. If/when we have time, find a way to avoid this
829 // without breaking the grpc_deadline_state abstraction.
Mark D. Roth72f6da82016-09-02 13:42:38 -0700830 grpc_deadline_state deadline_state;
Mark D. Rothf28763c2016-09-14 15:18:40 -0700831
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800832 grpc_slice path; // Request path.
Mark D. Rothe40dd292016-10-05 14:58:37 -0700833 gpr_timespec call_start_time;
Craig Tiller89c14282017-07-19 15:32:27 -0700834 grpc_millis deadline;
Craig Tillerbaa14a92017-11-03 09:09:36 -0700835 gpr_arena* arena;
836 grpc_call_stack* owning_call;
837 grpc_call_combiner* call_combiner;
Mark D. Roth76e264b2017-08-25 09:03:33 -0700838
Craig Tillerbaa14a92017-11-03 09:09:36 -0700839 grpc_server_retry_throttle_data* retry_throttle_data;
840 method_parameters* method_params;
Mark D. Rothaa850a72016-09-26 13:38:02 -0700841
Craig Tillerbaa14a92017-11-03 09:09:36 -0700842 grpc_subchannel_call* subchannel_call;
843 grpc_error* error;
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700844
Craig Tillerbaa14a92017-11-03 09:09:36 -0700845 grpc_lb_policy* lb_policy; // Holds ref while LB pick is pending.
Mark D. Roth60751fe2017-07-07 12:50:33 -0700846 grpc_closure lb_pick_closure;
Mark D. Roth66f3d2b2017-09-01 09:02:17 -0700847 grpc_closure lb_pick_cancel_closure;
Mark D. Roth60751fe2017-07-07 12:50:33 -0700848
Craig Tillerbaa14a92017-11-03 09:09:36 -0700849 grpc_connected_subchannel* connected_subchannel;
Mark D. Roth09e458c2017-05-02 08:13:26 -0700850 grpc_call_context_element subchannel_call_context[GRPC_CONTEXT_COUNT];
Craig Tillerbaa14a92017-11-03 09:09:36 -0700851 grpc_polling_entity* pollent;
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700852
Craig Tillerbaa14a92017-11-03 09:09:36 -0700853 grpc_transport_stream_op_batch* waiting_for_pick_batches[MAX_WAITING_BATCHES];
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700854 size_t waiting_for_pick_batches_count;
Mark D. Roth76e264b2017-08-25 09:03:33 -0700855 grpc_closure handle_pending_batch_in_call_combiner[MAX_WAITING_BATCHES];
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700856
Craig Tillerbaa14a92017-11-03 09:09:36 -0700857 grpc_transport_stream_op_batch* initial_metadata_batch;
David Garcia Quintasd1a47f12016-09-02 12:46:44 +0200858
859 grpc_linked_mdelem lb_token_mdelem;
Mark D. Rothd6d192d2017-02-23 08:58:42 -0800860
861 grpc_closure on_complete;
Craig Tillerbaa14a92017-11-03 09:09:36 -0700862 grpc_closure* original_on_complete;
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700863} call_data;
864
Craig Tillerbaa14a92017-11-03 09:09:36 -0700865grpc_subchannel_call* grpc_client_channel_get_subchannel_call(
866 grpc_call_element* elem) {
867 call_data* calld = (call_data*)elem->call_data;
Mark D. Roth76e264b2017-08-25 09:03:33 -0700868 return calld->subchannel_call;
Craig Tiller8b1d59c2016-12-27 15:15:30 -0800869}
870
Mark D. Roth76e264b2017-08-25 09:03:33 -0700871// This is called via the call combiner, so access to calld is synchronized.
872static void waiting_for_pick_batches_add(
Craig Tillerbaa14a92017-11-03 09:09:36 -0700873 call_data* calld, grpc_transport_stream_op_batch* batch) {
Mark D. Roth76e264b2017-08-25 09:03:33 -0700874 if (batch->send_initial_metadata) {
Craig Tiller4782d922017-11-10 09:53:21 -0800875 GPR_ASSERT(calld->initial_metadata_batch == nullptr);
Mark D. Roth76e264b2017-08-25 09:03:33 -0700876 calld->initial_metadata_batch = batch;
877 } else {
878 GPR_ASSERT(calld->waiting_for_pick_batches_count < MAX_WAITING_BATCHES);
879 calld->waiting_for_pick_batches[calld->waiting_for_pick_batches_count++] =
880 batch;
881 }
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700882}
883
Mark D. Roth76e264b2017-08-25 09:03:33 -0700884// This is called via the call combiner, so access to calld is synchronized.
Craig Tillerbaa14a92017-11-03 09:09:36 -0700885static void fail_pending_batch_in_call_combiner(grpc_exec_ctx* exec_ctx,
886 void* arg, grpc_error* error) {
887 call_data* calld = (call_data*)arg;
Mark D. Roth76e264b2017-08-25 09:03:33 -0700888 if (calld->waiting_for_pick_batches_count > 0) {
889 --calld->waiting_for_pick_batches_count;
890 grpc_transport_stream_op_batch_finish_with_failure(
891 exec_ctx,
892 calld->waiting_for_pick_batches[calld->waiting_for_pick_batches_count],
893 GRPC_ERROR_REF(error), calld->call_combiner);
894 }
895}
896
897// This is called via the call combiner, so access to calld is synchronized.
Craig Tillerbaa14a92017-11-03 09:09:36 -0700898static void waiting_for_pick_batches_fail(grpc_exec_ctx* exec_ctx,
899 grpc_call_element* elem,
900 grpc_error* error) {
901 call_data* calld = (call_data*)elem->call_data;
Mark D. Roth60751fe2017-07-07 12:50:33 -0700902 if (GRPC_TRACER_ON(grpc_client_channel_trace)) {
903 gpr_log(GPR_DEBUG,
Mark D. Rothe9b10832017-10-26 13:18:25 -0700904 "chand=%p calld=%p: failing %" PRIuPTR " pending batches: %s",
Mark D. Roth60751fe2017-07-07 12:50:33 -0700905 elem->channel_data, calld, calld->waiting_for_pick_batches_count,
906 grpc_error_string(error));
907 }
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700908 for (size_t i = 0; i < calld->waiting_for_pick_batches_count; ++i) {
Mark D. Roth76e264b2017-08-25 09:03:33 -0700909 GRPC_CLOSURE_INIT(&calld->handle_pending_batch_in_call_combiner[i],
910 fail_pending_batch_in_call_combiner, calld,
911 grpc_schedule_on_exec_ctx);
912 GRPC_CALL_COMBINER_START(exec_ctx, calld->call_combiner,
913 &calld->handle_pending_batch_in_call_combiner[i],
914 GRPC_ERROR_REF(error),
915 "waiting_for_pick_batches_fail");
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700916 }
Craig Tiller4782d922017-11-10 09:53:21 -0800917 if (calld->initial_metadata_batch != nullptr) {
Mark D. Roth76e264b2017-08-25 09:03:33 -0700918 grpc_transport_stream_op_batch_finish_with_failure(
919 exec_ctx, calld->initial_metadata_batch, GRPC_ERROR_REF(error),
920 calld->call_combiner);
921 } else {
922 GRPC_CALL_COMBINER_STOP(exec_ctx, calld->call_combiner,
923 "waiting_for_pick_batches_fail");
924 }
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700925 GRPC_ERROR_UNREF(error);
926}
927
Mark D. Roth76e264b2017-08-25 09:03:33 -0700928// This is called via the call combiner, so access to calld is synchronized.
Craig Tillerbaa14a92017-11-03 09:09:36 -0700929static void run_pending_batch_in_call_combiner(grpc_exec_ctx* exec_ctx,
930 void* arg, grpc_error* ignored) {
931 call_data* calld = (call_data*)arg;
Mark D. Roth76e264b2017-08-25 09:03:33 -0700932 if (calld->waiting_for_pick_batches_count > 0) {
933 --calld->waiting_for_pick_batches_count;
934 grpc_subchannel_call_process_op(
935 exec_ctx, calld->subchannel_call,
936 calld->waiting_for_pick_batches[calld->waiting_for_pick_batches_count]);
Craig Tiller57726ca2016-09-12 11:59:45 -0700937 }
Mark D. Roth76e264b2017-08-25 09:03:33 -0700938}
939
940// This is called via the call combiner, so access to calld is synchronized.
Craig Tillerbaa14a92017-11-03 09:09:36 -0700941static void waiting_for_pick_batches_resume(grpc_exec_ctx* exec_ctx,
942 grpc_call_element* elem) {
943 channel_data* chand = (channel_data*)elem->channel_data;
944 call_data* calld = (call_data*)elem->call_data;
Mark D. Roth60751fe2017-07-07 12:50:33 -0700945 if (GRPC_TRACER_ON(grpc_client_channel_trace)) {
Craig Tillerbaa14a92017-11-03 09:09:36 -0700946 gpr_log(GPR_DEBUG,
947 "chand=%p calld=%p: sending %" PRIuPTR
948 " pending batches to subchannel_call=%p",
Mark D. Roth76e264b2017-08-25 09:03:33 -0700949 chand, calld, calld->waiting_for_pick_batches_count,
950 calld->subchannel_call);
Mark D. Roth60751fe2017-07-07 12:50:33 -0700951 }
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700952 for (size_t i = 0; i < calld->waiting_for_pick_batches_count; ++i) {
Mark D. Roth76e264b2017-08-25 09:03:33 -0700953 GRPC_CLOSURE_INIT(&calld->handle_pending_batch_in_call_combiner[i],
954 run_pending_batch_in_call_combiner, calld,
955 grpc_schedule_on_exec_ctx);
956 GRPC_CALL_COMBINER_START(exec_ctx, calld->call_combiner,
957 &calld->handle_pending_batch_in_call_combiner[i],
958 GRPC_ERROR_NONE,
959 "waiting_for_pick_batches_resume");
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700960 }
Craig Tiller4782d922017-11-10 09:53:21 -0800961 GPR_ASSERT(calld->initial_metadata_batch != nullptr);
Mark D. Roth76e264b2017-08-25 09:03:33 -0700962 grpc_subchannel_call_process_op(exec_ctx, calld->subchannel_call,
963 calld->initial_metadata_batch);
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700964}
965
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700966// Applies service config to the call. Must be invoked once we know
967// that the resolver has returned results to the channel.
Craig Tillerbaa14a92017-11-03 09:09:36 -0700968static void apply_service_config_to_call_locked(grpc_exec_ctx* exec_ctx,
969 grpc_call_element* elem) {
970 channel_data* chand = (channel_data*)elem->channel_data;
971 call_data* calld = (call_data*)elem->call_data;
Mark D. Roth60751fe2017-07-07 12:50:33 -0700972 if (GRPC_TRACER_ON(grpc_client_channel_trace)) {
973 gpr_log(GPR_DEBUG, "chand=%p calld=%p: applying service config to call",
974 chand, calld);
975 }
Craig Tiller4782d922017-11-10 09:53:21 -0800976 if (chand->retry_throttle_data != nullptr) {
Mark D. Roth9ccbc4d2017-03-15 08:30:04 -0700977 calld->retry_throttle_data =
978 grpc_server_retry_throttle_data_ref(chand->retry_throttle_data);
979 }
Craig Tiller4782d922017-11-10 09:53:21 -0800980 if (chand->method_params_table != nullptr) {
Craig Tillerbaa14a92017-11-03 09:09:36 -0700981 calld->method_params = (method_parameters*)grpc_method_config_table_get(
Craig Tiller11c17d42017-03-13 13:36:34 -0700982 exec_ctx, chand->method_params_table, calld->path);
Craig Tiller4782d922017-11-10 09:53:21 -0800983 if (calld->method_params != nullptr) {
Craig Tiller11c17d42017-03-13 13:36:34 -0700984 method_parameters_ref(calld->method_params);
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700985 // If the deadline from the service config is shorter than the one
986 // from the client API, reset the deadline timer.
987 if (chand->deadline_checking_enabled &&
Craig Tiller89c14282017-07-19 15:32:27 -0700988 calld->method_params->timeout != 0) {
989 const grpc_millis per_method_deadline =
Craig Tiller9a8c3f32017-07-21 13:14:14 -0700990 grpc_timespec_to_millis_round_up(calld->call_start_time) +
Craig Tiller89c14282017-07-19 15:32:27 -0700991 calld->method_params->timeout;
992 if (per_method_deadline < calld->deadline) {
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700993 calld->deadline = per_method_deadline;
994 grpc_deadline_state_reset(exec_ctx, elem, calld->deadline);
995 }
Craig Tiller11c17d42017-03-13 13:36:34 -0700996 }
997 }
998 }
Craig Tiller11c17d42017-03-13 13:36:34 -0700999}
Craig Tillerea4a4f12017-03-13 13:36:52 -07001000
Craig Tillerbaa14a92017-11-03 09:09:36 -07001001static void create_subchannel_call_locked(grpc_exec_ctx* exec_ctx,
1002 grpc_call_element* elem,
1003 grpc_error* error) {
1004 channel_data* chand = (channel_data*)elem->channel_data;
1005 call_data* calld = (call_data*)elem->call_data;
Mark D. Roth0ca0be82017-06-20 07:49:33 -07001006 const grpc_connected_subchannel_call_args call_args = {
Yash Tibrewald8b84a22017-09-25 13:38:03 -07001007 calld->pollent, // pollent
1008 calld->path, // path
1009 calld->call_start_time, // start_time
1010 calld->deadline, // deadline
1011 calld->arena, // arena
1012 calld->subchannel_call_context, // context
1013 calld->call_combiner // call_combiner
1014 };
Craig Tillerbaa14a92017-11-03 09:09:36 -07001015 grpc_error* new_error = grpc_connected_subchannel_create_call(
Mark D. Roth76e264b2017-08-25 09:03:33 -07001016 exec_ctx, calld->connected_subchannel, &call_args,
1017 &calld->subchannel_call);
Mark D. Roth60751fe2017-07-07 12:50:33 -07001018 if (GRPC_TRACER_ON(grpc_client_channel_trace)) {
1019 gpr_log(GPR_DEBUG, "chand=%p calld=%p: create subchannel_call=%p: error=%s",
Mark D. Roth76e264b2017-08-25 09:03:33 -07001020 chand, calld, calld->subchannel_call, grpc_error_string(new_error));
Mark D. Roth60751fe2017-07-07 12:50:33 -07001021 }
Mark D. Roth0ca0be82017-06-20 07:49:33 -07001022 if (new_error != GRPC_ERROR_NONE) {
1023 new_error = grpc_error_add_child(new_error, error);
Mark D. Roth76e264b2017-08-25 09:03:33 -07001024 waiting_for_pick_batches_fail(exec_ctx, elem, new_error);
Mark D. Roth0ca0be82017-06-20 07:49:33 -07001025 } else {
Mark D. Roth76e264b2017-08-25 09:03:33 -07001026 waiting_for_pick_batches_resume(exec_ctx, elem);
Craig Tiller11c17d42017-03-13 13:36:34 -07001027 }
Mark D. Roth0ca0be82017-06-20 07:49:33 -07001028 GRPC_ERROR_UNREF(error);
Craig Tiller11c17d42017-03-13 13:36:34 -07001029}
1030
Mark D. Rothb2929602017-09-11 09:31:11 -07001031// Invoked when a pick is completed, on both success or failure.
Craig Tillerbaa14a92017-11-03 09:09:36 -07001032static void pick_done_locked(grpc_exec_ctx* exec_ctx, grpc_call_element* elem,
1033 grpc_error* error) {
1034 call_data* calld = (call_data*)elem->call_data;
1035 channel_data* chand = (channel_data*)elem->channel_data;
Craig Tiller4782d922017-11-10 09:53:21 -08001036 if (calld->connected_subchannel == nullptr) {
Mark D. Roth0ca0be82017-06-20 07:49:33 -07001037 // Failed to create subchannel.
Mark D. Roth76e264b2017-08-25 09:03:33 -07001038 GRPC_ERROR_UNREF(calld->error);
1039 calld->error = error == GRPC_ERROR_NONE
1040 ? GRPC_ERROR_CREATE_FROM_STATIC_STRING(
1041 "Call dropped by load balancing policy")
1042 : GRPC_ERROR_CREATE_REFERENCING_FROM_STATIC_STRING(
1043 "Failed to create subchannel", &error, 1);
Mark D. Roth60751fe2017-07-07 12:50:33 -07001044 if (GRPC_TRACER_ON(grpc_client_channel_trace)) {
1045 gpr_log(GPR_DEBUG,
1046 "chand=%p calld=%p: failed to create subchannel: error=%s", chand,
Mark D. Roth76e264b2017-08-25 09:03:33 -07001047 calld, grpc_error_string(calld->error));
Mark D. Roth60751fe2017-07-07 12:50:33 -07001048 }
Mark D. Roth76e264b2017-08-25 09:03:33 -07001049 waiting_for_pick_batches_fail(exec_ctx, elem, GRPC_ERROR_REF(calld->error));
Mark D. Roth2a5959f2016-09-01 08:20:27 -07001050 } else {
Mark D. Roth9fe284e2016-09-12 11:22:27 -07001051 /* Create call on subchannel. */
Mark D. Roth60751fe2017-07-07 12:50:33 -07001052 create_subchannel_call_locked(exec_ctx, elem, GRPC_ERROR_REF(error));
Mark D. Roth2a5959f2016-09-01 08:20:27 -07001053 }
Mark D. Roth0ca0be82017-06-20 07:49:33 -07001054 GRPC_ERROR_UNREF(error);
Mark D. Roth2a5959f2016-09-01 08:20:27 -07001055}
1056
Mark D. Rothb2929602017-09-11 09:31:11 -07001057// A wrapper around pick_done_locked() that is used in cases where
1058// either (a) the pick was deferred pending a resolver result or (b) the
1059// pick was done asynchronously. Removes the call's polling entity from
1060// chand->interested_parties before invoking pick_done_locked().
Craig Tillerbaa14a92017-11-03 09:09:36 -07001061static void async_pick_done_locked(grpc_exec_ctx* exec_ctx,
1062 grpc_call_element* elem, grpc_error* error) {
1063 channel_data* chand = (channel_data*)elem->channel_data;
1064 call_data* calld = (call_data*)elem->call_data;
Mark D. Rothb2929602017-09-11 09:31:11 -07001065 grpc_polling_entity_del_from_pollset_set(exec_ctx, calld->pollent,
1066 chand->interested_parties);
1067 pick_done_locked(exec_ctx, elem, error);
1068}
1069
1070// Note: This runs under the client_channel combiner, but will NOT be
1071// holding the call combiner.
Craig Tillerbaa14a92017-11-03 09:09:36 -07001072static void pick_callback_cancel_locked(grpc_exec_ctx* exec_ctx, void* arg,
1073 grpc_error* error) {
1074 grpc_call_element* elem = (grpc_call_element*)arg;
1075 channel_data* chand = (channel_data*)elem->channel_data;
1076 call_data* calld = (call_data*)elem->call_data;
Craig Tiller4782d922017-11-10 09:53:21 -08001077 if (calld->lb_policy != nullptr) {
Mark D. Rothb2929602017-09-11 09:31:11 -07001078 if (GRPC_TRACER_ON(grpc_client_channel_trace)) {
1079 gpr_log(GPR_DEBUG, "chand=%p calld=%p: cancelling pick from LB policy %p",
1080 chand, calld, calld->lb_policy);
1081 }
1082 grpc_lb_policy_cancel_pick_locked(exec_ctx, calld->lb_policy,
1083 &calld->connected_subchannel,
1084 GRPC_ERROR_REF(error));
1085 }
1086 GRPC_CALL_STACK_UNREF(exec_ctx, calld->owning_call, "pick_callback_cancel");
1087}
1088
1089// Callback invoked by grpc_lb_policy_pick_locked() for async picks.
1090// Unrefs the LB policy and invokes async_pick_done_locked().
Craig Tillerbaa14a92017-11-03 09:09:36 -07001091static void pick_callback_done_locked(grpc_exec_ctx* exec_ctx, void* arg,
1092 grpc_error* error) {
1093 grpc_call_element* elem = (grpc_call_element*)arg;
1094 channel_data* chand = (channel_data*)elem->channel_data;
1095 call_data* calld = (call_data*)elem->call_data;
Mark D. Rothb2929602017-09-11 09:31:11 -07001096 if (GRPC_TRACER_ON(grpc_client_channel_trace)) {
1097 gpr_log(GPR_DEBUG, "chand=%p calld=%p: pick completed asynchronously",
1098 chand, calld);
1099 }
Craig Tiller4782d922017-11-10 09:53:21 -08001100 GPR_ASSERT(calld->lb_policy != nullptr);
Mark D. Rothb2929602017-09-11 09:31:11 -07001101 GRPC_LB_POLICY_UNREF(exec_ctx, calld->lb_policy, "pick_subchannel");
Craig Tiller4782d922017-11-10 09:53:21 -08001102 calld->lb_policy = nullptr;
Mark D. Rothb2929602017-09-11 09:31:11 -07001103 async_pick_done_locked(exec_ctx, elem, GRPC_ERROR_REF(error));
1104}
1105
1106// Takes a ref to chand->lb_policy and calls grpc_lb_policy_pick_locked().
1107// If the pick was completed synchronously, unrefs the LB policy and
1108// returns true.
Craig Tillerbaa14a92017-11-03 09:09:36 -07001109static bool pick_callback_start_locked(grpc_exec_ctx* exec_ctx,
1110 grpc_call_element* elem) {
1111 channel_data* chand = (channel_data*)elem->channel_data;
1112 call_data* calld = (call_data*)elem->call_data;
Mark D. Rothb2929602017-09-11 09:31:11 -07001113 if (GRPC_TRACER_ON(grpc_client_channel_trace)) {
1114 gpr_log(GPR_DEBUG, "chand=%p calld=%p: starting pick on lb_policy=%p",
1115 chand, calld, chand->lb_policy);
1116 }
1117 apply_service_config_to_call_locked(exec_ctx, elem);
1118 // If the application explicitly set wait_for_ready, use that.
1119 // Otherwise, if the service config specified a value for this
1120 // method, use that.
1121 uint32_t initial_metadata_flags =
1122 calld->initial_metadata_batch->payload->send_initial_metadata
1123 .send_initial_metadata_flags;
1124 const bool wait_for_ready_set_from_api =
1125 initial_metadata_flags &
1126 GRPC_INITIAL_METADATA_WAIT_FOR_READY_EXPLICITLY_SET;
1127 const bool wait_for_ready_set_from_service_config =
Craig Tiller4782d922017-11-10 09:53:21 -08001128 calld->method_params != nullptr &&
Mark D. Rothb2929602017-09-11 09:31:11 -07001129 calld->method_params->wait_for_ready != WAIT_FOR_READY_UNSET;
1130 if (!wait_for_ready_set_from_api && wait_for_ready_set_from_service_config) {
1131 if (calld->method_params->wait_for_ready == WAIT_FOR_READY_TRUE) {
1132 initial_metadata_flags |= GRPC_INITIAL_METADATA_WAIT_FOR_READY;
1133 } else {
1134 initial_metadata_flags &= ~GRPC_INITIAL_METADATA_WAIT_FOR_READY;
1135 }
1136 }
1137 const grpc_lb_policy_pick_args inputs = {
1138 calld->initial_metadata_batch->payload->send_initial_metadata
1139 .send_initial_metadata,
1140 initial_metadata_flags, &calld->lb_token_mdelem};
1141 // Keep a ref to the LB policy in calld while the pick is pending.
1142 GRPC_LB_POLICY_REF(chand->lb_policy, "pick_subchannel");
1143 calld->lb_policy = chand->lb_policy;
1144 GRPC_CLOSURE_INIT(&calld->lb_pick_closure, pick_callback_done_locked, elem,
1145 grpc_combiner_scheduler(chand->combiner));
1146 const bool pick_done = grpc_lb_policy_pick_locked(
1147 exec_ctx, chand->lb_policy, &inputs, &calld->connected_subchannel,
Craig Tiller4782d922017-11-10 09:53:21 -08001148 calld->subchannel_call_context, nullptr, &calld->lb_pick_closure);
Mark D. Rothb2929602017-09-11 09:31:11 -07001149 if (pick_done) {
1150 /* synchronous grpc_lb_policy_pick call. Unref the LB policy. */
1151 if (GRPC_TRACER_ON(grpc_client_channel_trace)) {
1152 gpr_log(GPR_DEBUG, "chand=%p calld=%p: pick completed synchronously",
1153 chand, calld);
1154 }
1155 GRPC_LB_POLICY_UNREF(exec_ctx, calld->lb_policy, "pick_subchannel");
Craig Tiller4782d922017-11-10 09:53:21 -08001156 calld->lb_policy = nullptr;
Mark D. Rothb2929602017-09-11 09:31:11 -07001157 } else {
1158 GRPC_CALL_STACK_REF(calld->owning_call, "pick_callback_cancel");
1159 grpc_call_combiner_set_notify_on_cancel(
1160 exec_ctx, calld->call_combiner,
1161 GRPC_CLOSURE_INIT(&calld->lb_pick_cancel_closure,
1162 pick_callback_cancel_locked, elem,
1163 grpc_combiner_scheduler(chand->combiner)));
1164 }
1165 return pick_done;
1166}
Mark D. Roth0ca0be82017-06-20 07:49:33 -07001167
Craig Tiller577c9b22015-11-02 14:11:15 -08001168typedef struct {
Craig Tillerbaa14a92017-11-03 09:09:36 -07001169 grpc_call_element* elem;
Mark D. Roth66f3d2b2017-09-01 09:02:17 -07001170 bool finished;
Craig Tiller577c9b22015-11-02 14:11:15 -08001171 grpc_closure closure;
Mark D. Roth66f3d2b2017-09-01 09:02:17 -07001172 grpc_closure cancel_closure;
Mark D. Roth0ca0be82017-06-20 07:49:33 -07001173} pick_after_resolver_result_args;
Craig Tiller577c9b22015-11-02 14:11:15 -08001174
Mark D. Roth76e264b2017-08-25 09:03:33 -07001175// Note: This runs under the client_channel combiner, but will NOT be
1176// holding the call combiner.
Craig Tillerbaa14a92017-11-03 09:09:36 -07001177static void pick_after_resolver_result_cancel_locked(grpc_exec_ctx* exec_ctx,
1178 void* arg,
1179 grpc_error* error) {
1180 pick_after_resolver_result_args* args = (pick_after_resolver_result_args*)arg;
Mark D. Roth66f3d2b2017-09-01 09:02:17 -07001181 if (args->finished) {
1182 gpr_free(args);
Mark D. Rothb2b9a0f2017-09-01 09:06:47 -07001183 return;
Mark D. Roth764cf042017-09-01 09:00:06 -07001184 }
Mark D. Roth76e264b2017-08-25 09:03:33 -07001185 // If we don't yet have a resolver result, then a closure for
1186 // pick_after_resolver_result_done_locked() will have been added to
1187 // chand->waiting_for_resolver_result_closures, and it may not be invoked
1188 // until after this call has been destroyed. We mark the operation as
Mark D. Rothb2b9a0f2017-09-01 09:06:47 -07001189 // finished, so that when pick_after_resolver_result_done_locked()
Mark D. Roth76e264b2017-08-25 09:03:33 -07001190 // is called, it will be a no-op. We also immediately invoke
Mark D. Rothb2929602017-09-11 09:31:11 -07001191 // async_pick_done_locked() to propagate the error back to the caller.
1192 args->finished = true;
Craig Tillerbaa14a92017-11-03 09:09:36 -07001193 grpc_call_element* elem = args->elem;
1194 channel_data* chand = (channel_data*)elem->channel_data;
1195 call_data* calld = (call_data*)elem->call_data;
Mark D. Rothb2b9a0f2017-09-01 09:06:47 -07001196 if (GRPC_TRACER_ON(grpc_client_channel_trace)) {
1197 gpr_log(GPR_DEBUG,
1198 "chand=%p calld=%p: cancelling pick waiting for resolver result",
1199 chand, calld);
Mark D. Roth76e264b2017-08-25 09:03:33 -07001200 }
Mark D. Rothb2b9a0f2017-09-01 09:06:47 -07001201 // Note: Although we are not in the call combiner here, we are
1202 // basically stealing the call combiner from the pending pick, so
Mark D. Rothb2929602017-09-11 09:31:11 -07001203 // it's safe to call async_pick_done_locked() here -- we are
Mark D. Rothb2b9a0f2017-09-01 09:06:47 -07001204 // essentially calling it here instead of calling it in
1205 // pick_after_resolver_result_done_locked().
Mark D. Rothb2929602017-09-11 09:31:11 -07001206 async_pick_done_locked(exec_ctx, elem,
1207 GRPC_ERROR_CREATE_REFERENCING_FROM_STATIC_STRING(
1208 "Pick cancelled", &error, 1));
Mark D. Roth76e264b2017-08-25 09:03:33 -07001209}
1210
Craig Tillerbaa14a92017-11-03 09:09:36 -07001211static void pick_after_resolver_result_start_locked(grpc_exec_ctx* exec_ctx,
1212 grpc_call_element* elem);
Mark D. Roth48be9de2017-10-23 12:27:37 -07001213
Craig Tillerbaa14a92017-11-03 09:09:36 -07001214static void pick_after_resolver_result_done_locked(grpc_exec_ctx* exec_ctx,
1215 void* arg,
1216 grpc_error* error) {
1217 pick_after_resolver_result_args* args = (pick_after_resolver_result_args*)arg;
Mark D. Roth66f3d2b2017-09-01 09:02:17 -07001218 if (args->finished) {
Craig Tiller577c9b22015-11-02 14:11:15 -08001219 /* cancelled, do nothing */
Mark D. Roth60751fe2017-07-07 12:50:33 -07001220 if (GRPC_TRACER_ON(grpc_client_channel_trace)) {
1221 gpr_log(GPR_DEBUG, "call cancelled before resolver result");
1222 }
Mark D. Roth66f3d2b2017-09-01 09:02:17 -07001223 gpr_free(args);
Mark D. Rothb2b9a0f2017-09-01 09:06:47 -07001224 return;
1225 }
1226 args->finished = true;
Craig Tillerbaa14a92017-11-03 09:09:36 -07001227 grpc_call_element* elem = args->elem;
1228 channel_data* chand = (channel_data*)elem->channel_data;
1229 call_data* calld = (call_data*)elem->call_data;
Mark D. Rothb2b9a0f2017-09-01 09:06:47 -07001230 if (error != GRPC_ERROR_NONE) {
1231 if (GRPC_TRACER_ON(grpc_client_channel_trace)) {
1232 gpr_log(GPR_DEBUG, "chand=%p calld=%p: resolver failed to return data",
1233 chand, calld);
1234 }
Mark D. Rothb2929602017-09-11 09:31:11 -07001235 async_pick_done_locked(exec_ctx, elem, GRPC_ERROR_REF(error));
Craig Tiller4782d922017-11-10 09:53:21 -08001236 } else if (chand->lb_policy != nullptr) {
Mark D. Rothb2b9a0f2017-09-01 09:06:47 -07001237 if (GRPC_TRACER_ON(grpc_client_channel_trace)) {
1238 gpr_log(GPR_DEBUG, "chand=%p calld=%p: resolver returned, doing pick",
1239 chand, calld);
1240 }
Mark D. Rothb2929602017-09-11 09:31:11 -07001241 if (pick_callback_start_locked(exec_ctx, elem)) {
1242 // Even if the LB policy returns a result synchronously, we have
1243 // already added our polling entity to chand->interested_parties
1244 // in order to wait for the resolver result, so we need to
1245 // remove it here. Therefore, we call async_pick_done_locked()
1246 // instead of pick_done_locked().
1247 async_pick_done_locked(exec_ctx, elem, GRPC_ERROR_NONE);
Mark D. Roth9dab7d52016-10-07 07:48:03 -07001248 }
Craig Tiller577c9b22015-11-02 14:11:15 -08001249 }
Mark D. Roth48be9de2017-10-23 12:27:37 -07001250 // TODO(roth): It should be impossible for chand->lb_policy to be NULL
1251 // here, so the rest of this code should never actually be executed.
1252 // However, we have reports of a crash on iOS that triggers this case,
1253 // so we are temporarily adding this to restore branches that were
1254 // removed in https://github.com/grpc/grpc/pull/12297. Need to figure
1255 // out what is actually causing this to occur and then figure out the
1256 // right way to deal with it.
Craig Tiller4782d922017-11-10 09:53:21 -08001257 else if (chand->resolver != nullptr) {
Mark D. Roth48be9de2017-10-23 12:27:37 -07001258 // No LB policy, so try again.
1259 if (GRPC_TRACER_ON(grpc_client_channel_trace)) {
1260 gpr_log(GPR_DEBUG,
1261 "chand=%p calld=%p: resolver returned but no LB policy, "
1262 "trying again",
1263 chand, calld);
1264 }
1265 pick_after_resolver_result_start_locked(exec_ctx, elem);
1266 } else {
1267 if (GRPC_TRACER_ON(grpc_client_channel_trace)) {
1268 gpr_log(GPR_DEBUG, "chand=%p calld=%p: resolver disconnected", chand,
1269 calld);
1270 }
1271 async_pick_done_locked(
1272 exec_ctx, elem, GRPC_ERROR_CREATE_FROM_STATIC_STRING("Disconnected"));
1273 }
Craig Tiller577c9b22015-11-02 14:11:15 -08001274}
1275
Craig Tillerbaa14a92017-11-03 09:09:36 -07001276static void pick_after_resolver_result_start_locked(grpc_exec_ctx* exec_ctx,
1277 grpc_call_element* elem) {
1278 channel_data* chand = (channel_data*)elem->channel_data;
1279 call_data* calld = (call_data*)elem->call_data;
Mark D. Roth60751fe2017-07-07 12:50:33 -07001280 if (GRPC_TRACER_ON(grpc_client_channel_trace)) {
1281 gpr_log(GPR_DEBUG,
1282 "chand=%p calld=%p: deferring pick pending resolver result", chand,
1283 calld);
Mark D. Roth64a317c2017-05-02 08:27:08 -07001284 }
Craig Tillerbaa14a92017-11-03 09:09:36 -07001285 pick_after_resolver_result_args* args =
1286 (pick_after_resolver_result_args*)gpr_zalloc(sizeof(*args));
Mark D. Roth60751fe2017-07-07 12:50:33 -07001287 args->elem = elem;
1288 GRPC_CLOSURE_INIT(&args->closure, pick_after_resolver_result_done_locked,
1289 args, grpc_combiner_scheduler(chand->combiner));
1290 grpc_closure_list_append(&chand->waiting_for_resolver_result_closures,
1291 &args->closure, GRPC_ERROR_NONE);
Mark D. Roth76e264b2017-08-25 09:03:33 -07001292 grpc_call_combiner_set_notify_on_cancel(
1293 exec_ctx, calld->call_combiner,
Mark D. Roth66f3d2b2017-09-01 09:02:17 -07001294 GRPC_CLOSURE_INIT(&args->cancel_closure,
1295 pick_after_resolver_result_cancel_locked, args,
Mark D. Roth76e264b2017-08-25 09:03:33 -07001296 grpc_combiner_scheduler(chand->combiner)));
Mark D. Roth60751fe2017-07-07 12:50:33 -07001297}
1298
Craig Tillerbaa14a92017-11-03 09:09:36 -07001299static void start_pick_locked(grpc_exec_ctx* exec_ctx, void* arg,
1300 grpc_error* ignored) {
1301 grpc_call_element* elem = (grpc_call_element*)arg;
1302 call_data* calld = (call_data*)elem->call_data;
1303 channel_data* chand = (channel_data*)elem->channel_data;
Craig Tiller4782d922017-11-10 09:53:21 -08001304 GPR_ASSERT(calld->connected_subchannel == nullptr);
1305 if (chand->lb_policy != nullptr) {
Mark D. Rothb2929602017-09-11 09:31:11 -07001306 // We already have an LB policy, so ask it for a pick.
1307 if (pick_callback_start_locked(exec_ctx, elem)) {
1308 // Pick completed synchronously.
1309 pick_done_locked(exec_ctx, elem, GRPC_ERROR_NONE);
1310 return;
Mark D. Roth2a5959f2016-09-01 08:20:27 -07001311 }
Mark D. Roth76e264b2017-08-25 09:03:33 -07001312 } else {
Mark D. Rothb2929602017-09-11 09:31:11 -07001313 // We do not yet have an LB policy, so wait for a resolver result.
Craig Tiller4782d922017-11-10 09:53:21 -08001314 if (chand->resolver == nullptr) {
Mark D. Rothb2929602017-09-11 09:31:11 -07001315 pick_done_locked(exec_ctx, elem,
1316 GRPC_ERROR_CREATE_FROM_STATIC_STRING("Disconnected"));
1317 return;
1318 }
1319 if (!chand->started_resolving) {
1320 start_resolving_locked(exec_ctx, chand);
1321 }
1322 pick_after_resolver_result_start_locked(exec_ctx, elem);
Mark D. Roth2a5959f2016-09-01 08:20:27 -07001323 }
Mark D. Rothb2929602017-09-11 09:31:11 -07001324 // We need to wait for either a resolver result or for an async result
1325 // from the LB policy. Add the polling entity from call_data to the
1326 // channel_data's interested_parties, so that the I/O of the LB policy
1327 // and resolver can be done under it. The polling entity will be
1328 // removed in async_pick_done_locked().
1329 grpc_polling_entity_add_to_pollset_set(exec_ctx, calld->pollent,
1330 chand->interested_parties);
Craig Tillera11bfc82017-02-14 09:56:33 -08001331}
1332
Craig Tillerbaa14a92017-11-03 09:09:36 -07001333static void on_complete(grpc_exec_ctx* exec_ctx, void* arg, grpc_error* error) {
1334 grpc_call_element* elem = (grpc_call_element*)arg;
1335 call_data* calld = (call_data*)elem->call_data;
Craig Tiller4782d922017-11-10 09:53:21 -08001336 if (calld->retry_throttle_data != nullptr) {
Mark D. Rothd6d192d2017-02-23 08:58:42 -08001337 if (error == GRPC_ERROR_NONE) {
1338 grpc_server_retry_throttle_data_record_success(
Mark D. Roth9ccbc4d2017-03-15 08:30:04 -07001339 calld->retry_throttle_data);
Mark D. Rothd6d192d2017-02-23 08:58:42 -08001340 } else {
1341 // TODO(roth): In a subsequent PR, check the return value here and
Mark D. Rothb3322562017-02-23 14:38:02 -08001342 // decide whether or not to retry. Note that we should only
1343 // record failures whose statuses match the configured retryable
1344 // or non-fatal status codes.
Mark D. Rothd6d192d2017-02-23 08:58:42 -08001345 grpc_server_retry_throttle_data_record_failure(
Mark D. Roth9ccbc4d2017-03-15 08:30:04 -07001346 calld->retry_throttle_data);
Mark D. Rothd6d192d2017-02-23 08:58:42 -08001347 }
1348 }
ncteisen274bbbe2017-06-08 14:57:11 -07001349 GRPC_CLOSURE_RUN(exec_ctx, calld->original_on_complete,
Mark D. Roth95039b52017-02-24 07:59:45 -08001350 GRPC_ERROR_REF(error));
Mark D. Rothd6d192d2017-02-23 08:58:42 -08001351}
1352
Craig Tillere1b51da2017-03-31 15:44:33 -07001353static void cc_start_transport_stream_op_batch(
Craig Tillerbaa14a92017-11-03 09:09:36 -07001354 grpc_exec_ctx* exec_ctx, grpc_call_element* elem,
1355 grpc_transport_stream_op_batch* batch) {
1356 call_data* calld = (call_data*)elem->call_data;
1357 channel_data* chand = (channel_data*)elem->channel_data;
Craig Tiller3be7dd02017-04-03 14:30:03 -07001358 if (chand->deadline_checking_enabled) {
Craig Tiller29ebc572017-04-04 08:00:55 -07001359 grpc_deadline_state_client_start_transport_stream_op_batch(exec_ctx, elem,
Mark D. Roth60751fe2017-07-07 12:50:33 -07001360 batch);
Craig Tiller3be7dd02017-04-03 14:30:03 -07001361 }
Mark D. Roth76e264b2017-08-25 09:03:33 -07001362 GPR_TIMER_BEGIN("cc_start_transport_stream_op_batch", 0);
1363 // If we've previously been cancelled, immediately fail any new batches.
1364 if (calld->error != GRPC_ERROR_NONE) {
1365 if (GRPC_TRACER_ON(grpc_client_channel_trace)) {
1366 gpr_log(GPR_DEBUG, "chand=%p calld=%p: failing batch with error: %s",
1367 chand, calld, grpc_error_string(calld->error));
1368 }
1369 grpc_transport_stream_op_batch_finish_with_failure(
1370 exec_ctx, batch, GRPC_ERROR_REF(calld->error), calld->call_combiner);
1371 goto done;
1372 }
1373 if (batch->cancel_stream) {
1374 // Stash a copy of cancel_error in our call data, so that we can use
1375 // it for subsequent operations. This ensures that if the call is
1376 // cancelled before any batches are passed down (e.g., if the deadline
1377 // is in the past when the call starts), we can return the right
1378 // error to the caller when the first batch does get passed down.
1379 GRPC_ERROR_UNREF(calld->error);
1380 calld->error = GRPC_ERROR_REF(batch->payload->cancel_stream.cancel_error);
1381 if (GRPC_TRACER_ON(grpc_client_channel_trace)) {
1382 gpr_log(GPR_DEBUG, "chand=%p calld=%p: recording cancel_error=%s", chand,
1383 calld, grpc_error_string(calld->error));
1384 }
1385 // If we have a subchannel call, send the cancellation batch down.
1386 // Otherwise, fail all pending batches.
Craig Tiller4782d922017-11-10 09:53:21 -08001387 if (calld->subchannel_call != nullptr) {
Mark D. Roth76e264b2017-08-25 09:03:33 -07001388 grpc_subchannel_call_process_op(exec_ctx, calld->subchannel_call, batch);
1389 } else {
1390 waiting_for_pick_batches_add(calld, batch);
1391 waiting_for_pick_batches_fail(exec_ctx, elem,
1392 GRPC_ERROR_REF(calld->error));
1393 }
1394 goto done;
1395 }
Mark D. Roth0ca0be82017-06-20 07:49:33 -07001396 // Intercept on_complete for recv_trailing_metadata so that we can
1397 // check retry throttle status.
Mark D. Roth60751fe2017-07-07 12:50:33 -07001398 if (batch->recv_trailing_metadata) {
Craig Tiller4782d922017-11-10 09:53:21 -08001399 GPR_ASSERT(batch->on_complete != nullptr);
Mark D. Roth60751fe2017-07-07 12:50:33 -07001400 calld->original_on_complete = batch->on_complete;
Mark D. Roth0ca0be82017-06-20 07:49:33 -07001401 GRPC_CLOSURE_INIT(&calld->on_complete, on_complete, elem,
1402 grpc_schedule_on_exec_ctx);
Mark D. Roth60751fe2017-07-07 12:50:33 -07001403 batch->on_complete = &calld->on_complete;
Mark D. Roth0ca0be82017-06-20 07:49:33 -07001404 }
Mark D. Roth76e264b2017-08-25 09:03:33 -07001405 // Check if we've already gotten a subchannel call.
1406 // Note that once we have completed the pick, we do not need to enter
1407 // the channel combiner, which is more efficient (especially for
1408 // streaming calls).
Craig Tiller4782d922017-11-10 09:53:21 -08001409 if (calld->subchannel_call != nullptr) {
Mark D. Roth60751fe2017-07-07 12:50:33 -07001410 if (GRPC_TRACER_ON(grpc_client_channel_trace)) {
1411 gpr_log(GPR_DEBUG,
1412 "chand=%p calld=%p: sending batch to subchannel_call=%p", chand,
Mark D. Roth76e264b2017-08-25 09:03:33 -07001413 calld, calld->subchannel_call);
Mark D. Roth60751fe2017-07-07 12:50:33 -07001414 }
Mark D. Roth76e264b2017-08-25 09:03:33 -07001415 grpc_subchannel_call_process_op(exec_ctx, calld->subchannel_call, batch);
Mark D. Roth60751fe2017-07-07 12:50:33 -07001416 goto done;
Mark D. Roth2a5959f2016-09-01 08:20:27 -07001417 }
Mark D. Roth76e264b2017-08-25 09:03:33 -07001418 // We do not yet have a subchannel call.
1419 // Add the batch to the waiting-for-pick list.
1420 waiting_for_pick_batches_add(calld, batch);
1421 // For batches containing a send_initial_metadata op, enter the channel
1422 // combiner to start a pick.
1423 if (batch->send_initial_metadata) {
1424 if (GRPC_TRACER_ON(grpc_client_channel_trace)) {
Mark D. Rothb2929602017-09-11 09:31:11 -07001425 gpr_log(GPR_DEBUG, "chand=%p calld=%p: entering client_channel combiner",
1426 chand, calld);
Mark D. Roth76e264b2017-08-25 09:03:33 -07001427 }
1428 GRPC_CLOSURE_SCHED(
1429 exec_ctx,
1430 GRPC_CLOSURE_INIT(&batch->handler_private.closure, start_pick_locked,
1431 elem, grpc_combiner_scheduler(chand->combiner)),
1432 GRPC_ERROR_NONE);
1433 } else {
1434 // For all other batches, release the call combiner.
1435 if (GRPC_TRACER_ON(grpc_client_channel_trace)) {
1436 gpr_log(GPR_DEBUG,
1437 "chand=%p calld=%p: saved batch, yeilding call combiner", chand,
1438 calld);
1439 }
1440 GRPC_CALL_COMBINER_STOP(exec_ctx, calld->call_combiner,
1441 "batch does not include send_initial_metadata");
Mark D. Roth60751fe2017-07-07 12:50:33 -07001442 }
Mark D. Roth60751fe2017-07-07 12:50:33 -07001443done:
Craig Tillera0f3abd2017-03-31 15:42:16 -07001444 GPR_TIMER_END("cc_start_transport_stream_op_batch", 0);
Mark D. Roth2a5959f2016-09-01 08:20:27 -07001445}
1446
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001447/* Constructor for call_data */
Craig Tillerbaa14a92017-11-03 09:09:36 -07001448static grpc_error* cc_init_call_elem(grpc_exec_ctx* exec_ctx,
1449 grpc_call_element* elem,
1450 const grpc_call_element_args* args) {
1451 call_data* calld = (call_data*)elem->call_data;
1452 channel_data* chand = (channel_data*)elem->channel_data;
Mark D. Rothe40dd292016-10-05 14:58:37 -07001453 // Initialize data members.
Craig Tiller7c70b6c2017-01-23 07:48:42 -08001454 calld->path = grpc_slice_ref_internal(args->path);
Mark D. Rothff08f332016-10-14 13:01:01 -07001455 calld->call_start_time = args->start_time;
Craig Tiller89c14282017-07-19 15:32:27 -07001456 calld->deadline = args->deadline;
Craig Tillerd426cac2017-03-13 12:30:45 -07001457 calld->arena = args->arena;
Mark D. Roth66f3d2b2017-09-01 09:02:17 -07001458 calld->owning_call = args->call_stack;
Mark D. Roth76e264b2017-08-25 09:03:33 -07001459 calld->call_combiner = args->call_combiner;
Craig Tiller3be7dd02017-04-03 14:30:03 -07001460 if (chand->deadline_checking_enabled) {
Mark D. Roth76e264b2017-08-25 09:03:33 -07001461 grpc_deadline_state_init(exec_ctx, elem, args->call_stack,
1462 args->call_combiner, calld->deadline);
Craig Tiller3be7dd02017-04-03 14:30:03 -07001463 }
Mark D. Roth0badbe82016-06-23 10:15:12 -07001464 return GRPC_ERROR_NONE;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001465}
1466
1467/* Destructor for call_data */
Craig Tillerbaa14a92017-11-03 09:09:36 -07001468static void cc_destroy_call_elem(grpc_exec_ctx* exec_ctx,
1469 grpc_call_element* elem,
1470 const grpc_call_final_info* final_info,
1471 grpc_closure* then_schedule_closure) {
1472 call_data* calld = (call_data*)elem->call_data;
1473 channel_data* chand = (channel_data*)elem->channel_data;
Craig Tiller3be7dd02017-04-03 14:30:03 -07001474 if (chand->deadline_checking_enabled) {
1475 grpc_deadline_state_destroy(exec_ctx, elem);
1476 }
Craig Tiller7c70b6c2017-01-23 07:48:42 -08001477 grpc_slice_unref_internal(exec_ctx, calld->path);
Craig Tiller4782d922017-11-10 09:53:21 -08001478 if (calld->method_params != nullptr) {
Mark D. Roth95b627b2017-02-24 11:02:58 -08001479 method_parameters_unref(calld->method_params);
1480 }
Mark D. Roth76e264b2017-08-25 09:03:33 -07001481 GRPC_ERROR_UNREF(calld->error);
Craig Tiller4782d922017-11-10 09:53:21 -08001482 if (calld->subchannel_call != nullptr) {
Mark D. Roth76e264b2017-08-25 09:03:33 -07001483 grpc_subchannel_call_set_cleanup_closure(calld->subchannel_call,
Craig Tillerf7c8c9f2017-05-17 15:22:05 -07001484 then_schedule_closure);
Craig Tiller4782d922017-11-10 09:53:21 -08001485 then_schedule_closure = nullptr;
Mark D. Roth76e264b2017-08-25 09:03:33 -07001486 GRPC_SUBCHANNEL_CALL_UNREF(exec_ctx, calld->subchannel_call,
Craig Tillerf7c8c9f2017-05-17 15:22:05 -07001487 "client_channel_destroy_call");
Mark D. Roth4c0fe492016-08-31 13:51:55 -07001488 }
Craig Tiller4782d922017-11-10 09:53:21 -08001489 GPR_ASSERT(calld->lb_policy == nullptr);
Mark D. Roth0ca0be82017-06-20 07:49:33 -07001490 GPR_ASSERT(calld->waiting_for_pick_batches_count == 0);
Craig Tiller4782d922017-11-10 09:53:21 -08001491 if (calld->connected_subchannel != nullptr) {
Craig Tiller693d3942016-10-27 16:51:25 -07001492 GRPC_CONNECTED_SUBCHANNEL_UNREF(exec_ctx, calld->connected_subchannel,
1493 "picked");
1494 }
Mark D. Roth09e458c2017-05-02 08:13:26 -07001495 for (size_t i = 0; i < GRPC_CONTEXT_COUNT; ++i) {
Craig Tiller4782d922017-11-10 09:53:21 -08001496 if (calld->subchannel_call_context[i].value != nullptr) {
Mark D. Roth09e458c2017-05-02 08:13:26 -07001497 calld->subchannel_call_context[i].destroy(
1498 calld->subchannel_call_context[i].value);
1499 }
1500 }
ncteisen274bbbe2017-06-08 14:57:11 -07001501 GRPC_CLOSURE_SCHED(exec_ctx, then_schedule_closure, GRPC_ERROR_NONE);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001502}
1503
Craig Tillerbaa14a92017-11-03 09:09:36 -07001504static void cc_set_pollset_or_pollset_set(grpc_exec_ctx* exec_ctx,
1505 grpc_call_element* elem,
1506 grpc_polling_entity* pollent) {
1507 call_data* calld = (call_data*)elem->call_data;
David Garcia Quintas2a50dfe2016-05-31 15:09:12 -07001508 calld->pollent = pollent;
Craig Tiller577c9b22015-11-02 14:11:15 -08001509}
1510
Mark D. Roth2a5959f2016-09-01 08:20:27 -07001511/*************************************************************************
1512 * EXPORTED SYMBOLS
1513 */
1514
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001515const grpc_channel_filter grpc_client_channel_filter = {
Craig Tillera0f3abd2017-03-31 15:42:16 -07001516 cc_start_transport_stream_op_batch,
Craig Tillerf40df232016-03-25 13:38:14 -07001517 cc_start_transport_op,
1518 sizeof(call_data),
Mark D. Roth2a5959f2016-09-01 08:20:27 -07001519 cc_init_call_elem,
David Garcia Quintas4afce7e2016-04-18 16:25:17 -07001520 cc_set_pollset_or_pollset_set,
Mark D. Roth2a5959f2016-09-01 08:20:27 -07001521 cc_destroy_call_elem,
Craig Tillerf40df232016-03-25 13:38:14 -07001522 sizeof(channel_data),
Mark D. Roth2a5959f2016-09-01 08:20:27 -07001523 cc_init_channel_elem,
1524 cc_destroy_channel_elem,
Mark D. Rothb2d24882016-10-27 15:44:07 -07001525 cc_get_channel_info,
Craig Tillerf40df232016-03-25 13:38:14 -07001526 "client-channel",
Craig Tiller87d5b192015-04-16 14:37:57 -07001527};
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001528
Craig Tillerbaa14a92017-11-03 09:09:36 -07001529static void try_to_connect_locked(grpc_exec_ctx* exec_ctx, void* arg,
1530 grpc_error* error_ignored) {
1531 channel_data* chand = (channel_data*)arg;
Craig Tiller4782d922017-11-10 09:53:21 -08001532 if (chand->lb_policy != nullptr) {
Craig Tiller2400bf52017-02-09 16:25:19 -08001533 grpc_lb_policy_exit_idle_locked(exec_ctx, chand->lb_policy);
Craig Tiller613dafa2017-02-09 12:00:43 -08001534 } else {
1535 chand->exit_idle_when_lb_policy_arrives = true;
Craig Tiller4782d922017-11-10 09:53:21 -08001536 if (!chand->started_resolving && chand->resolver != nullptr) {
Mark D. Roth60751fe2017-07-07 12:50:33 -07001537 start_resolving_locked(exec_ctx, chand);
Craig Tiller613dafa2017-02-09 12:00:43 -08001538 }
1539 }
Craig Tillerd2e5cfc2017-02-09 13:02:20 -08001540 GRPC_CHANNEL_STACK_UNREF(exec_ctx, chand->owning_stack, "try_to_connect");
Craig Tiller613dafa2017-02-09 12:00:43 -08001541}
1542
Craig Tillera82950e2015-09-22 12:33:20 -07001543grpc_connectivity_state grpc_client_channel_check_connectivity_state(
Craig Tillerbaa14a92017-11-03 09:09:36 -07001544 grpc_exec_ctx* exec_ctx, grpc_channel_element* elem, int try_to_connect) {
1545 channel_data* chand = (channel_data*)elem->channel_data;
Craig Tillera8610c02017-02-14 10:05:11 -08001546 grpc_connectivity_state out =
1547 grpc_connectivity_state_check(&chand->state_tracker);
Craig Tillera82950e2015-09-22 12:33:20 -07001548 if (out == GRPC_CHANNEL_IDLE && try_to_connect) {
Craig Tillerd2e5cfc2017-02-09 13:02:20 -08001549 GRPC_CHANNEL_STACK_REF(chand->owning_stack, "try_to_connect");
ncteisen274bbbe2017-06-08 14:57:11 -07001550 GRPC_CLOSURE_SCHED(
Craig Tillerbaa14a92017-11-03 09:09:36 -07001551 exec_ctx,
1552 GRPC_CLOSURE_CREATE(try_to_connect_locked, chand,
1553 grpc_combiner_scheduler(chand->combiner)),
Craig Tiller613dafa2017-02-09 12:00:43 -08001554 GRPC_ERROR_NONE);
Craig Tillera82950e2015-09-22 12:33:20 -07001555 }
Craig Tiller48cb07c2015-07-15 16:16:15 -07001556 return out;
1557}
1558
Alexander Polcync3b1f182017-04-18 13:51:36 -07001559typedef struct external_connectivity_watcher {
Craig Tillerbaa14a92017-11-03 09:09:36 -07001560 channel_data* chand;
David Garcia Quintas87d5a312017-06-06 19:45:58 -07001561 grpc_polling_entity pollent;
Craig Tillerbaa14a92017-11-03 09:09:36 -07001562 grpc_closure* on_complete;
1563 grpc_closure* watcher_timer_init;
1564 grpc_connectivity_state* state;
Craig Tiller86c99582015-11-25 15:22:26 -08001565 grpc_closure my_closure;
Craig Tillerbaa14a92017-11-03 09:09:36 -07001566 struct external_connectivity_watcher* next;
Craig Tiller86c99582015-11-25 15:22:26 -08001567} external_connectivity_watcher;
1568
Craig Tillerbaa14a92017-11-03 09:09:36 -07001569static external_connectivity_watcher* lookup_external_connectivity_watcher(
1570 channel_data* chand, grpc_closure* on_complete) {
Alexander Polcync3b1f182017-04-18 13:51:36 -07001571 gpr_mu_lock(&chand->external_connectivity_watcher_list_mu);
Craig Tillerbaa14a92017-11-03 09:09:36 -07001572 external_connectivity_watcher* w =
Alexander Polcync3b1f182017-04-18 13:51:36 -07001573 chand->external_connectivity_watcher_list_head;
Craig Tiller4782d922017-11-10 09:53:21 -08001574 while (w != nullptr && w->on_complete != on_complete) {
Alexander Polcync3b1f182017-04-18 13:51:36 -07001575 w = w->next;
1576 }
1577 gpr_mu_unlock(&chand->external_connectivity_watcher_list_mu);
1578 return w;
1579}
1580
1581static void external_connectivity_watcher_list_append(
Craig Tillerbaa14a92017-11-03 09:09:36 -07001582 channel_data* chand, external_connectivity_watcher* w) {
Alexander Polcync3b1f182017-04-18 13:51:36 -07001583 GPR_ASSERT(!lookup_external_connectivity_watcher(chand, w->on_complete));
1584
1585 gpr_mu_lock(&w->chand->external_connectivity_watcher_list_mu);
1586 GPR_ASSERT(!w->next);
1587 w->next = chand->external_connectivity_watcher_list_head;
1588 chand->external_connectivity_watcher_list_head = w;
1589 gpr_mu_unlock(&w->chand->external_connectivity_watcher_list_mu);
1590}
1591
1592static void external_connectivity_watcher_list_remove(
Craig Tillerbaa14a92017-11-03 09:09:36 -07001593 channel_data* chand, external_connectivity_watcher* too_remove) {
Alexander Polcync3b1f182017-04-18 13:51:36 -07001594 GPR_ASSERT(
1595 lookup_external_connectivity_watcher(chand, too_remove->on_complete));
1596 gpr_mu_lock(&chand->external_connectivity_watcher_list_mu);
1597 if (too_remove == chand->external_connectivity_watcher_list_head) {
1598 chand->external_connectivity_watcher_list_head = too_remove->next;
1599 gpr_mu_unlock(&chand->external_connectivity_watcher_list_mu);
1600 return;
1601 }
Craig Tillerbaa14a92017-11-03 09:09:36 -07001602 external_connectivity_watcher* w =
Alexander Polcync3b1f182017-04-18 13:51:36 -07001603 chand->external_connectivity_watcher_list_head;
Craig Tiller4782d922017-11-10 09:53:21 -08001604 while (w != nullptr) {
Alexander Polcync3b1f182017-04-18 13:51:36 -07001605 if (w->next == too_remove) {
1606 w->next = w->next->next;
1607 gpr_mu_unlock(&chand->external_connectivity_watcher_list_mu);
1608 return;
1609 }
1610 w = w->next;
1611 }
1612 GPR_UNREACHABLE_CODE(return );
1613}
1614
1615int grpc_client_channel_num_external_connectivity_watchers(
Craig Tillerbaa14a92017-11-03 09:09:36 -07001616 grpc_channel_element* elem) {
1617 channel_data* chand = (channel_data*)elem->channel_data;
Alexander Polcync3b1f182017-04-18 13:51:36 -07001618 int count = 0;
1619
1620 gpr_mu_lock(&chand->external_connectivity_watcher_list_mu);
Craig Tillerbaa14a92017-11-03 09:09:36 -07001621 external_connectivity_watcher* w =
Alexander Polcync3b1f182017-04-18 13:51:36 -07001622 chand->external_connectivity_watcher_list_head;
Craig Tiller4782d922017-11-10 09:53:21 -08001623 while (w != nullptr) {
Alexander Polcync3b1f182017-04-18 13:51:36 -07001624 count++;
1625 w = w->next;
1626 }
1627 gpr_mu_unlock(&chand->external_connectivity_watcher_list_mu);
1628
1629 return count;
1630}
1631
Craig Tillerbaa14a92017-11-03 09:09:36 -07001632static void on_external_watch_complete_locked(grpc_exec_ctx* exec_ctx,
1633 void* arg, grpc_error* error) {
1634 external_connectivity_watcher* w = (external_connectivity_watcher*)arg;
1635 grpc_closure* follow_up = w->on_complete;
David Garcia Quintas87d5a312017-06-06 19:45:58 -07001636 grpc_polling_entity_del_from_pollset_set(exec_ctx, &w->pollent,
1637 w->chand->interested_parties);
Craig Tiller1d881fb2015-12-01 07:39:04 -08001638 GRPC_CHANNEL_STACK_UNREF(exec_ctx, w->chand->owning_stack,
1639 "external_connectivity_watcher");
Alexander Polcync3b1f182017-04-18 13:51:36 -07001640 external_connectivity_watcher_list_remove(w->chand, w);
Craig Tiller86c99582015-11-25 15:22:26 -08001641 gpr_free(w);
ncteisen274bbbe2017-06-08 14:57:11 -07001642 GRPC_CLOSURE_RUN(exec_ctx, follow_up, GRPC_ERROR_REF(error));
Craig Tiller613dafa2017-02-09 12:00:43 -08001643}
1644
Craig Tillerbaa14a92017-11-03 09:09:36 -07001645static void watch_connectivity_state_locked(grpc_exec_ctx* exec_ctx, void* arg,
1646 grpc_error* error_ignored) {
1647 external_connectivity_watcher* w = (external_connectivity_watcher*)arg;
Craig Tiller4782d922017-11-10 09:53:21 -08001648 external_connectivity_watcher* found = nullptr;
1649 if (w->state != nullptr) {
Alexander Polcync3b1f182017-04-18 13:51:36 -07001650 external_connectivity_watcher_list_append(w->chand, w);
ncteisen274bbbe2017-06-08 14:57:11 -07001651 GRPC_CLOSURE_RUN(exec_ctx, w->watcher_timer_init, GRPC_ERROR_NONE);
Alexander Polcyn2004e392017-10-16 15:14:46 -07001652 GRPC_CLOSURE_INIT(&w->my_closure, on_external_watch_complete_locked, w,
1653 grpc_combiner_scheduler(w->chand->combiner));
Alexander Polcync3b1f182017-04-18 13:51:36 -07001654 grpc_connectivity_state_notify_on_state_change(
1655 exec_ctx, &w->chand->state_tracker, w->state, &w->my_closure);
1656 } else {
Craig Tiller4782d922017-11-10 09:53:21 -08001657 GPR_ASSERT(w->watcher_timer_init == nullptr);
Alexander Polcync3b1f182017-04-18 13:51:36 -07001658 found = lookup_external_connectivity_watcher(w->chand, w->on_complete);
1659 if (found) {
1660 GPR_ASSERT(found->on_complete == w->on_complete);
1661 grpc_connectivity_state_notify_on_state_change(
Craig Tiller4782d922017-11-10 09:53:21 -08001662 exec_ctx, &found->chand->state_tracker, nullptr, &found->my_closure);
Alexander Polcync3b1f182017-04-18 13:51:36 -07001663 }
David Garcia Quintas87d5a312017-06-06 19:45:58 -07001664 grpc_polling_entity_del_from_pollset_set(exec_ctx, &w->pollent,
1665 w->chand->interested_parties);
Alexander Polcync3b1f182017-04-18 13:51:36 -07001666 GRPC_CHANNEL_STACK_UNREF(exec_ctx, w->chand->owning_stack,
1667 "external_connectivity_watcher");
1668 gpr_free(w);
1669 }
Craig Tiller86c99582015-11-25 15:22:26 -08001670}
1671
Craig Tillera82950e2015-09-22 12:33:20 -07001672void grpc_client_channel_watch_connectivity_state(
Craig Tillerbaa14a92017-11-03 09:09:36 -07001673 grpc_exec_ctx* exec_ctx, grpc_channel_element* elem,
1674 grpc_polling_entity pollent, grpc_connectivity_state* state,
1675 grpc_closure* closure, grpc_closure* watcher_timer_init) {
1676 channel_data* chand = (channel_data*)elem->channel_data;
1677 external_connectivity_watcher* w =
1678 (external_connectivity_watcher*)gpr_zalloc(sizeof(*w));
Craig Tiller86c99582015-11-25 15:22:26 -08001679 w->chand = chand;
David Garcia Quintas87d5a312017-06-06 19:45:58 -07001680 w->pollent = pollent;
Mark D. Roth92210832017-05-02 15:04:39 -07001681 w->on_complete = closure;
Craig Tiller613dafa2017-02-09 12:00:43 -08001682 w->state = state;
Alexander Polcync3b1f182017-04-18 13:51:36 -07001683 w->watcher_timer_init = watcher_timer_init;
David Garcia Quintas87d5a312017-06-06 19:45:58 -07001684 grpc_polling_entity_add_to_pollset_set(exec_ctx, &w->pollent,
1685 chand->interested_parties);
Craig Tiller1d881fb2015-12-01 07:39:04 -08001686 GRPC_CHANNEL_STACK_REF(w->chand->owning_stack,
1687 "external_connectivity_watcher");
ncteisen274bbbe2017-06-08 14:57:11 -07001688 GRPC_CLOSURE_SCHED(
Craig Tiller613dafa2017-02-09 12:00:43 -08001689 exec_ctx,
ncteisen274bbbe2017-06-08 14:57:11 -07001690 GRPC_CLOSURE_INIT(&w->my_closure, watch_connectivity_state_locked, w,
Craig Tilleree4b1452017-05-12 10:56:03 -07001691 grpc_combiner_scheduler(chand->combiner)),
Craig Tiller613dafa2017-02-09 12:00:43 -08001692 GRPC_ERROR_NONE);
Craig Tiller48cb07c2015-07-15 16:16:15 -07001693}