blob: e99022a91b84eda73bba5a56271b3678dc87afb7 [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
Craig Tiller694580f2017-10-18 14:48:14 -070059grpc_core::TraceFlag grpc_client_channel_trace(false, "client_channel");
Mark D. Roth60751fe2017-07-07 12:50:33 -070060
Mark D. Roth26b7be42016-10-24 10:08:07 -070061/*************************************************************************
62 * METHOD-CONFIG TABLE
63 */
64
Mark D. Roth9d480942016-10-19 14:18:05 -070065typedef enum {
Craig Tiller7acc37e2017-02-28 10:01:37 -080066 /* zero so it can be default initialized */
67 WAIT_FOR_READY_UNSET = 0,
Mark D. Roth9d480942016-10-19 14:18:05 -070068 WAIT_FOR_READY_FALSE,
69 WAIT_FOR_READY_TRUE
70} wait_for_ready_value;
71
Mark D. Roth95b627b2017-02-24 11:02:58 -080072typedef struct {
73 gpr_refcount refs;
Craig Tiller89c14282017-07-19 15:32:27 -070074 grpc_millis timeout;
Mark D. Roth9d480942016-10-19 14:18:05 -070075 wait_for_ready_value wait_for_ready;
76} method_parameters;
77
Craig Tillerbaa14a92017-11-03 09:09:36 -070078static method_parameters* method_parameters_ref(
79 method_parameters* method_params) {
Mark D. Roth95b627b2017-02-24 11:02:58 -080080 gpr_ref(&method_params->refs);
81 return method_params;
Mark D. Roth9d480942016-10-19 14:18:05 -070082}
83
Craig Tillerbaa14a92017-11-03 09:09:36 -070084static void method_parameters_unref(method_parameters* method_params) {
Mark D. Roth95b627b2017-02-24 11:02:58 -080085 if (gpr_unref(&method_params->refs)) {
86 gpr_free(method_params);
87 }
88}
89
Mark D. Roth76d0ec42017-10-26 11:08:14 -070090// Wrappers to pass to grpc_service_config_create_method_config_table().
Craig Tillera64b2b12017-11-03 15:23:13 -070091static void* method_parameters_ref_wrapper(void* value) {
92 return method_parameters_ref((method_parameters*)value);
Mark D. Roth76d0ec42017-10-26 11:08:14 -070093}
Yash Tibrewal8cf14702017-12-06 09:47:54 -080094static void method_parameters_unref_wrapper(void* value) {
Craig Tillerbaa14a92017-11-03 09:09:36 -070095 method_parameters_unref((method_parameters*)value);
Craig Tiller87a7e1f2016-11-09 09:42:19 -080096}
97
Craig Tillerbaa14a92017-11-03 09:09:36 -070098static bool parse_wait_for_ready(grpc_json* field,
99 wait_for_ready_value* wait_for_ready) {
Mark D. Roth95b627b2017-02-24 11:02:58 -0800100 if (field->type != GRPC_JSON_TRUE && field->type != GRPC_JSON_FALSE) {
101 return false;
102 }
103 *wait_for_ready = field->type == GRPC_JSON_TRUE ? WAIT_FOR_READY_TRUE
104 : WAIT_FOR_READY_FALSE;
105 return true;
106}
107
Craig Tillerbaa14a92017-11-03 09:09:36 -0700108static bool parse_timeout(grpc_json* field, grpc_millis* timeout) {
Mark D. Roth95b627b2017-02-24 11:02:58 -0800109 if (field->type != GRPC_JSON_STRING) return false;
110 size_t len = strlen(field->value);
111 if (field->value[len - 1] != 's') return false;
Craig Tillerbaa14a92017-11-03 09:09:36 -0700112 char* buf = gpr_strdup(field->value);
Mark D. Roth95b627b2017-02-24 11:02:58 -0800113 buf[len - 1] = '\0'; // Remove trailing 's'.
Craig Tillerbaa14a92017-11-03 09:09:36 -0700114 char* decimal_point = strchr(buf, '.');
Craig Tiller89c14282017-07-19 15:32:27 -0700115 int nanos = 0;
Craig Tiller4782d922017-11-10 09:53:21 -0800116 if (decimal_point != nullptr) {
Mark D. Roth95b627b2017-02-24 11:02:58 -0800117 *decimal_point = '\0';
Craig Tiller89c14282017-07-19 15:32:27 -0700118 nanos = gpr_parse_nonnegative_int(decimal_point + 1);
119 if (nanos == -1) {
Mark D. Roth95b627b2017-02-24 11:02:58 -0800120 gpr_free(buf);
121 return false;
122 }
Mark D. Rotha2821462017-10-26 11:31:58 -0700123 int num_digits = (int)strlen(decimal_point + 1);
124 if (num_digits > 9) { // We don't accept greater precision than nanos.
125 gpr_free(buf);
126 return false;
Mark D. Roth95b627b2017-02-24 11:02:58 -0800127 }
Mark D. Rotha2821462017-10-26 11:31:58 -0700128 for (int i = 0; i < (9 - num_digits); ++i) {
129 nanos *= 10;
130 }
Mark D. Roth95b627b2017-02-24 11:02:58 -0800131 }
Mark D. Rotha2821462017-10-26 11:31:58 -0700132 int seconds = decimal_point == buf ? 0 : gpr_parse_nonnegative_int(buf);
Mark D. Roth95b627b2017-02-24 11:02:58 -0800133 gpr_free(buf);
Craig Tiller89c14282017-07-19 15:32:27 -0700134 if (seconds == -1) return false;
135 *timeout = seconds * GPR_MS_PER_SEC + nanos / GPR_NS_PER_MS;
Mark D. Roth95b627b2017-02-24 11:02:58 -0800136 return true;
137}
138
Craig Tillerbaa14a92017-11-03 09:09:36 -0700139static void* method_parameters_create_from_json(const grpc_json* json) {
Mark D. Rothc968e602016-11-02 14:07:36 -0700140 wait_for_ready_value wait_for_ready = WAIT_FOR_READY_UNSET;
Craig Tiller89c14282017-07-19 15:32:27 -0700141 grpc_millis timeout = 0;
Craig Tiller4782d922017-11-10 09:53:21 -0800142 for (grpc_json* field = json->child; field != nullptr; field = field->next) {
143 if (field->key == nullptr) continue;
Mark D. Roth84c8a022016-11-10 09:39:34 -0800144 if (strcmp(field->key, "waitForReady") == 0) {
Craig Tiller4782d922017-11-10 09:53:21 -0800145 if (wait_for_ready != WAIT_FOR_READY_UNSET) return nullptr; // Duplicate.
146 if (!parse_wait_for_ready(field, &wait_for_ready)) return nullptr;
Mark D. Rothc968e602016-11-02 14:07:36 -0700147 } else if (strcmp(field->key, "timeout") == 0) {
Craig Tiller4782d922017-11-10 09:53:21 -0800148 if (timeout > 0) return nullptr; // Duplicate.
149 if (!parse_timeout(field, &timeout)) return nullptr;
Mark D. Rothc968e602016-11-02 14:07:36 -0700150 }
151 }
Craig Tillerbaa14a92017-11-03 09:09:36 -0700152 method_parameters* value =
153 (method_parameters*)gpr_malloc(sizeof(method_parameters));
Mark D. Roth95b627b2017-02-24 11:02:58 -0800154 gpr_ref_init(&value->refs, 1);
Mark D. Rothc968e602016-11-02 14:07:36 -0700155 value->timeout = timeout;
156 value->wait_for_ready = wait_for_ready;
Mark D. Roth9d480942016-10-19 14:18:05 -0700157 return value;
158}
159
Alexander Polcync3b1f182017-04-18 13:51:36 -0700160struct external_connectivity_watcher;
161
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700162/*************************************************************************
163 * CHANNEL-WIDE FUNCTIONS
164 */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800165
Craig Tiller800dacb2015-10-06 09:10:26 -0700166typedef struct client_channel_channel_data {
Craig Tillerf5f17122015-06-25 08:47:26 -0700167 /** resolver for this channel */
Craig Tillerbaa14a92017-11-03 09:09:36 -0700168 grpc_resolver* resolver;
Craig Tiller20a3c352015-08-05 08:39:50 -0700169 /** have we started resolving this channel */
Mark D. Roth4c0fe492016-08-31 13:51:55 -0700170 bool started_resolving;
Craig Tiller3be7dd02017-04-03 14:30:03 -0700171 /** is deadline checking enabled? */
172 bool deadline_checking_enabled;
Mark D. Roth0e48a9a2016-09-08 14:14:39 -0700173 /** client channel factory */
Craig Tillerbaa14a92017-11-03 09:09:36 -0700174 grpc_client_channel_factory* client_channel_factory;
Craig Tillerf5f17122015-06-25 08:47:26 -0700175
Craig Tillerbefafe62017-02-09 11:30:54 -0800176 /** combiner protecting all variables below in this data structure */
Craig Tillerbaa14a92017-11-03 09:09:36 -0700177 grpc_combiner* combiner;
Mark D. Roth046cf762016-09-26 11:13:51 -0700178 /** currently active load balancer */
Craig Tillerbaa14a92017-11-03 09:09:36 -0700179 grpc_lb_policy* lb_policy;
Mark D. Rothd6d192d2017-02-23 08:58:42 -0800180 /** retry throttle data */
Craig Tillerbaa14a92017-11-03 09:09:36 -0700181 grpc_server_retry_throttle_data* retry_throttle_data;
Mark D. Roth9d480942016-10-19 14:18:05 -0700182 /** maps method names to method_parameters structs */
Craig Tillerbaa14a92017-11-03 09:09:36 -0700183 grpc_slice_hash_table* method_params_table;
Mark D. Roth046cf762016-09-26 11:13:51 -0700184 /** incoming resolver result - set by resolver.next() */
Craig Tillerbaa14a92017-11-03 09:09:36 -0700185 grpc_channel_args* resolver_result;
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700186 /** a list of closures that are all waiting for resolver result to come in */
187 grpc_closure_list waiting_for_resolver_result_closures;
Craig Tiller3f475422015-06-25 10:43:05 -0700188 /** resolver callback */
Mark D. Rothff4df062016-08-22 15:02:49 -0700189 grpc_closure on_resolver_result_changed;
Craig Tiller3f475422015-06-25 10:43:05 -0700190 /** connectivity state being tracked */
Craig Tillerca3e9d32015-06-27 18:37:27 -0700191 grpc_connectivity_state_tracker state_tracker;
Craig Tiller48cb07c2015-07-15 16:16:15 -0700192 /** when an lb_policy arrives, should we try to exit idle */
Mark D. Roth4c0fe492016-08-31 13:51:55 -0700193 bool exit_idle_when_lb_policy_arrives;
Craig Tiller906e3bc2015-11-24 07:31:31 -0800194 /** owning stack */
Craig Tillerbaa14a92017-11-03 09:09:36 -0700195 grpc_channel_stack* owning_stack;
Craig Tiller69b093b2016-02-25 19:04:07 -0800196 /** interested parties (owned) */
Craig Tillerbaa14a92017-11-03 09:09:36 -0700197 grpc_pollset_set* interested_parties;
Craig Tiller613dafa2017-02-09 12:00:43 -0800198
Alexander Polcync3b1f182017-04-18 13:51:36 -0700199 /* external_connectivity_watcher_list head is guarded by its own mutex, since
200 * counts need to be grabbed immediately without polling on a cq */
201 gpr_mu external_connectivity_watcher_list_mu;
Craig Tillerbaa14a92017-11-03 09:09:36 -0700202 struct external_connectivity_watcher* external_connectivity_watcher_list_head;
Alexander Polcync3b1f182017-04-18 13:51:36 -0700203
Craig Tiller613dafa2017-02-09 12:00:43 -0800204 /* the following properties are guarded by a mutex since API's require them
Craig Tiller46dd7902017-02-23 09:42:16 -0800205 to be instantaneously available */
Craig Tiller613dafa2017-02-09 12:00:43 -0800206 gpr_mu info_mu;
Craig Tillerbaa14a92017-11-03 09:09:36 -0700207 char* info_lb_policy_name;
Craig Tiller613dafa2017-02-09 12:00:43 -0800208 /** service config in JSON form */
Craig Tillerbaa14a92017-11-03 09:09:36 -0700209 char* info_service_config_json;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800210} channel_data;
211
Juanli Shen592cf342017-12-04 20:52:01 -0800212typedef struct {
213 channel_data* chand;
214 /** used as an identifier, don't dereference it because the LB policy may be
215 * non-existing when the callback is run */
216 grpc_lb_policy* lb_policy;
217 grpc_closure closure;
218} reresolution_request_args;
219
Craig Tillerd6c98df2015-08-18 09:33:44 -0700220/** We create one watcher for each new lb_policy that is returned from a
Mark D. Roth4c0fe492016-08-31 13:51:55 -0700221 resolver, to watch for state changes from the lb_policy. When a state
222 change is seen, we update the channel, and create a new watcher. */
Craig Tillera82950e2015-09-22 12:33:20 -0700223typedef struct {
Craig Tillerbaa14a92017-11-03 09:09:36 -0700224 channel_data* chand;
Craig Tiller33825112015-09-18 07:44:19 -0700225 grpc_closure on_changed;
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700226 grpc_connectivity_state state;
Craig Tillerbaa14a92017-11-03 09:09:36 -0700227 grpc_lb_policy* lb_policy;
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700228} lb_policy_connectivity_watcher;
229
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800230static void watch_lb_policy_locked(channel_data* chand,
Craig Tillerbaa14a92017-11-03 09:09:36 -0700231 grpc_lb_policy* lb_policy,
Craig Tiller2400bf52017-02-09 16:25:19 -0800232 grpc_connectivity_state current_state);
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700233
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800234static void set_channel_connectivity_state_locked(channel_data* chand,
Craig Tiller8c0d96f2016-03-11 14:27:52 -0800235 grpc_connectivity_state state,
Craig Tillerbaa14a92017-11-03 09:09:36 -0700236 grpc_error* error,
237 const char* reason) {
David Garcia Quintas37251282017-04-14 13:46:03 -0700238 /* TODO: Improve failure handling:
239 * - Make it possible for policies to return GRPC_CHANNEL_TRANSIENT_FAILURE.
240 * - Hand over pending picks from old policies during the switch that happens
241 * when resolver provides an update. */
Craig Tiller4782d922017-11-10 09:53:21 -0800242 if (chand->lb_policy != nullptr) {
David Garcia Quintas956f7002017-04-13 15:40:06 -0700243 if (state == GRPC_CHANNEL_TRANSIENT_FAILURE) {
244 /* cancel picks with wait_for_ready=false */
245 grpc_lb_policy_cancel_picks_locked(
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800246 chand->lb_policy,
David Garcia Quintas956f7002017-04-13 15:40:06 -0700247 /* mask= */ GRPC_INITIAL_METADATA_WAIT_FOR_READY,
248 /* check= */ 0, GRPC_ERROR_REF(error));
249 } else if (state == GRPC_CHANNEL_SHUTDOWN) {
250 /* cancel all picks */
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800251 grpc_lb_policy_cancel_picks_locked(chand->lb_policy,
David Garcia Quintas956f7002017-04-13 15:40:06 -0700252 /* mask= */ 0, /* check= */ 0,
253 GRPC_ERROR_REF(error));
254 }
Craig Tiller8c0d96f2016-03-11 14:27:52 -0800255 }
Craig Tiller6014e8a2017-10-16 13:50:29 -0700256 if (grpc_client_channel_trace.enabled()) {
Mark D. Roth60751fe2017-07-07 12:50:33 -0700257 gpr_log(GPR_DEBUG, "chand=%p: setting connectivity state to %s", chand,
258 grpc_connectivity_state_name(state));
259 }
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800260 grpc_connectivity_state_set(&chand->state_tracker, state, error, reason);
Craig Tiller8c0d96f2016-03-11 14:27:52 -0800261}
262
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800263static void on_lb_policy_state_changed_locked(void* arg, grpc_error* error) {
Craig Tillerbaa14a92017-11-03 09:09:36 -0700264 lb_policy_connectivity_watcher* w = (lb_policy_connectivity_watcher*)arg;
Craig Tillerc5de8352017-02-09 14:08:05 -0800265 /* check if the notification is for the latest policy */
266 if (w->lb_policy == w->chand->lb_policy) {
Craig Tiller6014e8a2017-10-16 13:50:29 -0700267 if (grpc_client_channel_trace.enabled()) {
Mark D. Roth60751fe2017-07-07 12:50:33 -0700268 gpr_log(GPR_DEBUG, "chand=%p: lb_policy=%p state changed to %s", w->chand,
269 w->lb_policy, grpc_connectivity_state_name(w->state));
270 }
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800271 set_channel_connectivity_state_locked(w->chand, w->state,
Craig Tillerc5de8352017-02-09 14:08:05 -0800272 GRPC_ERROR_REF(error), "lb_changed");
273 if (w->state != GRPC_CHANNEL_SHUTDOWN) {
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800274 watch_lb_policy_locked(w->chand, w->lb_policy, w->state);
Craig Tillerc5de8352017-02-09 14:08:05 -0800275 }
Craig Tillera82950e2015-09-22 12:33:20 -0700276 }
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800277 GRPC_CHANNEL_STACK_UNREF(w->chand->owning_stack, "watch_lb_policy");
Craig Tillera82950e2015-09-22 12:33:20 -0700278 gpr_free(w);
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700279}
280
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800281static void watch_lb_policy_locked(channel_data* chand,
Craig Tillerbaa14a92017-11-03 09:09:36 -0700282 grpc_lb_policy* lb_policy,
Craig Tiller2400bf52017-02-09 16:25:19 -0800283 grpc_connectivity_state current_state) {
Craig Tillerbaa14a92017-11-03 09:09:36 -0700284 lb_policy_connectivity_watcher* w =
285 (lb_policy_connectivity_watcher*)gpr_malloc(sizeof(*w));
Craig Tiller906e3bc2015-11-24 07:31:31 -0800286 GRPC_CHANNEL_STACK_REF(chand->owning_stack, "watch_lb_policy");
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700287 w->chand = chand;
ncteisen274bbbe2017-06-08 14:57:11 -0700288 GRPC_CLOSURE_INIT(&w->on_changed, on_lb_policy_state_changed_locked, w,
Craig Tilleree4b1452017-05-12 10:56:03 -0700289 grpc_combiner_scheduler(chand->combiner));
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700290 w->state = current_state;
291 w->lb_policy = lb_policy;
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800292 grpc_lb_policy_notify_on_state_change_locked(lb_policy, &w->state,
Craig Tiller2400bf52017-02-09 16:25:19 -0800293 &w->on_changed);
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700294}
295
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800296static void start_resolving_locked(channel_data* chand) {
Craig Tiller6014e8a2017-10-16 13:50:29 -0700297 if (grpc_client_channel_trace.enabled()) {
Mark D. Roth60751fe2017-07-07 12:50:33 -0700298 gpr_log(GPR_DEBUG, "chand=%p: starting name resolution", chand);
299 }
300 GPR_ASSERT(!chand->started_resolving);
301 chand->started_resolving = true;
302 GRPC_CHANNEL_STACK_REF(chand->owning_stack, "resolver");
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800303 grpc_resolver_next_locked(chand->resolver, &chand->resolver_result,
Mark D. Roth60751fe2017-07-07 12:50:33 -0700304 &chand->on_resolver_result_changed);
305}
306
Mark D. Rothd6d192d2017-02-23 08:58:42 -0800307typedef struct {
Craig Tillerbaa14a92017-11-03 09:09:36 -0700308 char* server_name;
309 grpc_server_retry_throttle_data* retry_throttle_data;
Mark D. Rothd6d192d2017-02-23 08:58:42 -0800310} service_config_parsing_state;
311
Craig Tillerbaa14a92017-11-03 09:09:36 -0700312static void parse_retry_throttle_params(const grpc_json* field, void* arg) {
313 service_config_parsing_state* parsing_state =
314 (service_config_parsing_state*)arg;
Mark D. Rothd6d192d2017-02-23 08:58:42 -0800315 if (strcmp(field->key, "retryThrottling") == 0) {
Craig Tiller4782d922017-11-10 09:53:21 -0800316 if (parsing_state->retry_throttle_data != nullptr) return; // Duplicate.
Mark D. Rothd6d192d2017-02-23 08:58:42 -0800317 if (field->type != GRPC_JSON_OBJECT) return;
318 int max_milli_tokens = 0;
319 int milli_token_ratio = 0;
Craig Tiller4782d922017-11-10 09:53:21 -0800320 for (grpc_json* sub_field = field->child; sub_field != nullptr;
Mark D. Rothd6d192d2017-02-23 08:58:42 -0800321 sub_field = sub_field->next) {
Craig Tiller4782d922017-11-10 09:53:21 -0800322 if (sub_field->key == nullptr) return;
Mark D. Rothd6d192d2017-02-23 08:58:42 -0800323 if (strcmp(sub_field->key, "maxTokens") == 0) {
324 if (max_milli_tokens != 0) return; // Duplicate.
325 if (sub_field->type != GRPC_JSON_NUMBER) return;
326 max_milli_tokens = gpr_parse_nonnegative_int(sub_field->value);
327 if (max_milli_tokens == -1) return;
328 max_milli_tokens *= 1000;
329 } else if (strcmp(sub_field->key, "tokenRatio") == 0) {
330 if (milli_token_ratio != 0) return; // Duplicate.
331 if (sub_field->type != GRPC_JSON_NUMBER) return;
332 // We support up to 3 decimal digits.
333 size_t whole_len = strlen(sub_field->value);
334 uint32_t multiplier = 1;
335 uint32_t decimal_value = 0;
Craig Tillerbaa14a92017-11-03 09:09:36 -0700336 const char* decimal_point = strchr(sub_field->value, '.');
Craig Tiller4782d922017-11-10 09:53:21 -0800337 if (decimal_point != nullptr) {
Mark D. Rothd6d192d2017-02-23 08:58:42 -0800338 whole_len = (size_t)(decimal_point - sub_field->value);
339 multiplier = 1000;
340 size_t decimal_len = strlen(decimal_point + 1);
341 if (decimal_len > 3) decimal_len = 3;
342 if (!gpr_parse_bytes_to_uint32(decimal_point + 1, decimal_len,
343 &decimal_value)) {
344 return;
345 }
346 uint32_t decimal_multiplier = 1;
347 for (size_t i = 0; i < (3 - decimal_len); ++i) {
348 decimal_multiplier *= 10;
349 }
350 decimal_value *= decimal_multiplier;
351 }
352 uint32_t whole_value;
353 if (!gpr_parse_bytes_to_uint32(sub_field->value, whole_len,
354 &whole_value)) {
355 return;
356 }
357 milli_token_ratio = (int)((whole_value * multiplier) + decimal_value);
Mark D. Rothb3322562017-02-23 14:38:02 -0800358 if (milli_token_ratio <= 0) return;
Mark D. Rothd6d192d2017-02-23 08:58:42 -0800359 }
360 }
361 parsing_state->retry_throttle_data =
362 grpc_retry_throttle_map_get_data_for_server(
363 parsing_state->server_name, max_milli_tokens, milli_token_ratio);
364 }
365}
366
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800367static void request_reresolution_locked(void* arg, grpc_error* error) {
Juanli Shen592cf342017-12-04 20:52:01 -0800368 reresolution_request_args* args = (reresolution_request_args*)arg;
369 channel_data* chand = args->chand;
370 // If this invocation is for a stale LB policy, treat it as an LB shutdown
371 // signal.
372 if (args->lb_policy != chand->lb_policy || error != GRPC_ERROR_NONE ||
373 chand->resolver == nullptr) {
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800374 GRPC_CHANNEL_STACK_UNREF(chand->owning_stack, "re-resolution");
Juanli Shen592cf342017-12-04 20:52:01 -0800375 gpr_free(args);
376 return;
377 }
378 if (grpc_client_channel_trace.enabled()) {
379 gpr_log(GPR_DEBUG, "chand=%p: started name re-resolving", chand);
380 }
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800381 grpc_resolver_channel_saw_error_locked(chand->resolver);
Juanli Shen592cf342017-12-04 20:52:01 -0800382 // Give back the closure to the LB policy.
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800383 grpc_lb_policy_set_reresolve_closure_locked(chand->lb_policy, &args->closure);
Juanli Shen592cf342017-12-04 20:52:01 -0800384}
385
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800386static void on_resolver_result_changed_locked(void* arg, grpc_error* error) {
Craig Tillerbaa14a92017-11-03 09:09:36 -0700387 channel_data* chand = (channel_data*)arg;
Craig Tiller6014e8a2017-10-16 13:50:29 -0700388 if (grpc_client_channel_trace.enabled()) {
Mark D. Roth60751fe2017-07-07 12:50:33 -0700389 gpr_log(GPR_DEBUG, "chand=%p: got resolver result: error=%s", chand,
390 grpc_error_string(error));
391 }
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700392 // Extract the following fields from the resolver result, if non-NULL.
Mark D. Roth15494b52017-07-12 15:26:55 -0700393 bool lb_policy_updated = false;
Craig Tiller4782d922017-11-10 09:53:21 -0800394 char* lb_policy_name_dup = nullptr;
Mark D. Roth60751fe2017-07-07 12:50:33 -0700395 bool lb_policy_name_changed = false;
Craig Tiller4782d922017-11-10 09:53:21 -0800396 grpc_lb_policy* new_lb_policy = nullptr;
397 char* service_config_json = nullptr;
398 grpc_server_retry_throttle_data* retry_throttle_data = nullptr;
399 grpc_slice_hash_table* method_params_table = nullptr;
400 if (chand->resolver_result != nullptr) {
Juanli Shen592cf342017-12-04 20:52:01 -0800401 if (chand->resolver != nullptr) {
402 // Find LB policy name.
403 const char* lb_policy_name = nullptr;
404 const grpc_arg* channel_arg = grpc_channel_args_find(
405 chand->resolver_result, GRPC_ARG_LB_POLICY_NAME);
406 if (channel_arg != nullptr) {
Mark D. Rothd6d192d2017-02-23 08:58:42 -0800407 GPR_ASSERT(channel_arg->type == GRPC_ARG_STRING);
Juanli Shen592cf342017-12-04 20:52:01 -0800408 lb_policy_name = channel_arg->value.string;
Mark D. Rothbdc58b22016-11-04 09:25:57 -0700409 }
Juanli Shen592cf342017-12-04 20:52:01 -0800410 // Special case: If at least one balancer address is present, we use
411 // the grpclb policy, regardless of what the resolver actually specified.
412 channel_arg =
413 grpc_channel_args_find(chand->resolver_result, GRPC_ARG_LB_ADDRESSES);
414 if (channel_arg != nullptr && channel_arg->type == GRPC_ARG_POINTER) {
415 grpc_lb_addresses* addresses =
416 (grpc_lb_addresses*)channel_arg->value.pointer.p;
417 bool found_balancer_address = false;
418 for (size_t i = 0; i < addresses->num_addresses; ++i) {
419 if (addresses->addresses[i].is_balancer) {
420 found_balancer_address = true;
421 break;
422 }
423 }
424 if (found_balancer_address) {
425 if (lb_policy_name != nullptr &&
426 strcmp(lb_policy_name, "grpclb") != 0) {
427 gpr_log(GPR_INFO,
428 "resolver requested LB policy %s but provided at least one "
429 "balancer address -- forcing use of grpclb LB policy",
430 lb_policy_name);
431 }
432 lb_policy_name = "grpclb";
433 }
434 }
435 // Use pick_first if nothing was specified and we didn't select grpclb
436 // above.
437 if (lb_policy_name == nullptr) lb_policy_name = "pick_first";
438 grpc_lb_policy_args lb_policy_args;
439 lb_policy_args.args = chand->resolver_result;
440 lb_policy_args.client_channel_factory = chand->client_channel_factory;
441 lb_policy_args.combiner = chand->combiner;
442 // Check to see if we're already using the right LB policy.
443 // Note: It's safe to use chand->info_lb_policy_name here without
444 // taking a lock on chand->info_mu, because this function is the
445 // only thing that modifies its value, and it can only be invoked
446 // once at any given time.
447 lb_policy_name_changed =
448 chand->info_lb_policy_name == nullptr ||
449 gpr_stricmp(chand->info_lb_policy_name, lb_policy_name) != 0;
450 if (chand->lb_policy != nullptr && !lb_policy_name_changed) {
451 // Continue using the same LB policy. Update with new addresses.
452 lb_policy_updated = true;
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800453 grpc_lb_policy_update_locked(chand->lb_policy, &lb_policy_args);
Juanli Shen592cf342017-12-04 20:52:01 -0800454 } else {
455 // Instantiate new LB policy.
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800456 new_lb_policy = grpc_lb_policy_create(lb_policy_name, &lb_policy_args);
Juanli Shen592cf342017-12-04 20:52:01 -0800457 if (new_lb_policy == nullptr) {
458 gpr_log(GPR_ERROR, "could not create LB policy \"%s\"",
459 lb_policy_name);
460 } else {
461 reresolution_request_args* args =
462 (reresolution_request_args*)gpr_zalloc(sizeof(*args));
463 args->chand = chand;
464 args->lb_policy = new_lb_policy;
465 GRPC_CLOSURE_INIT(&args->closure, request_reresolution_locked, args,
466 grpc_combiner_scheduler(chand->combiner));
467 GRPC_CHANNEL_STACK_REF(chand->owning_stack, "re-resolution");
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800468 grpc_lb_policy_set_reresolve_closure_locked(new_lb_policy,
Juanli Shen592cf342017-12-04 20:52:01 -0800469 &args->closure);
470 }
471 }
472 // Find service config.
473 channel_arg = grpc_channel_args_find(chand->resolver_result,
474 GRPC_ARG_SERVICE_CONFIG);
475 if (channel_arg != nullptr) {
476 GPR_ASSERT(channel_arg->type == GRPC_ARG_STRING);
477 service_config_json = gpr_strdup(channel_arg->value.string);
478 grpc_service_config* service_config =
479 grpc_service_config_create(service_config_json);
480 if (service_config != nullptr) {
481 channel_arg = grpc_channel_args_find(chand->resolver_result,
482 GRPC_ARG_SERVER_URI);
483 GPR_ASSERT(channel_arg != nullptr);
484 GPR_ASSERT(channel_arg->type == GRPC_ARG_STRING);
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800485 grpc_uri* uri = grpc_uri_parse(channel_arg->value.string, true);
Juanli Shen592cf342017-12-04 20:52:01 -0800486 GPR_ASSERT(uri->path[0] != '\0');
487 service_config_parsing_state parsing_state;
488 memset(&parsing_state, 0, sizeof(parsing_state));
489 parsing_state.server_name =
490 uri->path[0] == '/' ? uri->path + 1 : uri->path;
491 grpc_service_config_parse_global_params(
492 service_config, parse_retry_throttle_params, &parsing_state);
493 grpc_uri_destroy(uri);
494 retry_throttle_data = parsing_state.retry_throttle_data;
495 method_params_table = grpc_service_config_create_method_config_table(
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800496 service_config, method_parameters_create_from_json,
Juanli Shen592cf342017-12-04 20:52:01 -0800497 method_parameters_ref_wrapper, method_parameters_unref_wrapper);
498 grpc_service_config_destroy(service_config);
499 }
500 }
501 // Before we clean up, save a copy of lb_policy_name, since it might
502 // be pointing to data inside chand->resolver_result.
503 // The copy will be saved in chand->lb_policy_name below.
504 lb_policy_name_dup = gpr_strdup(lb_policy_name);
Mark D. Roth9fe284e2016-09-12 11:22:27 -0700505 }
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800506 grpc_channel_args_destroy(chand->resolver_result);
Craig Tiller4782d922017-11-10 09:53:21 -0800507 chand->resolver_result = nullptr;
Craig Tillera82950e2015-09-22 12:33:20 -0700508 }
Craig Tiller6014e8a2017-10-16 13:50:29 -0700509 if (grpc_client_channel_trace.enabled()) {
Mark D. Roth60751fe2017-07-07 12:50:33 -0700510 gpr_log(GPR_DEBUG,
511 "chand=%p: resolver result: lb_policy_name=\"%s\"%s, "
512 "service_config=\"%s\"",
Yash Tibrewal9eb86722017-09-17 23:43:30 -0700513 chand, lb_policy_name_dup,
514 lb_policy_name_changed ? " (changed)" : "", service_config_json);
Mark D. Roth60751fe2017-07-07 12:50:33 -0700515 }
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700516 // Now swap out fields in chand. Note that the new values may still
517 // be NULL if (e.g.) the resolver failed to return results or the
518 // results did not contain the necessary data.
519 //
520 // First, swap out the data used by cc_get_channel_info().
Craig Tiller613dafa2017-02-09 12:00:43 -0800521 gpr_mu_lock(&chand->info_mu);
Craig Tiller4782d922017-11-10 09:53:21 -0800522 if (lb_policy_name_dup != nullptr) {
Craig Tiller613dafa2017-02-09 12:00:43 -0800523 gpr_free(chand->info_lb_policy_name);
Yash Tibrewal9eb86722017-09-17 23:43:30 -0700524 chand->info_lb_policy_name = lb_policy_name_dup;
Mark D. Rothb2d24882016-10-27 15:44:07 -0700525 }
Craig Tiller4782d922017-11-10 09:53:21 -0800526 if (service_config_json != nullptr) {
Craig Tiller613dafa2017-02-09 12:00:43 -0800527 gpr_free(chand->info_service_config_json);
528 chand->info_service_config_json = service_config_json;
Mark D. Rothc625c7a2016-11-09 14:12:37 -0800529 }
Craig Tiller613dafa2017-02-09 12:00:43 -0800530 gpr_mu_unlock(&chand->info_mu);
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700531 // Swap out the retry throttle data.
Craig Tiller4782d922017-11-10 09:53:21 -0800532 if (chand->retry_throttle_data != nullptr) {
Mark D. Rothd6d192d2017-02-23 08:58:42 -0800533 grpc_server_retry_throttle_data_unref(chand->retry_throttle_data);
534 }
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700535 chand->retry_throttle_data = retry_throttle_data;
536 // Swap out the method params table.
Craig Tiller4782d922017-11-10 09:53:21 -0800537 if (chand->method_params_table != nullptr) {
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800538 grpc_slice_hash_table_unref(chand->method_params_table);
Mark D. Roth046cf762016-09-26 11:13:51 -0700539 }
Mark D. Roth9d480942016-10-19 14:18:05 -0700540 chand->method_params_table = method_params_table;
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700541 // If we have a new LB policy or are shutting down (in which case
Juanli Shen592cf342017-12-04 20:52:01 -0800542 // new_lb_policy will be NULL), swap out the LB policy, unreffing the old one
543 // and removing its fds from chand->interested_parties. Note that we do NOT do
544 // this if either (a) we updated the existing LB policy above or (b) we failed
545 // to create the new LB policy (in which case we want to continue using the
546 // most recent one we had).
Craig Tiller4782d922017-11-10 09:53:21 -0800547 if (new_lb_policy != nullptr || error != GRPC_ERROR_NONE ||
548 chand->resolver == nullptr) {
549 if (chand->lb_policy != nullptr) {
Craig Tiller6014e8a2017-10-16 13:50:29 -0700550 if (grpc_client_channel_trace.enabled()) {
Mark D. Roth60751fe2017-07-07 12:50:33 -0700551 gpr_log(GPR_DEBUG, "chand=%p: unreffing lb_policy=%p", chand,
552 chand->lb_policy);
553 }
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800554 grpc_pollset_set_del_pollset_set(chand->lb_policy->interested_parties,
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700555 chand->interested_parties);
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800556 GRPC_LB_POLICY_UNREF(chand->lb_policy, "channel");
Craig Tiller45724b32015-09-22 10:42:19 -0700557 }
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700558 chand->lb_policy = new_lb_policy;
559 }
560 // Now that we've swapped out the relevant fields of chand, check for
561 // error or shutdown.
Craig Tiller4782d922017-11-10 09:53:21 -0800562 if (error != GRPC_ERROR_NONE || chand->resolver == nullptr) {
Craig Tiller6014e8a2017-10-16 13:50:29 -0700563 if (grpc_client_channel_trace.enabled()) {
Mark D. Roth60751fe2017-07-07 12:50:33 -0700564 gpr_log(GPR_DEBUG, "chand=%p: shutting down", chand);
565 }
Craig Tiller4782d922017-11-10 09:53:21 -0800566 if (chand->resolver != nullptr) {
Craig Tiller6014e8a2017-10-16 13:50:29 -0700567 if (grpc_client_channel_trace.enabled()) {
Mark D. Roth60751fe2017-07-07 12:50:33 -0700568 gpr_log(GPR_DEBUG, "chand=%p: shutting down resolver", chand);
569 }
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800570 grpc_resolver_shutdown_locked(chand->resolver);
571 GRPC_RESOLVER_UNREF(chand->resolver, "channel");
Craig Tiller4782d922017-11-10 09:53:21 -0800572 chand->resolver = nullptr;
Craig Tiller76a5c0e2016-03-09 09:05:30 -0800573 }
Craig Tiller8c0d96f2016-03-11 14:27:52 -0800574 set_channel_connectivity_state_locked(
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800575 chand, GRPC_CHANNEL_SHUTDOWN,
ncteisen4b36a3d2017-03-13 19:08:06 -0700576 GRPC_ERROR_CREATE_REFERENCING_FROM_STATIC_STRING(
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700577 "Got resolver result after disconnection", &error, 1),
Craig Tiller804ff712016-05-05 16:25:40 -0700578 "resolver_gone");
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800579 GRPC_CHANNEL_STACK_UNREF(chand->owning_stack, "resolver");
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700580 grpc_closure_list_fail_all(&chand->waiting_for_resolver_result_closures,
581 GRPC_ERROR_CREATE_REFERENCING_FROM_STATIC_STRING(
582 "Channel disconnected", &error, 1));
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800583 GRPC_CLOSURE_LIST_SCHED(&chand->waiting_for_resolver_result_closures);
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700584 } else { // Not shutting down.
585 grpc_connectivity_state state = GRPC_CHANNEL_TRANSIENT_FAILURE;
Craig Tillerbaa14a92017-11-03 09:09:36 -0700586 grpc_error* state_error =
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700587 GRPC_ERROR_CREATE_FROM_STATIC_STRING("No load balancing policy");
Craig Tiller4782d922017-11-10 09:53:21 -0800588 if (new_lb_policy != nullptr) {
Craig Tiller6014e8a2017-10-16 13:50:29 -0700589 if (grpc_client_channel_trace.enabled()) {
Mark D. Roth60751fe2017-07-07 12:50:33 -0700590 gpr_log(GPR_DEBUG, "chand=%p: initializing new LB policy", chand);
591 }
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700592 GRPC_ERROR_UNREF(state_error);
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800593 state =
594 grpc_lb_policy_check_connectivity_locked(new_lb_policy, &state_error);
595 grpc_pollset_set_add_pollset_set(new_lb_policy->interested_parties,
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700596 chand->interested_parties);
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800597 GRPC_CLOSURE_LIST_SCHED(&chand->waiting_for_resolver_result_closures);
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700598 if (chand->exit_idle_when_lb_policy_arrives) {
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800599 grpc_lb_policy_exit_idle_locked(new_lb_policy);
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700600 chand->exit_idle_when_lb_policy_arrives = false;
601 }
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800602 watch_lb_policy_locked(chand, new_lb_policy, state);
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700603 }
Mark D. Roth15494b52017-07-12 15:26:55 -0700604 if (!lb_policy_updated) {
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800605 set_channel_connectivity_state_locked(
606 chand, state, GRPC_ERROR_REF(state_error), "new_lb+resolver");
Mark D. Roth15494b52017-07-12 15:26:55 -0700607 }
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800608 grpc_resolver_next_locked(chand->resolver, &chand->resolver_result,
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700609 &chand->on_resolver_result_changed);
610 GRPC_ERROR_UNREF(state_error);
Craig Tillera82950e2015-09-22 12:33:20 -0700611 }
Craig Tiller3f475422015-06-25 10:43:05 -0700612}
613
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800614static void start_transport_op_locked(void* arg, grpc_error* error_ignored) {
Craig Tillerbaa14a92017-11-03 09:09:36 -0700615 grpc_transport_op* op = (grpc_transport_op*)arg;
616 grpc_channel_element* elem =
617 (grpc_channel_element*)op->handler_private.extra_arg;
618 channel_data* chand = (channel_data*)elem->channel_data;
Craig Tiller000cd8f2015-09-18 07:20:29 -0700619
Craig Tiller4782d922017-11-10 09:53:21 -0800620 if (op->on_connectivity_state_change != nullptr) {
Craig Tillera82950e2015-09-22 12:33:20 -0700621 grpc_connectivity_state_notify_on_state_change(
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800622 &chand->state_tracker, op->connectivity_state,
Craig Tillera82950e2015-09-22 12:33:20 -0700623 op->on_connectivity_state_change);
Craig Tiller4782d922017-11-10 09:53:21 -0800624 op->on_connectivity_state_change = nullptr;
625 op->connectivity_state = nullptr;
Craig Tillera82950e2015-09-22 12:33:20 -0700626 }
627
Yuchen Zengc272dd72017-12-05 12:18:34 -0800628 if (op->send_ping.on_initiate != nullptr || op->send_ping.on_ack != nullptr) {
Craig Tiller4782d922017-11-10 09:53:21 -0800629 if (chand->lb_policy == nullptr) {
ncteisen274bbbe2017-06-08 14:57:11 -0700630 GRPC_CLOSURE_SCHED(
Yash Tibrewald6c292f2017-12-07 19:38:43 -0800631 op->send_ping.on_initiate,
Yuchen Zengc272dd72017-12-05 12:18:34 -0800632 GRPC_ERROR_CREATE_FROM_STATIC_STRING("Ping with no load balancing"));
633 GRPC_CLOSURE_SCHED(
Yash Tibrewald6c292f2017-12-07 19:38:43 -0800634 op->send_ping.on_ack,
ncteisen4b36a3d2017-03-13 19:08:06 -0700635 GRPC_ERROR_CREATE_FROM_STATIC_STRING("Ping with no load balancing"));
Craig Tiller26dab312015-12-07 14:43:47 -0800636 } else {
Yash Tibrewald6c292f2017-12-07 19:38:43 -0800637 grpc_lb_policy_ping_one_locked(
638 chand->lb_policy, op->send_ping.on_initiate, op->send_ping.on_ack);
Craig Tiller4782d922017-11-10 09:53:21 -0800639 op->bind_pollset = nullptr;
Craig Tiller26dab312015-12-07 14:43:47 -0800640 }
Yuchen Zengc272dd72017-12-05 12:18:34 -0800641 op->send_ping.on_initiate = nullptr;
642 op->send_ping.on_ack = nullptr;
Craig Tiller26dab312015-12-07 14:43:47 -0800643 }
644
Craig Tiller1c51edc2016-05-07 16:18:43 -0700645 if (op->disconnect_with_error != GRPC_ERROR_NONE) {
Craig Tiller4782d922017-11-10 09:53:21 -0800646 if (chand->resolver != nullptr) {
Craig Tiller1c51edc2016-05-07 16:18:43 -0700647 set_channel_connectivity_state_locked(
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800648 chand, GRPC_CHANNEL_SHUTDOWN,
Craig Tiller1c51edc2016-05-07 16:18:43 -0700649 GRPC_ERROR_REF(op->disconnect_with_error), "disconnect");
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800650 grpc_resolver_shutdown_locked(chand->resolver);
651 GRPC_RESOLVER_UNREF(chand->resolver, "channel");
Craig Tiller4782d922017-11-10 09:53:21 -0800652 chand->resolver = nullptr;
Craig Tiller1c51edc2016-05-07 16:18:43 -0700653 if (!chand->started_resolving) {
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700654 grpc_closure_list_fail_all(&chand->waiting_for_resolver_result_closures,
Craig Tiller1c51edc2016-05-07 16:18:43 -0700655 GRPC_ERROR_REF(op->disconnect_with_error));
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800656 GRPC_CLOSURE_LIST_SCHED(&chand->waiting_for_resolver_result_closures);
Craig Tiller1c51edc2016-05-07 16:18:43 -0700657 }
Craig Tiller4782d922017-11-10 09:53:21 -0800658 if (chand->lb_policy != nullptr) {
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800659 grpc_pollset_set_del_pollset_set(chand->lb_policy->interested_parties,
Craig Tiller1c51edc2016-05-07 16:18:43 -0700660 chand->interested_parties);
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800661 GRPC_LB_POLICY_UNREF(chand->lb_policy, "channel");
Craig Tiller4782d922017-11-10 09:53:21 -0800662 chand->lb_policy = nullptr;
Craig Tiller1c51edc2016-05-07 16:18:43 -0700663 }
Craig Tillerb12d22a2016-04-23 12:50:21 -0700664 }
Craig Tiller1c51edc2016-05-07 16:18:43 -0700665 GRPC_ERROR_UNREF(op->disconnect_with_error);
Craig Tillera82950e2015-09-22 12:33:20 -0700666 }
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800667 GRPC_CHANNEL_STACK_UNREF(chand->owning_stack, "start_transport_op");
Craig Tillerd2e5cfc2017-02-09 13:02:20 -0800668
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800669 GRPC_CLOSURE_SCHED(op->on_consumed, GRPC_ERROR_NONE);
Craig Tillerbefafe62017-02-09 11:30:54 -0800670}
671
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800672static void cc_start_transport_op(grpc_channel_element* elem,
Craig Tillerbaa14a92017-11-03 09:09:36 -0700673 grpc_transport_op* op) {
674 channel_data* chand = (channel_data*)elem->channel_data;
Craig Tillerbefafe62017-02-09 11:30:54 -0800675
Craig Tillerbefafe62017-02-09 11:30:54 -0800676 GPR_ASSERT(op->set_accept_stream == false);
Craig Tiller4782d922017-11-10 09:53:21 -0800677 if (op->bind_pollset != nullptr) {
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800678 grpc_pollset_set_add_pollset(chand->interested_parties, op->bind_pollset);
Craig Tillerbefafe62017-02-09 11:30:54 -0800679 }
680
Craig Tillerc55c1022017-03-10 10:26:42 -0800681 op->handler_private.extra_arg = elem;
Craig Tillerd2e5cfc2017-02-09 13:02:20 -0800682 GRPC_CHANNEL_STACK_REF(chand->owning_stack, "start_transport_op");
ncteisen274bbbe2017-06-08 14:57:11 -0700683 GRPC_CLOSURE_SCHED(
ncteisen274bbbe2017-06-08 14:57:11 -0700684 GRPC_CLOSURE_INIT(&op->handler_private.closure, start_transport_op_locked,
Craig Tilleree4b1452017-05-12 10:56:03 -0700685 op, grpc_combiner_scheduler(chand->combiner)),
Craig Tillerbefafe62017-02-09 11:30:54 -0800686 GRPC_ERROR_NONE);
Craig Tillerca3e9d32015-06-27 18:37:27 -0700687}
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800688
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800689static void cc_get_channel_info(grpc_channel_element* elem,
Craig Tillerbaa14a92017-11-03 09:09:36 -0700690 const grpc_channel_info* info) {
691 channel_data* chand = (channel_data*)elem->channel_data;
Craig Tiller613dafa2017-02-09 12:00:43 -0800692 gpr_mu_lock(&chand->info_mu);
Craig Tiller4782d922017-11-10 09:53:21 -0800693 if (info->lb_policy_name != nullptr) {
694 *info->lb_policy_name = chand->info_lb_policy_name == nullptr
695 ? nullptr
Craig Tiller613dafa2017-02-09 12:00:43 -0800696 : gpr_strdup(chand->info_lb_policy_name);
Mark D. Rothb2d24882016-10-27 15:44:07 -0700697 }
Craig Tiller4782d922017-11-10 09:53:21 -0800698 if (info->service_config_json != nullptr) {
Craig Tiller613dafa2017-02-09 12:00:43 -0800699 *info->service_config_json =
Craig Tiller4782d922017-11-10 09:53:21 -0800700 chand->info_service_config_json == nullptr
701 ? nullptr
Craig Tiller613dafa2017-02-09 12:00:43 -0800702 : gpr_strdup(chand->info_service_config_json);
Mark D. Rothc625c7a2016-11-09 14:12:37 -0800703 }
Craig Tiller613dafa2017-02-09 12:00:43 -0800704 gpr_mu_unlock(&chand->info_mu);
Mark D. Rothb2d24882016-10-27 15:44:07 -0700705}
706
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700707/* Constructor for channel_data */
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800708static grpc_error* cc_init_channel_elem(grpc_channel_element* elem,
Craig Tillerbaa14a92017-11-03 09:09:36 -0700709 grpc_channel_element_args* args) {
710 channel_data* chand = (channel_data*)elem->channel_data;
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700711 GPR_ASSERT(args->is_last);
712 GPR_ASSERT(elem->filter == &grpc_client_channel_filter);
Mark D. Roth21d4b2d2016-11-18 09:53:41 -0800713 // Initialize data members.
Craig Tilleree4b1452017-05-12 10:56:03 -0700714 chand->combiner = grpc_combiner_create();
Craig Tillerd85477512017-02-09 12:02:39 -0800715 gpr_mu_init(&chand->info_mu);
Alexander Polcync3b1f182017-04-18 13:51:36 -0700716 gpr_mu_init(&chand->external_connectivity_watcher_list_mu);
717
718 gpr_mu_lock(&chand->external_connectivity_watcher_list_mu);
Craig Tiller4782d922017-11-10 09:53:21 -0800719 chand->external_connectivity_watcher_list_head = nullptr;
Alexander Polcync3b1f182017-04-18 13:51:36 -0700720 gpr_mu_unlock(&chand->external_connectivity_watcher_list_mu);
721
Mark D. Roth21d4b2d2016-11-18 09:53:41 -0800722 chand->owning_stack = args->channel_stack;
ncteisen274bbbe2017-06-08 14:57:11 -0700723 GRPC_CLOSURE_INIT(&chand->on_resolver_result_changed,
Craig Tillerbefafe62017-02-09 11:30:54 -0800724 on_resolver_result_changed_locked, chand,
Craig Tilleree4b1452017-05-12 10:56:03 -0700725 grpc_combiner_scheduler(chand->combiner));
Mark D. Roth21d4b2d2016-11-18 09:53:41 -0800726 chand->interested_parties = grpc_pollset_set_create();
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700727 grpc_connectivity_state_init(&chand->state_tracker, GRPC_CHANNEL_IDLE,
728 "client_channel");
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800729 grpc_client_channel_start_backup_polling(chand->interested_parties);
Mark D. Roth21d4b2d2016-11-18 09:53:41 -0800730 // Record client channel factory.
Craig Tillerbaa14a92017-11-03 09:09:36 -0700731 const grpc_arg* arg = grpc_channel_args_find(args->channel_args,
Mark D. Roth21d4b2d2016-11-18 09:53:41 -0800732 GRPC_ARG_CLIENT_CHANNEL_FACTORY);
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 client channel factory in args for client channel filter");
736 }
737 if (arg->type != GRPC_ARG_POINTER) {
738 return GRPC_ERROR_CREATE_FROM_STATIC_STRING(
739 "client channel factory arg must be a pointer");
740 }
Yash Tibrewalbc130da2017-09-12 22:44:08 -0700741 grpc_client_channel_factory_ref(
Craig Tillerbaa14a92017-11-03 09:09:36 -0700742 (grpc_client_channel_factory*)arg->value.pointer.p);
Yash Tibrewalca3c1c02017-09-07 22:47:16 -0700743 chand->client_channel_factory =
Craig Tillerbaa14a92017-11-03 09:09:36 -0700744 (grpc_client_channel_factory*)arg->value.pointer.p;
Mark D. Rothdc9bee72017-02-07 12:29:14 -0800745 // Get server name to resolve, using proxy mapper if needed.
Mark D. Roth86e90592016-11-18 09:56:40 -0800746 arg = grpc_channel_args_find(args->channel_args, GRPC_ARG_SERVER_URI);
Craig Tiller4782d922017-11-10 09:53:21 -0800747 if (arg == nullptr) {
David Garcia Quintas228a5142017-03-30 19:43:00 -0700748 return GRPC_ERROR_CREATE_FROM_STATIC_STRING(
749 "Missing server uri in args for client channel filter");
750 }
751 if (arg->type != GRPC_ARG_STRING) {
752 return GRPC_ERROR_CREATE_FROM_STATIC_STRING(
753 "server uri arg must be a string");
754 }
Craig Tiller4782d922017-11-10 09:53:21 -0800755 char* proxy_name = nullptr;
756 grpc_channel_args* new_args = nullptr;
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800757 grpc_proxy_mappers_map_name(arg->value.string, args->channel_args,
Mark D. Rothdc9bee72017-02-07 12:29:14 -0800758 &proxy_name, &new_args);
759 // Instantiate resolver.
Mark D. Roth45ccec52017-01-18 14:04:01 -0800760 chand->resolver = grpc_resolver_create(
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800761 proxy_name != nullptr ? proxy_name : arg->value.string,
Craig Tiller4782d922017-11-10 09:53:21 -0800762 new_args != nullptr ? new_args : args->channel_args,
Craig Tiller972470b2017-02-09 15:05:36 -0800763 chand->interested_parties, chand->combiner);
Craig Tiller4782d922017-11-10 09:53:21 -0800764 if (proxy_name != nullptr) gpr_free(proxy_name);
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800765 if (new_args != nullptr) grpc_channel_args_destroy(new_args);
Craig Tiller4782d922017-11-10 09:53:21 -0800766 if (chand->resolver == nullptr) {
ncteisen4b36a3d2017-03-13 19:08:06 -0700767 return GRPC_ERROR_CREATE_FROM_STATIC_STRING("resolver creation failed");
Mark D. Roth5e2566e2016-11-18 10:53:13 -0800768 }
Craig Tiller3be7dd02017-04-03 14:30:03 -0700769 chand->deadline_checking_enabled =
770 grpc_deadline_checking_enabled(args->channel_args);
Mark D. Roth5e2566e2016-11-18 10:53:13 -0800771 return GRPC_ERROR_NONE;
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700772}
773
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800774static void shutdown_resolver_locked(void* arg, grpc_error* error) {
Craig Tillerbaa14a92017-11-03 09:09:36 -0700775 grpc_resolver* resolver = (grpc_resolver*)arg;
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800776 grpc_resolver_shutdown_locked(resolver);
777 GRPC_RESOLVER_UNREF(resolver, "channel");
Craig Tiller972470b2017-02-09 15:05:36 -0800778}
779
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700780/* Destructor for channel_data */
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800781static void cc_destroy_channel_elem(grpc_channel_element* elem) {
Craig Tillerbaa14a92017-11-03 09:09:36 -0700782 channel_data* chand = (channel_data*)elem->channel_data;
Craig Tiller4782d922017-11-10 09:53:21 -0800783 if (chand->resolver != nullptr) {
ncteisen274bbbe2017-06-08 14:57:11 -0700784 GRPC_CLOSURE_SCHED(
Yash Tibrewal0ee75742017-10-13 16:07:13 -0700785 GRPC_CLOSURE_CREATE(shutdown_resolver_locked, chand->resolver,
786 grpc_combiner_scheduler(chand->combiner)),
Craig Tiller972470b2017-02-09 15:05:36 -0800787 GRPC_ERROR_NONE);
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700788 }
Craig Tiller4782d922017-11-10 09:53:21 -0800789 if (chand->client_channel_factory != nullptr) {
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800790 grpc_client_channel_factory_unref(chand->client_channel_factory);
Mark D. Roth0e48a9a2016-09-08 14:14:39 -0700791 }
Craig Tiller4782d922017-11-10 09:53:21 -0800792 if (chand->lb_policy != nullptr) {
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800793 grpc_pollset_set_del_pollset_set(chand->lb_policy->interested_parties,
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700794 chand->interested_parties);
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800795 GRPC_LB_POLICY_UNREF(chand->lb_policy, "channel");
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700796 }
Craig Tiller613dafa2017-02-09 12:00:43 -0800797 gpr_free(chand->info_lb_policy_name);
798 gpr_free(chand->info_service_config_json);
Craig Tiller4782d922017-11-10 09:53:21 -0800799 if (chand->retry_throttle_data != nullptr) {
Mark D. Rothd6d192d2017-02-23 08:58:42 -0800800 grpc_server_retry_throttle_data_unref(chand->retry_throttle_data);
801 }
Craig Tiller4782d922017-11-10 09:53:21 -0800802 if (chand->method_params_table != nullptr) {
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800803 grpc_slice_hash_table_unref(chand->method_params_table);
Mark D. Roth9fe284e2016-09-12 11:22:27 -0700804 }
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800805 grpc_client_channel_stop_backup_polling(chand->interested_parties);
806 grpc_connectivity_state_destroy(&chand->state_tracker);
807 grpc_pollset_set_destroy(chand->interested_parties);
808 GRPC_COMBINER_UNREF(chand->combiner, "client_channel");
Craig Tillerd85477512017-02-09 12:02:39 -0800809 gpr_mu_destroy(&chand->info_mu);
Alexander Polcync3b1f182017-04-18 13:51:36 -0700810 gpr_mu_destroy(&chand->external_connectivity_watcher_list_mu);
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700811}
812
813/*************************************************************************
814 * PER-CALL FUNCTIONS
815 */
816
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700817// Max number of batches that can be pending on a call at any given
818// time. This includes:
819// recv_initial_metadata
820// send_initial_metadata
821// recv_message
822// send_message
823// recv_trailing_metadata
824// send_trailing_metadata
Mark D. Roth76e264b2017-08-25 09:03:33 -0700825// We also add room for a single cancel_stream batch.
826#define MAX_WAITING_BATCHES 7
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700827
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700828/** Call data. Holds a pointer to grpc_subchannel_call and the
829 associated machinery to create such a pointer.
830 Handles queueing of stream ops until a call object is ready, waiting
831 for initial metadata before trying to create a call object,
832 and handling cancellation gracefully. */
833typedef struct client_channel_call_data {
Mark D. Roth72f6da82016-09-02 13:42:38 -0700834 // State for handling deadlines.
835 // The code in deadline_filter.c requires this to be the first field.
Mark D. Roth72f6da82016-09-02 13:42:38 -0700836 // TODO(roth): This is slightly sub-optimal in that grpc_deadline_state
Mark D. Roth66f3d2b2017-09-01 09:02:17 -0700837 // and this struct both independently store pointers to the call stack
838 // and call combiner. If/when we have time, find a way to avoid this
839 // without breaking the grpc_deadline_state abstraction.
Mark D. Roth72f6da82016-09-02 13:42:38 -0700840 grpc_deadline_state deadline_state;
Mark D. Rothf28763c2016-09-14 15:18:40 -0700841
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800842 grpc_slice path; // Request path.
Mark D. Rothe40dd292016-10-05 14:58:37 -0700843 gpr_timespec call_start_time;
Craig Tiller89c14282017-07-19 15:32:27 -0700844 grpc_millis deadline;
Craig Tillerbaa14a92017-11-03 09:09:36 -0700845 gpr_arena* arena;
846 grpc_call_stack* owning_call;
847 grpc_call_combiner* call_combiner;
Mark D. Roth76e264b2017-08-25 09:03:33 -0700848
Craig Tillerbaa14a92017-11-03 09:09:36 -0700849 grpc_server_retry_throttle_data* retry_throttle_data;
850 method_parameters* method_params;
Mark D. Rothaa850a72016-09-26 13:38:02 -0700851
Craig Tillerbaa14a92017-11-03 09:09:36 -0700852 grpc_subchannel_call* subchannel_call;
853 grpc_error* error;
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700854
Craig Tillerbaa14a92017-11-03 09:09:36 -0700855 grpc_lb_policy* lb_policy; // Holds ref while LB pick is pending.
Mark D. Roth60751fe2017-07-07 12:50:33 -0700856 grpc_closure lb_pick_closure;
Mark D. Roth66f3d2b2017-09-01 09:02:17 -0700857 grpc_closure lb_pick_cancel_closure;
Mark D. Roth60751fe2017-07-07 12:50:33 -0700858
Craig Tillerbaa14a92017-11-03 09:09:36 -0700859 grpc_connected_subchannel* connected_subchannel;
Mark D. Roth09e458c2017-05-02 08:13:26 -0700860 grpc_call_context_element subchannel_call_context[GRPC_CONTEXT_COUNT];
Craig Tillerbaa14a92017-11-03 09:09:36 -0700861 grpc_polling_entity* pollent;
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700862
Craig Tillerbaa14a92017-11-03 09:09:36 -0700863 grpc_transport_stream_op_batch* waiting_for_pick_batches[MAX_WAITING_BATCHES];
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700864 size_t waiting_for_pick_batches_count;
Mark D. Roth76e264b2017-08-25 09:03:33 -0700865 grpc_closure handle_pending_batch_in_call_combiner[MAX_WAITING_BATCHES];
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700866
Craig Tillerbaa14a92017-11-03 09:09:36 -0700867 grpc_transport_stream_op_batch* initial_metadata_batch;
David Garcia Quintasd1a47f12016-09-02 12:46:44 +0200868
869 grpc_linked_mdelem lb_token_mdelem;
Mark D. Rothd6d192d2017-02-23 08:58:42 -0800870
871 grpc_closure on_complete;
Craig Tillerbaa14a92017-11-03 09:09:36 -0700872 grpc_closure* original_on_complete;
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700873} call_data;
874
Craig Tillerbaa14a92017-11-03 09:09:36 -0700875grpc_subchannel_call* grpc_client_channel_get_subchannel_call(
876 grpc_call_element* elem) {
877 call_data* calld = (call_data*)elem->call_data;
Mark D. Roth76e264b2017-08-25 09:03:33 -0700878 return calld->subchannel_call;
Craig Tiller8b1d59c2016-12-27 15:15:30 -0800879}
880
Mark D. Roth76e264b2017-08-25 09:03:33 -0700881// This is called via the call combiner, so access to calld is synchronized.
882static void waiting_for_pick_batches_add(
Craig Tillerbaa14a92017-11-03 09:09:36 -0700883 call_data* calld, grpc_transport_stream_op_batch* batch) {
Mark D. Roth76e264b2017-08-25 09:03:33 -0700884 if (batch->send_initial_metadata) {
Craig Tiller4782d922017-11-10 09:53:21 -0800885 GPR_ASSERT(calld->initial_metadata_batch == nullptr);
Mark D. Roth76e264b2017-08-25 09:03:33 -0700886 calld->initial_metadata_batch = batch;
887 } else {
888 GPR_ASSERT(calld->waiting_for_pick_batches_count < MAX_WAITING_BATCHES);
889 calld->waiting_for_pick_batches[calld->waiting_for_pick_batches_count++] =
890 batch;
891 }
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700892}
893
Mark D. Roth76e264b2017-08-25 09:03:33 -0700894// This is called via the call combiner, so access to calld is synchronized.
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800895static void fail_pending_batch_in_call_combiner(void* arg, grpc_error* error) {
Craig Tillerbaa14a92017-11-03 09:09:36 -0700896 call_data* calld = (call_data*)arg;
Mark D. Roth76e264b2017-08-25 09:03:33 -0700897 if (calld->waiting_for_pick_batches_count > 0) {
898 --calld->waiting_for_pick_batches_count;
899 grpc_transport_stream_op_batch_finish_with_failure(
Mark D. Roth76e264b2017-08-25 09:03:33 -0700900 calld->waiting_for_pick_batches[calld->waiting_for_pick_batches_count],
901 GRPC_ERROR_REF(error), calld->call_combiner);
902 }
903}
904
905// This is called via the call combiner, so access to calld is synchronized.
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800906static void waiting_for_pick_batches_fail(grpc_call_element* elem,
Craig Tillerbaa14a92017-11-03 09:09:36 -0700907 grpc_error* error) {
908 call_data* calld = (call_data*)elem->call_data;
Craig Tiller6014e8a2017-10-16 13:50:29 -0700909 if (grpc_client_channel_trace.enabled()) {
Mark D. Roth60751fe2017-07-07 12:50:33 -0700910 gpr_log(GPR_DEBUG,
Mark D. Rothe9b10832017-10-26 13:18:25 -0700911 "chand=%p calld=%p: failing %" PRIuPTR " pending batches: %s",
Mark D. Roth60751fe2017-07-07 12:50:33 -0700912 elem->channel_data, calld, calld->waiting_for_pick_batches_count,
913 grpc_error_string(error));
914 }
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700915 for (size_t i = 0; i < calld->waiting_for_pick_batches_count; ++i) {
Mark D. Roth76e264b2017-08-25 09:03:33 -0700916 GRPC_CLOSURE_INIT(&calld->handle_pending_batch_in_call_combiner[i],
917 fail_pending_batch_in_call_combiner, calld,
918 grpc_schedule_on_exec_ctx);
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800919 GRPC_CALL_COMBINER_START(
920 calld->call_combiner, &calld->handle_pending_batch_in_call_combiner[i],
921 GRPC_ERROR_REF(error), "waiting_for_pick_batches_fail");
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700922 }
Craig Tiller4782d922017-11-10 09:53:21 -0800923 if (calld->initial_metadata_batch != nullptr) {
Mark D. Roth76e264b2017-08-25 09:03:33 -0700924 grpc_transport_stream_op_batch_finish_with_failure(
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800925 calld->initial_metadata_batch, GRPC_ERROR_REF(error),
Mark D. Roth76e264b2017-08-25 09:03:33 -0700926 calld->call_combiner);
927 } else {
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800928 GRPC_CALL_COMBINER_STOP(calld->call_combiner,
Mark D. Roth76e264b2017-08-25 09:03:33 -0700929 "waiting_for_pick_batches_fail");
930 }
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700931 GRPC_ERROR_UNREF(error);
932}
933
Mark D. Roth76e264b2017-08-25 09:03:33 -0700934// This is called via the call combiner, so access to calld is synchronized.
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800935static void run_pending_batch_in_call_combiner(void* arg, grpc_error* ignored) {
Craig Tillerbaa14a92017-11-03 09:09:36 -0700936 call_data* calld = (call_data*)arg;
Mark D. Roth76e264b2017-08-25 09:03:33 -0700937 if (calld->waiting_for_pick_batches_count > 0) {
938 --calld->waiting_for_pick_batches_count;
939 grpc_subchannel_call_process_op(
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800940 calld->subchannel_call,
Mark D. Roth76e264b2017-08-25 09:03:33 -0700941 calld->waiting_for_pick_batches[calld->waiting_for_pick_batches_count]);
Craig Tiller57726ca2016-09-12 11:59:45 -0700942 }
Mark D. Roth76e264b2017-08-25 09:03:33 -0700943}
944
945// This is called via the call combiner, so access to calld is synchronized.
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800946static void waiting_for_pick_batches_resume(grpc_call_element* elem) {
Craig Tillerbaa14a92017-11-03 09:09:36 -0700947 channel_data* chand = (channel_data*)elem->channel_data;
948 call_data* calld = (call_data*)elem->call_data;
Craig Tiller6014e8a2017-10-16 13:50:29 -0700949 if (grpc_client_channel_trace.enabled()) {
Craig Tillerbaa14a92017-11-03 09:09:36 -0700950 gpr_log(GPR_DEBUG,
951 "chand=%p calld=%p: sending %" PRIuPTR
952 " pending batches to subchannel_call=%p",
Mark D. Roth76e264b2017-08-25 09:03:33 -0700953 chand, calld, calld->waiting_for_pick_batches_count,
954 calld->subchannel_call);
Mark D. Roth60751fe2017-07-07 12:50:33 -0700955 }
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700956 for (size_t i = 0; i < calld->waiting_for_pick_batches_count; ++i) {
Mark D. Roth76e264b2017-08-25 09:03:33 -0700957 GRPC_CLOSURE_INIT(&calld->handle_pending_batch_in_call_combiner[i],
958 run_pending_batch_in_call_combiner, calld,
959 grpc_schedule_on_exec_ctx);
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800960 GRPC_CALL_COMBINER_START(
961 calld->call_combiner, &calld->handle_pending_batch_in_call_combiner[i],
962 GRPC_ERROR_NONE, "waiting_for_pick_batches_resume");
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700963 }
Craig Tiller4782d922017-11-10 09:53:21 -0800964 GPR_ASSERT(calld->initial_metadata_batch != nullptr);
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800965 grpc_subchannel_call_process_op(calld->subchannel_call,
Mark D. Roth76e264b2017-08-25 09:03:33 -0700966 calld->initial_metadata_batch);
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700967}
968
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700969// Applies service config to the call. Must be invoked once we know
970// that the resolver has returned results to the channel.
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800971static void apply_service_config_to_call_locked(grpc_call_element* elem) {
Craig Tillerbaa14a92017-11-03 09:09:36 -0700972 channel_data* chand = (channel_data*)elem->channel_data;
973 call_data* calld = (call_data*)elem->call_data;
Craig Tiller6014e8a2017-10-16 13:50:29 -0700974 if (grpc_client_channel_trace.enabled()) {
Mark D. Roth60751fe2017-07-07 12:50:33 -0700975 gpr_log(GPR_DEBUG, "chand=%p calld=%p: applying service config to call",
976 chand, calld);
977 }
Craig Tiller4782d922017-11-10 09:53:21 -0800978 if (chand->retry_throttle_data != nullptr) {
Mark D. Roth9ccbc4d2017-03-15 08:30:04 -0700979 calld->retry_throttle_data =
980 grpc_server_retry_throttle_data_ref(chand->retry_throttle_data);
981 }
Craig Tiller4782d922017-11-10 09:53:21 -0800982 if (chand->method_params_table != nullptr) {
Craig Tillerbaa14a92017-11-03 09:09:36 -0700983 calld->method_params = (method_parameters*)grpc_method_config_table_get(
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800984 chand->method_params_table, calld->path);
Craig Tiller4782d922017-11-10 09:53:21 -0800985 if (calld->method_params != nullptr) {
Craig Tiller11c17d42017-03-13 13:36:34 -0700986 method_parameters_ref(calld->method_params);
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700987 // If the deadline from the service config is shorter than the one
988 // from the client API, reset the deadline timer.
989 if (chand->deadline_checking_enabled &&
Craig Tiller89c14282017-07-19 15:32:27 -0700990 calld->method_params->timeout != 0) {
991 const grpc_millis per_method_deadline =
Craig Tiller9a8c3f32017-07-21 13:14:14 -0700992 grpc_timespec_to_millis_round_up(calld->call_start_time) +
Craig Tiller89c14282017-07-19 15:32:27 -0700993 calld->method_params->timeout;
994 if (per_method_deadline < calld->deadline) {
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700995 calld->deadline = per_method_deadline;
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800996 grpc_deadline_state_reset(elem, calld->deadline);
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700997 }
Craig Tiller11c17d42017-03-13 13:36:34 -0700998 }
999 }
1000 }
Craig Tiller11c17d42017-03-13 13:36:34 -07001001}
Craig Tillerea4a4f12017-03-13 13:36:52 -07001002
Yash Tibrewal8cf14702017-12-06 09:47:54 -08001003static void create_subchannel_call_locked(grpc_call_element* elem,
Craig Tillerbaa14a92017-11-03 09:09:36 -07001004 grpc_error* error) {
1005 channel_data* chand = (channel_data*)elem->channel_data;
1006 call_data* calld = (call_data*)elem->call_data;
Mark D. Roth0ca0be82017-06-20 07:49:33 -07001007 const grpc_connected_subchannel_call_args call_args = {
Yash Tibrewald8b84a22017-09-25 13:38:03 -07001008 calld->pollent, // pollent
1009 calld->path, // path
1010 calld->call_start_time, // start_time
1011 calld->deadline, // deadline
1012 calld->arena, // arena
1013 calld->subchannel_call_context, // context
1014 calld->call_combiner // call_combiner
1015 };
Craig Tillerbaa14a92017-11-03 09:09:36 -07001016 grpc_error* new_error = grpc_connected_subchannel_create_call(
Yash Tibrewal8cf14702017-12-06 09:47:54 -08001017 calld->connected_subchannel, &call_args, &calld->subchannel_call);
Craig Tiller6014e8a2017-10-16 13:50:29 -07001018 if (grpc_client_channel_trace.enabled()) {
Mark D. Roth60751fe2017-07-07 12:50:33 -07001019 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);
Yash Tibrewal8cf14702017-12-06 09:47:54 -08001024 waiting_for_pick_batches_fail(elem, new_error);
Mark D. Roth0ca0be82017-06-20 07:49:33 -07001025 } else {
Yash Tibrewal8cf14702017-12-06 09:47:54 -08001026 waiting_for_pick_batches_resume(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.
Yash Tibrewal8cf14702017-12-06 09:47:54 -08001032static void pick_done_locked(grpc_call_element* elem, grpc_error* error) {
Craig Tillerbaa14a92017-11-03 09:09:36 -07001033 call_data* calld = (call_data*)elem->call_data;
1034 channel_data* chand = (channel_data*)elem->channel_data;
Craig Tiller4782d922017-11-10 09:53:21 -08001035 if (calld->connected_subchannel == nullptr) {
Mark D. Roth0ca0be82017-06-20 07:49:33 -07001036 // Failed to create subchannel.
Mark D. Roth76e264b2017-08-25 09:03:33 -07001037 GRPC_ERROR_UNREF(calld->error);
1038 calld->error = error == GRPC_ERROR_NONE
1039 ? GRPC_ERROR_CREATE_FROM_STATIC_STRING(
1040 "Call dropped by load balancing policy")
1041 : GRPC_ERROR_CREATE_REFERENCING_FROM_STATIC_STRING(
1042 "Failed to create subchannel", &error, 1);
Craig Tiller6014e8a2017-10-16 13:50:29 -07001043 if (grpc_client_channel_trace.enabled()) {
Mark D. Roth60751fe2017-07-07 12:50:33 -07001044 gpr_log(GPR_DEBUG,
1045 "chand=%p calld=%p: failed to create subchannel: error=%s", chand,
Mark D. Roth76e264b2017-08-25 09:03:33 -07001046 calld, grpc_error_string(calld->error));
Mark D. Roth60751fe2017-07-07 12:50:33 -07001047 }
Yash Tibrewal8cf14702017-12-06 09:47:54 -08001048 waiting_for_pick_batches_fail(elem, GRPC_ERROR_REF(calld->error));
Mark D. Roth2a5959f2016-09-01 08:20:27 -07001049 } else {
Mark D. Roth9fe284e2016-09-12 11:22:27 -07001050 /* Create call on subchannel. */
Yash Tibrewal8cf14702017-12-06 09:47:54 -08001051 create_subchannel_call_locked(elem, GRPC_ERROR_REF(error));
Mark D. Roth2a5959f2016-09-01 08:20:27 -07001052 }
Mark D. Roth0ca0be82017-06-20 07:49:33 -07001053 GRPC_ERROR_UNREF(error);
Mark D. Roth2a5959f2016-09-01 08:20:27 -07001054}
1055
Mark D. Rothb2929602017-09-11 09:31:11 -07001056// A wrapper around pick_done_locked() that is used in cases where
1057// either (a) the pick was deferred pending a resolver result or (b) the
1058// pick was done asynchronously. Removes the call's polling entity from
1059// chand->interested_parties before invoking pick_done_locked().
Yash Tibrewal8cf14702017-12-06 09:47:54 -08001060static void async_pick_done_locked(grpc_call_element* elem, grpc_error* error) {
Craig Tillerbaa14a92017-11-03 09:09:36 -07001061 channel_data* chand = (channel_data*)elem->channel_data;
1062 call_data* calld = (call_data*)elem->call_data;
Yash Tibrewal8cf14702017-12-06 09:47:54 -08001063 grpc_polling_entity_del_from_pollset_set(calld->pollent,
Mark D. Rothb2929602017-09-11 09:31:11 -07001064 chand->interested_parties);
Yash Tibrewal8cf14702017-12-06 09:47:54 -08001065 pick_done_locked(elem, error);
Mark D. Rothb2929602017-09-11 09:31:11 -07001066}
1067
1068// Note: This runs under the client_channel combiner, but will NOT be
1069// holding the call combiner.
Yash Tibrewal8cf14702017-12-06 09:47:54 -08001070static void pick_callback_cancel_locked(void* arg, grpc_error* error) {
Craig Tillerbaa14a92017-11-03 09:09:36 -07001071 grpc_call_element* elem = (grpc_call_element*)arg;
1072 channel_data* chand = (channel_data*)elem->channel_data;
1073 call_data* calld = (call_data*)elem->call_data;
Craig Tiller4782d922017-11-10 09:53:21 -08001074 if (calld->lb_policy != nullptr) {
Craig Tiller6014e8a2017-10-16 13:50:29 -07001075 if (grpc_client_channel_trace.enabled()) {
Mark D. Rothb2929602017-09-11 09:31:11 -07001076 gpr_log(GPR_DEBUG, "chand=%p calld=%p: cancelling pick from LB policy %p",
1077 chand, calld, calld->lb_policy);
1078 }
Yash Tibrewal8cf14702017-12-06 09:47:54 -08001079 grpc_lb_policy_cancel_pick_locked(
1080 calld->lb_policy, &calld->connected_subchannel, GRPC_ERROR_REF(error));
Mark D. Rothb2929602017-09-11 09:31:11 -07001081 }
Yash Tibrewal8cf14702017-12-06 09:47:54 -08001082 GRPC_CALL_STACK_UNREF(calld->owning_call, "pick_callback_cancel");
Mark D. Rothb2929602017-09-11 09:31:11 -07001083}
1084
1085// Callback invoked by grpc_lb_policy_pick_locked() for async picks.
1086// Unrefs the LB policy and invokes async_pick_done_locked().
Yash Tibrewal8cf14702017-12-06 09:47:54 -08001087static void pick_callback_done_locked(void* arg, grpc_error* error) {
Craig Tillerbaa14a92017-11-03 09:09:36 -07001088 grpc_call_element* elem = (grpc_call_element*)arg;
1089 channel_data* chand = (channel_data*)elem->channel_data;
1090 call_data* calld = (call_data*)elem->call_data;
Craig Tiller6014e8a2017-10-16 13:50:29 -07001091 if (grpc_client_channel_trace.enabled()) {
Mark D. Rothb2929602017-09-11 09:31:11 -07001092 gpr_log(GPR_DEBUG, "chand=%p calld=%p: pick completed asynchronously",
1093 chand, calld);
1094 }
Craig Tiller4782d922017-11-10 09:53:21 -08001095 GPR_ASSERT(calld->lb_policy != nullptr);
Yash Tibrewal8cf14702017-12-06 09:47:54 -08001096 GRPC_LB_POLICY_UNREF(calld->lb_policy, "pick_subchannel");
Craig Tiller4782d922017-11-10 09:53:21 -08001097 calld->lb_policy = nullptr;
Yash Tibrewal8cf14702017-12-06 09:47:54 -08001098 async_pick_done_locked(elem, GRPC_ERROR_REF(error));
Mark D. Rothb2929602017-09-11 09:31:11 -07001099}
1100
1101// Takes a ref to chand->lb_policy and calls grpc_lb_policy_pick_locked().
1102// If the pick was completed synchronously, unrefs the LB policy and
1103// returns true.
Yash Tibrewal8cf14702017-12-06 09:47:54 -08001104static bool pick_callback_start_locked(grpc_call_element* elem) {
Craig Tillerbaa14a92017-11-03 09:09:36 -07001105 channel_data* chand = (channel_data*)elem->channel_data;
1106 call_data* calld = (call_data*)elem->call_data;
Craig Tiller6014e8a2017-10-16 13:50:29 -07001107 if (grpc_client_channel_trace.enabled()) {
Mark D. Rothb2929602017-09-11 09:31:11 -07001108 gpr_log(GPR_DEBUG, "chand=%p calld=%p: starting pick on lb_policy=%p",
1109 chand, calld, chand->lb_policy);
1110 }
Yash Tibrewal8cf14702017-12-06 09:47:54 -08001111 apply_service_config_to_call_locked(elem);
Mark D. Rothb2929602017-09-11 09:31:11 -07001112 // If the application explicitly set wait_for_ready, use that.
1113 // Otherwise, if the service config specified a value for this
1114 // method, use that.
1115 uint32_t initial_metadata_flags =
1116 calld->initial_metadata_batch->payload->send_initial_metadata
1117 .send_initial_metadata_flags;
1118 const bool wait_for_ready_set_from_api =
1119 initial_metadata_flags &
1120 GRPC_INITIAL_METADATA_WAIT_FOR_READY_EXPLICITLY_SET;
1121 const bool wait_for_ready_set_from_service_config =
Craig Tiller4782d922017-11-10 09:53:21 -08001122 calld->method_params != nullptr &&
Mark D. Rothb2929602017-09-11 09:31:11 -07001123 calld->method_params->wait_for_ready != WAIT_FOR_READY_UNSET;
1124 if (!wait_for_ready_set_from_api && wait_for_ready_set_from_service_config) {
1125 if (calld->method_params->wait_for_ready == WAIT_FOR_READY_TRUE) {
1126 initial_metadata_flags |= GRPC_INITIAL_METADATA_WAIT_FOR_READY;
1127 } else {
1128 initial_metadata_flags &= ~GRPC_INITIAL_METADATA_WAIT_FOR_READY;
1129 }
1130 }
1131 const grpc_lb_policy_pick_args inputs = {
1132 calld->initial_metadata_batch->payload->send_initial_metadata
1133 .send_initial_metadata,
1134 initial_metadata_flags, &calld->lb_token_mdelem};
1135 // Keep a ref to the LB policy in calld while the pick is pending.
1136 GRPC_LB_POLICY_REF(chand->lb_policy, "pick_subchannel");
1137 calld->lb_policy = chand->lb_policy;
1138 GRPC_CLOSURE_INIT(&calld->lb_pick_closure, pick_callback_done_locked, elem,
1139 grpc_combiner_scheduler(chand->combiner));
1140 const bool pick_done = grpc_lb_policy_pick_locked(
Yash Tibrewal8cf14702017-12-06 09:47:54 -08001141 chand->lb_policy, &inputs, &calld->connected_subchannel,
Craig Tiller4782d922017-11-10 09:53:21 -08001142 calld->subchannel_call_context, nullptr, &calld->lb_pick_closure);
Mark D. Rothb2929602017-09-11 09:31:11 -07001143 if (pick_done) {
1144 /* synchronous grpc_lb_policy_pick call. Unref the LB policy. */
Craig Tiller6014e8a2017-10-16 13:50:29 -07001145 if (grpc_client_channel_trace.enabled()) {
Mark D. Rothb2929602017-09-11 09:31:11 -07001146 gpr_log(GPR_DEBUG, "chand=%p calld=%p: pick completed synchronously",
1147 chand, calld);
1148 }
Yash Tibrewal8cf14702017-12-06 09:47:54 -08001149 GRPC_LB_POLICY_UNREF(calld->lb_policy, "pick_subchannel");
Craig Tiller4782d922017-11-10 09:53:21 -08001150 calld->lb_policy = nullptr;
Mark D. Rothb2929602017-09-11 09:31:11 -07001151 } else {
1152 GRPC_CALL_STACK_REF(calld->owning_call, "pick_callback_cancel");
1153 grpc_call_combiner_set_notify_on_cancel(
Yash Tibrewal8cf14702017-12-06 09:47:54 -08001154 calld->call_combiner,
Mark D. Rothb2929602017-09-11 09:31:11 -07001155 GRPC_CLOSURE_INIT(&calld->lb_pick_cancel_closure,
1156 pick_callback_cancel_locked, elem,
1157 grpc_combiner_scheduler(chand->combiner)));
1158 }
1159 return pick_done;
1160}
Mark D. Roth0ca0be82017-06-20 07:49:33 -07001161
Craig Tiller577c9b22015-11-02 14:11:15 -08001162typedef struct {
Craig Tillerbaa14a92017-11-03 09:09:36 -07001163 grpc_call_element* elem;
Mark D. Roth66f3d2b2017-09-01 09:02:17 -07001164 bool finished;
Craig Tiller577c9b22015-11-02 14:11:15 -08001165 grpc_closure closure;
Mark D. Roth66f3d2b2017-09-01 09:02:17 -07001166 grpc_closure cancel_closure;
Mark D. Roth0ca0be82017-06-20 07:49:33 -07001167} pick_after_resolver_result_args;
Craig Tiller577c9b22015-11-02 14:11:15 -08001168
Mark D. Roth76e264b2017-08-25 09:03:33 -07001169// Note: This runs under the client_channel combiner, but will NOT be
1170// holding the call combiner.
Yash Tibrewal8cf14702017-12-06 09:47:54 -08001171static void pick_after_resolver_result_cancel_locked(void* arg,
Craig Tillerbaa14a92017-11-03 09:09:36 -07001172 grpc_error* error) {
1173 pick_after_resolver_result_args* args = (pick_after_resolver_result_args*)arg;
Mark D. Roth66f3d2b2017-09-01 09:02:17 -07001174 if (args->finished) {
1175 gpr_free(args);
Mark D. Rothb2b9a0f2017-09-01 09:06:47 -07001176 return;
Mark D. Roth764cf042017-09-01 09:00:06 -07001177 }
Mark D. Roth76e264b2017-08-25 09:03:33 -07001178 // If we don't yet have a resolver result, then a closure for
1179 // pick_after_resolver_result_done_locked() will have been added to
1180 // chand->waiting_for_resolver_result_closures, and it may not be invoked
1181 // until after this call has been destroyed. We mark the operation as
Mark D. Rothb2b9a0f2017-09-01 09:06:47 -07001182 // finished, so that when pick_after_resolver_result_done_locked()
Mark D. Roth76e264b2017-08-25 09:03:33 -07001183 // is called, it will be a no-op. We also immediately invoke
Mark D. Rothb2929602017-09-11 09:31:11 -07001184 // async_pick_done_locked() to propagate the error back to the caller.
1185 args->finished = true;
Craig Tillerbaa14a92017-11-03 09:09:36 -07001186 grpc_call_element* elem = args->elem;
1187 channel_data* chand = (channel_data*)elem->channel_data;
1188 call_data* calld = (call_data*)elem->call_data;
Craig Tiller6014e8a2017-10-16 13:50:29 -07001189 if (grpc_client_channel_trace.enabled()) {
Mark D. Rothb2b9a0f2017-09-01 09:06:47 -07001190 gpr_log(GPR_DEBUG,
1191 "chand=%p calld=%p: cancelling pick waiting for resolver result",
1192 chand, calld);
Mark D. Roth76e264b2017-08-25 09:03:33 -07001193 }
Mark D. Rothb2b9a0f2017-09-01 09:06:47 -07001194 // Note: Although we are not in the call combiner here, we are
1195 // basically stealing the call combiner from the pending pick, so
Mark D. Rothb2929602017-09-11 09:31:11 -07001196 // it's safe to call async_pick_done_locked() here -- we are
Mark D. Rothb2b9a0f2017-09-01 09:06:47 -07001197 // essentially calling it here instead of calling it in
1198 // pick_after_resolver_result_done_locked().
Yash Tibrewal8cf14702017-12-06 09:47:54 -08001199 async_pick_done_locked(elem, GRPC_ERROR_CREATE_REFERENCING_FROM_STATIC_STRING(
1200 "Pick cancelled", &error, 1));
Mark D. Roth76e264b2017-08-25 09:03:33 -07001201}
1202
Yash Tibrewal8cf14702017-12-06 09:47:54 -08001203static void pick_after_resolver_result_start_locked(grpc_call_element* elem);
Mark D. Roth48be9de2017-10-23 12:27:37 -07001204
Yash Tibrewal8cf14702017-12-06 09:47:54 -08001205static void pick_after_resolver_result_done_locked(void* arg,
Craig Tillerbaa14a92017-11-03 09:09:36 -07001206 grpc_error* error) {
1207 pick_after_resolver_result_args* args = (pick_after_resolver_result_args*)arg;
Mark D. Roth66f3d2b2017-09-01 09:02:17 -07001208 if (args->finished) {
Craig Tiller577c9b22015-11-02 14:11:15 -08001209 /* cancelled, do nothing */
Craig Tiller6014e8a2017-10-16 13:50:29 -07001210 if (grpc_client_channel_trace.enabled()) {
Mark D. Roth60751fe2017-07-07 12:50:33 -07001211 gpr_log(GPR_DEBUG, "call cancelled before resolver result");
1212 }
Mark D. Roth66f3d2b2017-09-01 09:02:17 -07001213 gpr_free(args);
Mark D. Rothb2b9a0f2017-09-01 09:06:47 -07001214 return;
1215 }
1216 args->finished = true;
Craig Tillerbaa14a92017-11-03 09:09:36 -07001217 grpc_call_element* elem = args->elem;
1218 channel_data* chand = (channel_data*)elem->channel_data;
1219 call_data* calld = (call_data*)elem->call_data;
Mark D. Rothb2b9a0f2017-09-01 09:06:47 -07001220 if (error != GRPC_ERROR_NONE) {
Craig Tiller6014e8a2017-10-16 13:50:29 -07001221 if (grpc_client_channel_trace.enabled()) {
Mark D. Rothb2b9a0f2017-09-01 09:06:47 -07001222 gpr_log(GPR_DEBUG, "chand=%p calld=%p: resolver failed to return data",
1223 chand, calld);
1224 }
Yash Tibrewal8cf14702017-12-06 09:47:54 -08001225 async_pick_done_locked(elem, GRPC_ERROR_REF(error));
Craig Tiller4782d922017-11-10 09:53:21 -08001226 } else if (chand->lb_policy != nullptr) {
Craig Tiller6014e8a2017-10-16 13:50:29 -07001227 if (grpc_client_channel_trace.enabled()) {
Mark D. Rothb2b9a0f2017-09-01 09:06:47 -07001228 gpr_log(GPR_DEBUG, "chand=%p calld=%p: resolver returned, doing pick",
1229 chand, calld);
1230 }
Yash Tibrewal8cf14702017-12-06 09:47:54 -08001231 if (pick_callback_start_locked(elem)) {
Mark D. Rothb2929602017-09-11 09:31:11 -07001232 // Even if the LB policy returns a result synchronously, we have
1233 // already added our polling entity to chand->interested_parties
1234 // in order to wait for the resolver result, so we need to
1235 // remove it here. Therefore, we call async_pick_done_locked()
1236 // instead of pick_done_locked().
Yash Tibrewal8cf14702017-12-06 09:47:54 -08001237 async_pick_done_locked(elem, GRPC_ERROR_NONE);
Mark D. Roth9dab7d52016-10-07 07:48:03 -07001238 }
Craig Tiller577c9b22015-11-02 14:11:15 -08001239 }
Mark D. Roth48be9de2017-10-23 12:27:37 -07001240 // TODO(roth): It should be impossible for chand->lb_policy to be NULL
1241 // here, so the rest of this code should never actually be executed.
1242 // However, we have reports of a crash on iOS that triggers this case,
1243 // so we are temporarily adding this to restore branches that were
1244 // removed in https://github.com/grpc/grpc/pull/12297. Need to figure
1245 // out what is actually causing this to occur and then figure out the
1246 // right way to deal with it.
Craig Tiller4782d922017-11-10 09:53:21 -08001247 else if (chand->resolver != nullptr) {
Mark D. Roth48be9de2017-10-23 12:27:37 -07001248 // No LB policy, so try again.
ncteisen72afb762017-11-10 12:23:12 -08001249 if (grpc_client_channel_trace.enabled()) {
Mark D. Roth48be9de2017-10-23 12:27:37 -07001250 gpr_log(GPR_DEBUG,
1251 "chand=%p calld=%p: resolver returned but no LB policy, "
1252 "trying again",
1253 chand, calld);
1254 }
Yash Tibrewal8cf14702017-12-06 09:47:54 -08001255 pick_after_resolver_result_start_locked(elem);
Mark D. Roth48be9de2017-10-23 12:27:37 -07001256 } else {
ncteisen72afb762017-11-10 12:23:12 -08001257 if (grpc_client_channel_trace.enabled()) {
Mark D. Roth48be9de2017-10-23 12:27:37 -07001258 gpr_log(GPR_DEBUG, "chand=%p calld=%p: resolver disconnected", chand,
1259 calld);
1260 }
1261 async_pick_done_locked(
Yash Tibrewal8cf14702017-12-06 09:47:54 -08001262 elem, GRPC_ERROR_CREATE_FROM_STATIC_STRING("Disconnected"));
Mark D. Roth48be9de2017-10-23 12:27:37 -07001263 }
Craig Tiller577c9b22015-11-02 14:11:15 -08001264}
1265
Yash Tibrewal8cf14702017-12-06 09:47:54 -08001266static void pick_after_resolver_result_start_locked(grpc_call_element* elem) {
Craig Tillerbaa14a92017-11-03 09:09:36 -07001267 channel_data* chand = (channel_data*)elem->channel_data;
1268 call_data* calld = (call_data*)elem->call_data;
Craig Tiller6014e8a2017-10-16 13:50:29 -07001269 if (grpc_client_channel_trace.enabled()) {
Mark D. Roth60751fe2017-07-07 12:50:33 -07001270 gpr_log(GPR_DEBUG,
1271 "chand=%p calld=%p: deferring pick pending resolver result", chand,
1272 calld);
Mark D. Roth64a317c2017-05-02 08:27:08 -07001273 }
Craig Tillerbaa14a92017-11-03 09:09:36 -07001274 pick_after_resolver_result_args* args =
1275 (pick_after_resolver_result_args*)gpr_zalloc(sizeof(*args));
Mark D. Roth60751fe2017-07-07 12:50:33 -07001276 args->elem = elem;
1277 GRPC_CLOSURE_INIT(&args->closure, pick_after_resolver_result_done_locked,
1278 args, grpc_combiner_scheduler(chand->combiner));
1279 grpc_closure_list_append(&chand->waiting_for_resolver_result_closures,
1280 &args->closure, GRPC_ERROR_NONE);
Mark D. Roth76e264b2017-08-25 09:03:33 -07001281 grpc_call_combiner_set_notify_on_cancel(
Yash Tibrewal8cf14702017-12-06 09:47:54 -08001282 calld->call_combiner,
Mark D. Roth66f3d2b2017-09-01 09:02:17 -07001283 GRPC_CLOSURE_INIT(&args->cancel_closure,
1284 pick_after_resolver_result_cancel_locked, args,
Mark D. Roth76e264b2017-08-25 09:03:33 -07001285 grpc_combiner_scheduler(chand->combiner)));
Mark D. Roth60751fe2017-07-07 12:50:33 -07001286}
1287
Yash Tibrewal8cf14702017-12-06 09:47:54 -08001288static void start_pick_locked(void* arg, grpc_error* ignored) {
Craig Tillerbaa14a92017-11-03 09:09:36 -07001289 grpc_call_element* elem = (grpc_call_element*)arg;
1290 call_data* calld = (call_data*)elem->call_data;
1291 channel_data* chand = (channel_data*)elem->channel_data;
Craig Tiller4782d922017-11-10 09:53:21 -08001292 GPR_ASSERT(calld->connected_subchannel == nullptr);
1293 if (chand->lb_policy != nullptr) {
Mark D. Rothb2929602017-09-11 09:31:11 -07001294 // We already have an LB policy, so ask it for a pick.
Yash Tibrewal8cf14702017-12-06 09:47:54 -08001295 if (pick_callback_start_locked(elem)) {
Mark D. Rothb2929602017-09-11 09:31:11 -07001296 // Pick completed synchronously.
Yash Tibrewal8cf14702017-12-06 09:47:54 -08001297 pick_done_locked(elem, GRPC_ERROR_NONE);
Mark D. Rothb2929602017-09-11 09:31:11 -07001298 return;
Mark D. Roth2a5959f2016-09-01 08:20:27 -07001299 }
Mark D. Roth76e264b2017-08-25 09:03:33 -07001300 } else {
Mark D. Rothb2929602017-09-11 09:31:11 -07001301 // We do not yet have an LB policy, so wait for a resolver result.
Craig Tiller4782d922017-11-10 09:53:21 -08001302 if (chand->resolver == nullptr) {
Yash Tibrewal8cf14702017-12-06 09:47:54 -08001303 pick_done_locked(elem,
Mark D. Rothb2929602017-09-11 09:31:11 -07001304 GRPC_ERROR_CREATE_FROM_STATIC_STRING("Disconnected"));
1305 return;
1306 }
1307 if (!chand->started_resolving) {
Yash Tibrewal8cf14702017-12-06 09:47:54 -08001308 start_resolving_locked(chand);
Mark D. Rothb2929602017-09-11 09:31:11 -07001309 }
Yash Tibrewal8cf14702017-12-06 09:47:54 -08001310 pick_after_resolver_result_start_locked(elem);
Mark D. Roth2a5959f2016-09-01 08:20:27 -07001311 }
Mark D. Rothb2929602017-09-11 09:31:11 -07001312 // We need to wait for either a resolver result or for an async result
1313 // from the LB policy. Add the polling entity from call_data to the
1314 // channel_data's interested_parties, so that the I/O of the LB policy
1315 // and resolver can be done under it. The polling entity will be
1316 // removed in async_pick_done_locked().
Yash Tibrewal8cf14702017-12-06 09:47:54 -08001317 grpc_polling_entity_add_to_pollset_set(calld->pollent,
Mark D. Rothb2929602017-09-11 09:31:11 -07001318 chand->interested_parties);
Craig Tillera11bfc82017-02-14 09:56:33 -08001319}
1320
Yash Tibrewal8cf14702017-12-06 09:47:54 -08001321static void on_complete(void* arg, grpc_error* error) {
Craig Tillerbaa14a92017-11-03 09:09:36 -07001322 grpc_call_element* elem = (grpc_call_element*)arg;
1323 call_data* calld = (call_data*)elem->call_data;
Craig Tiller4782d922017-11-10 09:53:21 -08001324 if (calld->retry_throttle_data != nullptr) {
Mark D. Rothd6d192d2017-02-23 08:58:42 -08001325 if (error == GRPC_ERROR_NONE) {
1326 grpc_server_retry_throttle_data_record_success(
Mark D. Roth9ccbc4d2017-03-15 08:30:04 -07001327 calld->retry_throttle_data);
Mark D. Rothd6d192d2017-02-23 08:58:42 -08001328 } else {
1329 // TODO(roth): In a subsequent PR, check the return value here and
Mark D. Rothb3322562017-02-23 14:38:02 -08001330 // decide whether or not to retry. Note that we should only
1331 // record failures whose statuses match the configured retryable
1332 // or non-fatal status codes.
Mark D. Rothd6d192d2017-02-23 08:58:42 -08001333 grpc_server_retry_throttle_data_record_failure(
Mark D. Roth9ccbc4d2017-03-15 08:30:04 -07001334 calld->retry_throttle_data);
Mark D. Rothd6d192d2017-02-23 08:58:42 -08001335 }
1336 }
Yash Tibrewal8cf14702017-12-06 09:47:54 -08001337 GRPC_CLOSURE_RUN(calld->original_on_complete, GRPC_ERROR_REF(error));
Mark D. Rothd6d192d2017-02-23 08:58:42 -08001338}
1339
Craig Tillere1b51da2017-03-31 15:44:33 -07001340static void cc_start_transport_stream_op_batch(
Yash Tibrewal8cf14702017-12-06 09:47:54 -08001341 grpc_call_element* elem, grpc_transport_stream_op_batch* batch) {
Craig Tillerbaa14a92017-11-03 09:09:36 -07001342 call_data* calld = (call_data*)elem->call_data;
1343 channel_data* chand = (channel_data*)elem->channel_data;
Craig Tiller3be7dd02017-04-03 14:30:03 -07001344 if (chand->deadline_checking_enabled) {
Yash Tibrewal8cf14702017-12-06 09:47:54 -08001345 grpc_deadline_state_client_start_transport_stream_op_batch(elem, batch);
Craig Tiller3be7dd02017-04-03 14:30:03 -07001346 }
Mark D. Roth76e264b2017-08-25 09:03:33 -07001347 GPR_TIMER_BEGIN("cc_start_transport_stream_op_batch", 0);
1348 // If we've previously been cancelled, immediately fail any new batches.
1349 if (calld->error != GRPC_ERROR_NONE) {
Craig Tiller6014e8a2017-10-16 13:50:29 -07001350 if (grpc_client_channel_trace.enabled()) {
Mark D. Roth76e264b2017-08-25 09:03:33 -07001351 gpr_log(GPR_DEBUG, "chand=%p calld=%p: failing batch with error: %s",
1352 chand, calld, grpc_error_string(calld->error));
1353 }
1354 grpc_transport_stream_op_batch_finish_with_failure(
Yash Tibrewal8cf14702017-12-06 09:47:54 -08001355 batch, GRPC_ERROR_REF(calld->error), calld->call_combiner);
Mark D. Roth76e264b2017-08-25 09:03:33 -07001356 goto done;
1357 }
1358 if (batch->cancel_stream) {
1359 // Stash a copy of cancel_error in our call data, so that we can use
1360 // it for subsequent operations. This ensures that if the call is
1361 // cancelled before any batches are passed down (e.g., if the deadline
1362 // is in the past when the call starts), we can return the right
1363 // error to the caller when the first batch does get passed down.
1364 GRPC_ERROR_UNREF(calld->error);
1365 calld->error = GRPC_ERROR_REF(batch->payload->cancel_stream.cancel_error);
Craig Tiller6014e8a2017-10-16 13:50:29 -07001366 if (grpc_client_channel_trace.enabled()) {
Mark D. Roth76e264b2017-08-25 09:03:33 -07001367 gpr_log(GPR_DEBUG, "chand=%p calld=%p: recording cancel_error=%s", chand,
1368 calld, grpc_error_string(calld->error));
1369 }
1370 // If we have a subchannel call, send the cancellation batch down.
1371 // Otherwise, fail all pending batches.
Craig Tiller4782d922017-11-10 09:53:21 -08001372 if (calld->subchannel_call != nullptr) {
Yash Tibrewal8cf14702017-12-06 09:47:54 -08001373 grpc_subchannel_call_process_op(calld->subchannel_call, batch);
Mark D. Roth76e264b2017-08-25 09:03:33 -07001374 } else {
1375 waiting_for_pick_batches_add(calld, batch);
Yash Tibrewal8cf14702017-12-06 09:47:54 -08001376 waiting_for_pick_batches_fail(elem, GRPC_ERROR_REF(calld->error));
Mark D. Roth76e264b2017-08-25 09:03:33 -07001377 }
1378 goto done;
1379 }
Mark D. Roth0ca0be82017-06-20 07:49:33 -07001380 // Intercept on_complete for recv_trailing_metadata so that we can
1381 // check retry throttle status.
Mark D. Roth60751fe2017-07-07 12:50:33 -07001382 if (batch->recv_trailing_metadata) {
Craig Tiller4782d922017-11-10 09:53:21 -08001383 GPR_ASSERT(batch->on_complete != nullptr);
Mark D. Roth60751fe2017-07-07 12:50:33 -07001384 calld->original_on_complete = batch->on_complete;
Mark D. Roth0ca0be82017-06-20 07:49:33 -07001385 GRPC_CLOSURE_INIT(&calld->on_complete, on_complete, elem,
1386 grpc_schedule_on_exec_ctx);
Mark D. Roth60751fe2017-07-07 12:50:33 -07001387 batch->on_complete = &calld->on_complete;
Mark D. Roth0ca0be82017-06-20 07:49:33 -07001388 }
Mark D. Roth76e264b2017-08-25 09:03:33 -07001389 // Check if we've already gotten a subchannel call.
1390 // Note that once we have completed the pick, we do not need to enter
1391 // the channel combiner, which is more efficient (especially for
1392 // streaming calls).
Craig Tiller4782d922017-11-10 09:53:21 -08001393 if (calld->subchannel_call != nullptr) {
Craig Tiller6014e8a2017-10-16 13:50:29 -07001394 if (grpc_client_channel_trace.enabled()) {
Mark D. Roth60751fe2017-07-07 12:50:33 -07001395 gpr_log(GPR_DEBUG,
1396 "chand=%p calld=%p: sending batch to subchannel_call=%p", chand,
Mark D. Roth76e264b2017-08-25 09:03:33 -07001397 calld, calld->subchannel_call);
Mark D. Roth60751fe2017-07-07 12:50:33 -07001398 }
Yash Tibrewal8cf14702017-12-06 09:47:54 -08001399 grpc_subchannel_call_process_op(calld->subchannel_call, batch);
Mark D. Roth60751fe2017-07-07 12:50:33 -07001400 goto done;
Mark D. Roth2a5959f2016-09-01 08:20:27 -07001401 }
Mark D. Roth76e264b2017-08-25 09:03:33 -07001402 // We do not yet have a subchannel call.
1403 // Add the batch to the waiting-for-pick list.
1404 waiting_for_pick_batches_add(calld, batch);
1405 // For batches containing a send_initial_metadata op, enter the channel
1406 // combiner to start a pick.
1407 if (batch->send_initial_metadata) {
Craig Tiller6014e8a2017-10-16 13:50:29 -07001408 if (grpc_client_channel_trace.enabled()) {
Mark D. Rothb2929602017-09-11 09:31:11 -07001409 gpr_log(GPR_DEBUG, "chand=%p calld=%p: entering client_channel combiner",
1410 chand, calld);
Mark D. Roth76e264b2017-08-25 09:03:33 -07001411 }
1412 GRPC_CLOSURE_SCHED(
Mark D. Roth76e264b2017-08-25 09:03:33 -07001413 GRPC_CLOSURE_INIT(&batch->handler_private.closure, start_pick_locked,
1414 elem, grpc_combiner_scheduler(chand->combiner)),
1415 GRPC_ERROR_NONE);
1416 } else {
1417 // For all other batches, release the call combiner.
Craig Tiller6014e8a2017-10-16 13:50:29 -07001418 if (grpc_client_channel_trace.enabled()) {
Mark D. Roth76e264b2017-08-25 09:03:33 -07001419 gpr_log(GPR_DEBUG,
1420 "chand=%p calld=%p: saved batch, yeilding call combiner", chand,
1421 calld);
1422 }
Yash Tibrewal8cf14702017-12-06 09:47:54 -08001423 GRPC_CALL_COMBINER_STOP(calld->call_combiner,
Mark D. Roth76e264b2017-08-25 09:03:33 -07001424 "batch does not include send_initial_metadata");
Mark D. Roth60751fe2017-07-07 12:50:33 -07001425 }
Mark D. Roth60751fe2017-07-07 12:50:33 -07001426done:
Craig Tillera0f3abd2017-03-31 15:42:16 -07001427 GPR_TIMER_END("cc_start_transport_stream_op_batch", 0);
Mark D. Roth2a5959f2016-09-01 08:20:27 -07001428}
1429
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001430/* Constructor for call_data */
Yash Tibrewal8cf14702017-12-06 09:47:54 -08001431static grpc_error* cc_init_call_elem(grpc_call_element* elem,
Craig Tillerbaa14a92017-11-03 09:09:36 -07001432 const grpc_call_element_args* args) {
1433 call_data* calld = (call_data*)elem->call_data;
1434 channel_data* chand = (channel_data*)elem->channel_data;
Mark D. Rothe40dd292016-10-05 14:58:37 -07001435 // Initialize data members.
Craig Tiller7c70b6c2017-01-23 07:48:42 -08001436 calld->path = grpc_slice_ref_internal(args->path);
Mark D. Rothff08f332016-10-14 13:01:01 -07001437 calld->call_start_time = args->start_time;
Craig Tiller89c14282017-07-19 15:32:27 -07001438 calld->deadline = args->deadline;
Craig Tillerd426cac2017-03-13 12:30:45 -07001439 calld->arena = args->arena;
Mark D. Roth66f3d2b2017-09-01 09:02:17 -07001440 calld->owning_call = args->call_stack;
Mark D. Roth76e264b2017-08-25 09:03:33 -07001441 calld->call_combiner = args->call_combiner;
Craig Tiller3be7dd02017-04-03 14:30:03 -07001442 if (chand->deadline_checking_enabled) {
Yash Tibrewal8cf14702017-12-06 09:47:54 -08001443 grpc_deadline_state_init(elem, args->call_stack, args->call_combiner,
1444 calld->deadline);
Craig Tiller3be7dd02017-04-03 14:30:03 -07001445 }
Mark D. Roth0badbe82016-06-23 10:15:12 -07001446 return GRPC_ERROR_NONE;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001447}
1448
1449/* Destructor for call_data */
Yash Tibrewal8cf14702017-12-06 09:47:54 -08001450static void cc_destroy_call_elem(grpc_call_element* elem,
Craig Tillerbaa14a92017-11-03 09:09:36 -07001451 const grpc_call_final_info* final_info,
1452 grpc_closure* then_schedule_closure) {
1453 call_data* calld = (call_data*)elem->call_data;
1454 channel_data* chand = (channel_data*)elem->channel_data;
Craig Tiller3be7dd02017-04-03 14:30:03 -07001455 if (chand->deadline_checking_enabled) {
Yash Tibrewal8cf14702017-12-06 09:47:54 -08001456 grpc_deadline_state_destroy(elem);
Craig Tiller3be7dd02017-04-03 14:30:03 -07001457 }
Yash Tibrewal8cf14702017-12-06 09:47:54 -08001458 grpc_slice_unref_internal(calld->path);
Craig Tiller4782d922017-11-10 09:53:21 -08001459 if (calld->method_params != nullptr) {
Mark D. Roth95b627b2017-02-24 11:02:58 -08001460 method_parameters_unref(calld->method_params);
1461 }
Mark D. Roth76e264b2017-08-25 09:03:33 -07001462 GRPC_ERROR_UNREF(calld->error);
Craig Tiller4782d922017-11-10 09:53:21 -08001463 if (calld->subchannel_call != nullptr) {
Mark D. Roth76e264b2017-08-25 09:03:33 -07001464 grpc_subchannel_call_set_cleanup_closure(calld->subchannel_call,
Craig Tillerf7c8c9f2017-05-17 15:22:05 -07001465 then_schedule_closure);
Craig Tiller4782d922017-11-10 09:53:21 -08001466 then_schedule_closure = nullptr;
Yash Tibrewal8cf14702017-12-06 09:47:54 -08001467 GRPC_SUBCHANNEL_CALL_UNREF(calld->subchannel_call,
Craig Tillerf7c8c9f2017-05-17 15:22:05 -07001468 "client_channel_destroy_call");
Mark D. Roth4c0fe492016-08-31 13:51:55 -07001469 }
Craig Tiller4782d922017-11-10 09:53:21 -08001470 GPR_ASSERT(calld->lb_policy == nullptr);
Mark D. Roth0ca0be82017-06-20 07:49:33 -07001471 GPR_ASSERT(calld->waiting_for_pick_batches_count == 0);
Craig Tiller4782d922017-11-10 09:53:21 -08001472 if (calld->connected_subchannel != nullptr) {
Yash Tibrewal8cf14702017-12-06 09:47:54 -08001473 GRPC_CONNECTED_SUBCHANNEL_UNREF(calld->connected_subchannel, "picked");
Craig Tiller693d3942016-10-27 16:51:25 -07001474 }
Mark D. Roth09e458c2017-05-02 08:13:26 -07001475 for (size_t i = 0; i < GRPC_CONTEXT_COUNT; ++i) {
Craig Tiller4782d922017-11-10 09:53:21 -08001476 if (calld->subchannel_call_context[i].value != nullptr) {
Mark D. Roth09e458c2017-05-02 08:13:26 -07001477 calld->subchannel_call_context[i].destroy(
1478 calld->subchannel_call_context[i].value);
1479 }
1480 }
Yash Tibrewal8cf14702017-12-06 09:47:54 -08001481 GRPC_CLOSURE_SCHED(then_schedule_closure, GRPC_ERROR_NONE);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001482}
1483
Yash Tibrewal8cf14702017-12-06 09:47:54 -08001484static void cc_set_pollset_or_pollset_set(grpc_call_element* elem,
Craig Tillerbaa14a92017-11-03 09:09:36 -07001485 grpc_polling_entity* pollent) {
1486 call_data* calld = (call_data*)elem->call_data;
David Garcia Quintas2a50dfe2016-05-31 15:09:12 -07001487 calld->pollent = pollent;
Craig Tiller577c9b22015-11-02 14:11:15 -08001488}
1489
Mark D. Roth2a5959f2016-09-01 08:20:27 -07001490/*************************************************************************
1491 * EXPORTED SYMBOLS
1492 */
1493
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001494const grpc_channel_filter grpc_client_channel_filter = {
Craig Tillera0f3abd2017-03-31 15:42:16 -07001495 cc_start_transport_stream_op_batch,
Craig Tillerf40df232016-03-25 13:38:14 -07001496 cc_start_transport_op,
1497 sizeof(call_data),
Mark D. Roth2a5959f2016-09-01 08:20:27 -07001498 cc_init_call_elem,
David Garcia Quintas4afce7e2016-04-18 16:25:17 -07001499 cc_set_pollset_or_pollset_set,
Mark D. Roth2a5959f2016-09-01 08:20:27 -07001500 cc_destroy_call_elem,
Craig Tillerf40df232016-03-25 13:38:14 -07001501 sizeof(channel_data),
Mark D. Roth2a5959f2016-09-01 08:20:27 -07001502 cc_init_channel_elem,
1503 cc_destroy_channel_elem,
Mark D. Rothb2d24882016-10-27 15:44:07 -07001504 cc_get_channel_info,
Craig Tillerf40df232016-03-25 13:38:14 -07001505 "client-channel",
Craig Tiller87d5b192015-04-16 14:37:57 -07001506};
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001507
Yash Tibrewal8cf14702017-12-06 09:47:54 -08001508static void try_to_connect_locked(void* arg, grpc_error* error_ignored) {
Craig Tillerbaa14a92017-11-03 09:09:36 -07001509 channel_data* chand = (channel_data*)arg;
Craig Tiller4782d922017-11-10 09:53:21 -08001510 if (chand->lb_policy != nullptr) {
Yash Tibrewal8cf14702017-12-06 09:47:54 -08001511 grpc_lb_policy_exit_idle_locked(chand->lb_policy);
Craig Tiller613dafa2017-02-09 12:00:43 -08001512 } else {
1513 chand->exit_idle_when_lb_policy_arrives = true;
Craig Tiller4782d922017-11-10 09:53:21 -08001514 if (!chand->started_resolving && chand->resolver != nullptr) {
Yash Tibrewal8cf14702017-12-06 09:47:54 -08001515 start_resolving_locked(chand);
Craig Tiller613dafa2017-02-09 12:00:43 -08001516 }
1517 }
Yash Tibrewal8cf14702017-12-06 09:47:54 -08001518 GRPC_CHANNEL_STACK_UNREF(chand->owning_stack, "try_to_connect");
Craig Tiller613dafa2017-02-09 12:00:43 -08001519}
1520
Craig Tillera82950e2015-09-22 12:33:20 -07001521grpc_connectivity_state grpc_client_channel_check_connectivity_state(
Yash Tibrewal8cf14702017-12-06 09:47:54 -08001522 grpc_channel_element* elem, int try_to_connect) {
Craig Tillerbaa14a92017-11-03 09:09:36 -07001523 channel_data* chand = (channel_data*)elem->channel_data;
Craig Tillera8610c02017-02-14 10:05:11 -08001524 grpc_connectivity_state out =
1525 grpc_connectivity_state_check(&chand->state_tracker);
Craig Tillera82950e2015-09-22 12:33:20 -07001526 if (out == GRPC_CHANNEL_IDLE && try_to_connect) {
Craig Tillerd2e5cfc2017-02-09 13:02:20 -08001527 GRPC_CHANNEL_STACK_REF(chand->owning_stack, "try_to_connect");
ncteisen274bbbe2017-06-08 14:57:11 -07001528 GRPC_CLOSURE_SCHED(
Yash Tibrewal0ee75742017-10-13 16:07:13 -07001529 GRPC_CLOSURE_CREATE(try_to_connect_locked, chand,
1530 grpc_combiner_scheduler(chand->combiner)),
Craig Tiller613dafa2017-02-09 12:00:43 -08001531 GRPC_ERROR_NONE);
Craig Tillera82950e2015-09-22 12:33:20 -07001532 }
Craig Tiller48cb07c2015-07-15 16:16:15 -07001533 return out;
1534}
1535
Alexander Polcync3b1f182017-04-18 13:51:36 -07001536typedef struct external_connectivity_watcher {
Craig Tillerbaa14a92017-11-03 09:09:36 -07001537 channel_data* chand;
David Garcia Quintas87d5a312017-06-06 19:45:58 -07001538 grpc_polling_entity pollent;
Craig Tillerbaa14a92017-11-03 09:09:36 -07001539 grpc_closure* on_complete;
1540 grpc_closure* watcher_timer_init;
1541 grpc_connectivity_state* state;
Craig Tiller86c99582015-11-25 15:22:26 -08001542 grpc_closure my_closure;
Craig Tillerbaa14a92017-11-03 09:09:36 -07001543 struct external_connectivity_watcher* next;
Craig Tiller86c99582015-11-25 15:22:26 -08001544} external_connectivity_watcher;
1545
Craig Tillerbaa14a92017-11-03 09:09:36 -07001546static external_connectivity_watcher* lookup_external_connectivity_watcher(
1547 channel_data* chand, grpc_closure* on_complete) {
Alexander Polcync3b1f182017-04-18 13:51:36 -07001548 gpr_mu_lock(&chand->external_connectivity_watcher_list_mu);
Craig Tillerbaa14a92017-11-03 09:09:36 -07001549 external_connectivity_watcher* w =
Alexander Polcync3b1f182017-04-18 13:51:36 -07001550 chand->external_connectivity_watcher_list_head;
Craig Tiller4782d922017-11-10 09:53:21 -08001551 while (w != nullptr && w->on_complete != on_complete) {
Alexander Polcync3b1f182017-04-18 13:51:36 -07001552 w = w->next;
1553 }
1554 gpr_mu_unlock(&chand->external_connectivity_watcher_list_mu);
1555 return w;
1556}
1557
1558static void external_connectivity_watcher_list_append(
Craig Tillerbaa14a92017-11-03 09:09:36 -07001559 channel_data* chand, external_connectivity_watcher* w) {
Alexander Polcync3b1f182017-04-18 13:51:36 -07001560 GPR_ASSERT(!lookup_external_connectivity_watcher(chand, w->on_complete));
1561
1562 gpr_mu_lock(&w->chand->external_connectivity_watcher_list_mu);
1563 GPR_ASSERT(!w->next);
1564 w->next = chand->external_connectivity_watcher_list_head;
1565 chand->external_connectivity_watcher_list_head = w;
1566 gpr_mu_unlock(&w->chand->external_connectivity_watcher_list_mu);
1567}
1568
1569static void external_connectivity_watcher_list_remove(
Craig Tillerbaa14a92017-11-03 09:09:36 -07001570 channel_data* chand, external_connectivity_watcher* too_remove) {
Alexander Polcync3b1f182017-04-18 13:51:36 -07001571 GPR_ASSERT(
1572 lookup_external_connectivity_watcher(chand, too_remove->on_complete));
1573 gpr_mu_lock(&chand->external_connectivity_watcher_list_mu);
1574 if (too_remove == chand->external_connectivity_watcher_list_head) {
1575 chand->external_connectivity_watcher_list_head = too_remove->next;
1576 gpr_mu_unlock(&chand->external_connectivity_watcher_list_mu);
1577 return;
1578 }
Craig Tillerbaa14a92017-11-03 09:09:36 -07001579 external_connectivity_watcher* w =
Alexander Polcync3b1f182017-04-18 13:51:36 -07001580 chand->external_connectivity_watcher_list_head;
Craig Tiller4782d922017-11-10 09:53:21 -08001581 while (w != nullptr) {
Alexander Polcync3b1f182017-04-18 13:51:36 -07001582 if (w->next == too_remove) {
1583 w->next = w->next->next;
1584 gpr_mu_unlock(&chand->external_connectivity_watcher_list_mu);
1585 return;
1586 }
1587 w = w->next;
1588 }
1589 GPR_UNREACHABLE_CODE(return );
1590}
1591
1592int grpc_client_channel_num_external_connectivity_watchers(
Craig Tillerbaa14a92017-11-03 09:09:36 -07001593 grpc_channel_element* elem) {
1594 channel_data* chand = (channel_data*)elem->channel_data;
Alexander Polcync3b1f182017-04-18 13:51:36 -07001595 int count = 0;
1596
1597 gpr_mu_lock(&chand->external_connectivity_watcher_list_mu);
Craig Tillerbaa14a92017-11-03 09:09:36 -07001598 external_connectivity_watcher* w =
Alexander Polcync3b1f182017-04-18 13:51:36 -07001599 chand->external_connectivity_watcher_list_head;
Craig Tiller4782d922017-11-10 09:53:21 -08001600 while (w != nullptr) {
Alexander Polcync3b1f182017-04-18 13:51:36 -07001601 count++;
1602 w = w->next;
1603 }
1604 gpr_mu_unlock(&chand->external_connectivity_watcher_list_mu);
1605
1606 return count;
1607}
1608
Yash Tibrewal8cf14702017-12-06 09:47:54 -08001609static void on_external_watch_complete_locked(void* arg, grpc_error* error) {
Craig Tillerbaa14a92017-11-03 09:09:36 -07001610 external_connectivity_watcher* w = (external_connectivity_watcher*)arg;
1611 grpc_closure* follow_up = w->on_complete;
Yash Tibrewal8cf14702017-12-06 09:47:54 -08001612 grpc_polling_entity_del_from_pollset_set(&w->pollent,
David Garcia Quintas87d5a312017-06-06 19:45:58 -07001613 w->chand->interested_parties);
Yash Tibrewal8cf14702017-12-06 09:47:54 -08001614 GRPC_CHANNEL_STACK_UNREF(w->chand->owning_stack,
Craig Tiller1d881fb2015-12-01 07:39:04 -08001615 "external_connectivity_watcher");
Alexander Polcync3b1f182017-04-18 13:51:36 -07001616 external_connectivity_watcher_list_remove(w->chand, w);
Craig Tiller86c99582015-11-25 15:22:26 -08001617 gpr_free(w);
Yash Tibrewal8cf14702017-12-06 09:47:54 -08001618 GRPC_CLOSURE_RUN(follow_up, GRPC_ERROR_REF(error));
Craig Tiller613dafa2017-02-09 12:00:43 -08001619}
1620
Yash Tibrewal8cf14702017-12-06 09:47:54 -08001621static void watch_connectivity_state_locked(void* arg,
Craig Tillerbaa14a92017-11-03 09:09:36 -07001622 grpc_error* error_ignored) {
1623 external_connectivity_watcher* w = (external_connectivity_watcher*)arg;
Craig Tiller4782d922017-11-10 09:53:21 -08001624 external_connectivity_watcher* found = nullptr;
1625 if (w->state != nullptr) {
Alexander Polcync3b1f182017-04-18 13:51:36 -07001626 external_connectivity_watcher_list_append(w->chand, w);
Yash Tibrewal8cf14702017-12-06 09:47:54 -08001627 GRPC_CLOSURE_RUN(w->watcher_timer_init, GRPC_ERROR_NONE);
Alexander Polcyn2004e392017-10-16 15:14:46 -07001628 GRPC_CLOSURE_INIT(&w->my_closure, on_external_watch_complete_locked, w,
1629 grpc_combiner_scheduler(w->chand->combiner));
Yash Tibrewal8cf14702017-12-06 09:47:54 -08001630 grpc_connectivity_state_notify_on_state_change(&w->chand->state_tracker,
1631 w->state, &w->my_closure);
Alexander Polcync3b1f182017-04-18 13:51:36 -07001632 } else {
Craig Tiller4782d922017-11-10 09:53:21 -08001633 GPR_ASSERT(w->watcher_timer_init == nullptr);
Alexander Polcync3b1f182017-04-18 13:51:36 -07001634 found = lookup_external_connectivity_watcher(w->chand, w->on_complete);
1635 if (found) {
1636 GPR_ASSERT(found->on_complete == w->on_complete);
1637 grpc_connectivity_state_notify_on_state_change(
Yash Tibrewal8cf14702017-12-06 09:47:54 -08001638 &found->chand->state_tracker, nullptr, &found->my_closure);
Alexander Polcync3b1f182017-04-18 13:51:36 -07001639 }
Yash Tibrewal8cf14702017-12-06 09:47:54 -08001640 grpc_polling_entity_del_from_pollset_set(&w->pollent,
David Garcia Quintas87d5a312017-06-06 19:45:58 -07001641 w->chand->interested_parties);
Yash Tibrewal8cf14702017-12-06 09:47:54 -08001642 GRPC_CHANNEL_STACK_UNREF(w->chand->owning_stack,
Alexander Polcync3b1f182017-04-18 13:51:36 -07001643 "external_connectivity_watcher");
1644 gpr_free(w);
1645 }
Craig Tiller86c99582015-11-25 15:22:26 -08001646}
1647
Craig Tillera82950e2015-09-22 12:33:20 -07001648void grpc_client_channel_watch_connectivity_state(
Yash Tibrewal8cf14702017-12-06 09:47:54 -08001649 grpc_channel_element* elem, grpc_polling_entity pollent,
1650 grpc_connectivity_state* state, grpc_closure* closure,
1651 grpc_closure* watcher_timer_init) {
Craig Tillerbaa14a92017-11-03 09:09:36 -07001652 channel_data* chand = (channel_data*)elem->channel_data;
1653 external_connectivity_watcher* w =
1654 (external_connectivity_watcher*)gpr_zalloc(sizeof(*w));
Craig Tiller86c99582015-11-25 15:22:26 -08001655 w->chand = chand;
David Garcia Quintas87d5a312017-06-06 19:45:58 -07001656 w->pollent = pollent;
Mark D. Roth92210832017-05-02 15:04:39 -07001657 w->on_complete = closure;
Craig Tiller613dafa2017-02-09 12:00:43 -08001658 w->state = state;
Alexander Polcync3b1f182017-04-18 13:51:36 -07001659 w->watcher_timer_init = watcher_timer_init;
Yash Tibrewal8cf14702017-12-06 09:47:54 -08001660 grpc_polling_entity_add_to_pollset_set(&w->pollent,
David Garcia Quintas87d5a312017-06-06 19:45:58 -07001661 chand->interested_parties);
Craig Tiller1d881fb2015-12-01 07:39:04 -08001662 GRPC_CHANNEL_STACK_REF(w->chand->owning_stack,
1663 "external_connectivity_watcher");
ncteisen274bbbe2017-06-08 14:57:11 -07001664 GRPC_CLOSURE_SCHED(
ncteisen274bbbe2017-06-08 14:57:11 -07001665 GRPC_CLOSURE_INIT(&w->my_closure, watch_connectivity_state_locked, w,
Craig Tilleree4b1452017-05-12 10:56:03 -07001666 grpc_combiner_scheduler(chand->combiner)),
Craig Tiller613dafa2017-02-09 12:00:43 -08001667 GRPC_ERROR_NONE);
Craig Tiller48cb07c2015-07-15 16:16:15 -07001668}