blob: a62fcef98f280b06bac43f2d20146f9eeb953270 [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. Roth718c8342018-02-28 13:00:04 -080024#include <limits.h>
Mark D. Roth4c0fe492016-08-31 13:51:55 -070025#include <stdbool.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080026#include <stdio.h>
Craig Tillereb3b12e2015-06-26 14:42:49 -070027#include <string.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080028
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080029#include <grpc/support/alloc.h>
30#include <grpc/support/log.h>
Mark D. Rothb2d24882016-10-27 15:44:07 -070031#include <grpc/support/string_util.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080032#include <grpc/support/sync.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080033
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"
Mark D. Roth3e7f2df2018-02-26 13:17:06 -080037#include "src/core/ext/filters/client_channel/method_params.h"
Craig Tiller9eb0fde2017-03-31 16:59:30 -070038#include "src/core/ext/filters/client_channel/proxy_mapper_registry.h"
39#include "src/core/ext/filters/client_channel/resolver_registry.h"
40#include "src/core/ext/filters/client_channel/retry_throttle.h"
41#include "src/core/ext/filters/client_channel/subchannel.h"
Craig Tiller3be7dd02017-04-03 14:30:03 -070042#include "src/core/ext/filters/deadline/deadline_filter.h"
Mark D. Roth718c8342018-02-28 13:00:04 -080043#include "src/core/lib/backoff/backoff.h"
Craig Tiller9533d042016-03-25 17:11:06 -070044#include "src/core/lib/channel/channel_args.h"
45#include "src/core/lib/channel/connected_channel.h"
ncteisen3b42f832018-03-19 13:22:35 -070046#include "src/core/lib/channel/status_util.h"
Mark D. Rothdbdf4952018-01-18 11:21:12 -080047#include "src/core/lib/gpr/string.h"
Mark D. Roth718c8342018-02-28 13:00:04 -080048#include "src/core/lib/gprpp/inlined_vector.h"
49#include "src/core/lib/gprpp/manual_constructor.h"
Craig Tillerbefafe62017-02-09 11:30:54 -080050#include "src/core/lib/iomgr/combiner.h"
Craig Tiller9533d042016-03-25 17:11:06 -070051#include "src/core/lib/iomgr/iomgr.h"
Mark D. Roth4c0fe492016-08-31 13:51:55 -070052#include "src/core/lib/iomgr/polling_entity.h"
Craig Tiller9533d042016-03-25 17:11:06 -070053#include "src/core/lib/profiling/timers.h"
Craig Tiller7c70b6c2017-01-23 07:48:42 -080054#include "src/core/lib/slice/slice_internal.h"
Mark D. Roth718c8342018-02-28 13:00:04 -080055#include "src/core/lib/slice/slice_string_helpers.h"
Craig Tiller9533d042016-03-25 17:11:06 -070056#include "src/core/lib/surface/channel.h"
57#include "src/core/lib/transport/connectivity_state.h"
Mark D. Roth718c8342018-02-28 13:00:04 -080058#include "src/core/lib/transport/error_utils.h"
Mark D. Roth9fe284e2016-09-12 11:22:27 -070059#include "src/core/lib/transport/metadata.h"
60#include "src/core/lib/transport/metadata_batch.h"
Mark D. Rothea846a02016-11-03 11:32:54 -070061#include "src/core/lib/transport/service_config.h"
Mark D. Roth9fe284e2016-09-12 11:22:27 -070062#include "src/core/lib/transport/static_metadata.h"
Mark D. Roth718c8342018-02-28 13:00:04 -080063#include "src/core/lib/transport/status_metadata.h"
Craig Tiller8910ac62015-10-08 16:49:15 -070064
Mark D. Roth3e7f2df2018-02-26 13:17:06 -080065using grpc_core::internal::ClientChannelMethodParams;
Mark D. Roth9db86fc2018-03-28 07:42:20 -070066using grpc_core::internal::ServerRetryThrottleData;
Mark D. Roth3e7f2df2018-02-26 13:17:06 -080067
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080068/* Client channel implementation */
69
Mark D. Roth718c8342018-02-28 13:00:04 -080070// By default, we buffer 256 KiB per RPC for retries.
71// TODO(roth): Do we have any data to suggest a better value?
72#define DEFAULT_PER_RPC_RETRY_BUFFER_SIZE (256 << 10)
73
74// This value was picked arbitrarily. It can be changed if there is
75// any even moderately compelling reason to do so.
76#define RETRY_BACKOFF_JITTER 0.2
77
Craig Tiller694580f2017-10-18 14:48:14 -070078grpc_core::TraceFlag grpc_client_channel_trace(false, "client_channel");
Mark D. Roth60751fe2017-07-07 12:50:33 -070079
Mark D. Roth26b7be42016-10-24 10:08:07 -070080/*************************************************************************
Mark D. Roth3e7f2df2018-02-26 13:17:06 -080081 * CHANNEL-WIDE FUNCTIONS
Mark D. Roth26b7be42016-10-24 10:08:07 -070082 */
83
Alexander Polcync3b1f182017-04-18 13:51:36 -070084struct external_connectivity_watcher;
85
Mark D. Roth3e7f2df2018-02-26 13:17:06 -080086typedef grpc_core::SliceHashTable<
87 grpc_core::RefCountedPtr<ClientChannelMethodParams>>
88 MethodParamsTable;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080089
Craig Tiller800dacb2015-10-06 09:10:26 -070090typedef struct client_channel_channel_data {
Mark D. Roth209f6442018-02-08 10:26:46 -080091 grpc_core::OrphanablePtr<grpc_core::Resolver> resolver;
Mark D. Roth4c0fe492016-08-31 13:51:55 -070092 bool started_resolving;
Craig Tiller3be7dd02017-04-03 14:30:03 -070093 bool deadline_checking_enabled;
Craig Tillerbaa14a92017-11-03 09:09:36 -070094 grpc_client_channel_factory* client_channel_factory;
Mark D. Roth718c8342018-02-28 13:00:04 -080095 bool enable_retries;
96 size_t per_rpc_retry_buffer_size;
Craig Tillerf5f17122015-06-25 08:47:26 -070097
Craig Tillerbefafe62017-02-09 11:30:54 -080098 /** combiner protecting all variables below in this data structure */
Craig Tillerbaa14a92017-11-03 09:09:36 -070099 grpc_combiner* combiner;
Mark D. Roth046cf762016-09-26 11:13:51 -0700100 /** currently active load balancer */
Mark D. Rothc8875492018-02-20 08:33:48 -0800101 grpc_core::OrphanablePtr<grpc_core::LoadBalancingPolicy> lb_policy;
Mark D. Rothd6d192d2017-02-23 08:58:42 -0800102 /** retry throttle data */
Mark D. Roth9db86fc2018-03-28 07:42:20 -0700103 grpc_core::RefCountedPtr<ServerRetryThrottleData> retry_throttle_data;
Mark D. Roth9d480942016-10-19 14:18:05 -0700104 /** maps method names to method_parameters structs */
Mark D. Roth3e7f2df2018-02-26 13:17:06 -0800105 grpc_core::RefCountedPtr<MethodParamsTable> method_params_table;
Mark D. Roth046cf762016-09-26 11:13:51 -0700106 /** incoming resolver result - set by resolver.next() */
Craig Tillerbaa14a92017-11-03 09:09:36 -0700107 grpc_channel_args* resolver_result;
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700108 /** a list of closures that are all waiting for resolver result to come in */
109 grpc_closure_list waiting_for_resolver_result_closures;
Craig Tiller3f475422015-06-25 10:43:05 -0700110 /** resolver callback */
Mark D. Rothff4df062016-08-22 15:02:49 -0700111 grpc_closure on_resolver_result_changed;
Craig Tiller3f475422015-06-25 10:43:05 -0700112 /** connectivity state being tracked */
Craig Tillerca3e9d32015-06-27 18:37:27 -0700113 grpc_connectivity_state_tracker state_tracker;
Craig Tiller48cb07c2015-07-15 16:16:15 -0700114 /** when an lb_policy arrives, should we try to exit idle */
Mark D. Roth4c0fe492016-08-31 13:51:55 -0700115 bool exit_idle_when_lb_policy_arrives;
Craig Tiller906e3bc2015-11-24 07:31:31 -0800116 /** owning stack */
Craig Tillerbaa14a92017-11-03 09:09:36 -0700117 grpc_channel_stack* owning_stack;
Craig Tiller69b093b2016-02-25 19:04:07 -0800118 /** interested parties (owned) */
Craig Tillerbaa14a92017-11-03 09:09:36 -0700119 grpc_pollset_set* interested_parties;
Craig Tiller613dafa2017-02-09 12:00:43 -0800120
Alexander Polcync3b1f182017-04-18 13:51:36 -0700121 /* external_connectivity_watcher_list head is guarded by its own mutex, since
122 * counts need to be grabbed immediately without polling on a cq */
123 gpr_mu external_connectivity_watcher_list_mu;
Craig Tillerbaa14a92017-11-03 09:09:36 -0700124 struct external_connectivity_watcher* external_connectivity_watcher_list_head;
Alexander Polcync3b1f182017-04-18 13:51:36 -0700125
Mark D. Roth718c8342018-02-28 13:00:04 -0800126 /* the following properties are guarded by a mutex since APIs require them
Craig Tiller46dd7902017-02-23 09:42:16 -0800127 to be instantaneously available */
Craig Tiller613dafa2017-02-09 12:00:43 -0800128 gpr_mu info_mu;
Craig Tillerbaa14a92017-11-03 09:09:36 -0700129 char* info_lb_policy_name;
Craig Tiller613dafa2017-02-09 12:00:43 -0800130 /** service config in JSON form */
Craig Tillerbaa14a92017-11-03 09:09:36 -0700131 char* info_service_config_json;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800132} channel_data;
133
Juanli Shen592cf342017-12-04 20:52:01 -0800134typedef struct {
135 channel_data* chand;
136 /** used as an identifier, don't dereference it because the LB policy may be
137 * non-existing when the callback is run */
Mark D. Rothc8875492018-02-20 08:33:48 -0800138 grpc_core::LoadBalancingPolicy* lb_policy;
Juanli Shen592cf342017-12-04 20:52:01 -0800139 grpc_closure closure;
140} reresolution_request_args;
141
Craig Tillerd6c98df2015-08-18 09:33:44 -0700142/** We create one watcher for each new lb_policy that is returned from a
Mark D. Roth4c0fe492016-08-31 13:51:55 -0700143 resolver, to watch for state changes from the lb_policy. When a state
144 change is seen, we update the channel, and create a new watcher. */
Craig Tillera82950e2015-09-22 12:33:20 -0700145typedef struct {
Craig Tillerbaa14a92017-11-03 09:09:36 -0700146 channel_data* chand;
Craig Tiller33825112015-09-18 07:44:19 -0700147 grpc_closure on_changed;
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700148 grpc_connectivity_state state;
Mark D. Rothc8875492018-02-20 08:33:48 -0800149 grpc_core::LoadBalancingPolicy* lb_policy;
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700150} lb_policy_connectivity_watcher;
151
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800152static void watch_lb_policy_locked(channel_data* chand,
Mark D. Rothc8875492018-02-20 08:33:48 -0800153 grpc_core::LoadBalancingPolicy* lb_policy,
Craig Tiller2400bf52017-02-09 16:25:19 -0800154 grpc_connectivity_state current_state);
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700155
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800156static void set_channel_connectivity_state_locked(channel_data* chand,
Craig Tiller8c0d96f2016-03-11 14:27:52 -0800157 grpc_connectivity_state state,
Craig Tillerbaa14a92017-11-03 09:09:36 -0700158 grpc_error* error,
159 const char* reason) {
David Garcia Quintas37251282017-04-14 13:46:03 -0700160 /* TODO: Improve failure handling:
161 * - Make it possible for policies to return GRPC_CHANNEL_TRANSIENT_FAILURE.
162 * - Hand over pending picks from old policies during the switch that happens
163 * when resolver provides an update. */
Craig Tiller4782d922017-11-10 09:53:21 -0800164 if (chand->lb_policy != nullptr) {
David Garcia Quintas956f7002017-04-13 15:40:06 -0700165 if (state == GRPC_CHANNEL_TRANSIENT_FAILURE) {
166 /* cancel picks with wait_for_ready=false */
Mark D. Rothc8875492018-02-20 08:33:48 -0800167 chand->lb_policy->CancelMatchingPicksLocked(
David Garcia Quintas956f7002017-04-13 15:40:06 -0700168 /* mask= */ GRPC_INITIAL_METADATA_WAIT_FOR_READY,
169 /* check= */ 0, GRPC_ERROR_REF(error));
170 } else if (state == GRPC_CHANNEL_SHUTDOWN) {
171 /* cancel all picks */
Mark D. Rothc8875492018-02-20 08:33:48 -0800172 chand->lb_policy->CancelMatchingPicksLocked(/* mask= */ 0, /* check= */ 0,
173 GRPC_ERROR_REF(error));
David Garcia Quintas956f7002017-04-13 15:40:06 -0700174 }
Craig Tiller8c0d96f2016-03-11 14:27:52 -0800175 }
Craig Tiller6014e8a2017-10-16 13:50:29 -0700176 if (grpc_client_channel_trace.enabled()) {
Mark D. Roth48854d22018-04-25 13:05:26 -0700177 gpr_log(GPR_INFO, "chand=%p: setting connectivity state to %s", chand,
Mark D. Roth60751fe2017-07-07 12:50:33 -0700178 grpc_connectivity_state_name(state));
179 }
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800180 grpc_connectivity_state_set(&chand->state_tracker, state, error, reason);
Craig Tiller8c0d96f2016-03-11 14:27:52 -0800181}
182
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800183static void on_lb_policy_state_changed_locked(void* arg, grpc_error* error) {
Noah Eisen4d20a662018-02-09 09:34:04 -0800184 lb_policy_connectivity_watcher* w =
185 static_cast<lb_policy_connectivity_watcher*>(arg);
Craig Tillerc5de8352017-02-09 14:08:05 -0800186 /* check if the notification is for the latest policy */
Mark D. Rothc8875492018-02-20 08:33:48 -0800187 if (w->lb_policy == w->chand->lb_policy.get()) {
Craig Tiller6014e8a2017-10-16 13:50:29 -0700188 if (grpc_client_channel_trace.enabled()) {
Mark D. Roth48854d22018-04-25 13:05:26 -0700189 gpr_log(GPR_INFO, "chand=%p: lb_policy=%p state changed to %s", w->chand,
Mark D. Roth60751fe2017-07-07 12:50:33 -0700190 w->lb_policy, grpc_connectivity_state_name(w->state));
191 }
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800192 set_channel_connectivity_state_locked(w->chand, w->state,
Craig Tillerc5de8352017-02-09 14:08:05 -0800193 GRPC_ERROR_REF(error), "lb_changed");
194 if (w->state != GRPC_CHANNEL_SHUTDOWN) {
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800195 watch_lb_policy_locked(w->chand, w->lb_policy, w->state);
Craig Tillerc5de8352017-02-09 14:08:05 -0800196 }
Craig Tillera82950e2015-09-22 12:33:20 -0700197 }
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800198 GRPC_CHANNEL_STACK_UNREF(w->chand->owning_stack, "watch_lb_policy");
Craig Tillera82950e2015-09-22 12:33:20 -0700199 gpr_free(w);
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700200}
201
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800202static void watch_lb_policy_locked(channel_data* chand,
Mark D. Rothc8875492018-02-20 08:33:48 -0800203 grpc_core::LoadBalancingPolicy* lb_policy,
Craig Tiller2400bf52017-02-09 16:25:19 -0800204 grpc_connectivity_state current_state) {
Craig Tillerbaa14a92017-11-03 09:09:36 -0700205 lb_policy_connectivity_watcher* w =
Noah Eisenbe82e642018-02-09 09:16:55 -0800206 static_cast<lb_policy_connectivity_watcher*>(gpr_malloc(sizeof(*w)));
Craig Tiller906e3bc2015-11-24 07:31:31 -0800207 GRPC_CHANNEL_STACK_REF(chand->owning_stack, "watch_lb_policy");
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700208 w->chand = chand;
ncteisen274bbbe2017-06-08 14:57:11 -0700209 GRPC_CLOSURE_INIT(&w->on_changed, on_lb_policy_state_changed_locked, w,
Craig Tilleree4b1452017-05-12 10:56:03 -0700210 grpc_combiner_scheduler(chand->combiner));
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700211 w->state = current_state;
212 w->lb_policy = lb_policy;
Mark D. Rothc8875492018-02-20 08:33:48 -0800213 lb_policy->NotifyOnStateChangeLocked(&w->state, &w->on_changed);
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700214}
215
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800216static void start_resolving_locked(channel_data* chand) {
Craig Tiller6014e8a2017-10-16 13:50:29 -0700217 if (grpc_client_channel_trace.enabled()) {
Mark D. Roth48854d22018-04-25 13:05:26 -0700218 gpr_log(GPR_INFO, "chand=%p: starting name resolution", chand);
Mark D. Roth60751fe2017-07-07 12:50:33 -0700219 }
220 GPR_ASSERT(!chand->started_resolving);
221 chand->started_resolving = true;
222 GRPC_CHANNEL_STACK_REF(chand->owning_stack, "resolver");
Mark D. Roth209f6442018-02-08 10:26:46 -0800223 chand->resolver->NextLocked(&chand->resolver_result,
224 &chand->on_resolver_result_changed);
Mark D. Roth60751fe2017-07-07 12:50:33 -0700225}
226
Mark D. Rothd6d192d2017-02-23 08:58:42 -0800227typedef struct {
Craig Tillerbaa14a92017-11-03 09:09:36 -0700228 char* server_name;
Mark D. Roth9db86fc2018-03-28 07:42:20 -0700229 grpc_core::RefCountedPtr<ServerRetryThrottleData> retry_throttle_data;
Mark D. Rothd6d192d2017-02-23 08:58:42 -0800230} service_config_parsing_state;
231
Mark D. Roth3e7f2df2018-02-26 13:17:06 -0800232static void parse_retry_throttle_params(
233 const grpc_json* field, service_config_parsing_state* parsing_state) {
Mark D. Rothd6d192d2017-02-23 08:58:42 -0800234 if (strcmp(field->key, "retryThrottling") == 0) {
Craig Tiller4782d922017-11-10 09:53:21 -0800235 if (parsing_state->retry_throttle_data != nullptr) return; // Duplicate.
Mark D. Rothd6d192d2017-02-23 08:58:42 -0800236 if (field->type != GRPC_JSON_OBJECT) return;
237 int max_milli_tokens = 0;
238 int milli_token_ratio = 0;
Craig Tiller4782d922017-11-10 09:53:21 -0800239 for (grpc_json* sub_field = field->child; sub_field != nullptr;
Mark D. Rothd6d192d2017-02-23 08:58:42 -0800240 sub_field = sub_field->next) {
Craig Tiller4782d922017-11-10 09:53:21 -0800241 if (sub_field->key == nullptr) return;
Mark D. Rothd6d192d2017-02-23 08:58:42 -0800242 if (strcmp(sub_field->key, "maxTokens") == 0) {
243 if (max_milli_tokens != 0) return; // Duplicate.
244 if (sub_field->type != GRPC_JSON_NUMBER) return;
245 max_milli_tokens = gpr_parse_nonnegative_int(sub_field->value);
246 if (max_milli_tokens == -1) return;
247 max_milli_tokens *= 1000;
248 } else if (strcmp(sub_field->key, "tokenRatio") == 0) {
249 if (milli_token_ratio != 0) return; // Duplicate.
250 if (sub_field->type != GRPC_JSON_NUMBER) return;
251 // We support up to 3 decimal digits.
252 size_t whole_len = strlen(sub_field->value);
253 uint32_t multiplier = 1;
254 uint32_t decimal_value = 0;
Craig Tillerbaa14a92017-11-03 09:09:36 -0700255 const char* decimal_point = strchr(sub_field->value, '.');
Craig Tiller4782d922017-11-10 09:53:21 -0800256 if (decimal_point != nullptr) {
Noah Eisenbe82e642018-02-09 09:16:55 -0800257 whole_len = static_cast<size_t>(decimal_point - sub_field->value);
Mark D. Rothd6d192d2017-02-23 08:58:42 -0800258 multiplier = 1000;
259 size_t decimal_len = strlen(decimal_point + 1);
260 if (decimal_len > 3) decimal_len = 3;
261 if (!gpr_parse_bytes_to_uint32(decimal_point + 1, decimal_len,
262 &decimal_value)) {
263 return;
264 }
265 uint32_t decimal_multiplier = 1;
266 for (size_t i = 0; i < (3 - decimal_len); ++i) {
267 decimal_multiplier *= 10;
268 }
269 decimal_value *= decimal_multiplier;
270 }
271 uint32_t whole_value;
272 if (!gpr_parse_bytes_to_uint32(sub_field->value, whole_len,
273 &whole_value)) {
274 return;
275 }
Noah Eisen4d20a662018-02-09 09:34:04 -0800276 milli_token_ratio =
277 static_cast<int>((whole_value * multiplier) + decimal_value);
Mark D. Rothb3322562017-02-23 14:38:02 -0800278 if (milli_token_ratio <= 0) return;
Mark D. Rothd6d192d2017-02-23 08:58:42 -0800279 }
280 }
281 parsing_state->retry_throttle_data =
Mark D. Roth9db86fc2018-03-28 07:42:20 -0700282 grpc_core::internal::ServerRetryThrottleMap::GetDataForServer(
Mark D. Rothd6d192d2017-02-23 08:58:42 -0800283 parsing_state->server_name, max_milli_tokens, milli_token_ratio);
284 }
285}
286
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800287static void request_reresolution_locked(void* arg, grpc_error* error) {
Noah Eisen4d20a662018-02-09 09:34:04 -0800288 reresolution_request_args* args =
289 static_cast<reresolution_request_args*>(arg);
Juanli Shen592cf342017-12-04 20:52:01 -0800290 channel_data* chand = args->chand;
291 // If this invocation is for a stale LB policy, treat it as an LB shutdown
292 // signal.
Mark D. Rothc8875492018-02-20 08:33:48 -0800293 if (args->lb_policy != chand->lb_policy.get() || error != GRPC_ERROR_NONE ||
Juanli Shen592cf342017-12-04 20:52:01 -0800294 chand->resolver == nullptr) {
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800295 GRPC_CHANNEL_STACK_UNREF(chand->owning_stack, "re-resolution");
Juanli Shen592cf342017-12-04 20:52:01 -0800296 gpr_free(args);
297 return;
298 }
299 if (grpc_client_channel_trace.enabled()) {
Mark D. Roth48854d22018-04-25 13:05:26 -0700300 gpr_log(GPR_INFO, "chand=%p: started name re-resolving", chand);
Juanli Shen592cf342017-12-04 20:52:01 -0800301 }
Mark D. Roth209f6442018-02-08 10:26:46 -0800302 chand->resolver->RequestReresolutionLocked();
Juanli Shen592cf342017-12-04 20:52:01 -0800303 // Give back the closure to the LB policy.
Mark D. Rothc8875492018-02-20 08:33:48 -0800304 chand->lb_policy->SetReresolutionClosureLocked(&args->closure);
Juanli Shen592cf342017-12-04 20:52:01 -0800305}
306
Mark D. Rothe63e06d2018-03-23 08:12:11 -0700307// TODO(roth): The logic in this function is very hard to follow. We
308// should refactor this so that it's easier to understand, perhaps as
309// part of changing the resolver API to more clearly differentiate
310// between transient failures and shutdown.
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800311static void on_resolver_result_changed_locked(void* arg, grpc_error* error) {
Noah Eisenbe82e642018-02-09 09:16:55 -0800312 channel_data* chand = static_cast<channel_data*>(arg);
Craig Tiller6014e8a2017-10-16 13:50:29 -0700313 if (grpc_client_channel_trace.enabled()) {
Mark D. Roth48854d22018-04-25 13:05:26 -0700314 gpr_log(GPR_INFO,
Mark D. Rothe63e06d2018-03-23 08:12:11 -0700315 "chand=%p: got resolver result: resolver_result=%p error=%s", chand,
316 chand->resolver_result, grpc_error_string(error));
Mark D. Roth60751fe2017-07-07 12:50:33 -0700317 }
Mark D. Roth718c8342018-02-28 13:00:04 -0800318 // Extract the following fields from the resolver result, if non-nullptr.
Mark D. Roth15494b52017-07-12 15:26:55 -0700319 bool lb_policy_updated = false;
Mark D. Rothc8875492018-02-20 08:33:48 -0800320 bool lb_policy_created = false;
Craig Tiller4782d922017-11-10 09:53:21 -0800321 char* lb_policy_name_dup = nullptr;
Mark D. Roth60751fe2017-07-07 12:50:33 -0700322 bool lb_policy_name_changed = false;
Mark D. Rothc8875492018-02-20 08:33:48 -0800323 grpc_core::OrphanablePtr<grpc_core::LoadBalancingPolicy> new_lb_policy;
Craig Tiller4782d922017-11-10 09:53:21 -0800324 char* service_config_json = nullptr;
Mark D. Roth9db86fc2018-03-28 07:42:20 -0700325 grpc_core::RefCountedPtr<ServerRetryThrottleData> retry_throttle_data;
Mark D. Roth3e7f2df2018-02-26 13:17:06 -0800326 grpc_core::RefCountedPtr<MethodParamsTable> method_params_table;
Craig Tiller4782d922017-11-10 09:53:21 -0800327 if (chand->resolver_result != nullptr) {
Juanli Shen592cf342017-12-04 20:52:01 -0800328 if (chand->resolver != nullptr) {
329 // Find LB policy name.
Juanli Shen592cf342017-12-04 20:52:01 -0800330 const grpc_arg* channel_arg = grpc_channel_args_find(
331 chand->resolver_result, GRPC_ARG_LB_POLICY_NAME);
ncteisenbf323a92018-02-14 17:34:05 -0800332 const char* lb_policy_name = grpc_channel_arg_get_string(channel_arg);
Juanli Shen592cf342017-12-04 20:52:01 -0800333 // Special case: If at least one balancer address is present, we use
334 // the grpclb policy, regardless of what the resolver actually specified.
335 channel_arg =
336 grpc_channel_args_find(chand->resolver_result, GRPC_ARG_LB_ADDRESSES);
337 if (channel_arg != nullptr && channel_arg->type == GRPC_ARG_POINTER) {
338 grpc_lb_addresses* addresses =
Noah Eisenbe82e642018-02-09 09:16:55 -0800339 static_cast<grpc_lb_addresses*>(channel_arg->value.pointer.p);
Juanli Shen592cf342017-12-04 20:52:01 -0800340 bool found_balancer_address = false;
341 for (size_t i = 0; i < addresses->num_addresses; ++i) {
342 if (addresses->addresses[i].is_balancer) {
343 found_balancer_address = true;
344 break;
345 }
346 }
347 if (found_balancer_address) {
348 if (lb_policy_name != nullptr &&
349 strcmp(lb_policy_name, "grpclb") != 0) {
350 gpr_log(GPR_INFO,
351 "resolver requested LB policy %s but provided at least one "
352 "balancer address -- forcing use of grpclb LB policy",
353 lb_policy_name);
354 }
355 lb_policy_name = "grpclb";
356 }
357 }
358 // Use pick_first if nothing was specified and we didn't select grpclb
359 // above.
360 if (lb_policy_name == nullptr) lb_policy_name = "pick_first";
Juanli Shen592cf342017-12-04 20:52:01 -0800361 // Check to see if we're already using the right LB policy.
362 // Note: It's safe to use chand->info_lb_policy_name here without
363 // taking a lock on chand->info_mu, because this function is the
364 // only thing that modifies its value, and it can only be invoked
365 // once at any given time.
366 lb_policy_name_changed =
367 chand->info_lb_policy_name == nullptr ||
368 gpr_stricmp(chand->info_lb_policy_name, lb_policy_name) != 0;
369 if (chand->lb_policy != nullptr && !lb_policy_name_changed) {
370 // Continue using the same LB policy. Update with new addresses.
371 lb_policy_updated = true;
Mark D. Rothc8875492018-02-20 08:33:48 -0800372 chand->lb_policy->UpdateLocked(*chand->resolver_result);
Juanli Shen592cf342017-12-04 20:52:01 -0800373 } else {
374 // Instantiate new LB policy.
Mark D. Rothc8875492018-02-20 08:33:48 -0800375 grpc_core::LoadBalancingPolicy::Args lb_policy_args;
376 lb_policy_args.combiner = chand->combiner;
377 lb_policy_args.client_channel_factory = chand->client_channel_factory;
378 lb_policy_args.args = chand->resolver_result;
379 new_lb_policy =
380 grpc_core::LoadBalancingPolicyRegistry::CreateLoadBalancingPolicy(
381 lb_policy_name, lb_policy_args);
Yash Tibrewal7f51ba82018-04-12 13:21:20 -0700382 if (GPR_UNLIKELY(new_lb_policy == nullptr)) {
Juanli Shen592cf342017-12-04 20:52:01 -0800383 gpr_log(GPR_ERROR, "could not create LB policy \"%s\"",
384 lb_policy_name);
385 } else {
Mark D. Roth3ef4af22018-02-21 07:53:26 -0800386 lb_policy_created = true;
Juanli Shen592cf342017-12-04 20:52:01 -0800387 reresolution_request_args* args =
Noah Eisen4d20a662018-02-09 09:34:04 -0800388 static_cast<reresolution_request_args*>(
389 gpr_zalloc(sizeof(*args)));
Juanli Shen592cf342017-12-04 20:52:01 -0800390 args->chand = chand;
Mark D. Rothc8875492018-02-20 08:33:48 -0800391 args->lb_policy = new_lb_policy.get();
Juanli Shen592cf342017-12-04 20:52:01 -0800392 GRPC_CLOSURE_INIT(&args->closure, request_reresolution_locked, args,
393 grpc_combiner_scheduler(chand->combiner));
394 GRPC_CHANNEL_STACK_REF(chand->owning_stack, "re-resolution");
Mark D. Rothc8875492018-02-20 08:33:48 -0800395 new_lb_policy->SetReresolutionClosureLocked(&args->closure);
Juanli Shen592cf342017-12-04 20:52:01 -0800396 }
397 }
Mark D. Roth718c8342018-02-28 13:00:04 -0800398 // Before we clean up, save a copy of lb_policy_name, since it might
399 // be pointing to data inside chand->resolver_result.
400 // The copy will be saved in chand->lb_policy_name below.
401 lb_policy_name_dup = gpr_strdup(lb_policy_name);
Juanli Shen592cf342017-12-04 20:52:01 -0800402 // Find service config.
403 channel_arg = grpc_channel_args_find(chand->resolver_result,
404 GRPC_ARG_SERVICE_CONFIG);
ncteisenbf323a92018-02-14 17:34:05 -0800405 service_config_json =
406 gpr_strdup(grpc_channel_arg_get_string(channel_arg));
407 if (service_config_json != nullptr) {
Mark D. Roth3e7f2df2018-02-26 13:17:06 -0800408 grpc_core::UniquePtr<grpc_core::ServiceConfig> service_config =
409 grpc_core::ServiceConfig::Create(service_config_json);
Juanli Shen592cf342017-12-04 20:52:01 -0800410 if (service_config != nullptr) {
Mark D. Roth718c8342018-02-28 13:00:04 -0800411 if (chand->enable_retries) {
412 channel_arg = grpc_channel_args_find(chand->resolver_result,
413 GRPC_ARG_SERVER_URI);
414 const char* server_uri = grpc_channel_arg_get_string(channel_arg);
415 GPR_ASSERT(server_uri != nullptr);
416 grpc_uri* uri = grpc_uri_parse(server_uri, true);
417 GPR_ASSERT(uri->path[0] != '\0');
418 service_config_parsing_state parsing_state;
419 memset(&parsing_state, 0, sizeof(parsing_state));
420 parsing_state.server_name =
421 uri->path[0] == '/' ? uri->path + 1 : uri->path;
422 service_config->ParseGlobalParams(parse_retry_throttle_params,
423 &parsing_state);
424 grpc_uri_destroy(uri);
Mark D. Roth9db86fc2018-03-28 07:42:20 -0700425 retry_throttle_data = std::move(parsing_state.retry_throttle_data);
Mark D. Roth718c8342018-02-28 13:00:04 -0800426 }
Mark D. Roth3e7f2df2018-02-26 13:17:06 -0800427 method_params_table = service_config->CreateMethodConfigTable(
428 ClientChannelMethodParams::CreateFromJson);
Juanli Shen592cf342017-12-04 20:52:01 -0800429 }
430 }
Mark D. Roth9fe284e2016-09-12 11:22:27 -0700431 }
Craig Tillera82950e2015-09-22 12:33:20 -0700432 }
Craig Tiller6014e8a2017-10-16 13:50:29 -0700433 if (grpc_client_channel_trace.enabled()) {
Mark D. Roth48854d22018-04-25 13:05:26 -0700434 gpr_log(GPR_INFO,
Mark D. Roth60751fe2017-07-07 12:50:33 -0700435 "chand=%p: resolver result: lb_policy_name=\"%s\"%s, "
436 "service_config=\"%s\"",
Yash Tibrewal9eb86722017-09-17 23:43:30 -0700437 chand, lb_policy_name_dup,
438 lb_policy_name_changed ? " (changed)" : "", service_config_json);
Mark D. Roth60751fe2017-07-07 12:50:33 -0700439 }
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700440 // Now swap out fields in chand. Note that the new values may still
Mark D. Roth718c8342018-02-28 13:00:04 -0800441 // be nullptr if (e.g.) the resolver failed to return results or the
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700442 // results did not contain the necessary data.
443 //
444 // First, swap out the data used by cc_get_channel_info().
Craig Tiller613dafa2017-02-09 12:00:43 -0800445 gpr_mu_lock(&chand->info_mu);
Craig Tiller4782d922017-11-10 09:53:21 -0800446 if (lb_policy_name_dup != nullptr) {
Craig Tiller613dafa2017-02-09 12:00:43 -0800447 gpr_free(chand->info_lb_policy_name);
Yash Tibrewal9eb86722017-09-17 23:43:30 -0700448 chand->info_lb_policy_name = lb_policy_name_dup;
Mark D. Rothb2d24882016-10-27 15:44:07 -0700449 }
Craig Tiller4782d922017-11-10 09:53:21 -0800450 if (service_config_json != nullptr) {
Craig Tiller613dafa2017-02-09 12:00:43 -0800451 gpr_free(chand->info_service_config_json);
452 chand->info_service_config_json = service_config_json;
Mark D. Rothc625c7a2016-11-09 14:12:37 -0800453 }
Craig Tiller613dafa2017-02-09 12:00:43 -0800454 gpr_mu_unlock(&chand->info_mu);
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700455 // Swap out the retry throttle data.
Mark D. Roth9db86fc2018-03-28 07:42:20 -0700456 chand->retry_throttle_data = std::move(retry_throttle_data);
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700457 // Swap out the method params table.
Mark D. Roth3e7f2df2018-02-26 13:17:06 -0800458 chand->method_params_table = std::move(method_params_table);
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700459 // If we have a new LB policy or are shutting down (in which case
Mark D. Roth718c8342018-02-28 13:00:04 -0800460 // new_lb_policy will be nullptr), swap out the LB policy, unreffing the
461 // old one and removing its fds from chand->interested_parties.
462 // Note that we do NOT do this if either (a) we updated the existing
463 // LB policy above or (b) we failed to create the new LB policy (in
464 // which case we want to continue using the most recent one we had).
Craig Tiller4782d922017-11-10 09:53:21 -0800465 if (new_lb_policy != nullptr || error != GRPC_ERROR_NONE ||
466 chand->resolver == nullptr) {
467 if (chand->lb_policy != nullptr) {
Craig Tiller6014e8a2017-10-16 13:50:29 -0700468 if (grpc_client_channel_trace.enabled()) {
Mark D. Roth48854d22018-04-25 13:05:26 -0700469 gpr_log(GPR_INFO, "chand=%p: unreffing lb_policy=%p", chand,
Mark D. Rothc8875492018-02-20 08:33:48 -0800470 chand->lb_policy.get());
Mark D. Roth60751fe2017-07-07 12:50:33 -0700471 }
Mark D. Rothc8875492018-02-20 08:33:48 -0800472 grpc_pollset_set_del_pollset_set(chand->lb_policy->interested_parties(),
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700473 chand->interested_parties);
Mark D. Rothc8875492018-02-20 08:33:48 -0800474 chand->lb_policy->HandOffPendingPicksLocked(new_lb_policy.get());
475 chand->lb_policy.reset();
Craig Tiller45724b32015-09-22 10:42:19 -0700476 }
Mark D. Rothc8875492018-02-20 08:33:48 -0800477 chand->lb_policy = std::move(new_lb_policy);
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700478 }
479 // Now that we've swapped out the relevant fields of chand, check for
480 // error or shutdown.
Craig Tiller4782d922017-11-10 09:53:21 -0800481 if (error != GRPC_ERROR_NONE || chand->resolver == nullptr) {
Craig Tiller6014e8a2017-10-16 13:50:29 -0700482 if (grpc_client_channel_trace.enabled()) {
Mark D. Roth48854d22018-04-25 13:05:26 -0700483 gpr_log(GPR_INFO, "chand=%p: shutting down", chand);
Mark D. Roth60751fe2017-07-07 12:50:33 -0700484 }
Craig Tiller4782d922017-11-10 09:53:21 -0800485 if (chand->resolver != nullptr) {
Craig Tiller6014e8a2017-10-16 13:50:29 -0700486 if (grpc_client_channel_trace.enabled()) {
Mark D. Roth48854d22018-04-25 13:05:26 -0700487 gpr_log(GPR_INFO, "chand=%p: shutting down resolver", chand);
Mark D. Roth60751fe2017-07-07 12:50:33 -0700488 }
Mark D. Roth209f6442018-02-08 10:26:46 -0800489 chand->resolver.reset();
Craig Tiller76a5c0e2016-03-09 09:05:30 -0800490 }
Craig Tiller8c0d96f2016-03-11 14:27:52 -0800491 set_channel_connectivity_state_locked(
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800492 chand, GRPC_CHANNEL_SHUTDOWN,
ncteisen4b36a3d2017-03-13 19:08:06 -0700493 GRPC_ERROR_CREATE_REFERENCING_FROM_STATIC_STRING(
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700494 "Got resolver result after disconnection", &error, 1),
Craig Tiller804ff712016-05-05 16:25:40 -0700495 "resolver_gone");
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700496 grpc_closure_list_fail_all(&chand->waiting_for_resolver_result_closures,
497 GRPC_ERROR_CREATE_REFERENCING_FROM_STATIC_STRING(
498 "Channel disconnected", &error, 1));
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800499 GRPC_CLOSURE_LIST_SCHED(&chand->waiting_for_resolver_result_closures);
Mark D. Roth1b95f472018-02-15 12:54:02 -0800500 GRPC_CHANNEL_STACK_UNREF(chand->owning_stack, "resolver");
Mark D. Rothe63e06d2018-03-23 08:12:11 -0700501 grpc_channel_args_destroy(chand->resolver_result);
502 chand->resolver_result = nullptr;
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700503 } else { // Not shutting down.
504 grpc_connectivity_state state = GRPC_CHANNEL_TRANSIENT_FAILURE;
Craig Tillerbaa14a92017-11-03 09:09:36 -0700505 grpc_error* state_error =
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700506 GRPC_ERROR_CREATE_FROM_STATIC_STRING("No load balancing policy");
Mark D. Rothc8875492018-02-20 08:33:48 -0800507 if (lb_policy_created) {
Craig Tiller6014e8a2017-10-16 13:50:29 -0700508 if (grpc_client_channel_trace.enabled()) {
Mark D. Roth48854d22018-04-25 13:05:26 -0700509 gpr_log(GPR_INFO, "chand=%p: initializing new LB policy", chand);
Mark D. Roth60751fe2017-07-07 12:50:33 -0700510 }
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700511 GRPC_ERROR_UNREF(state_error);
Mark D. Rothc8875492018-02-20 08:33:48 -0800512 state = chand->lb_policy->CheckConnectivityLocked(&state_error);
513 grpc_pollset_set_add_pollset_set(chand->lb_policy->interested_parties(),
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700514 chand->interested_parties);
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800515 GRPC_CLOSURE_LIST_SCHED(&chand->waiting_for_resolver_result_closures);
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700516 if (chand->exit_idle_when_lb_policy_arrives) {
Mark D. Rothc8875492018-02-20 08:33:48 -0800517 chand->lb_policy->ExitIdleLocked();
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700518 chand->exit_idle_when_lb_policy_arrives = false;
519 }
Mark D. Rothc8875492018-02-20 08:33:48 -0800520 watch_lb_policy_locked(chand, chand->lb_policy.get(), state);
Mark D. Rothe63e06d2018-03-23 08:12:11 -0700521 } else if (chand->resolver_result == nullptr) {
522 // Transient failure.
523 GRPC_CLOSURE_LIST_SCHED(&chand->waiting_for_resolver_result_closures);
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700524 }
Mark D. Roth15494b52017-07-12 15:26:55 -0700525 if (!lb_policy_updated) {
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800526 set_channel_connectivity_state_locked(
527 chand, state, GRPC_ERROR_REF(state_error), "new_lb+resolver");
Mark D. Roth15494b52017-07-12 15:26:55 -0700528 }
Mark D. Rothe63e06d2018-03-23 08:12:11 -0700529 grpc_channel_args_destroy(chand->resolver_result);
530 chand->resolver_result = nullptr;
Mark D. Roth209f6442018-02-08 10:26:46 -0800531 chand->resolver->NextLocked(&chand->resolver_result,
532 &chand->on_resolver_result_changed);
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700533 GRPC_ERROR_UNREF(state_error);
Craig Tillera82950e2015-09-22 12:33:20 -0700534 }
Craig Tiller3f475422015-06-25 10:43:05 -0700535}
536
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800537static void start_transport_op_locked(void* arg, grpc_error* error_ignored) {
Noah Eisenbe82e642018-02-09 09:16:55 -0800538 grpc_transport_op* op = static_cast<grpc_transport_op*>(arg);
Craig Tillerbaa14a92017-11-03 09:09:36 -0700539 grpc_channel_element* elem =
Noah Eisenbe82e642018-02-09 09:16:55 -0800540 static_cast<grpc_channel_element*>(op->handler_private.extra_arg);
541 channel_data* chand = static_cast<channel_data*>(elem->channel_data);
Craig Tiller000cd8f2015-09-18 07:20:29 -0700542
Craig Tiller4782d922017-11-10 09:53:21 -0800543 if (op->on_connectivity_state_change != nullptr) {
Craig Tillera82950e2015-09-22 12:33:20 -0700544 grpc_connectivity_state_notify_on_state_change(
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800545 &chand->state_tracker, op->connectivity_state,
Craig Tillera82950e2015-09-22 12:33:20 -0700546 op->on_connectivity_state_change);
Craig Tiller4782d922017-11-10 09:53:21 -0800547 op->on_connectivity_state_change = nullptr;
548 op->connectivity_state = nullptr;
Craig Tillera82950e2015-09-22 12:33:20 -0700549 }
550
Yuchen Zengc272dd72017-12-05 12:18:34 -0800551 if (op->send_ping.on_initiate != nullptr || op->send_ping.on_ack != nullptr) {
Craig Tiller4782d922017-11-10 09:53:21 -0800552 if (chand->lb_policy == nullptr) {
ncteisen274bbbe2017-06-08 14:57:11 -0700553 GRPC_CLOSURE_SCHED(
Yash Tibrewald6c292f2017-12-07 19:38:43 -0800554 op->send_ping.on_initiate,
Yuchen Zengc272dd72017-12-05 12:18:34 -0800555 GRPC_ERROR_CREATE_FROM_STATIC_STRING("Ping with no load balancing"));
556 GRPC_CLOSURE_SCHED(
Yash Tibrewald6c292f2017-12-07 19:38:43 -0800557 op->send_ping.on_ack,
ncteisen4b36a3d2017-03-13 19:08:06 -0700558 GRPC_ERROR_CREATE_FROM_STATIC_STRING("Ping with no load balancing"));
Craig Tiller26dab312015-12-07 14:43:47 -0800559 } else {
Mark D. Rothc8875492018-02-20 08:33:48 -0800560 chand->lb_policy->PingOneLocked(op->send_ping.on_initiate,
561 op->send_ping.on_ack);
Craig Tiller4782d922017-11-10 09:53:21 -0800562 op->bind_pollset = nullptr;
Craig Tiller26dab312015-12-07 14:43:47 -0800563 }
Yuchen Zengc272dd72017-12-05 12:18:34 -0800564 op->send_ping.on_initiate = nullptr;
565 op->send_ping.on_ack = nullptr;
Craig Tiller26dab312015-12-07 14:43:47 -0800566 }
567
Craig Tiller1c51edc2016-05-07 16:18:43 -0700568 if (op->disconnect_with_error != GRPC_ERROR_NONE) {
Craig Tiller4782d922017-11-10 09:53:21 -0800569 if (chand->resolver != nullptr) {
Craig Tiller1c51edc2016-05-07 16:18:43 -0700570 set_channel_connectivity_state_locked(
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800571 chand, GRPC_CHANNEL_SHUTDOWN,
Craig Tiller1c51edc2016-05-07 16:18:43 -0700572 GRPC_ERROR_REF(op->disconnect_with_error), "disconnect");
Mark D. Roth209f6442018-02-08 10:26:46 -0800573 chand->resolver.reset();
Craig Tiller1c51edc2016-05-07 16:18:43 -0700574 if (!chand->started_resolving) {
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700575 grpc_closure_list_fail_all(&chand->waiting_for_resolver_result_closures,
Craig Tiller1c51edc2016-05-07 16:18:43 -0700576 GRPC_ERROR_REF(op->disconnect_with_error));
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800577 GRPC_CLOSURE_LIST_SCHED(&chand->waiting_for_resolver_result_closures);
Craig Tiller1c51edc2016-05-07 16:18:43 -0700578 }
Craig Tiller4782d922017-11-10 09:53:21 -0800579 if (chand->lb_policy != nullptr) {
Mark D. Rothc8875492018-02-20 08:33:48 -0800580 grpc_pollset_set_del_pollset_set(chand->lb_policy->interested_parties(),
Craig Tiller1c51edc2016-05-07 16:18:43 -0700581 chand->interested_parties);
Mark D. Rothc8875492018-02-20 08:33:48 -0800582 chand->lb_policy.reset();
Craig Tiller1c51edc2016-05-07 16:18:43 -0700583 }
Craig Tillerb12d22a2016-04-23 12:50:21 -0700584 }
Craig Tiller1c51edc2016-05-07 16:18:43 -0700585 GRPC_ERROR_UNREF(op->disconnect_with_error);
Craig Tillera82950e2015-09-22 12:33:20 -0700586 }
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800587 GRPC_CHANNEL_STACK_UNREF(chand->owning_stack, "start_transport_op");
Craig Tillerd2e5cfc2017-02-09 13:02:20 -0800588
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800589 GRPC_CLOSURE_SCHED(op->on_consumed, GRPC_ERROR_NONE);
Craig Tillerbefafe62017-02-09 11:30:54 -0800590}
591
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800592static void cc_start_transport_op(grpc_channel_element* elem,
Craig Tillerbaa14a92017-11-03 09:09:36 -0700593 grpc_transport_op* op) {
Noah Eisenbe82e642018-02-09 09:16:55 -0800594 channel_data* chand = static_cast<channel_data*>(elem->channel_data);
Craig Tillerbefafe62017-02-09 11:30:54 -0800595
Craig Tillerbefafe62017-02-09 11:30:54 -0800596 GPR_ASSERT(op->set_accept_stream == false);
Craig Tiller4782d922017-11-10 09:53:21 -0800597 if (op->bind_pollset != nullptr) {
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800598 grpc_pollset_set_add_pollset(chand->interested_parties, op->bind_pollset);
Craig Tillerbefafe62017-02-09 11:30:54 -0800599 }
600
Craig Tillerc55c1022017-03-10 10:26:42 -0800601 op->handler_private.extra_arg = elem;
Craig Tillerd2e5cfc2017-02-09 13:02:20 -0800602 GRPC_CHANNEL_STACK_REF(chand->owning_stack, "start_transport_op");
ncteisen274bbbe2017-06-08 14:57:11 -0700603 GRPC_CLOSURE_SCHED(
ncteisen274bbbe2017-06-08 14:57:11 -0700604 GRPC_CLOSURE_INIT(&op->handler_private.closure, start_transport_op_locked,
Craig Tilleree4b1452017-05-12 10:56:03 -0700605 op, grpc_combiner_scheduler(chand->combiner)),
Craig Tillerbefafe62017-02-09 11:30:54 -0800606 GRPC_ERROR_NONE);
Craig Tillerca3e9d32015-06-27 18:37:27 -0700607}
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800608
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800609static void cc_get_channel_info(grpc_channel_element* elem,
Craig Tillerbaa14a92017-11-03 09:09:36 -0700610 const grpc_channel_info* info) {
Noah Eisenbe82e642018-02-09 09:16:55 -0800611 channel_data* chand = static_cast<channel_data*>(elem->channel_data);
Craig Tiller613dafa2017-02-09 12:00:43 -0800612 gpr_mu_lock(&chand->info_mu);
Craig Tiller4782d922017-11-10 09:53:21 -0800613 if (info->lb_policy_name != nullptr) {
614 *info->lb_policy_name = chand->info_lb_policy_name == nullptr
615 ? nullptr
Craig Tiller613dafa2017-02-09 12:00:43 -0800616 : gpr_strdup(chand->info_lb_policy_name);
Mark D. Rothb2d24882016-10-27 15:44:07 -0700617 }
Craig Tiller4782d922017-11-10 09:53:21 -0800618 if (info->service_config_json != nullptr) {
Craig Tiller613dafa2017-02-09 12:00:43 -0800619 *info->service_config_json =
Craig Tiller4782d922017-11-10 09:53:21 -0800620 chand->info_service_config_json == nullptr
621 ? nullptr
Craig Tiller613dafa2017-02-09 12:00:43 -0800622 : gpr_strdup(chand->info_service_config_json);
Mark D. Rothc625c7a2016-11-09 14:12:37 -0800623 }
Craig Tiller613dafa2017-02-09 12:00:43 -0800624 gpr_mu_unlock(&chand->info_mu);
Mark D. Rothb2d24882016-10-27 15:44:07 -0700625}
626
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700627/* Constructor for channel_data */
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800628static grpc_error* cc_init_channel_elem(grpc_channel_element* elem,
Craig Tillerbaa14a92017-11-03 09:09:36 -0700629 grpc_channel_element_args* args) {
Noah Eisenbe82e642018-02-09 09:16:55 -0800630 channel_data* chand = static_cast<channel_data*>(elem->channel_data);
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700631 GPR_ASSERT(args->is_last);
632 GPR_ASSERT(elem->filter == &grpc_client_channel_filter);
Mark D. Roth21d4b2d2016-11-18 09:53:41 -0800633 // Initialize data members.
Craig Tilleree4b1452017-05-12 10:56:03 -0700634 chand->combiner = grpc_combiner_create();
Craig Tillerd85477512017-02-09 12:02:39 -0800635 gpr_mu_init(&chand->info_mu);
Alexander Polcync3b1f182017-04-18 13:51:36 -0700636 gpr_mu_init(&chand->external_connectivity_watcher_list_mu);
637
638 gpr_mu_lock(&chand->external_connectivity_watcher_list_mu);
Craig Tiller4782d922017-11-10 09:53:21 -0800639 chand->external_connectivity_watcher_list_head = nullptr;
Alexander Polcync3b1f182017-04-18 13:51:36 -0700640 gpr_mu_unlock(&chand->external_connectivity_watcher_list_mu);
641
Mark D. Roth21d4b2d2016-11-18 09:53:41 -0800642 chand->owning_stack = args->channel_stack;
ncteisen274bbbe2017-06-08 14:57:11 -0700643 GRPC_CLOSURE_INIT(&chand->on_resolver_result_changed,
Craig Tillerbefafe62017-02-09 11:30:54 -0800644 on_resolver_result_changed_locked, chand,
Craig Tilleree4b1452017-05-12 10:56:03 -0700645 grpc_combiner_scheduler(chand->combiner));
Mark D. Roth21d4b2d2016-11-18 09:53:41 -0800646 chand->interested_parties = grpc_pollset_set_create();
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700647 grpc_connectivity_state_init(&chand->state_tracker, GRPC_CHANNEL_IDLE,
648 "client_channel");
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800649 grpc_client_channel_start_backup_polling(chand->interested_parties);
Mark D. Roth718c8342018-02-28 13:00:04 -0800650 // Record max per-RPC retry buffer size.
651 const grpc_arg* arg = grpc_channel_args_find(
652 args->channel_args, GRPC_ARG_PER_RPC_RETRY_BUFFER_SIZE);
653 chand->per_rpc_retry_buffer_size = (size_t)grpc_channel_arg_get_integer(
654 arg, {DEFAULT_PER_RPC_RETRY_BUFFER_SIZE, 0, INT_MAX});
655 // Record enable_retries.
656 arg = grpc_channel_args_find(args->channel_args, GRPC_ARG_ENABLE_RETRIES);
657 chand->enable_retries = grpc_channel_arg_get_bool(arg, true);
Mark D. Roth21d4b2d2016-11-18 09:53:41 -0800658 // Record client channel factory.
Mark D. Roth718c8342018-02-28 13:00:04 -0800659 arg = grpc_channel_args_find(args->channel_args,
660 GRPC_ARG_CLIENT_CHANNEL_FACTORY);
Craig Tiller4782d922017-11-10 09:53:21 -0800661 if (arg == nullptr) {
David Garcia Quintas228a5142017-03-30 19:43:00 -0700662 return GRPC_ERROR_CREATE_FROM_STATIC_STRING(
663 "Missing client channel factory in args for client channel filter");
664 }
665 if (arg->type != GRPC_ARG_POINTER) {
666 return GRPC_ERROR_CREATE_FROM_STATIC_STRING(
667 "client channel factory arg must be a pointer");
668 }
Yash Tibrewalbc130da2017-09-12 22:44:08 -0700669 grpc_client_channel_factory_ref(
Noah Eisenbe82e642018-02-09 09:16:55 -0800670 static_cast<grpc_client_channel_factory*>(arg->value.pointer.p));
Yash Tibrewalca3c1c02017-09-07 22:47:16 -0700671 chand->client_channel_factory =
Noah Eisenbe82e642018-02-09 09:16:55 -0800672 static_cast<grpc_client_channel_factory*>(arg->value.pointer.p);
Mark D. Rothdc9bee72017-02-07 12:29:14 -0800673 // Get server name to resolve, using proxy mapper if needed.
Mark D. Roth86e90592016-11-18 09:56:40 -0800674 arg = grpc_channel_args_find(args->channel_args, GRPC_ARG_SERVER_URI);
Craig Tiller4782d922017-11-10 09:53:21 -0800675 if (arg == nullptr) {
David Garcia Quintas228a5142017-03-30 19:43:00 -0700676 return GRPC_ERROR_CREATE_FROM_STATIC_STRING(
677 "Missing server uri in args for client channel filter");
678 }
679 if (arg->type != GRPC_ARG_STRING) {
680 return GRPC_ERROR_CREATE_FROM_STATIC_STRING(
681 "server uri arg must be a string");
682 }
Craig Tiller4782d922017-11-10 09:53:21 -0800683 char* proxy_name = nullptr;
684 grpc_channel_args* new_args = nullptr;
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800685 grpc_proxy_mappers_map_name(arg->value.string, args->channel_args,
Mark D. Rothdc9bee72017-02-07 12:29:14 -0800686 &proxy_name, &new_args);
687 // Instantiate resolver.
Mark D. Roth209f6442018-02-08 10:26:46 -0800688 chand->resolver = grpc_core::ResolverRegistry::CreateResolver(
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800689 proxy_name != nullptr ? proxy_name : arg->value.string,
Craig Tiller4782d922017-11-10 09:53:21 -0800690 new_args != nullptr ? new_args : args->channel_args,
Craig Tiller972470b2017-02-09 15:05:36 -0800691 chand->interested_parties, chand->combiner);
Craig Tiller4782d922017-11-10 09:53:21 -0800692 if (proxy_name != nullptr) gpr_free(proxy_name);
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800693 if (new_args != nullptr) grpc_channel_args_destroy(new_args);
Craig Tiller4782d922017-11-10 09:53:21 -0800694 if (chand->resolver == nullptr) {
ncteisen4b36a3d2017-03-13 19:08:06 -0700695 return GRPC_ERROR_CREATE_FROM_STATIC_STRING("resolver creation failed");
Mark D. Roth5e2566e2016-11-18 10:53:13 -0800696 }
Craig Tiller3be7dd02017-04-03 14:30:03 -0700697 chand->deadline_checking_enabled =
698 grpc_deadline_checking_enabled(args->channel_args);
Mark D. Roth5e2566e2016-11-18 10:53:13 -0800699 return GRPC_ERROR_NONE;
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700700}
701
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800702static void shutdown_resolver_locked(void* arg, grpc_error* error) {
Mark D. Roth209f6442018-02-08 10:26:46 -0800703 grpc_core::Resolver* resolver = static_cast<grpc_core::Resolver*>(arg);
704 resolver->Orphan();
Craig Tiller972470b2017-02-09 15:05:36 -0800705}
706
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700707/* Destructor for channel_data */
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800708static void cc_destroy_channel_elem(grpc_channel_element* elem) {
Noah Eisenbe82e642018-02-09 09:16:55 -0800709 channel_data* chand = static_cast<channel_data*>(elem->channel_data);
Craig Tiller4782d922017-11-10 09:53:21 -0800710 if (chand->resolver != nullptr) {
ncteisen274bbbe2017-06-08 14:57:11 -0700711 GRPC_CLOSURE_SCHED(
Mark D. Roth209f6442018-02-08 10:26:46 -0800712 GRPC_CLOSURE_CREATE(shutdown_resolver_locked, chand->resolver.release(),
Yash Tibrewal0ee75742017-10-13 16:07:13 -0700713 grpc_combiner_scheduler(chand->combiner)),
Craig Tiller972470b2017-02-09 15:05:36 -0800714 GRPC_ERROR_NONE);
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700715 }
Craig Tiller4782d922017-11-10 09:53:21 -0800716 if (chand->client_channel_factory != nullptr) {
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800717 grpc_client_channel_factory_unref(chand->client_channel_factory);
Mark D. Roth0e48a9a2016-09-08 14:14:39 -0700718 }
Craig Tiller4782d922017-11-10 09:53:21 -0800719 if (chand->lb_policy != nullptr) {
Mark D. Rothc8875492018-02-20 08:33:48 -0800720 grpc_pollset_set_del_pollset_set(chand->lb_policy->interested_parties(),
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700721 chand->interested_parties);
Mark D. Rothc8875492018-02-20 08:33:48 -0800722 chand->lb_policy.reset();
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700723 }
Craig Tiller613dafa2017-02-09 12:00:43 -0800724 gpr_free(chand->info_lb_policy_name);
725 gpr_free(chand->info_service_config_json);
Mark D. Roth9db86fc2018-03-28 07:42:20 -0700726 chand->retry_throttle_data.reset();
727 chand->method_params_table.reset();
Yash Tibrewal8cf14702017-12-06 09:47:54 -0800728 grpc_client_channel_stop_backup_polling(chand->interested_parties);
729 grpc_connectivity_state_destroy(&chand->state_tracker);
730 grpc_pollset_set_destroy(chand->interested_parties);
731 GRPC_COMBINER_UNREF(chand->combiner, "client_channel");
Craig Tillerd85477512017-02-09 12:02:39 -0800732 gpr_mu_destroy(&chand->info_mu);
Alexander Polcync3b1f182017-04-18 13:51:36 -0700733 gpr_mu_destroy(&chand->external_connectivity_watcher_list_mu);
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700734}
735
736/*************************************************************************
737 * PER-CALL FUNCTIONS
738 */
739
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700740// Max number of batches that can be pending on a call at any given
Mark D. Roth718c8342018-02-28 13:00:04 -0800741// time. This includes one batch for each of the following ops:
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700742// recv_initial_metadata
743// send_initial_metadata
744// recv_message
745// send_message
746// recv_trailing_metadata
747// send_trailing_metadata
Mark D. Roth718c8342018-02-28 13:00:04 -0800748#define MAX_PENDING_BATCHES 6
749
750// Retry support:
751//
752// In order to support retries, we act as a proxy for stream op batches.
753// When we get a batch from the surface, we add it to our list of pending
754// batches, and we then use those batches to construct separate "child"
755// batches to be started on the subchannel call. When the child batches
756// return, we then decide which pending batches have been completed and
757// schedule their callbacks accordingly. If a subchannel call fails and
758// we want to retry it, we do a new pick and start again, constructing
759// new "child" batches for the new subchannel call.
760//
761// Note that retries are committed when receiving data from the server
762// (except for Trailers-Only responses). However, there may be many
763// send ops started before receiving any data, so we may have already
764// completed some number of send ops (and returned the completions up to
765// the surface) by the time we realize that we need to retry. To deal
766// with this, we cache data for send ops, so that we can replay them on a
767// different subchannel call even after we have completed the original
768// batches.
769//
770// There are two sets of data to maintain:
771// - In call_data (in the parent channel), we maintain a list of pending
772// ops and cached data for send ops.
773// - In the subchannel call, we maintain state to indicate what ops have
774// already been sent down to that call.
775//
776// When constructing the "child" batches, we compare those two sets of
777// data to see which batches need to be sent to the subchannel call.
778
779// TODO(roth): In subsequent PRs:
780// - add support for transparent retries (including initial metadata)
781// - figure out how to record stats in census for retries
782// (census filter is on top of this one)
783// - add census stats for retries
784
785// State used for starting a retryable batch on a subchannel call.
786// This provides its own grpc_transport_stream_op_batch and other data
787// structures needed to populate the ops in the batch.
788// We allocate one struct on the arena for each attempt at starting a
789// batch on a given subchannel call.
790typedef struct {
791 gpr_refcount refs;
792 grpc_call_element* elem;
793 grpc_subchannel_call* subchannel_call; // Holds a ref.
794 // The batch to use in the subchannel call.
795 // Its payload field points to subchannel_call_retry_state.batch_payload.
796 grpc_transport_stream_op_batch batch;
797 // For send_initial_metadata.
798 // Note that we need to make a copy of the initial metadata for each
799 // subchannel call instead of just referring to the copy in call_data,
800 // because filters in the subchannel stack will probably add entries,
801 // so we need to start in a pristine state for each attempt of the call.
802 grpc_linked_mdelem* send_initial_metadata_storage;
803 grpc_metadata_batch send_initial_metadata;
804 // For send_message.
Mark D. Roth3d8b32d2018-03-09 13:25:40 -0800805 grpc_core::ManualConstructor<grpc_core::ByteStreamCache::CachingByteStream>
806 send_message;
Mark D. Roth718c8342018-02-28 13:00:04 -0800807 // For send_trailing_metadata.
808 grpc_linked_mdelem* send_trailing_metadata_storage;
809 grpc_metadata_batch send_trailing_metadata;
810 // For intercepting recv_initial_metadata.
811 grpc_metadata_batch recv_initial_metadata;
812 grpc_closure recv_initial_metadata_ready;
813 bool trailing_metadata_available;
814 // For intercepting recv_message.
815 grpc_closure recv_message_ready;
Mark D. Roth3d8b32d2018-03-09 13:25:40 -0800816 grpc_core::OrphanablePtr<grpc_core::ByteStream> recv_message;
Mark D. Roth718c8342018-02-28 13:00:04 -0800817 // For intercepting recv_trailing_metadata.
818 grpc_metadata_batch recv_trailing_metadata;
819 grpc_transport_stream_stats collect_stats;
820 // For intercepting on_complete.
821 grpc_closure on_complete;
822} subchannel_batch_data;
823
824// Retry state associated with a subchannel call.
825// Stored in the parent_data of the subchannel call object.
826typedef struct {
827 // subchannel_batch_data.batch.payload points to this.
828 grpc_transport_stream_op_batch_payload batch_payload;
829 // These fields indicate which ops have been started and completed on
830 // this subchannel call.
831 size_t started_send_message_count;
832 size_t completed_send_message_count;
833 size_t started_recv_message_count;
834 size_t completed_recv_message_count;
835 bool started_send_initial_metadata : 1;
836 bool completed_send_initial_metadata : 1;
837 bool started_send_trailing_metadata : 1;
838 bool completed_send_trailing_metadata : 1;
839 bool started_recv_initial_metadata : 1;
840 bool completed_recv_initial_metadata : 1;
841 bool started_recv_trailing_metadata : 1;
842 bool completed_recv_trailing_metadata : 1;
843 // State for callback processing.
844 bool retry_dispatched : 1;
Mark D. Rothde077ac2018-04-12 08:05:44 -0700845 subchannel_batch_data* recv_initial_metadata_ready_deferred_batch;
Mark D. Roth718c8342018-02-28 13:00:04 -0800846 grpc_error* recv_initial_metadata_error;
Mark D. Rothde077ac2018-04-12 08:05:44 -0700847 subchannel_batch_data* recv_message_ready_deferred_batch;
Mark D. Roth718c8342018-02-28 13:00:04 -0800848 grpc_error* recv_message_error;
Mark D. Rothde077ac2018-04-12 08:05:44 -0700849 subchannel_batch_data* recv_trailing_metadata_internal_batch;
Mark D. Roth718c8342018-02-28 13:00:04 -0800850} subchannel_call_retry_state;
851
852// Pending batches stored in call data.
853typedef struct {
854 // The pending batch. If nullptr, this slot is empty.
855 grpc_transport_stream_op_batch* batch;
856 // Indicates whether payload for send ops has been cached in call data.
857 bool send_ops_cached;
858} pending_batch;
Mark D. Roth0ca0be82017-06-20 07:49:33 -0700859
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700860/** Call data. Holds a pointer to grpc_subchannel_call and the
861 associated machinery to create such a pointer.
862 Handles queueing of stream ops until a call object is ready, waiting
863 for initial metadata before trying to create a call object,
864 and handling cancellation gracefully. */
865typedef struct client_channel_call_data {
Mark D. Roth72f6da82016-09-02 13:42:38 -0700866 // State for handling deadlines.
867 // The code in deadline_filter.c requires this to be the first field.
Mark D. Roth72f6da82016-09-02 13:42:38 -0700868 // TODO(roth): This is slightly sub-optimal in that grpc_deadline_state
Mark D. Roth66f3d2b2017-09-01 09:02:17 -0700869 // and this struct both independently store pointers to the call stack
870 // and call combiner. If/when we have time, find a way to avoid this
871 // without breaking the grpc_deadline_state abstraction.
Mark D. Roth72f6da82016-09-02 13:42:38 -0700872 grpc_deadline_state deadline_state;
Mark D. Rothf28763c2016-09-14 15:18:40 -0700873
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800874 grpc_slice path; // Request path.
Mark D. Rothe40dd292016-10-05 14:58:37 -0700875 gpr_timespec call_start_time;
Craig Tiller89c14282017-07-19 15:32:27 -0700876 grpc_millis deadline;
Craig Tillerbaa14a92017-11-03 09:09:36 -0700877 gpr_arena* arena;
878 grpc_call_stack* owning_call;
879 grpc_call_combiner* call_combiner;
Mark D. Roth76e264b2017-08-25 09:03:33 -0700880
Mark D. Roth9db86fc2018-03-28 07:42:20 -0700881 grpc_core::RefCountedPtr<ServerRetryThrottleData> retry_throttle_data;
Mark D. Roth3e7f2df2018-02-26 13:17:06 -0800882 grpc_core::RefCountedPtr<ClientChannelMethodParams> method_params;
Mark D. Rothaa850a72016-09-26 13:38:02 -0700883
Craig Tillerbaa14a92017-11-03 09:09:36 -0700884 grpc_subchannel_call* subchannel_call;
Mark D. Roth718c8342018-02-28 13:00:04 -0800885
886 // Set when we get a cancel_stream op.
887 grpc_error* cancel_error;
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700888
Mark D. Rothc8875492018-02-20 08:33:48 -0800889 grpc_core::LoadBalancingPolicy::PickState pick;
Mark D. Roth718c8342018-02-28 13:00:04 -0800890 grpc_closure pick_closure;
891 grpc_closure pick_cancel_closure;
Mark D. Roth60751fe2017-07-07 12:50:33 -0700892
Craig Tillerbaa14a92017-11-03 09:09:36 -0700893 grpc_polling_entity* pollent;
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700894
Mark D. Roth718c8342018-02-28 13:00:04 -0800895 // Batches are added to this list when received from above.
896 // They are removed when we are done handling the batch (i.e., when
897 // either we have invoked all of the batch's callbacks or we have
898 // passed the batch down to the subchannel call and are not
899 // intercepting any of its callbacks).
900 pending_batch pending_batches[MAX_PENDING_BATCHES];
901 bool pending_send_initial_metadata : 1;
902 bool pending_send_message : 1;
903 bool pending_send_trailing_metadata : 1;
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700904
Mark D. Roth718c8342018-02-28 13:00:04 -0800905 // Retry state.
906 bool enable_retries : 1;
907 bool retry_committed : 1;
908 bool last_attempt_got_server_pushback : 1;
909 int num_attempts_completed;
910 size_t bytes_buffered_for_retry;
911 grpc_core::ManualConstructor<grpc_core::BackOff> retry_backoff;
912 grpc_timer retry_timer;
David Garcia Quintasd1a47f12016-09-02 12:46:44 +0200913
Mark D. Roth718c8342018-02-28 13:00:04 -0800914 // Cached data for retrying send ops.
915 // send_initial_metadata
916 bool seen_send_initial_metadata;
917 grpc_linked_mdelem* send_initial_metadata_storage;
918 grpc_metadata_batch send_initial_metadata;
919 uint32_t send_initial_metadata_flags;
920 gpr_atm* peer_string;
921 // send_message
922 // When we get a send_message op, we replace the original byte stream
Mark D. Roth3d8b32d2018-03-09 13:25:40 -0800923 // with a CachingByteStream that caches the slices to a local buffer for
924 // use in retries.
Mark D. Roth718c8342018-02-28 13:00:04 -0800925 // Note: We inline the cache for the first 3 send_message ops and use
926 // dynamic allocation after that. This number was essentially picked
927 // at random; it could be changed in the future to tune performance.
Mark D. Rothefcd45b2018-03-28 10:49:59 -0700928 grpc_core::ManualConstructor<
929 grpc_core::InlinedVector<grpc_core::ByteStreamCache*, 3>>
930 send_messages;
Mark D. Roth718c8342018-02-28 13:00:04 -0800931 // send_trailing_metadata
932 bool seen_send_trailing_metadata;
933 grpc_linked_mdelem* send_trailing_metadata_storage;
934 grpc_metadata_batch send_trailing_metadata;
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700935} call_data;
936
Mark D. Roth718c8342018-02-28 13:00:04 -0800937// Forward declarations.
938static void retry_commit(grpc_call_element* elem,
939 subchannel_call_retry_state* retry_state);
940static void start_internal_recv_trailing_metadata(grpc_call_element* elem);
941static void on_complete(void* arg, grpc_error* error);
942static void start_retriable_subchannel_batches(void* arg, grpc_error* ignored);
943static void pick_after_resolver_result_start_locked(grpc_call_element* elem);
944static void start_pick_locked(void* arg, grpc_error* ignored);
Craig Tiller8b1d59c2016-12-27 15:15:30 -0800945
Mark D. Roth718c8342018-02-28 13:00:04 -0800946//
947// send op data caching
948//
949
950// Caches data for send ops so that it can be retried later, if not
951// already cached.
952static void maybe_cache_send_ops_for_batch(call_data* calld,
953 pending_batch* pending) {
954 if (pending->send_ops_cached) return;
955 pending->send_ops_cached = true;
956 grpc_transport_stream_op_batch* batch = pending->batch;
957 // Save a copy of metadata for send_initial_metadata ops.
Mark D. Roth76e264b2017-08-25 09:03:33 -0700958 if (batch->send_initial_metadata) {
Mark D. Roth718c8342018-02-28 13:00:04 -0800959 calld->seen_send_initial_metadata = true;
960 GPR_ASSERT(calld->send_initial_metadata_storage == nullptr);
961 grpc_metadata_batch* send_initial_metadata =
962 batch->payload->send_initial_metadata.send_initial_metadata;
963 calld->send_initial_metadata_storage = (grpc_linked_mdelem*)gpr_arena_alloc(
964 calld->arena,
965 sizeof(grpc_linked_mdelem) * send_initial_metadata->list.count);
966 grpc_metadata_batch_copy(send_initial_metadata,
967 &calld->send_initial_metadata,
968 calld->send_initial_metadata_storage);
969 calld->send_initial_metadata_flags =
970 batch->payload->send_initial_metadata.send_initial_metadata_flags;
971 calld->peer_string = batch->payload->send_initial_metadata.peer_string;
972 }
973 // Set up cache for send_message ops.
974 if (batch->send_message) {
Mark D. Roth3d8b32d2018-03-09 13:25:40 -0800975 grpc_core::ByteStreamCache* cache =
976 static_cast<grpc_core::ByteStreamCache*>(
977 gpr_arena_alloc(calld->arena, sizeof(grpc_core::ByteStreamCache)));
978 new (cache) grpc_core::ByteStreamCache(
979 std::move(batch->payload->send_message.send_message));
Mark D. Rothefcd45b2018-03-28 10:49:59 -0700980 calld->send_messages->push_back(cache);
Mark D. Roth718c8342018-02-28 13:00:04 -0800981 }
982 // Save metadata batch for send_trailing_metadata ops.
983 if (batch->send_trailing_metadata) {
984 calld->seen_send_trailing_metadata = true;
985 GPR_ASSERT(calld->send_trailing_metadata_storage == nullptr);
986 grpc_metadata_batch* send_trailing_metadata =
987 batch->payload->send_trailing_metadata.send_trailing_metadata;
988 calld->send_trailing_metadata_storage =
989 (grpc_linked_mdelem*)gpr_arena_alloc(
990 calld->arena,
991 sizeof(grpc_linked_mdelem) * send_trailing_metadata->list.count);
992 grpc_metadata_batch_copy(send_trailing_metadata,
993 &calld->send_trailing_metadata,
994 calld->send_trailing_metadata_storage);
Mark D. Roth76e264b2017-08-25 09:03:33 -0700995 }
Mark D. Roth2a5959f2016-09-01 08:20:27 -0700996}
997
Mark D. Rothde077ac2018-04-12 08:05:44 -0700998// Frees cached send_initial_metadata.
999static void free_cached_send_initial_metadata(channel_data* chand,
1000 call_data* calld) {
1001 if (grpc_client_channel_trace.enabled()) {
Mark D. Roth48854d22018-04-25 13:05:26 -07001002 gpr_log(GPR_INFO,
Mark D. Rothde077ac2018-04-12 08:05:44 -07001003 "chand=%p calld=%p: destroying calld->send_initial_metadata", chand,
1004 calld);
1005 }
1006 grpc_metadata_batch_destroy(&calld->send_initial_metadata);
1007}
1008
1009// Frees cached send_message at index idx.
1010static void free_cached_send_message(channel_data* chand, call_data* calld,
1011 size_t idx) {
1012 if (grpc_client_channel_trace.enabled()) {
Mark D. Roth48854d22018-04-25 13:05:26 -07001013 gpr_log(GPR_INFO,
Mark D. Rothde077ac2018-04-12 08:05:44 -07001014 "chand=%p calld=%p: destroying calld->send_messages[%" PRIuPTR "]",
1015 chand, calld, idx);
1016 }
1017 (*calld->send_messages)[idx]->Destroy();
1018}
1019
1020// Frees cached send_trailing_metadata.
1021static void free_cached_send_trailing_metadata(channel_data* chand,
1022 call_data* calld) {
1023 if (grpc_client_channel_trace.enabled()) {
Mark D. Roth48854d22018-04-25 13:05:26 -07001024 gpr_log(GPR_INFO,
Mark D. Rothde077ac2018-04-12 08:05:44 -07001025 "chand=%p calld=%p: destroying calld->send_trailing_metadata",
1026 chand, calld);
1027 }
1028 grpc_metadata_batch_destroy(&calld->send_trailing_metadata);
1029}
1030
Mark D. Roth718c8342018-02-28 13:00:04 -08001031// Frees cached send ops that have already been completed after
1032// committing the call.
1033static void free_cached_send_op_data_after_commit(
1034 grpc_call_element* elem, subchannel_call_retry_state* retry_state) {
Noah Eisenbe82e642018-02-09 09:16:55 -08001035 channel_data* chand = static_cast<channel_data*>(elem->channel_data);
1036 call_data* calld = static_cast<call_data*>(elem->call_data);
Mark D. Roth718c8342018-02-28 13:00:04 -08001037 if (retry_state->completed_send_initial_metadata) {
Mark D. Rothde077ac2018-04-12 08:05:44 -07001038 free_cached_send_initial_metadata(chand, calld);
Mark D. Roth60751fe2017-07-07 12:50:33 -07001039 }
Mark D. Roth718c8342018-02-28 13:00:04 -08001040 for (size_t i = 0; i < retry_state->completed_send_message_count; ++i) {
Mark D. Rothde077ac2018-04-12 08:05:44 -07001041 free_cached_send_message(chand, calld, i);
Mark D. Roth2a5959f2016-09-01 08:20:27 -07001042 }
Mark D. Roth718c8342018-02-28 13:00:04 -08001043 if (retry_state->completed_send_trailing_metadata) {
Mark D. Rothde077ac2018-04-12 08:05:44 -07001044 free_cached_send_trailing_metadata(chand, calld);
Mark D. Roth718c8342018-02-28 13:00:04 -08001045 }
Mark D. Roth2a5959f2016-09-01 08:20:27 -07001046}
1047
Mark D. Roth718c8342018-02-28 13:00:04 -08001048// Frees cached send ops that were completed by the completed batch in
1049// batch_data. Used when batches are completed after the call is committed.
1050static void free_cached_send_op_data_for_completed_batch(
1051 grpc_call_element* elem, subchannel_batch_data* batch_data,
1052 subchannel_call_retry_state* retry_state) {
Noah Eisenbe82e642018-02-09 09:16:55 -08001053 channel_data* chand = static_cast<channel_data*>(elem->channel_data);
1054 call_data* calld = static_cast<call_data*>(elem->call_data);
Mark D. Roth718c8342018-02-28 13:00:04 -08001055 if (batch_data->batch.send_initial_metadata) {
Mark D. Rothde077ac2018-04-12 08:05:44 -07001056 free_cached_send_initial_metadata(chand, calld);
Mark D. Roth718c8342018-02-28 13:00:04 -08001057 }
1058 if (batch_data->batch.send_message) {
Mark D. Rothde077ac2018-04-12 08:05:44 -07001059 free_cached_send_message(chand, calld,
1060 retry_state->completed_send_message_count - 1);
Mark D. Roth718c8342018-02-28 13:00:04 -08001061 }
1062 if (batch_data->batch.send_trailing_metadata) {
Mark D. Rothde077ac2018-04-12 08:05:44 -07001063 free_cached_send_trailing_metadata(chand, calld);
Mark D. Roth718c8342018-02-28 13:00:04 -08001064 }
1065}
1066
1067//
1068// pending_batches management
1069//
1070
1071// Returns the index into calld->pending_batches to be used for batch.
1072static size_t get_batch_index(grpc_transport_stream_op_batch* batch) {
1073 // Note: It is important the send_initial_metadata be the first entry
1074 // here, since the code in pick_subchannel_locked() assumes it will be.
1075 if (batch->send_initial_metadata) return 0;
1076 if (batch->send_message) return 1;
1077 if (batch->send_trailing_metadata) return 2;
1078 if (batch->recv_initial_metadata) return 3;
1079 if (batch->recv_message) return 4;
1080 if (batch->recv_trailing_metadata) return 5;
1081 GPR_UNREACHABLE_CODE(return (size_t)-1);
1082}
1083
1084// This is called via the call combiner, so access to calld is synchronized.
1085static void pending_batches_add(grpc_call_element* elem,
1086 grpc_transport_stream_op_batch* batch) {
1087 channel_data* chand = static_cast<channel_data*>(elem->channel_data);
1088 call_data* calld = static_cast<call_data*>(elem->call_data);
1089 const size_t idx = get_batch_index(batch);
Craig Tiller6014e8a2017-10-16 13:50:29 -07001090 if (grpc_client_channel_trace.enabled()) {
Mark D. Roth48854d22018-04-25 13:05:26 -07001091 gpr_log(GPR_INFO,
Mark D. Roth718c8342018-02-28 13:00:04 -08001092 "chand=%p calld=%p: adding pending batch at index %" PRIuPTR, chand,
1093 calld, idx);
Mark D. Roth60751fe2017-07-07 12:50:33 -07001094 }
Mark D. Roth718c8342018-02-28 13:00:04 -08001095 pending_batch* pending = &calld->pending_batches[idx];
1096 GPR_ASSERT(pending->batch == nullptr);
1097 pending->batch = batch;
1098 pending->send_ops_cached = false;
1099 if (calld->enable_retries) {
1100 // Update state in calld about pending batches.
1101 // Also check if the batch takes us over the retry buffer limit.
1102 // Note: We don't check the size of trailing metadata here, because
1103 // gRPC clients do not send trailing metadata.
1104 if (batch->send_initial_metadata) {
1105 calld->pending_send_initial_metadata = true;
1106 calld->bytes_buffered_for_retry += grpc_metadata_batch_size(
1107 batch->payload->send_initial_metadata.send_initial_metadata);
1108 }
1109 if (batch->send_message) {
1110 calld->pending_send_message = true;
1111 calld->bytes_buffered_for_retry +=
Mark D. Roth3d8b32d2018-03-09 13:25:40 -08001112 batch->payload->send_message.send_message->length();
Mark D. Roth718c8342018-02-28 13:00:04 -08001113 }
1114 if (batch->send_trailing_metadata) {
1115 calld->pending_send_trailing_metadata = true;
1116 }
Yash Tibrewal8c5b8832018-05-14 11:44:18 -07001117 if (GPR_UNLIKELY(calld->bytes_buffered_for_retry >
1118 chand->per_rpc_retry_buffer_size)) {
Mark D. Roth718c8342018-02-28 13:00:04 -08001119 if (grpc_client_channel_trace.enabled()) {
Mark D. Roth48854d22018-04-25 13:05:26 -07001120 gpr_log(GPR_INFO,
Mark D. Roth718c8342018-02-28 13:00:04 -08001121 "chand=%p calld=%p: exceeded retry buffer size, committing",
1122 chand, calld);
1123 }
1124 subchannel_call_retry_state* retry_state =
1125 calld->subchannel_call == nullptr
1126 ? nullptr
1127 : static_cast<subchannel_call_retry_state*>(
1128 grpc_connected_subchannel_call_get_parent_data(
1129 calld->subchannel_call));
1130 retry_commit(elem, retry_state);
1131 // If we are not going to retry and have not yet started, pretend
1132 // retries are disabled so that we don't bother with retry overhead.
1133 if (calld->num_attempts_completed == 0) {
1134 if (grpc_client_channel_trace.enabled()) {
Mark D. Roth48854d22018-04-25 13:05:26 -07001135 gpr_log(GPR_INFO,
Mark D. Roth718c8342018-02-28 13:00:04 -08001136 "chand=%p calld=%p: disabling retries before first attempt",
1137 chand, calld);
Mark D. Roth0ca0be82017-06-20 07:49:33 -07001138 }
Mark D. Roth718c8342018-02-28 13:00:04 -08001139 calld->enable_retries = false;
Craig Tiller11c17d42017-03-13 13:36:34 -07001140 }
1141 }
1142 }
Craig Tiller11c17d42017-03-13 13:36:34 -07001143}
Craig Tillerea4a4f12017-03-13 13:36:52 -07001144
Mark D. Roth718c8342018-02-28 13:00:04 -08001145static void pending_batch_clear(call_data* calld, pending_batch* pending) {
1146 if (calld->enable_retries) {
1147 if (pending->batch->send_initial_metadata) {
1148 calld->pending_send_initial_metadata = false;
1149 }
1150 if (pending->batch->send_message) {
1151 calld->pending_send_message = false;
1152 }
1153 if (pending->batch->send_trailing_metadata) {
1154 calld->pending_send_trailing_metadata = false;
1155 }
1156 }
1157 pending->batch = nullptr;
1158}
1159
1160// This is called via the call combiner, so access to calld is synchronized.
1161static void fail_pending_batch_in_call_combiner(void* arg, grpc_error* error) {
1162 grpc_transport_stream_op_batch* batch =
1163 static_cast<grpc_transport_stream_op_batch*>(arg);
1164 call_data* calld = static_cast<call_data*>(batch->handler_private.extra_arg);
1165 // Note: This will release the call combiner.
1166 grpc_transport_stream_op_batch_finish_with_failure(
1167 batch, GRPC_ERROR_REF(error), calld->call_combiner);
1168}
1169
1170// This is called via the call combiner, so access to calld is synchronized.
1171// If yield_call_combiner is true, assumes responsibility for yielding
1172// the call combiner.
1173static void pending_batches_fail(grpc_call_element* elem, grpc_error* error,
1174 bool yield_call_combiner) {
1175 GPR_ASSERT(error != GRPC_ERROR_NONE);
1176 call_data* calld = static_cast<call_data*>(elem->call_data);
1177 if (grpc_client_channel_trace.enabled()) {
1178 size_t num_batches = 0;
1179 for (size_t i = 0; i < GPR_ARRAY_SIZE(calld->pending_batches); ++i) {
1180 if (calld->pending_batches[i].batch != nullptr) ++num_batches;
1181 }
Mark D. Roth48854d22018-04-25 13:05:26 -07001182 gpr_log(GPR_INFO,
Mark D. Roth718c8342018-02-28 13:00:04 -08001183 "chand=%p calld=%p: failing %" PRIuPTR " pending batches: %s",
1184 elem->channel_data, calld, num_batches, grpc_error_string(error));
1185 }
1186 grpc_transport_stream_op_batch*
1187 batches[GPR_ARRAY_SIZE(calld->pending_batches)];
1188 size_t num_batches = 0;
1189 for (size_t i = 0; i < GPR_ARRAY_SIZE(calld->pending_batches); ++i) {
1190 pending_batch* pending = &calld->pending_batches[i];
1191 grpc_transport_stream_op_batch* batch = pending->batch;
1192 if (batch != nullptr) {
1193 batches[num_batches++] = batch;
1194 pending_batch_clear(calld, pending);
1195 }
1196 }
1197 for (size_t i = yield_call_combiner ? 1 : 0; i < num_batches; ++i) {
1198 grpc_transport_stream_op_batch* batch = batches[i];
1199 batch->handler_private.extra_arg = calld;
1200 GRPC_CLOSURE_INIT(&batch->handler_private.closure,
1201 fail_pending_batch_in_call_combiner, batch,
1202 grpc_schedule_on_exec_ctx);
1203 GRPC_CALL_COMBINER_START(calld->call_combiner,
1204 &batch->handler_private.closure,
1205 GRPC_ERROR_REF(error), "pending_batches_fail");
1206 }
1207 if (yield_call_combiner) {
1208 if (num_batches > 0) {
1209 // Note: This will release the call combiner.
1210 grpc_transport_stream_op_batch_finish_with_failure(
1211 batches[0], GRPC_ERROR_REF(error), calld->call_combiner);
1212 } else {
1213 GRPC_CALL_COMBINER_STOP(calld->call_combiner, "pending_batches_fail");
1214 }
1215 }
1216 GRPC_ERROR_UNREF(error);
1217}
1218
1219// This is called via the call combiner, so access to calld is synchronized.
1220static void resume_pending_batch_in_call_combiner(void* arg,
1221 grpc_error* ignored) {
1222 grpc_transport_stream_op_batch* batch =
1223 static_cast<grpc_transport_stream_op_batch*>(arg);
1224 grpc_subchannel_call* subchannel_call =
1225 static_cast<grpc_subchannel_call*>(batch->handler_private.extra_arg);
1226 // Note: This will release the call combiner.
1227 grpc_subchannel_call_process_op(subchannel_call, batch);
1228}
1229
1230// This is called via the call combiner, so access to calld is synchronized.
1231static void pending_batches_resume(grpc_call_element* elem) {
Noah Eisenbe82e642018-02-09 09:16:55 -08001232 channel_data* chand = static_cast<channel_data*>(elem->channel_data);
1233 call_data* calld = static_cast<call_data*>(elem->call_data);
Mark D. Roth718c8342018-02-28 13:00:04 -08001234 if (calld->enable_retries) {
1235 start_retriable_subchannel_batches(elem, GRPC_ERROR_NONE);
1236 return;
1237 }
1238 // Retries not enabled; send down batches as-is.
1239 if (grpc_client_channel_trace.enabled()) {
1240 size_t num_batches = 0;
1241 for (size_t i = 0; i < GPR_ARRAY_SIZE(calld->pending_batches); ++i) {
1242 if (calld->pending_batches[i].batch != nullptr) ++num_batches;
1243 }
Mark D. Roth48854d22018-04-25 13:05:26 -07001244 gpr_log(GPR_INFO,
Mark D. Roth718c8342018-02-28 13:00:04 -08001245 "chand=%p calld=%p: starting %" PRIuPTR
1246 " pending batches on subchannel_call=%p",
1247 chand, calld, num_batches, calld->subchannel_call);
1248 }
1249 grpc_transport_stream_op_batch*
1250 batches[GPR_ARRAY_SIZE(calld->pending_batches)];
1251 size_t num_batches = 0;
1252 for (size_t i = 0; i < GPR_ARRAY_SIZE(calld->pending_batches); ++i) {
1253 pending_batch* pending = &calld->pending_batches[i];
1254 grpc_transport_stream_op_batch* batch = pending->batch;
1255 if (batch != nullptr) {
1256 batches[num_batches++] = batch;
1257 pending_batch_clear(calld, pending);
1258 }
1259 }
1260 for (size_t i = 1; i < num_batches; ++i) {
1261 grpc_transport_stream_op_batch* batch = batches[i];
1262 batch->handler_private.extra_arg = calld->subchannel_call;
1263 GRPC_CLOSURE_INIT(&batch->handler_private.closure,
1264 resume_pending_batch_in_call_combiner, batch,
1265 grpc_schedule_on_exec_ctx);
1266 GRPC_CALL_COMBINER_START(calld->call_combiner,
1267 &batch->handler_private.closure, GRPC_ERROR_NONE,
1268 "pending_batches_resume");
1269 }
1270 GPR_ASSERT(num_batches > 0);
1271 // Note: This will release the call combiner.
1272 grpc_subchannel_call_process_op(calld->subchannel_call, batches[0]);
1273}
1274
1275static void maybe_clear_pending_batch(grpc_call_element* elem,
1276 pending_batch* pending) {
1277 channel_data* chand = static_cast<channel_data*>(elem->channel_data);
1278 call_data* calld = static_cast<call_data*>(elem->call_data);
1279 grpc_transport_stream_op_batch* batch = pending->batch;
1280 // We clear the pending batch if all of its callbacks have been
1281 // scheduled and reset to nullptr.
1282 if (batch->on_complete == nullptr &&
1283 (!batch->recv_initial_metadata ||
1284 batch->payload->recv_initial_metadata.recv_initial_metadata_ready ==
1285 nullptr) &&
1286 (!batch->recv_message ||
1287 batch->payload->recv_message.recv_message_ready == nullptr)) {
1288 if (grpc_client_channel_trace.enabled()) {
Mark D. Roth48854d22018-04-25 13:05:26 -07001289 gpr_log(GPR_INFO, "chand=%p calld=%p: clearing pending batch", chand,
Mark D. Roth718c8342018-02-28 13:00:04 -08001290 calld);
1291 }
1292 pending_batch_clear(calld, pending);
1293 }
1294}
1295
1296// Returns true if all ops in the pending batch have been completed.
1297static bool pending_batch_is_completed(
1298 pending_batch* pending, call_data* calld,
1299 subchannel_call_retry_state* retry_state) {
1300 if (pending->batch == nullptr || pending->batch->on_complete == nullptr) {
1301 return false;
1302 }
1303 if (pending->batch->send_initial_metadata &&
1304 !retry_state->completed_send_initial_metadata) {
1305 return false;
1306 }
1307 if (pending->batch->send_message &&
Mark D. Rothefcd45b2018-03-28 10:49:59 -07001308 retry_state->completed_send_message_count <
1309 calld->send_messages->size()) {
Mark D. Roth718c8342018-02-28 13:00:04 -08001310 return false;
1311 }
1312 if (pending->batch->send_trailing_metadata &&
1313 !retry_state->completed_send_trailing_metadata) {
1314 return false;
1315 }
1316 if (pending->batch->recv_initial_metadata &&
1317 !retry_state->completed_recv_initial_metadata) {
1318 return false;
1319 }
1320 if (pending->batch->recv_message &&
1321 retry_state->completed_recv_message_count <
1322 retry_state->started_recv_message_count) {
1323 return false;
1324 }
1325 if (pending->batch->recv_trailing_metadata &&
1326 !retry_state->completed_recv_trailing_metadata) {
1327 return false;
1328 }
1329 return true;
1330}
1331
1332// Returns true if any op in the batch was not yet started.
1333static bool pending_batch_is_unstarted(
1334 pending_batch* pending, call_data* calld,
1335 subchannel_call_retry_state* retry_state) {
1336 if (pending->batch == nullptr || pending->batch->on_complete == nullptr) {
1337 return false;
1338 }
1339 if (pending->batch->send_initial_metadata &&
1340 !retry_state->started_send_initial_metadata) {
1341 return true;
1342 }
1343 if (pending->batch->send_message &&
Mark D. Rothefcd45b2018-03-28 10:49:59 -07001344 retry_state->started_send_message_count < calld->send_messages->size()) {
Mark D. Roth718c8342018-02-28 13:00:04 -08001345 return true;
1346 }
1347 if (pending->batch->send_trailing_metadata &&
1348 !retry_state->started_send_trailing_metadata) {
1349 return true;
1350 }
1351 if (pending->batch->recv_initial_metadata &&
1352 !retry_state->started_recv_initial_metadata) {
1353 return true;
1354 }
1355 if (pending->batch->recv_message &&
1356 retry_state->completed_recv_message_count ==
1357 retry_state->started_recv_message_count) {
1358 return true;
1359 }
1360 if (pending->batch->recv_trailing_metadata &&
1361 !retry_state->started_recv_trailing_metadata) {
1362 return true;
1363 }
1364 return false;
1365}
1366
1367//
1368// retry code
1369//
1370
1371// Commits the call so that no further retry attempts will be performed.
1372static void retry_commit(grpc_call_element* elem,
1373 subchannel_call_retry_state* retry_state) {
1374 channel_data* chand = static_cast<channel_data*>(elem->channel_data);
1375 call_data* calld = static_cast<call_data*>(elem->call_data);
1376 if (calld->retry_committed) return;
1377 calld->retry_committed = true;
1378 if (grpc_client_channel_trace.enabled()) {
Mark D. Roth48854d22018-04-25 13:05:26 -07001379 gpr_log(GPR_INFO, "chand=%p calld=%p: committing retries", chand, calld);
Mark D. Roth718c8342018-02-28 13:00:04 -08001380 }
1381 if (retry_state != nullptr) {
1382 free_cached_send_op_data_after_commit(elem, retry_state);
1383 }
1384}
1385
1386// Starts a retry after appropriate back-off.
1387static void do_retry(grpc_call_element* elem,
1388 subchannel_call_retry_state* retry_state,
1389 grpc_millis server_pushback_ms) {
1390 channel_data* chand = static_cast<channel_data*>(elem->channel_data);
1391 call_data* calld = static_cast<call_data*>(elem->call_data);
1392 GPR_ASSERT(calld->method_params != nullptr);
1393 const ClientChannelMethodParams::RetryPolicy* retry_policy =
1394 calld->method_params->retry_policy();
1395 GPR_ASSERT(retry_policy != nullptr);
1396 // Reset subchannel call and connected subchannel.
1397 if (calld->subchannel_call != nullptr) {
1398 GRPC_SUBCHANNEL_CALL_UNREF(calld->subchannel_call,
1399 "client_channel_call_retry");
1400 calld->subchannel_call = nullptr;
1401 }
1402 if (calld->pick.connected_subchannel != nullptr) {
1403 calld->pick.connected_subchannel.reset();
1404 }
1405 // Compute backoff delay.
1406 grpc_millis next_attempt_time;
1407 if (server_pushback_ms >= 0) {
1408 next_attempt_time = grpc_core::ExecCtx::Get()->Now() + server_pushback_ms;
1409 calld->last_attempt_got_server_pushback = true;
1410 } else {
1411 if (calld->num_attempts_completed == 1 ||
1412 calld->last_attempt_got_server_pushback) {
1413 calld->retry_backoff.Init(
1414 grpc_core::BackOff::Options()
1415 .set_initial_backoff(retry_policy->initial_backoff)
1416 .set_multiplier(retry_policy->backoff_multiplier)
1417 .set_jitter(RETRY_BACKOFF_JITTER)
1418 .set_max_backoff(retry_policy->max_backoff));
1419 calld->last_attempt_got_server_pushback = false;
1420 }
1421 next_attempt_time = calld->retry_backoff->NextAttemptTime();
1422 }
1423 if (grpc_client_channel_trace.enabled()) {
Mark D. Roth48854d22018-04-25 13:05:26 -07001424 gpr_log(GPR_INFO,
Mark D. Roth718c8342018-02-28 13:00:04 -08001425 "chand=%p calld=%p: retrying failed call in %" PRIuPTR " ms", chand,
1426 calld, next_attempt_time - grpc_core::ExecCtx::Get()->Now());
1427 }
1428 // Schedule retry after computed delay.
1429 GRPC_CLOSURE_INIT(&calld->pick_closure, start_pick_locked, elem,
1430 grpc_combiner_scheduler(chand->combiner));
1431 grpc_timer_init(&calld->retry_timer, next_attempt_time, &calld->pick_closure);
1432 // Update bookkeeping.
1433 if (retry_state != nullptr) retry_state->retry_dispatched = true;
1434}
1435
1436// Returns true if the call is being retried.
1437static bool maybe_retry(grpc_call_element* elem,
1438 subchannel_batch_data* batch_data,
1439 grpc_status_code status,
1440 grpc_mdelem* server_pushback_md) {
1441 channel_data* chand = static_cast<channel_data*>(elem->channel_data);
1442 call_data* calld = static_cast<call_data*>(elem->call_data);
1443 // Get retry policy.
1444 if (calld->method_params == nullptr) return false;
1445 const ClientChannelMethodParams::RetryPolicy* retry_policy =
1446 calld->method_params->retry_policy();
1447 if (retry_policy == nullptr) return false;
1448 // If we've already dispatched a retry from this call, return true.
1449 // This catches the case where the batch has multiple callbacks
1450 // (i.e., it includes either recv_message or recv_initial_metadata).
1451 subchannel_call_retry_state* retry_state = nullptr;
1452 if (batch_data != nullptr) {
1453 retry_state = static_cast<subchannel_call_retry_state*>(
1454 grpc_connected_subchannel_call_get_parent_data(
1455 batch_data->subchannel_call));
1456 if (retry_state->retry_dispatched) {
1457 if (grpc_client_channel_trace.enabled()) {
Mark D. Roth48854d22018-04-25 13:05:26 -07001458 gpr_log(GPR_INFO, "chand=%p calld=%p: retry already dispatched", chand,
Mark D. Roth718c8342018-02-28 13:00:04 -08001459 calld);
1460 }
1461 return true;
1462 }
1463 }
1464 // Check status.
Yash Tibrewal8c5b8832018-05-14 11:44:18 -07001465 if (GPR_LIKELY(status == GRPC_STATUS_OK)) {
Mark D. Roth9db86fc2018-03-28 07:42:20 -07001466 if (calld->retry_throttle_data != nullptr) {
1467 calld->retry_throttle_data->RecordSuccess();
1468 }
Mark D. Roth718c8342018-02-28 13:00:04 -08001469 if (grpc_client_channel_trace.enabled()) {
Mark D. Roth48854d22018-04-25 13:05:26 -07001470 gpr_log(GPR_INFO, "chand=%p calld=%p: call succeeded", chand, calld);
Mark D. Roth718c8342018-02-28 13:00:04 -08001471 }
1472 return false;
1473 }
1474 // Status is not OK. Check whether the status is retryable.
1475 if (!retry_policy->retryable_status_codes.Contains(status)) {
1476 if (grpc_client_channel_trace.enabled()) {
Mark D. Roth48854d22018-04-25 13:05:26 -07001477 gpr_log(GPR_INFO,
Mark D. Roth718c8342018-02-28 13:00:04 -08001478 "chand=%p calld=%p: status %s not configured as retryable", chand,
1479 calld, grpc_status_code_to_string(status));
1480 }
1481 return false;
1482 }
1483 // Record the failure and check whether retries are throttled.
1484 // Note that it's important for this check to come after the status
1485 // code check above, since we should only record failures whose statuses
1486 // match the configured retryable status codes, so that we don't count
1487 // things like failures due to malformed requests (INVALID_ARGUMENT).
1488 // Conversely, it's important for this to come before the remaining
1489 // checks, so that we don't fail to record failures due to other factors.
Mark D. Roth9db86fc2018-03-28 07:42:20 -07001490 if (calld->retry_throttle_data != nullptr &&
1491 !calld->retry_throttle_data->RecordFailure()) {
Mark D. Roth718c8342018-02-28 13:00:04 -08001492 if (grpc_client_channel_trace.enabled()) {
Mark D. Roth48854d22018-04-25 13:05:26 -07001493 gpr_log(GPR_INFO, "chand=%p calld=%p: retries throttled", chand, calld);
Mark D. Roth718c8342018-02-28 13:00:04 -08001494 }
1495 return false;
1496 }
1497 // Check whether the call is committed.
1498 if (calld->retry_committed) {
1499 if (grpc_client_channel_trace.enabled()) {
Mark D. Roth48854d22018-04-25 13:05:26 -07001500 gpr_log(GPR_INFO, "chand=%p calld=%p: retries already committed", chand,
Mark D. Roth718c8342018-02-28 13:00:04 -08001501 calld);
1502 }
1503 return false;
1504 }
1505 // Check whether we have retries remaining.
1506 ++calld->num_attempts_completed;
1507 if (calld->num_attempts_completed >= retry_policy->max_attempts) {
1508 if (grpc_client_channel_trace.enabled()) {
Mark D. Roth48854d22018-04-25 13:05:26 -07001509 gpr_log(GPR_INFO, "chand=%p calld=%p: exceeded %d retry attempts", chand,
Mark D. Roth718c8342018-02-28 13:00:04 -08001510 calld, retry_policy->max_attempts);
1511 }
1512 return false;
1513 }
1514 // If the call was cancelled from the surface, don't retry.
1515 if (calld->cancel_error != GRPC_ERROR_NONE) {
1516 if (grpc_client_channel_trace.enabled()) {
Mark D. Roth48854d22018-04-25 13:05:26 -07001517 gpr_log(GPR_INFO,
Mark D. Roth718c8342018-02-28 13:00:04 -08001518 "chand=%p calld=%p: call cancelled from surface, not retrying",
1519 chand, calld);
1520 }
1521 return false;
1522 }
1523 // Check server push-back.
1524 grpc_millis server_pushback_ms = -1;
1525 if (server_pushback_md != nullptr) {
1526 // If the value is "-1" or any other unparseable string, we do not retry.
1527 uint32_t ms;
1528 if (!grpc_parse_slice_to_uint32(GRPC_MDVALUE(*server_pushback_md), &ms)) {
1529 if (grpc_client_channel_trace.enabled()) {
Mark D. Roth48854d22018-04-25 13:05:26 -07001530 gpr_log(GPR_INFO,
Mark D. Roth718c8342018-02-28 13:00:04 -08001531 "chand=%p calld=%p: not retrying due to server push-back",
1532 chand, calld);
1533 }
1534 return false;
1535 } else {
1536 if (grpc_client_channel_trace.enabled()) {
Mark D. Roth48854d22018-04-25 13:05:26 -07001537 gpr_log(GPR_INFO, "chand=%p calld=%p: server push-back: retry in %u ms",
1538 chand, calld, ms);
Mark D. Roth718c8342018-02-28 13:00:04 -08001539 }
1540 server_pushback_ms = (grpc_millis)ms;
1541 }
1542 }
1543 do_retry(elem, retry_state, server_pushback_ms);
1544 return true;
1545}
1546
1547//
1548// subchannel_batch_data
1549//
1550
1551static subchannel_batch_data* batch_data_create(grpc_call_element* elem,
1552 int refcount) {
1553 call_data* calld = static_cast<call_data*>(elem->call_data);
1554 subchannel_call_retry_state* retry_state =
1555 static_cast<subchannel_call_retry_state*>(
1556 grpc_connected_subchannel_call_get_parent_data(
1557 calld->subchannel_call));
1558 subchannel_batch_data* batch_data = static_cast<subchannel_batch_data*>(
1559 gpr_arena_alloc(calld->arena, sizeof(*batch_data)));
1560 batch_data->elem = elem;
1561 batch_data->subchannel_call =
1562 GRPC_SUBCHANNEL_CALL_REF(calld->subchannel_call, "batch_data_create");
1563 batch_data->batch.payload = &retry_state->batch_payload;
1564 gpr_ref_init(&batch_data->refs, refcount);
1565 GRPC_CLOSURE_INIT(&batch_data->on_complete, on_complete, batch_data,
1566 grpc_schedule_on_exec_ctx);
1567 batch_data->batch.on_complete = &batch_data->on_complete;
1568 GRPC_CALL_STACK_REF(calld->owning_call, "batch_data");
1569 return batch_data;
1570}
1571
1572static void batch_data_unref(subchannel_batch_data* batch_data) {
1573 if (gpr_unref(&batch_data->refs)) {
1574 if (batch_data->send_initial_metadata_storage != nullptr) {
1575 grpc_metadata_batch_destroy(&batch_data->send_initial_metadata);
1576 }
1577 if (batch_data->send_trailing_metadata_storage != nullptr) {
1578 grpc_metadata_batch_destroy(&batch_data->send_trailing_metadata);
1579 }
1580 if (batch_data->batch.recv_initial_metadata) {
1581 grpc_metadata_batch_destroy(&batch_data->recv_initial_metadata);
1582 }
1583 if (batch_data->batch.recv_trailing_metadata) {
1584 grpc_metadata_batch_destroy(&batch_data->recv_trailing_metadata);
1585 }
1586 GRPC_SUBCHANNEL_CALL_UNREF(batch_data->subchannel_call, "batch_data_unref");
1587 call_data* calld = static_cast<call_data*>(batch_data->elem->call_data);
1588 GRPC_CALL_STACK_UNREF(calld->owning_call, "batch_data");
1589 }
1590}
1591
1592//
1593// recv_initial_metadata callback handling
1594//
1595
1596// Invokes recv_initial_metadata_ready for a subchannel batch.
1597static void invoke_recv_initial_metadata_callback(void* arg,
1598 grpc_error* error) {
1599 subchannel_batch_data* batch_data = static_cast<subchannel_batch_data*>(arg);
1600 channel_data* chand =
1601 static_cast<channel_data*>(batch_data->elem->channel_data);
1602 call_data* calld = static_cast<call_data*>(batch_data->elem->call_data);
1603 // Find pending batch.
1604 pending_batch* pending = nullptr;
1605 for (size_t i = 0; i < GPR_ARRAY_SIZE(calld->pending_batches); ++i) {
1606 grpc_transport_stream_op_batch* batch = calld->pending_batches[i].batch;
1607 if (batch != nullptr && batch->recv_initial_metadata &&
1608 batch->payload->recv_initial_metadata.recv_initial_metadata_ready !=
1609 nullptr) {
1610 if (grpc_client_channel_trace.enabled()) {
Mark D. Roth48854d22018-04-25 13:05:26 -07001611 gpr_log(GPR_INFO,
Mark D. Roth718c8342018-02-28 13:00:04 -08001612 "chand=%p calld=%p: invoking recv_initial_metadata_ready for "
1613 "pending batch at index %" PRIuPTR,
1614 chand, calld, i);
1615 }
1616 pending = &calld->pending_batches[i];
1617 break;
1618 }
1619 }
1620 GPR_ASSERT(pending != nullptr);
1621 // Return metadata.
1622 grpc_metadata_batch_move(
1623 &batch_data->recv_initial_metadata,
1624 pending->batch->payload->recv_initial_metadata.recv_initial_metadata);
1625 // Update bookkeeping.
1626 // Note: Need to do this before invoking the callback, since invoking
1627 // the callback will result in yielding the call combiner.
1628 grpc_closure* recv_initial_metadata_ready =
1629 pending->batch->payload->recv_initial_metadata
1630 .recv_initial_metadata_ready;
1631 pending->batch->payload->recv_initial_metadata.recv_initial_metadata_ready =
1632 nullptr;
1633 maybe_clear_pending_batch(batch_data->elem, pending);
1634 batch_data_unref(batch_data);
1635 // Invoke callback.
1636 GRPC_CLOSURE_RUN(recv_initial_metadata_ready, GRPC_ERROR_REF(error));
1637}
1638
1639// Intercepts recv_initial_metadata_ready callback for retries.
1640// Commits the call and returns the initial metadata up the stack.
1641static void recv_initial_metadata_ready(void* arg, grpc_error* error) {
1642 subchannel_batch_data* batch_data = static_cast<subchannel_batch_data*>(arg);
1643 grpc_call_element* elem = batch_data->elem;
1644 channel_data* chand = static_cast<channel_data*>(elem->channel_data);
1645 call_data* calld = static_cast<call_data*>(elem->call_data);
1646 if (grpc_client_channel_trace.enabled()) {
Mark D. Roth48854d22018-04-25 13:05:26 -07001647 gpr_log(GPR_INFO,
Mark D. Roth718c8342018-02-28 13:00:04 -08001648 "chand=%p calld=%p: got recv_initial_metadata_ready, error=%s",
1649 chand, calld, grpc_error_string(error));
1650 }
1651 subchannel_call_retry_state* retry_state =
1652 static_cast<subchannel_call_retry_state*>(
1653 grpc_connected_subchannel_call_get_parent_data(
1654 batch_data->subchannel_call));
1655 // If we got an error or a Trailers-Only response and have not yet gotten
1656 // the recv_trailing_metadata on_complete callback, then defer
1657 // propagating this callback back to the surface. We can evaluate whether
1658 // to retry when recv_trailing_metadata comes back.
Yash Tibrewal8c5b8832018-05-14 11:44:18 -07001659 if (GPR_UNLIKELY((batch_data->trailing_metadata_available ||
1660 error != GRPC_ERROR_NONE) &&
1661 !retry_state->completed_recv_trailing_metadata)) {
Mark D. Roth718c8342018-02-28 13:00:04 -08001662 if (grpc_client_channel_trace.enabled()) {
Mark D. Roth48854d22018-04-25 13:05:26 -07001663 gpr_log(GPR_INFO,
Mark D. Roth718c8342018-02-28 13:00:04 -08001664 "chand=%p calld=%p: deferring recv_initial_metadata_ready "
1665 "(Trailers-Only)",
1666 chand, calld);
1667 }
Mark D. Rothde077ac2018-04-12 08:05:44 -07001668 retry_state->recv_initial_metadata_ready_deferred_batch = batch_data;
Mark D. Roth718c8342018-02-28 13:00:04 -08001669 retry_state->recv_initial_metadata_error = GRPC_ERROR_REF(error);
1670 if (!retry_state->started_recv_trailing_metadata) {
1671 // recv_trailing_metadata not yet started by application; start it
1672 // ourselves to get status.
1673 start_internal_recv_trailing_metadata(elem);
1674 } else {
1675 GRPC_CALL_COMBINER_STOP(
1676 calld->call_combiner,
1677 "recv_initial_metadata_ready trailers-only or error");
1678 }
1679 return;
1680 }
1681 // Received valid initial metadata, so commit the call.
1682 retry_commit(elem, retry_state);
1683 // Manually invoking a callback function; it does not take ownership of error.
1684 invoke_recv_initial_metadata_callback(batch_data, error);
1685 GRPC_ERROR_UNREF(error);
1686}
1687
1688//
1689// recv_message callback handling
1690//
1691
1692// Invokes recv_message_ready for a subchannel batch.
1693static void invoke_recv_message_callback(void* arg, grpc_error* error) {
1694 subchannel_batch_data* batch_data = static_cast<subchannel_batch_data*>(arg);
1695 channel_data* chand =
1696 static_cast<channel_data*>(batch_data->elem->channel_data);
1697 call_data* calld = static_cast<call_data*>(batch_data->elem->call_data);
1698 // Find pending op.
1699 pending_batch* pending = nullptr;
1700 for (size_t i = 0; i < GPR_ARRAY_SIZE(calld->pending_batches); ++i) {
1701 grpc_transport_stream_op_batch* batch = calld->pending_batches[i].batch;
1702 if (batch != nullptr && batch->recv_message &&
1703 batch->payload->recv_message.recv_message_ready != nullptr) {
1704 if (grpc_client_channel_trace.enabled()) {
Mark D. Roth48854d22018-04-25 13:05:26 -07001705 gpr_log(GPR_INFO,
Mark D. Roth718c8342018-02-28 13:00:04 -08001706 "chand=%p calld=%p: invoking recv_message_ready for "
1707 "pending batch at index %" PRIuPTR,
1708 chand, calld, i);
1709 }
1710 pending = &calld->pending_batches[i];
1711 break;
1712 }
1713 }
1714 GPR_ASSERT(pending != nullptr);
1715 // Return payload.
1716 *pending->batch->payload->recv_message.recv_message =
Mark D. Roth3d8b32d2018-03-09 13:25:40 -08001717 std::move(batch_data->recv_message);
Mark D. Roth718c8342018-02-28 13:00:04 -08001718 // Update bookkeeping.
1719 // Note: Need to do this before invoking the callback, since invoking
1720 // the callback will result in yielding the call combiner.
1721 grpc_closure* recv_message_ready =
1722 pending->batch->payload->recv_message.recv_message_ready;
1723 pending->batch->payload->recv_message.recv_message_ready = nullptr;
1724 maybe_clear_pending_batch(batch_data->elem, pending);
1725 batch_data_unref(batch_data);
1726 // Invoke callback.
1727 GRPC_CLOSURE_RUN(recv_message_ready, GRPC_ERROR_REF(error));
1728}
1729
1730// Intercepts recv_message_ready callback for retries.
1731// Commits the call and returns the message up the stack.
1732static void recv_message_ready(void* arg, grpc_error* error) {
1733 subchannel_batch_data* batch_data = static_cast<subchannel_batch_data*>(arg);
1734 grpc_call_element* elem = batch_data->elem;
1735 channel_data* chand = static_cast<channel_data*>(elem->channel_data);
1736 call_data* calld = static_cast<call_data*>(elem->call_data);
1737 if (grpc_client_channel_trace.enabled()) {
Mark D. Roth48854d22018-04-25 13:05:26 -07001738 gpr_log(GPR_INFO, "chand=%p calld=%p: got recv_message_ready, error=%s",
Mark D. Roth718c8342018-02-28 13:00:04 -08001739 chand, calld, grpc_error_string(error));
1740 }
1741 subchannel_call_retry_state* retry_state =
1742 static_cast<subchannel_call_retry_state*>(
1743 grpc_connected_subchannel_call_get_parent_data(
1744 batch_data->subchannel_call));
1745 // If we got an error or the payload was nullptr and we have not yet gotten
1746 // the recv_trailing_metadata on_complete callback, then defer
1747 // propagating this callback back to the surface. We can evaluate whether
1748 // to retry when recv_trailing_metadata comes back.
Yash Tibrewal8c5b8832018-05-14 11:44:18 -07001749 if (GPR_UNLIKELY(
1750 (batch_data->recv_message == nullptr || error != GRPC_ERROR_NONE) &&
1751 !retry_state->completed_recv_trailing_metadata)) {
Mark D. Roth718c8342018-02-28 13:00:04 -08001752 if (grpc_client_channel_trace.enabled()) {
Mark D. Roth48854d22018-04-25 13:05:26 -07001753 gpr_log(GPR_INFO,
Mark D. Roth718c8342018-02-28 13:00:04 -08001754 "chand=%p calld=%p: deferring recv_message_ready (nullptr "
1755 "message and recv_trailing_metadata pending)",
1756 chand, calld);
1757 }
Mark D. Rothde077ac2018-04-12 08:05:44 -07001758 retry_state->recv_message_ready_deferred_batch = batch_data;
Mark D. Roth718c8342018-02-28 13:00:04 -08001759 retry_state->recv_message_error = GRPC_ERROR_REF(error);
1760 if (!retry_state->started_recv_trailing_metadata) {
1761 // recv_trailing_metadata not yet started by application; start it
1762 // ourselves to get status.
1763 start_internal_recv_trailing_metadata(elem);
1764 } else {
1765 GRPC_CALL_COMBINER_STOP(calld->call_combiner, "recv_message_ready null");
1766 }
1767 return;
1768 }
1769 // Received a valid message, so commit the call.
1770 retry_commit(elem, retry_state);
1771 // Manually invoking a callback function; it does not take ownership of error.
1772 invoke_recv_message_callback(batch_data, error);
1773 GRPC_ERROR_UNREF(error);
1774}
1775
1776//
Mark D. Rothde077ac2018-04-12 08:05:44 -07001777// list of closures to execute in call combiner
1778//
1779
1780// Represents a closure that needs to run in the call combiner as part of
1781// starting or completing a batch.
1782typedef struct {
1783 grpc_closure* closure;
1784 grpc_error* error;
1785 const char* reason;
1786 bool free_reason = false;
1787} closure_to_execute;
1788
1789static void execute_closures_in_call_combiner(grpc_call_element* elem,
1790 const char* caller,
1791 closure_to_execute* closures,
1792 size_t num_closures) {
1793 channel_data* chand = static_cast<channel_data*>(elem->channel_data);
1794 call_data* calld = static_cast<call_data*>(elem->call_data);
1795 // Note that the call combiner will be yielded for each closure that
1796 // we schedule. We're already running in the call combiner, so one of
1797 // the closures can be scheduled directly, but the others will
1798 // have to re-enter the call combiner.
1799 if (num_closures > 0) {
1800 if (grpc_client_channel_trace.enabled()) {
Mark D. Roth48854d22018-04-25 13:05:26 -07001801 gpr_log(GPR_INFO, "chand=%p calld=%p: %s starting closure: %s", chand,
Mark D. Rothde077ac2018-04-12 08:05:44 -07001802 calld, caller, closures[0].reason);
1803 }
1804 GRPC_CLOSURE_SCHED(closures[0].closure, closures[0].error);
1805 if (closures[0].free_reason) {
1806 gpr_free(const_cast<char*>(closures[0].reason));
1807 }
1808 for (size_t i = 1; i < num_closures; ++i) {
1809 if (grpc_client_channel_trace.enabled()) {
Mark D. Roth48854d22018-04-25 13:05:26 -07001810 gpr_log(GPR_INFO,
Mark D. Rothde077ac2018-04-12 08:05:44 -07001811 "chand=%p calld=%p: %s starting closure in call combiner: %s",
1812 chand, calld, caller, closures[i].reason);
1813 }
1814 GRPC_CALL_COMBINER_START(calld->call_combiner, closures[i].closure,
1815 closures[i].error, closures[i].reason);
1816 if (closures[i].free_reason) {
1817 gpr_free(const_cast<char*>(closures[i].reason));
1818 }
1819 }
1820 } else {
1821 if (grpc_client_channel_trace.enabled()) {
Mark D. Roth48854d22018-04-25 13:05:26 -07001822 gpr_log(GPR_INFO, "chand=%p calld=%p: no closures to run for %s", chand,
Mark D. Rothde077ac2018-04-12 08:05:44 -07001823 calld, caller);
1824 }
1825 GRPC_CALL_COMBINER_STOP(calld->call_combiner, "no closures to run");
1826 }
1827}
1828
1829//
Mark D. Roth718c8342018-02-28 13:00:04 -08001830// on_complete callback handling
1831//
1832
1833// Updates retry_state to reflect the ops completed in batch_data.
1834static void update_retry_state_for_completed_batch(
1835 subchannel_batch_data* batch_data,
1836 subchannel_call_retry_state* retry_state) {
1837 if (batch_data->batch.send_initial_metadata) {
1838 retry_state->completed_send_initial_metadata = true;
1839 }
1840 if (batch_data->batch.send_message) {
1841 ++retry_state->completed_send_message_count;
1842 }
1843 if (batch_data->batch.send_trailing_metadata) {
1844 retry_state->completed_send_trailing_metadata = true;
1845 }
1846 if (batch_data->batch.recv_initial_metadata) {
1847 retry_state->completed_recv_initial_metadata = true;
1848 }
1849 if (batch_data->batch.recv_message) {
1850 ++retry_state->completed_recv_message_count;
1851 }
1852 if (batch_data->batch.recv_trailing_metadata) {
1853 retry_state->completed_recv_trailing_metadata = true;
1854 }
1855}
1856
Mark D. Roth718c8342018-02-28 13:00:04 -08001857// Adds any necessary closures for deferred recv_initial_metadata and
1858// recv_message callbacks to closures, updating *num_closures as needed.
1859static void add_closures_for_deferred_recv_callbacks(
1860 subchannel_batch_data* batch_data, subchannel_call_retry_state* retry_state,
1861 closure_to_execute* closures, size_t* num_closures) {
Mark D. Rothde077ac2018-04-12 08:05:44 -07001862 if (batch_data->batch.recv_trailing_metadata) {
1863 // Add closure for deferred recv_initial_metadata_ready.
1864 if (retry_state->recv_initial_metadata_ready_deferred_batch != nullptr) {
1865 closure_to_execute* closure = &closures[(*num_closures)++];
1866 closure->closure = GRPC_CLOSURE_INIT(
1867 &batch_data->recv_initial_metadata_ready,
1868 invoke_recv_initial_metadata_callback,
1869 retry_state->recv_initial_metadata_ready_deferred_batch,
1870 grpc_schedule_on_exec_ctx);
1871 closure->error = retry_state->recv_initial_metadata_error;
1872 closure->reason = "resuming recv_initial_metadata_ready";
1873 retry_state->recv_initial_metadata_ready_deferred_batch = nullptr;
1874 }
1875 // Add closure for deferred recv_message_ready.
Yash Tibrewal8c5b8832018-05-14 11:44:18 -07001876 if (GPR_UNLIKELY(retry_state->recv_message_ready_deferred_batch !=
1877 nullptr)) {
Mark D. Rothde077ac2018-04-12 08:05:44 -07001878 closure_to_execute* closure = &closures[(*num_closures)++];
1879 closure->closure = GRPC_CLOSURE_INIT(
1880 &batch_data->recv_message_ready, invoke_recv_message_callback,
1881 retry_state->recv_message_ready_deferred_batch,
1882 grpc_schedule_on_exec_ctx);
1883 closure->error = retry_state->recv_message_error;
1884 closure->reason = "resuming recv_message_ready";
1885 retry_state->recv_message_ready_deferred_batch = nullptr;
1886 }
Mark D. Roth718c8342018-02-28 13:00:04 -08001887 }
1888}
1889
1890// If there are any cached ops to replay or pending ops to start on the
1891// subchannel call, adds a closure to closures to invoke
1892// start_retriable_subchannel_batches(), updating *num_closures as needed.
1893static void add_closures_for_replay_or_pending_send_ops(
1894 grpc_call_element* elem, subchannel_batch_data* batch_data,
1895 subchannel_call_retry_state* retry_state, closure_to_execute* closures,
1896 size_t* num_closures) {
1897 channel_data* chand = static_cast<channel_data*>(elem->channel_data);
1898 call_data* calld = static_cast<call_data*>(elem->call_data);
1899 bool have_pending_send_message_ops =
Mark D. Rothefcd45b2018-03-28 10:49:59 -07001900 retry_state->started_send_message_count < calld->send_messages->size();
Mark D. Roth718c8342018-02-28 13:00:04 -08001901 bool have_pending_send_trailing_metadata_op =
1902 calld->seen_send_trailing_metadata &&
1903 !retry_state->started_send_trailing_metadata;
1904 if (!have_pending_send_message_ops &&
1905 !have_pending_send_trailing_metadata_op) {
1906 for (size_t i = 0; i < GPR_ARRAY_SIZE(calld->pending_batches); ++i) {
1907 pending_batch* pending = &calld->pending_batches[i];
1908 grpc_transport_stream_op_batch* batch = pending->batch;
1909 if (batch == nullptr || pending->send_ops_cached) continue;
1910 if (batch->send_message) have_pending_send_message_ops = true;
1911 if (batch->send_trailing_metadata) {
1912 have_pending_send_trailing_metadata_op = true;
1913 }
1914 }
1915 }
1916 if (have_pending_send_message_ops || have_pending_send_trailing_metadata_op) {
1917 if (grpc_client_channel_trace.enabled()) {
Mark D. Roth48854d22018-04-25 13:05:26 -07001918 gpr_log(GPR_INFO,
Mark D. Roth718c8342018-02-28 13:00:04 -08001919 "chand=%p calld=%p: starting next batch for pending send op(s)",
1920 chand, calld);
1921 }
1922 closure_to_execute* closure = &closures[(*num_closures)++];
1923 closure->closure = GRPC_CLOSURE_INIT(
1924 &batch_data->batch.handler_private.closure,
1925 start_retriable_subchannel_batches, elem, grpc_schedule_on_exec_ctx);
1926 closure->error = GRPC_ERROR_NONE;
1927 closure->reason = "starting next batch for send_* op(s)";
1928 }
1929}
1930
1931// For any pending batch completed in batch_data, adds the necessary
1932// completion closures to closures, updating *num_closures as needed.
1933static void add_closures_for_completed_pending_batches(
1934 grpc_call_element* elem, subchannel_batch_data* batch_data,
1935 subchannel_call_retry_state* retry_state, grpc_error* error,
1936 closure_to_execute* closures, size_t* num_closures) {
1937 channel_data* chand = static_cast<channel_data*>(elem->channel_data);
1938 call_data* calld = static_cast<call_data*>(elem->call_data);
1939 for (size_t i = 0; i < GPR_ARRAY_SIZE(calld->pending_batches); ++i) {
1940 pending_batch* pending = &calld->pending_batches[i];
1941 if (pending_batch_is_completed(pending, calld, retry_state)) {
1942 if (grpc_client_channel_trace.enabled()) {
Mark D. Roth48854d22018-04-25 13:05:26 -07001943 gpr_log(GPR_INFO,
Mark D. Roth718c8342018-02-28 13:00:04 -08001944 "chand=%p calld=%p: pending batch completed at index %" PRIuPTR,
1945 chand, calld, i);
1946 }
1947 // Copy the trailing metadata to return it to the surface.
1948 if (batch_data->batch.recv_trailing_metadata) {
1949 grpc_metadata_batch_move(&batch_data->recv_trailing_metadata,
1950 pending->batch->payload->recv_trailing_metadata
1951 .recv_trailing_metadata);
1952 }
1953 closure_to_execute* closure = &closures[(*num_closures)++];
1954 closure->closure = pending->batch->on_complete;
1955 closure->error = GRPC_ERROR_REF(error);
1956 closure->reason = "on_complete for pending batch";
1957 pending->batch->on_complete = nullptr;
1958 maybe_clear_pending_batch(elem, pending);
1959 }
1960 }
1961 GRPC_ERROR_UNREF(error);
1962}
1963
1964// For any pending batch containing an op that has not yet been started,
1965// adds the pending batch's completion closures to closures, updating
1966// *num_closures as needed.
1967static void add_closures_to_fail_unstarted_pending_batches(
1968 grpc_call_element* elem, subchannel_call_retry_state* retry_state,
1969 grpc_error* error, closure_to_execute* closures, size_t* num_closures) {
1970 channel_data* chand = static_cast<channel_data*>(elem->channel_data);
1971 call_data* calld = static_cast<call_data*>(elem->call_data);
1972 for (size_t i = 0; i < GPR_ARRAY_SIZE(calld->pending_batches); ++i) {
1973 pending_batch* pending = &calld->pending_batches[i];
1974 if (pending_batch_is_unstarted(pending, calld, retry_state)) {
1975 if (grpc_client_channel_trace.enabled()) {
Mark D. Roth48854d22018-04-25 13:05:26 -07001976 gpr_log(GPR_INFO,
Mark D. Roth718c8342018-02-28 13:00:04 -08001977 "chand=%p calld=%p: failing unstarted pending batch at index "
1978 "%" PRIuPTR,
1979 chand, calld, i);
1980 }
1981 if (pending->batch->recv_initial_metadata) {
1982 closure_to_execute* closure = &closures[(*num_closures)++];
1983 closure->closure = pending->batch->payload->recv_initial_metadata
1984 .recv_initial_metadata_ready;
1985 closure->error = GRPC_ERROR_REF(error);
1986 closure->reason =
1987 "failing recv_initial_metadata_ready for pending batch";
1988 pending->batch->payload->recv_initial_metadata
1989 .recv_initial_metadata_ready = nullptr;
1990 }
1991 if (pending->batch->recv_message) {
1992 *pending->batch->payload->recv_message.recv_message = nullptr;
1993 closure_to_execute* closure = &closures[(*num_closures)++];
1994 closure->closure =
1995 pending->batch->payload->recv_message.recv_message_ready;
1996 closure->error = GRPC_ERROR_REF(error);
1997 closure->reason = "failing recv_message_ready for pending batch";
1998 pending->batch->payload->recv_message.recv_message_ready = nullptr;
1999 }
2000 closure_to_execute* closure = &closures[(*num_closures)++];
2001 closure->closure = pending->batch->on_complete;
2002 closure->error = GRPC_ERROR_REF(error);
2003 closure->reason = "failing on_complete for pending batch";
2004 pending->batch->on_complete = nullptr;
2005 maybe_clear_pending_batch(elem, pending);
2006 }
2007 }
2008 GRPC_ERROR_UNREF(error);
2009}
2010
2011// Callback used to intercept on_complete from subchannel calls.
2012// Called only when retries are enabled.
2013static void on_complete(void* arg, grpc_error* error) {
2014 subchannel_batch_data* batch_data = static_cast<subchannel_batch_data*>(arg);
2015 grpc_call_element* elem = batch_data->elem;
2016 channel_data* chand = static_cast<channel_data*>(elem->channel_data);
2017 call_data* calld = static_cast<call_data*>(elem->call_data);
2018 if (grpc_client_channel_trace.enabled()) {
2019 char* batch_str = grpc_transport_stream_op_batch_string(&batch_data->batch);
Mark D. Roth48854d22018-04-25 13:05:26 -07002020 gpr_log(GPR_INFO, "chand=%p calld=%p: got on_complete, error=%s, batch=%s",
Mark D. Roth718c8342018-02-28 13:00:04 -08002021 chand, calld, grpc_error_string(error), batch_str);
2022 gpr_free(batch_str);
2023 }
2024 subchannel_call_retry_state* retry_state =
2025 static_cast<subchannel_call_retry_state*>(
2026 grpc_connected_subchannel_call_get_parent_data(
2027 batch_data->subchannel_call));
2028 // If we have previously completed recv_trailing_metadata, then the
2029 // call is finished.
2030 bool call_finished = retry_state->completed_recv_trailing_metadata;
Mark D. Rothde077ac2018-04-12 08:05:44 -07002031 // Record whether we were already committed before receiving this callback.
2032 const bool previously_committed = calld->retry_committed;
Mark D. Roth718c8342018-02-28 13:00:04 -08002033 // Update bookkeeping in retry_state.
2034 update_retry_state_for_completed_batch(batch_data, retry_state);
2035 if (call_finished) {
2036 if (grpc_client_channel_trace.enabled()) {
Mark D. Roth48854d22018-04-25 13:05:26 -07002037 gpr_log(GPR_INFO, "chand=%p calld=%p: call already finished", chand,
Mark D. Roth718c8342018-02-28 13:00:04 -08002038 calld);
2039 }
2040 } else {
2041 // Check if this batch finished the call, and if so, get its status.
2042 // The call is finished if either (a) this callback was invoked with
2043 // an error or (b) we receive status.
2044 grpc_status_code status = GRPC_STATUS_OK;
2045 grpc_mdelem* server_pushback_md = nullptr;
Yash Tibrewal8c5b8832018-05-14 11:44:18 -07002046 if (GPR_UNLIKELY(error != GRPC_ERROR_NONE)) { // Case (a).
Mark D. Roth718c8342018-02-28 13:00:04 -08002047 call_finished = true;
2048 grpc_error_get_status(error, calld->deadline, &status, nullptr, nullptr,
2049 nullptr);
2050 } else if (batch_data->batch.recv_trailing_metadata) { // Case (b).
2051 call_finished = true;
2052 grpc_metadata_batch* md_batch =
2053 batch_data->batch.payload->recv_trailing_metadata
2054 .recv_trailing_metadata;
2055 GPR_ASSERT(md_batch->idx.named.grpc_status != nullptr);
2056 status = grpc_get_status_code_from_metadata(
2057 md_batch->idx.named.grpc_status->md);
2058 if (md_batch->idx.named.grpc_retry_pushback_ms != nullptr) {
2059 server_pushback_md = &md_batch->idx.named.grpc_retry_pushback_ms->md;
2060 }
Mark D. Roth718c8342018-02-28 13:00:04 -08002061 }
Mark D. Rothde077ac2018-04-12 08:05:44 -07002062 // If the call just finished, check if we should retry.
2063 if (call_finished) {
2064 if (grpc_client_channel_trace.enabled()) {
Mark D. Roth48854d22018-04-25 13:05:26 -07002065 gpr_log(GPR_INFO, "chand=%p calld=%p: call finished, status=%s", chand,
Mark D. Rothde077ac2018-04-12 08:05:44 -07002066 calld, grpc_status_code_to_string(status));
Mark D. Roth718c8342018-02-28 13:00:04 -08002067 }
Mark D. Rothde077ac2018-04-12 08:05:44 -07002068 if (maybe_retry(elem, batch_data, status, server_pushback_md)) {
2069 // Unref batch_data for deferred recv_initial_metadata_ready or
2070 // recv_message_ready callbacks, if any.
2071 if (batch_data->batch.recv_trailing_metadata &&
2072 retry_state->recv_initial_metadata_ready_deferred_batch !=
2073 nullptr) {
2074 batch_data_unref(batch_data);
2075 GRPC_ERROR_UNREF(retry_state->recv_initial_metadata_error);
2076 }
2077 if (batch_data->batch.recv_trailing_metadata &&
2078 retry_state->recv_message_ready_deferred_batch != nullptr) {
2079 batch_data_unref(batch_data);
2080 GRPC_ERROR_UNREF(retry_state->recv_message_error);
2081 }
Mark D. Roth718c8342018-02-28 13:00:04 -08002082 batch_data_unref(batch_data);
Mark D. Rothde077ac2018-04-12 08:05:44 -07002083 return;
Mark D. Roth718c8342018-02-28 13:00:04 -08002084 }
Mark D. Rothde077ac2018-04-12 08:05:44 -07002085 // Not retrying, so commit the call.
2086 retry_commit(elem, retry_state);
Mark D. Roth718c8342018-02-28 13:00:04 -08002087 }
2088 }
Mark D. Rothde077ac2018-04-12 08:05:44 -07002089 // If we were already committed before receiving this callback, free
2090 // cached data for send ops that we've just completed. (If the call has
2091 // just now finished, the call to retry_commit() above will have freed all
2092 // cached send ops, so we don't need to do it here.)
2093 if (previously_committed) {
Mark D. Roth718c8342018-02-28 13:00:04 -08002094 free_cached_send_op_data_for_completed_batch(elem, batch_data, retry_state);
2095 }
2096 // Call not being retried.
2097 // Construct list of closures to execute.
2098 // Max number of closures is number of pending batches plus one for
2099 // each of:
2100 // - recv_initial_metadata_ready (either deferred or unstarted)
2101 // - recv_message_ready (either deferred or unstarted)
2102 // - starting a new batch for pending send ops
2103 closure_to_execute closures[GPR_ARRAY_SIZE(calld->pending_batches) + 3];
2104 size_t num_closures = 0;
2105 // If there are deferred recv_initial_metadata_ready or recv_message_ready
2106 // callbacks, add them to closures.
2107 add_closures_for_deferred_recv_callbacks(batch_data, retry_state, closures,
2108 &num_closures);
2109 // Find pending batches whose ops are now complete and add their
2110 // on_complete callbacks to closures.
2111 add_closures_for_completed_pending_batches(elem, batch_data, retry_state,
2112 GRPC_ERROR_REF(error), closures,
2113 &num_closures);
2114 // Add closures to handle any pending batches that have not yet been started.
2115 // If the call is finished, we fail these batches; otherwise, we add a
2116 // callback to start_retriable_subchannel_batches() to start them on
2117 // the subchannel call.
2118 if (call_finished) {
2119 add_closures_to_fail_unstarted_pending_batches(
2120 elem, retry_state, GRPC_ERROR_REF(error), closures, &num_closures);
2121 } else {
2122 add_closures_for_replay_or_pending_send_ops(elem, batch_data, retry_state,
2123 closures, &num_closures);
2124 }
2125 // Don't need batch_data anymore.
2126 batch_data_unref(batch_data);
2127 // Schedule all of the closures identified above.
Mark D. Rothde077ac2018-04-12 08:05:44 -07002128 execute_closures_in_call_combiner(elem, "on_complete", closures,
2129 num_closures);
Mark D. Roth718c8342018-02-28 13:00:04 -08002130}
2131
2132//
2133// subchannel batch construction
2134//
2135
2136// Helper function used to start a subchannel batch in the call combiner.
2137static void start_batch_in_call_combiner(void* arg, grpc_error* ignored) {
2138 grpc_transport_stream_op_batch* batch =
2139 static_cast<grpc_transport_stream_op_batch*>(arg);
2140 grpc_subchannel_call* subchannel_call =
2141 static_cast<grpc_subchannel_call*>(batch->handler_private.extra_arg);
2142 // Note: This will release the call combiner.
2143 grpc_subchannel_call_process_op(subchannel_call, batch);
2144}
2145
Mark D. Rothde077ac2018-04-12 08:05:44 -07002146// Adds a closure to closures that will execute batch in the call combiner.
2147static void add_closure_for_subchannel_batch(
2148 call_data* calld, grpc_transport_stream_op_batch* batch,
2149 closure_to_execute* closures, size_t* num_closures) {
2150 batch->handler_private.extra_arg = calld->subchannel_call;
2151 GRPC_CLOSURE_INIT(&batch->handler_private.closure,
2152 start_batch_in_call_combiner, batch,
2153 grpc_schedule_on_exec_ctx);
2154 closure_to_execute* closure = &closures[(*num_closures)++];
2155 closure->closure = &batch->handler_private.closure;
2156 closure->error = GRPC_ERROR_NONE;
2157 // If the tracer is enabled, we log a more detailed message, which
2158 // requires dynamic allocation. This will be freed in
2159 // start_retriable_subchannel_batches().
2160 if (grpc_client_channel_trace.enabled()) {
2161 char* batch_str = grpc_transport_stream_op_batch_string(batch);
2162 gpr_asprintf(const_cast<char**>(&closure->reason),
2163 "starting batch in call combiner: %s", batch_str);
2164 gpr_free(batch_str);
2165 closure->free_reason = true;
2166 } else {
2167 closure->reason = "start_subchannel_batch";
2168 }
2169}
2170
Mark D. Roth718c8342018-02-28 13:00:04 -08002171// Adds retriable send_initial_metadata op to batch_data.
2172static void add_retriable_send_initial_metadata_op(
2173 call_data* calld, subchannel_call_retry_state* retry_state,
2174 subchannel_batch_data* batch_data) {
2175 // Maps the number of retries to the corresponding metadata value slice.
2176 static const grpc_slice* retry_count_strings[] = {
2177 &GRPC_MDSTR_1, &GRPC_MDSTR_2, &GRPC_MDSTR_3, &GRPC_MDSTR_4};
2178 // We need to make a copy of the metadata batch for each attempt, since
2179 // the filters in the subchannel stack may modify this batch, and we don't
2180 // want those modifications to be passed forward to subsequent attempts.
2181 //
2182 // If we've already completed one or more attempts, add the
2183 // grpc-retry-attempts header.
2184 batch_data->send_initial_metadata_storage =
2185 static_cast<grpc_linked_mdelem*>(gpr_arena_alloc(
2186 calld->arena, sizeof(grpc_linked_mdelem) *
2187 (calld->send_initial_metadata.list.count +
2188 (calld->num_attempts_completed > 0))));
2189 grpc_metadata_batch_copy(&calld->send_initial_metadata,
2190 &batch_data->send_initial_metadata,
2191 batch_data->send_initial_metadata_storage);
Yash Tibrewal8c5b8832018-05-14 11:44:18 -07002192 if (GPR_UNLIKELY(batch_data->send_initial_metadata.idx.named
2193 .grpc_previous_rpc_attempts != nullptr)) {
Mark D. Roth718c8342018-02-28 13:00:04 -08002194 grpc_metadata_batch_remove(
2195 &batch_data->send_initial_metadata,
2196 batch_data->send_initial_metadata.idx.named.grpc_previous_rpc_attempts);
2197 }
Yash Tibrewal8c5b8832018-05-14 11:44:18 -07002198 if (GPR_UNLIKELY(calld->num_attempts_completed > 0)) {
Mark D. Roth718c8342018-02-28 13:00:04 -08002199 grpc_mdelem retry_md = grpc_mdelem_from_slices(
2200 GRPC_MDSTR_GRPC_PREVIOUS_RPC_ATTEMPTS,
2201 *retry_count_strings[calld->num_attempts_completed - 1]);
2202 grpc_error* error = grpc_metadata_batch_add_tail(
2203 &batch_data->send_initial_metadata,
2204 &batch_data->send_initial_metadata_storage[calld->send_initial_metadata
2205 .list.count],
2206 retry_md);
Yash Tibrewal7f51ba82018-04-12 13:21:20 -07002207 if (GPR_UNLIKELY(error != GRPC_ERROR_NONE)) {
Mark D. Roth718c8342018-02-28 13:00:04 -08002208 gpr_log(GPR_ERROR, "error adding retry metadata: %s",
2209 grpc_error_string(error));
2210 GPR_ASSERT(false);
2211 }
2212 }
2213 retry_state->started_send_initial_metadata = true;
2214 batch_data->batch.send_initial_metadata = true;
2215 batch_data->batch.payload->send_initial_metadata.send_initial_metadata =
2216 &batch_data->send_initial_metadata;
2217 batch_data->batch.payload->send_initial_metadata.send_initial_metadata_flags =
2218 calld->send_initial_metadata_flags;
2219 batch_data->batch.payload->send_initial_metadata.peer_string =
2220 calld->peer_string;
2221}
2222
2223// Adds retriable send_message op to batch_data.
2224static void add_retriable_send_message_op(
2225 grpc_call_element* elem, subchannel_call_retry_state* retry_state,
2226 subchannel_batch_data* batch_data) {
2227 channel_data* chand = static_cast<channel_data*>(elem->channel_data);
2228 call_data* calld = static_cast<call_data*>(elem->call_data);
2229 if (grpc_client_channel_trace.enabled()) {
Mark D. Roth48854d22018-04-25 13:05:26 -07002230 gpr_log(GPR_INFO,
Mark D. Roth718c8342018-02-28 13:00:04 -08002231 "chand=%p calld=%p: starting calld->send_messages[%" PRIuPTR "]",
2232 chand, calld, retry_state->started_send_message_count);
2233 }
Mark D. Roth3d8b32d2018-03-09 13:25:40 -08002234 grpc_core::ByteStreamCache* cache =
Mark D. Rothefcd45b2018-03-28 10:49:59 -07002235 (*calld->send_messages)[retry_state->started_send_message_count];
Mark D. Roth718c8342018-02-28 13:00:04 -08002236 ++retry_state->started_send_message_count;
Mark D. Roth3d8b32d2018-03-09 13:25:40 -08002237 batch_data->send_message.Init(cache);
Mark D. Roth718c8342018-02-28 13:00:04 -08002238 batch_data->batch.send_message = true;
Mark D. Roth3d8b32d2018-03-09 13:25:40 -08002239 batch_data->batch.payload->send_message.send_message.reset(
2240 batch_data->send_message.get());
Mark D. Roth718c8342018-02-28 13:00:04 -08002241}
2242
2243// Adds retriable send_trailing_metadata op to batch_data.
2244static void add_retriable_send_trailing_metadata_op(
2245 call_data* calld, subchannel_call_retry_state* retry_state,
2246 subchannel_batch_data* batch_data) {
2247 // We need to make a copy of the metadata batch for each attempt, since
2248 // the filters in the subchannel stack may modify this batch, and we don't
2249 // want those modifications to be passed forward to subsequent attempts.
2250 batch_data->send_trailing_metadata_storage =
2251 static_cast<grpc_linked_mdelem*>(gpr_arena_alloc(
2252 calld->arena, sizeof(grpc_linked_mdelem) *
2253 calld->send_trailing_metadata.list.count));
2254 grpc_metadata_batch_copy(&calld->send_trailing_metadata,
2255 &batch_data->send_trailing_metadata,
2256 batch_data->send_trailing_metadata_storage);
2257 retry_state->started_send_trailing_metadata = true;
2258 batch_data->batch.send_trailing_metadata = true;
2259 batch_data->batch.payload->send_trailing_metadata.send_trailing_metadata =
2260 &batch_data->send_trailing_metadata;
2261}
2262
2263// Adds retriable recv_initial_metadata op to batch_data.
2264static void add_retriable_recv_initial_metadata_op(
2265 call_data* calld, subchannel_call_retry_state* retry_state,
2266 subchannel_batch_data* batch_data) {
2267 retry_state->started_recv_initial_metadata = true;
2268 batch_data->batch.recv_initial_metadata = true;
2269 grpc_metadata_batch_init(&batch_data->recv_initial_metadata);
2270 batch_data->batch.payload->recv_initial_metadata.recv_initial_metadata =
2271 &batch_data->recv_initial_metadata;
2272 batch_data->batch.payload->recv_initial_metadata.trailing_metadata_available =
2273 &batch_data->trailing_metadata_available;
2274 GRPC_CLOSURE_INIT(&batch_data->recv_initial_metadata_ready,
2275 recv_initial_metadata_ready, batch_data,
2276 grpc_schedule_on_exec_ctx);
2277 batch_data->batch.payload->recv_initial_metadata.recv_initial_metadata_ready =
2278 &batch_data->recv_initial_metadata_ready;
2279}
2280
2281// Adds retriable recv_message op to batch_data.
2282static void add_retriable_recv_message_op(
2283 call_data* calld, subchannel_call_retry_state* retry_state,
2284 subchannel_batch_data* batch_data) {
2285 ++retry_state->started_recv_message_count;
2286 batch_data->batch.recv_message = true;
2287 batch_data->batch.payload->recv_message.recv_message =
2288 &batch_data->recv_message;
2289 GRPC_CLOSURE_INIT(&batch_data->recv_message_ready, recv_message_ready,
2290 batch_data, grpc_schedule_on_exec_ctx);
2291 batch_data->batch.payload->recv_message.recv_message_ready =
2292 &batch_data->recv_message_ready;
2293}
2294
2295// Adds retriable recv_trailing_metadata op to batch_data.
2296static void add_retriable_recv_trailing_metadata_op(
2297 call_data* calld, subchannel_call_retry_state* retry_state,
2298 subchannel_batch_data* batch_data) {
2299 retry_state->started_recv_trailing_metadata = true;
2300 batch_data->batch.recv_trailing_metadata = true;
2301 grpc_metadata_batch_init(&batch_data->recv_trailing_metadata);
2302 batch_data->batch.payload->recv_trailing_metadata.recv_trailing_metadata =
2303 &batch_data->recv_trailing_metadata;
2304 batch_data->batch.collect_stats = true;
2305 batch_data->batch.payload->collect_stats.collect_stats =
2306 &batch_data->collect_stats;
2307}
2308
2309// Helper function used to start a recv_trailing_metadata batch. This
2310// is used in the case where a recv_initial_metadata or recv_message
2311// op fails in a way that we know the call is over but when the application
2312// has not yet started its own recv_trailing_metadata op.
2313static void start_internal_recv_trailing_metadata(grpc_call_element* elem) {
2314 channel_data* chand = static_cast<channel_data*>(elem->channel_data);
2315 call_data* calld = static_cast<call_data*>(elem->call_data);
2316 if (grpc_client_channel_trace.enabled()) {
Mark D. Roth48854d22018-04-25 13:05:26 -07002317 gpr_log(GPR_INFO,
Mark D. Roth718c8342018-02-28 13:00:04 -08002318 "chand=%p calld=%p: call failed but recv_trailing_metadata not "
2319 "started; starting it internally",
2320 chand, calld);
2321 }
2322 subchannel_call_retry_state* retry_state =
2323 static_cast<subchannel_call_retry_state*>(
2324 grpc_connected_subchannel_call_get_parent_data(
2325 calld->subchannel_call));
Mark D. Rothde077ac2018-04-12 08:05:44 -07002326 // Create batch_data with 2 refs, since this batch will be unreffed twice:
2327 // once when the subchannel batch returns, and again when we actually get
2328 // a recv_trailing_metadata op from the surface.
2329 subchannel_batch_data* batch_data = batch_data_create(elem, 2);
Mark D. Roth718c8342018-02-28 13:00:04 -08002330 add_retriable_recv_trailing_metadata_op(calld, retry_state, batch_data);
Mark D. Rothde077ac2018-04-12 08:05:44 -07002331 retry_state->recv_trailing_metadata_internal_batch = batch_data;
Mark D. Roth718c8342018-02-28 13:00:04 -08002332 // Note: This will release the call combiner.
2333 grpc_subchannel_call_process_op(calld->subchannel_call, &batch_data->batch);
2334}
2335
2336// If there are any cached send ops that need to be replayed on the
2337// current subchannel call, creates and returns a new subchannel batch
2338// to replay those ops. Otherwise, returns nullptr.
2339static subchannel_batch_data* maybe_create_subchannel_batch_for_replay(
2340 grpc_call_element* elem, subchannel_call_retry_state* retry_state) {
2341 channel_data* chand = static_cast<channel_data*>(elem->channel_data);
2342 call_data* calld = static_cast<call_data*>(elem->call_data);
2343 subchannel_batch_data* replay_batch_data = nullptr;
2344 // send_initial_metadata.
2345 if (calld->seen_send_initial_metadata &&
2346 !retry_state->started_send_initial_metadata &&
2347 !calld->pending_send_initial_metadata) {
2348 if (grpc_client_channel_trace.enabled()) {
Mark D. Roth48854d22018-04-25 13:05:26 -07002349 gpr_log(GPR_INFO,
Mark D. Roth718c8342018-02-28 13:00:04 -08002350 "chand=%p calld=%p: replaying previously completed "
2351 "send_initial_metadata op",
2352 chand, calld);
2353 }
2354 replay_batch_data = batch_data_create(elem, 1);
2355 add_retriable_send_initial_metadata_op(calld, retry_state,
2356 replay_batch_data);
2357 }
2358 // send_message.
2359 // Note that we can only have one send_message op in flight at a time.
Mark D. Rothefcd45b2018-03-28 10:49:59 -07002360 if (retry_state->started_send_message_count < calld->send_messages->size() &&
Mark D. Roth718c8342018-02-28 13:00:04 -08002361 retry_state->started_send_message_count ==
2362 retry_state->completed_send_message_count &&
2363 !calld->pending_send_message) {
2364 if (grpc_client_channel_trace.enabled()) {
Mark D. Roth48854d22018-04-25 13:05:26 -07002365 gpr_log(GPR_INFO,
Mark D. Roth718c8342018-02-28 13:00:04 -08002366 "chand=%p calld=%p: replaying previously completed "
2367 "send_message op",
2368 chand, calld);
2369 }
2370 if (replay_batch_data == nullptr) {
2371 replay_batch_data = batch_data_create(elem, 1);
2372 }
2373 add_retriable_send_message_op(elem, retry_state, replay_batch_data);
2374 }
2375 // send_trailing_metadata.
2376 // Note that we only add this op if we have no more send_message ops
2377 // to start, since we can't send down any more send_message ops after
2378 // send_trailing_metadata.
2379 if (calld->seen_send_trailing_metadata &&
Mark D. Rothefcd45b2018-03-28 10:49:59 -07002380 retry_state->started_send_message_count == calld->send_messages->size() &&
Mark D. Roth718c8342018-02-28 13:00:04 -08002381 !retry_state->started_send_trailing_metadata &&
2382 !calld->pending_send_trailing_metadata) {
2383 if (grpc_client_channel_trace.enabled()) {
Mark D. Roth48854d22018-04-25 13:05:26 -07002384 gpr_log(GPR_INFO,
Mark D. Roth718c8342018-02-28 13:00:04 -08002385 "chand=%p calld=%p: replaying previously completed "
2386 "send_trailing_metadata op",
2387 chand, calld);
2388 }
2389 if (replay_batch_data == nullptr) {
2390 replay_batch_data = batch_data_create(elem, 1);
2391 }
2392 add_retriable_send_trailing_metadata_op(calld, retry_state,
2393 replay_batch_data);
2394 }
2395 return replay_batch_data;
2396}
2397
2398// Adds subchannel batches for pending batches to batches, updating
2399// *num_batches as needed.
2400static void add_subchannel_batches_for_pending_batches(
2401 grpc_call_element* elem, subchannel_call_retry_state* retry_state,
Mark D. Rothde077ac2018-04-12 08:05:44 -07002402 closure_to_execute* closures, size_t* num_closures) {
Mark D. Roth718c8342018-02-28 13:00:04 -08002403 call_data* calld = static_cast<call_data*>(elem->call_data);
2404 for (size_t i = 0; i < GPR_ARRAY_SIZE(calld->pending_batches); ++i) {
2405 pending_batch* pending = &calld->pending_batches[i];
2406 grpc_transport_stream_op_batch* batch = pending->batch;
2407 if (batch == nullptr) continue;
2408 // Skip any batch that either (a) has already been started on this
2409 // subchannel call or (b) we can't start yet because we're still
2410 // replaying send ops that need to be completed first.
2411 // TODO(roth): Note that if any one op in the batch can't be sent
2412 // yet due to ops that we're replaying, we don't start any of the ops
2413 // in the batch. This is probably okay, but it could conceivably
2414 // lead to increased latency in some cases -- e.g., we could delay
2415 // starting a recv op due to it being in the same batch with a send
2416 // op. If/when we revamp the callback protocol in
2417 // transport_stream_op_batch, we may be able to fix this.
2418 if (batch->send_initial_metadata &&
2419 retry_state->started_send_initial_metadata) {
2420 continue;
2421 }
2422 if (batch->send_message && retry_state->completed_send_message_count <
2423 retry_state->started_send_message_count) {
2424 continue;
2425 }
2426 // Note that we only start send_trailing_metadata if we have no more
2427 // send_message ops to start, since we can't send down any more
2428 // send_message ops after send_trailing_metadata.
2429 if (batch->send_trailing_metadata &&
2430 (retry_state->started_send_message_count + batch->send_message <
Mark D. Rothefcd45b2018-03-28 10:49:59 -07002431 calld->send_messages->size() ||
Mark D. Roth718c8342018-02-28 13:00:04 -08002432 retry_state->started_send_trailing_metadata)) {
2433 continue;
2434 }
2435 if (batch->recv_initial_metadata &&
2436 retry_state->started_recv_initial_metadata) {
2437 continue;
2438 }
2439 if (batch->recv_message && retry_state->completed_recv_message_count <
2440 retry_state->started_recv_message_count) {
2441 continue;
2442 }
2443 if (batch->recv_trailing_metadata &&
2444 retry_state->started_recv_trailing_metadata) {
Mark D. Rothde077ac2018-04-12 08:05:44 -07002445 // If we previously completed a recv_trailing_metadata op
2446 // initiated by start_internal_recv_trailing_metadata(), use the
2447 // result of that instead of trying to re-start this op.
Yash Tibrewal8c5b8832018-05-14 11:44:18 -07002448 if (GPR_UNLIKELY((retry_state->recv_trailing_metadata_internal_batch !=
2449 nullptr))) {
Mark D. Rothde077ac2018-04-12 08:05:44 -07002450 // If the batch completed, then trigger the completion callback
2451 // directly, so that we return the previously returned results to
2452 // the application. Otherwise, just unref the internally
2453 // started subchannel batch, since we'll propagate the
2454 // completion when it completes.
2455 if (retry_state->completed_recv_trailing_metadata) {
2456 subchannel_batch_data* batch_data =
2457 retry_state->recv_trailing_metadata_internal_batch;
2458 closure_to_execute* closure = &closures[(*num_closures)++];
2459 closure->closure = &batch_data->on_complete;
2460 // Batches containing recv_trailing_metadata always succeed.
2461 closure->error = GRPC_ERROR_NONE;
2462 closure->reason =
2463 "re-executing on_complete for recv_trailing_metadata "
2464 "to propagate internally triggered result";
2465 } else {
2466 batch_data_unref(retry_state->recv_trailing_metadata_internal_batch);
2467 }
2468 retry_state->recv_trailing_metadata_internal_batch = nullptr;
2469 }
Mark D. Roth718c8342018-02-28 13:00:04 -08002470 continue;
2471 }
2472 // If we're not retrying, just send the batch as-is.
2473 if (calld->method_params == nullptr ||
2474 calld->method_params->retry_policy() == nullptr ||
2475 calld->retry_committed) {
Mark D. Rothde077ac2018-04-12 08:05:44 -07002476 add_closure_for_subchannel_batch(calld, batch, closures, num_closures);
Mark D. Roth718c8342018-02-28 13:00:04 -08002477 pending_batch_clear(calld, pending);
2478 continue;
2479 }
2480 // Create batch with the right number of callbacks.
2481 const int num_callbacks =
2482 1 + batch->recv_initial_metadata + batch->recv_message;
2483 subchannel_batch_data* batch_data = batch_data_create(elem, num_callbacks);
2484 // Cache send ops if needed.
2485 maybe_cache_send_ops_for_batch(calld, pending);
2486 // send_initial_metadata.
2487 if (batch->send_initial_metadata) {
2488 add_retriable_send_initial_metadata_op(calld, retry_state, batch_data);
2489 }
2490 // send_message.
2491 if (batch->send_message) {
2492 add_retriable_send_message_op(elem, retry_state, batch_data);
2493 }
2494 // send_trailing_metadata.
2495 if (batch->send_trailing_metadata) {
2496 add_retriable_send_trailing_metadata_op(calld, retry_state, batch_data);
2497 }
2498 // recv_initial_metadata.
2499 if (batch->recv_initial_metadata) {
2500 // recv_flags is only used on the server side.
2501 GPR_ASSERT(batch->payload->recv_initial_metadata.recv_flags == nullptr);
2502 add_retriable_recv_initial_metadata_op(calld, retry_state, batch_data);
2503 }
2504 // recv_message.
2505 if (batch->recv_message) {
2506 add_retriable_recv_message_op(calld, retry_state, batch_data);
2507 }
2508 // recv_trailing_metadata.
2509 if (batch->recv_trailing_metadata) {
2510 GPR_ASSERT(batch->collect_stats);
2511 add_retriable_recv_trailing_metadata_op(calld, retry_state, batch_data);
2512 }
Mark D. Rothde077ac2018-04-12 08:05:44 -07002513 add_closure_for_subchannel_batch(calld, &batch_data->batch, closures,
2514 num_closures);
Mark D. Roth718c8342018-02-28 13:00:04 -08002515 }
2516}
2517
2518// Constructs and starts whatever subchannel batches are needed on the
2519// subchannel call.
2520static void start_retriable_subchannel_batches(void* arg, grpc_error* ignored) {
2521 grpc_call_element* elem = static_cast<grpc_call_element*>(arg);
2522 channel_data* chand = static_cast<channel_data*>(elem->channel_data);
2523 call_data* calld = static_cast<call_data*>(elem->call_data);
2524 if (grpc_client_channel_trace.enabled()) {
Mark D. Roth48854d22018-04-25 13:05:26 -07002525 gpr_log(GPR_INFO, "chand=%p calld=%p: constructing retriable batches",
Mark D. Roth718c8342018-02-28 13:00:04 -08002526 chand, calld);
2527 }
2528 subchannel_call_retry_state* retry_state =
2529 static_cast<subchannel_call_retry_state*>(
2530 grpc_connected_subchannel_call_get_parent_data(
2531 calld->subchannel_call));
Mark D. Rothde077ac2018-04-12 08:05:44 -07002532 // Construct list of closures to execute, one for each pending batch.
Mark D. Roth718c8342018-02-28 13:00:04 -08002533 // We can start up to 6 batches.
Mark D. Rothde077ac2018-04-12 08:05:44 -07002534 closure_to_execute closures[GPR_ARRAY_SIZE(calld->pending_batches)];
2535 size_t num_closures = 0;
Mark D. Roth718c8342018-02-28 13:00:04 -08002536 // Replay previously-returned send_* ops if needed.
2537 subchannel_batch_data* replay_batch_data =
2538 maybe_create_subchannel_batch_for_replay(elem, retry_state);
2539 if (replay_batch_data != nullptr) {
Mark D. Rothde077ac2018-04-12 08:05:44 -07002540 add_closure_for_subchannel_batch(calld, &replay_batch_data->batch, closures,
2541 &num_closures);
Mark D. Roth718c8342018-02-28 13:00:04 -08002542 }
2543 // Now add pending batches.
Mark D. Rothde077ac2018-04-12 08:05:44 -07002544 add_subchannel_batches_for_pending_batches(elem, retry_state, closures,
2545 &num_closures);
Mark D. Roth718c8342018-02-28 13:00:04 -08002546 // Start batches on subchannel call.
Mark D. Roth718c8342018-02-28 13:00:04 -08002547 if (grpc_client_channel_trace.enabled()) {
Mark D. Roth48854d22018-04-25 13:05:26 -07002548 gpr_log(GPR_INFO,
Mark D. Roth718c8342018-02-28 13:00:04 -08002549 "chand=%p calld=%p: starting %" PRIuPTR
2550 " retriable batches on subchannel_call=%p",
Mark D. Rothde077ac2018-04-12 08:05:44 -07002551 chand, calld, num_closures, calld->subchannel_call);
Mark D. Roth718c8342018-02-28 13:00:04 -08002552 }
Mark D. Rothde077ac2018-04-12 08:05:44 -07002553 execute_closures_in_call_combiner(elem, "start_retriable_subchannel_batches",
2554 closures, num_closures);
Mark D. Roth718c8342018-02-28 13:00:04 -08002555}
2556
2557//
2558// LB pick
2559//
2560
2561static void create_subchannel_call(grpc_call_element* elem, grpc_error* error) {
2562 channel_data* chand = static_cast<channel_data*>(elem->channel_data);
2563 call_data* calld = static_cast<call_data*>(elem->call_data);
2564 const size_t parent_data_size =
2565 calld->enable_retries ? sizeof(subchannel_call_retry_state) : 0;
David Garcia Quintasbaf1ac72018-01-09 14:24:32 -08002566 const grpc_core::ConnectedSubchannel::CallArgs call_args = {
Mark D. Rothc0febd32018-01-09 10:25:24 -08002567 calld->pollent, // pollent
2568 calld->path, // path
2569 calld->call_start_time, // start_time
2570 calld->deadline, // deadline
2571 calld->arena, // arena
2572 calld->pick.subchannel_call_context, // context
Mark D. Roth718c8342018-02-28 13:00:04 -08002573 calld->call_combiner, // call_combiner
2574 parent_data_size // parent_data_size
Yash Tibrewald8b84a22017-09-25 13:38:03 -07002575 };
David Garcia Quintas70fbe622018-01-09 19:27:46 -08002576 grpc_error* new_error = calld->pick.connected_subchannel->CreateCall(
David Garcia Quintasbaf1ac72018-01-09 14:24:32 -08002577 call_args, &calld->subchannel_call);
Craig Tiller6014e8a2017-10-16 13:50:29 -07002578 if (grpc_client_channel_trace.enabled()) {
Mark D. Roth48854d22018-04-25 13:05:26 -07002579 gpr_log(GPR_INFO, "chand=%p calld=%p: create subchannel_call=%p: error=%s",
Mark D. Roth76e264b2017-08-25 09:03:33 -07002580 chand, calld, calld->subchannel_call, grpc_error_string(new_error));
Mark D. Roth60751fe2017-07-07 12:50:33 -07002581 }
Yash Tibrewal8c5b8832018-05-14 11:44:18 -07002582 if (GPR_UNLIKELY(new_error != GRPC_ERROR_NONE)) {
Mark D. Roth0ca0be82017-06-20 07:49:33 -07002583 new_error = grpc_error_add_child(new_error, error);
Mark D. Roth718c8342018-02-28 13:00:04 -08002584 pending_batches_fail(elem, new_error, true /* yield_call_combiner */);
Mark D. Roth0ca0be82017-06-20 07:49:33 -07002585 } else {
Mark D. Roth718c8342018-02-28 13:00:04 -08002586 if (parent_data_size > 0) {
2587 subchannel_call_retry_state* retry_state =
2588 static_cast<subchannel_call_retry_state*>(
2589 grpc_connected_subchannel_call_get_parent_data(
2590 calld->subchannel_call));
2591 retry_state->batch_payload.context = calld->pick.subchannel_call_context;
2592 }
2593 pending_batches_resume(elem);
Craig Tiller11c17d42017-03-13 13:36:34 -07002594 }
Mark D. Roth0ca0be82017-06-20 07:49:33 -07002595 GRPC_ERROR_UNREF(error);
Craig Tiller11c17d42017-03-13 13:36:34 -07002596}
2597
Mark D. Rothb2929602017-09-11 09:31:11 -07002598// Invoked when a pick is completed, on both success or failure.
Mark D. Roth718c8342018-02-28 13:00:04 -08002599static void pick_done(void* arg, grpc_error* error) {
2600 grpc_call_element* elem = static_cast<grpc_call_element*>(arg);
Noah Eisenbe82e642018-02-09 09:16:55 -08002601 channel_data* chand = static_cast<channel_data*>(elem->channel_data);
Mark D. Roth718c8342018-02-28 13:00:04 -08002602 call_data* calld = static_cast<call_data*>(elem->call_data);
Yash Tibrewal8c5b8832018-05-14 11:44:18 -07002603 if (GPR_UNLIKELY(calld->pick.connected_subchannel == nullptr)) {
Mark D. Roth0ca0be82017-06-20 07:49:33 -07002604 // Failed to create subchannel.
Mark D. Roth718c8342018-02-28 13:00:04 -08002605 // If there was no error, this is an LB policy drop, in which case
2606 // we return an error; otherwise, we may retry.
2607 grpc_status_code status = GRPC_STATUS_OK;
2608 grpc_error_get_status(error, calld->deadline, &status, nullptr, nullptr,
2609 nullptr);
2610 if (error == GRPC_ERROR_NONE || !calld->enable_retries ||
2611 !maybe_retry(elem, nullptr /* batch_data */, status,
2612 nullptr /* server_pushback_md */)) {
2613 grpc_error* new_error =
2614 error == GRPC_ERROR_NONE
2615 ? GRPC_ERROR_CREATE_FROM_STATIC_STRING(
2616 "Call dropped by load balancing policy")
2617 : GRPC_ERROR_CREATE_REFERENCING_FROM_STATIC_STRING(
2618 "Failed to create subchannel", &error, 1);
2619 if (grpc_client_channel_trace.enabled()) {
Mark D. Roth48854d22018-04-25 13:05:26 -07002620 gpr_log(GPR_INFO,
Mark D. Roth718c8342018-02-28 13:00:04 -08002621 "chand=%p calld=%p: failed to create subchannel: error=%s",
2622 chand, calld, grpc_error_string(new_error));
2623 }
2624 pending_batches_fail(elem, new_error, true /* yield_call_combiner */);
Mark D. Roth60751fe2017-07-07 12:50:33 -07002625 }
Mark D. Roth2a5959f2016-09-01 08:20:27 -07002626 } else {
Mark D. Roth9fe284e2016-09-12 11:22:27 -07002627 /* Create call on subchannel. */
Mark D. Roth718c8342018-02-28 13:00:04 -08002628 create_subchannel_call(elem, GRPC_ERROR_REF(error));
Mark D. Roth2a5959f2016-09-01 08:20:27 -07002629 }
Mark D. Roth718c8342018-02-28 13:00:04 -08002630}
2631
2632// Invoked when a pick is completed to leave the client_channel combiner
2633// and continue processing in the call combiner.
2634static void pick_done_locked(grpc_call_element* elem, grpc_error* error) {
2635 call_data* calld = static_cast<call_data*>(elem->call_data);
2636 GRPC_CLOSURE_INIT(&calld->pick_closure, pick_done, elem,
2637 grpc_schedule_on_exec_ctx);
2638 GRPC_CLOSURE_SCHED(&calld->pick_closure, error);
Mark D. Roth2a5959f2016-09-01 08:20:27 -07002639}
2640
Mark D. Rothb2929602017-09-11 09:31:11 -07002641// A wrapper around pick_done_locked() that is used in cases where
2642// either (a) the pick was deferred pending a resolver result or (b) the
2643// pick was done asynchronously. Removes the call's polling entity from
2644// chand->interested_parties before invoking pick_done_locked().
Yash Tibrewal8cf14702017-12-06 09:47:54 -08002645static void async_pick_done_locked(grpc_call_element* elem, grpc_error* error) {
Noah Eisenbe82e642018-02-09 09:16:55 -08002646 channel_data* chand = static_cast<channel_data*>(elem->channel_data);
2647 call_data* calld = static_cast<call_data*>(elem->call_data);
Yash Tibrewal8cf14702017-12-06 09:47:54 -08002648 grpc_polling_entity_del_from_pollset_set(calld->pollent,
Mark D. Rothb2929602017-09-11 09:31:11 -07002649 chand->interested_parties);
Yash Tibrewal8cf14702017-12-06 09:47:54 -08002650 pick_done_locked(elem, error);
Mark D. Rothb2929602017-09-11 09:31:11 -07002651}
2652
2653// Note: This runs under the client_channel combiner, but will NOT be
2654// holding the call combiner.
Yash Tibrewal8cf14702017-12-06 09:47:54 -08002655static void pick_callback_cancel_locked(void* arg, grpc_error* error) {
Noah Eisenbe82e642018-02-09 09:16:55 -08002656 grpc_call_element* elem = static_cast<grpc_call_element*>(arg);
2657 channel_data* chand = static_cast<channel_data*>(elem->channel_data);
2658 call_data* calld = static_cast<call_data*>(elem->call_data);
Mark D. Rothc0febd32018-01-09 10:25:24 -08002659 // Note: chand->lb_policy may have changed since we started our pick,
2660 // in which case we will be cancelling the pick on a policy other than
2661 // the one we started it on. However, this will just be a no-op.
Yash Tibrewal8c5b8832018-05-14 11:44:18 -07002662 if (GPR_LIKELY(error != GRPC_ERROR_NONE && chand->lb_policy != nullptr)) {
Craig Tiller6014e8a2017-10-16 13:50:29 -07002663 if (grpc_client_channel_trace.enabled()) {
Mark D. Roth48854d22018-04-25 13:05:26 -07002664 gpr_log(GPR_INFO, "chand=%p calld=%p: cancelling pick from LB policy %p",
Mark D. Rothc8875492018-02-20 08:33:48 -08002665 chand, calld, chand->lb_policy.get());
Mark D. Rothb2929602017-09-11 09:31:11 -07002666 }
Mark D. Rothc8875492018-02-20 08:33:48 -08002667 chand->lb_policy->CancelPickLocked(&calld->pick, GRPC_ERROR_REF(error));
Mark D. Rothb2929602017-09-11 09:31:11 -07002668 }
Yash Tibrewal8cf14702017-12-06 09:47:54 -08002669 GRPC_CALL_STACK_UNREF(calld->owning_call, "pick_callback_cancel");
Mark D. Rothb2929602017-09-11 09:31:11 -07002670}
2671
Mark D. Rothc8875492018-02-20 08:33:48 -08002672// Callback invoked by LoadBalancingPolicy::PickLocked() for async picks.
Mark D. Rothb2929602017-09-11 09:31:11 -07002673// Unrefs the LB policy and invokes async_pick_done_locked().
Yash Tibrewal8cf14702017-12-06 09:47:54 -08002674static void pick_callback_done_locked(void* arg, grpc_error* error) {
Noah Eisenbe82e642018-02-09 09:16:55 -08002675 grpc_call_element* elem = static_cast<grpc_call_element*>(arg);
2676 channel_data* chand = static_cast<channel_data*>(elem->channel_data);
2677 call_data* calld = static_cast<call_data*>(elem->call_data);
Craig Tiller6014e8a2017-10-16 13:50:29 -07002678 if (grpc_client_channel_trace.enabled()) {
Mark D. Roth48854d22018-04-25 13:05:26 -07002679 gpr_log(GPR_INFO, "chand=%p calld=%p: pick completed asynchronously", chand,
2680 calld);
Mark D. Rothb2929602017-09-11 09:31:11 -07002681 }
Yash Tibrewal8cf14702017-12-06 09:47:54 -08002682 async_pick_done_locked(elem, GRPC_ERROR_REF(error));
Ken Paysonf069dd42018-02-05 09:15:05 -08002683 GRPC_CALL_STACK_UNREF(calld->owning_call, "pick_callback");
Mark D. Rothb2929602017-09-11 09:31:11 -07002684}
2685
Mark D. Roth718c8342018-02-28 13:00:04 -08002686// Applies service config to the call. Must be invoked once we know
2687// that the resolver has returned results to the channel.
2688static void apply_service_config_to_call_locked(grpc_call_element* elem) {
2689 channel_data* chand = static_cast<channel_data*>(elem->channel_data);
2690 call_data* calld = static_cast<call_data*>(elem->call_data);
2691 if (grpc_client_channel_trace.enabled()) {
Mark D. Roth48854d22018-04-25 13:05:26 -07002692 gpr_log(GPR_INFO, "chand=%p calld=%p: applying service config to call",
Mark D. Roth718c8342018-02-28 13:00:04 -08002693 chand, calld);
2694 }
2695 if (chand->retry_throttle_data != nullptr) {
Mark D. Roth9db86fc2018-03-28 07:42:20 -07002696 calld->retry_throttle_data = chand->retry_throttle_data->Ref();
Mark D. Roth718c8342018-02-28 13:00:04 -08002697 }
2698 if (chand->method_params_table != nullptr) {
2699 calld->method_params = grpc_core::ServiceConfig::MethodConfigTableLookup(
2700 *chand->method_params_table, calld->path);
2701 if (calld->method_params != nullptr) {
2702 // If the deadline from the service config is shorter than the one
2703 // from the client API, reset the deadline timer.
2704 if (chand->deadline_checking_enabled &&
2705 calld->method_params->timeout() != 0) {
2706 const grpc_millis per_method_deadline =
2707 grpc_timespec_to_millis_round_up(calld->call_start_time) +
2708 calld->method_params->timeout();
2709 if (per_method_deadline < calld->deadline) {
2710 calld->deadline = per_method_deadline;
2711 grpc_deadline_state_reset(elem, calld->deadline);
2712 }
2713 }
2714 }
2715 }
2716 // If no retry policy, disable retries.
2717 // TODO(roth): Remove this when adding support for transparent retries.
2718 if (calld->method_params == nullptr ||
2719 calld->method_params->retry_policy() == nullptr) {
2720 calld->enable_retries = false;
2721 }
2722}
2723
Mark D. Rothc8875492018-02-20 08:33:48 -08002724// Starts a pick on chand->lb_policy.
2725// Returns true if pick is completed synchronously.
Yash Tibrewal8cf14702017-12-06 09:47:54 -08002726static bool pick_callback_start_locked(grpc_call_element* elem) {
Noah Eisenbe82e642018-02-09 09:16:55 -08002727 channel_data* chand = static_cast<channel_data*>(elem->channel_data);
2728 call_data* calld = static_cast<call_data*>(elem->call_data);
Craig Tiller6014e8a2017-10-16 13:50:29 -07002729 if (grpc_client_channel_trace.enabled()) {
Mark D. Roth48854d22018-04-25 13:05:26 -07002730 gpr_log(GPR_INFO, "chand=%p calld=%p: starting pick on lb_policy=%p", chand,
2731 calld, chand->lb_policy.get());
Mark D. Rothb2929602017-09-11 09:31:11 -07002732 }
Mark D. Roth718c8342018-02-28 13:00:04 -08002733 // Only get service config data on the first attempt.
Yash Tibrewal8c5b8832018-05-14 11:44:18 -07002734 if (GPR_LIKELY(calld->num_attempts_completed == 0)) {
Mark D. Roth718c8342018-02-28 13:00:04 -08002735 apply_service_config_to_call_locked(elem);
2736 }
Mark D. Rothb2929602017-09-11 09:31:11 -07002737 // If the application explicitly set wait_for_ready, use that.
2738 // Otherwise, if the service config specified a value for this
2739 // method, use that.
Mark D. Roth718c8342018-02-28 13:00:04 -08002740 //
2741 // The send_initial_metadata batch will be the first one in the list,
2742 // as set by get_batch_index() above.
2743 calld->pick.initial_metadata =
2744 calld->seen_send_initial_metadata
2745 ? &calld->send_initial_metadata
2746 : calld->pending_batches[0]
2747 .batch->payload->send_initial_metadata.send_initial_metadata;
2748 uint32_t send_initial_metadata_flags =
2749 calld->seen_send_initial_metadata
2750 ? calld->send_initial_metadata_flags
2751 : calld->pending_batches[0]
2752 .batch->payload->send_initial_metadata
2753 .send_initial_metadata_flags;
Mark D. Rothb2929602017-09-11 09:31:11 -07002754 const bool wait_for_ready_set_from_api =
Mark D. Roth718c8342018-02-28 13:00:04 -08002755 send_initial_metadata_flags &
Mark D. Rothb2929602017-09-11 09:31:11 -07002756 GRPC_INITIAL_METADATA_WAIT_FOR_READY_EXPLICITLY_SET;
2757 const bool wait_for_ready_set_from_service_config =
Craig Tiller4782d922017-11-10 09:53:21 -08002758 calld->method_params != nullptr &&
Mark D. Roth3e7f2df2018-02-26 13:17:06 -08002759 calld->method_params->wait_for_ready() !=
2760 ClientChannelMethodParams::WAIT_FOR_READY_UNSET;
Yash Tibrewal8c5b8832018-05-14 11:44:18 -07002761 if (GPR_UNLIKELY(!wait_for_ready_set_from_api &&
2762 wait_for_ready_set_from_service_config)) {
Mark D. Roth3e7f2df2018-02-26 13:17:06 -08002763 if (calld->method_params->wait_for_ready() ==
2764 ClientChannelMethodParams::WAIT_FOR_READY_TRUE) {
Mark D. Roth718c8342018-02-28 13:00:04 -08002765 send_initial_metadata_flags |= GRPC_INITIAL_METADATA_WAIT_FOR_READY;
Mark D. Rothb2929602017-09-11 09:31:11 -07002766 } else {
Mark D. Roth718c8342018-02-28 13:00:04 -08002767 send_initial_metadata_flags &= ~GRPC_INITIAL_METADATA_WAIT_FOR_READY;
Mark D. Rothb2929602017-09-11 09:31:11 -07002768 }
2769 }
Mark D. Roth718c8342018-02-28 13:00:04 -08002770 calld->pick.initial_metadata_flags = send_initial_metadata_flags;
2771 GRPC_CLOSURE_INIT(&calld->pick_closure, pick_callback_done_locked, elem,
Mark D. Rothb2929602017-09-11 09:31:11 -07002772 grpc_combiner_scheduler(chand->combiner));
Mark D. Roth718c8342018-02-28 13:00:04 -08002773 calld->pick.on_complete = &calld->pick_closure;
Ken Paysonf069dd42018-02-05 09:15:05 -08002774 GRPC_CALL_STACK_REF(calld->owning_call, "pick_callback");
Mark D. Rothc8875492018-02-20 08:33:48 -08002775 const bool pick_done = chand->lb_policy->PickLocked(&calld->pick);
Yash Tibrewal8c5b8832018-05-14 11:44:18 -07002776 if (GPR_LIKELY(pick_done)) {
Mark D. Rothc8875492018-02-20 08:33:48 -08002777 // Pick completed synchronously.
Craig Tiller6014e8a2017-10-16 13:50:29 -07002778 if (grpc_client_channel_trace.enabled()) {
Mark D. Roth48854d22018-04-25 13:05:26 -07002779 gpr_log(GPR_INFO, "chand=%p calld=%p: pick completed synchronously",
Mark D. Rothb2929602017-09-11 09:31:11 -07002780 chand, calld);
2781 }
Ken Paysonf069dd42018-02-05 09:15:05 -08002782 GRPC_CALL_STACK_UNREF(calld->owning_call, "pick_callback");
Mark D. Rothb2929602017-09-11 09:31:11 -07002783 } else {
2784 GRPC_CALL_STACK_REF(calld->owning_call, "pick_callback_cancel");
2785 grpc_call_combiner_set_notify_on_cancel(
Yash Tibrewal8cf14702017-12-06 09:47:54 -08002786 calld->call_combiner,
Mark D. Roth718c8342018-02-28 13:00:04 -08002787 GRPC_CLOSURE_INIT(&calld->pick_cancel_closure,
Mark D. Rothb2929602017-09-11 09:31:11 -07002788 pick_callback_cancel_locked, elem,
2789 grpc_combiner_scheduler(chand->combiner)));
2790 }
2791 return pick_done;
2792}
Mark D. Roth0ca0be82017-06-20 07:49:33 -07002793
Craig Tiller577c9b22015-11-02 14:11:15 -08002794typedef struct {
Craig Tillerbaa14a92017-11-03 09:09:36 -07002795 grpc_call_element* elem;
Mark D. Roth66f3d2b2017-09-01 09:02:17 -07002796 bool finished;
Craig Tiller577c9b22015-11-02 14:11:15 -08002797 grpc_closure closure;
Mark D. Roth66f3d2b2017-09-01 09:02:17 -07002798 grpc_closure cancel_closure;
Mark D. Roth0ca0be82017-06-20 07:49:33 -07002799} pick_after_resolver_result_args;
Craig Tiller577c9b22015-11-02 14:11:15 -08002800
Mark D. Roth76e264b2017-08-25 09:03:33 -07002801// Note: This runs under the client_channel combiner, but will NOT be
2802// holding the call combiner.
Yash Tibrewal8cf14702017-12-06 09:47:54 -08002803static void pick_after_resolver_result_cancel_locked(void* arg,
Craig Tillerbaa14a92017-11-03 09:09:36 -07002804 grpc_error* error) {
Noah Eisen4d20a662018-02-09 09:34:04 -08002805 pick_after_resolver_result_args* args =
2806 static_cast<pick_after_resolver_result_args*>(arg);
Yash Tibrewal8c5b8832018-05-14 11:44:18 -07002807 if (GPR_LIKELY(args->finished)) {
Mark D. Roth66f3d2b2017-09-01 09:02:17 -07002808 gpr_free(args);
Mark D. Rothb2b9a0f2017-09-01 09:06:47 -07002809 return;
Mark D. Roth764cf042017-09-01 09:00:06 -07002810 }
Mark D. Roth76e264b2017-08-25 09:03:33 -07002811 // If we don't yet have a resolver result, then a closure for
2812 // pick_after_resolver_result_done_locked() will have been added to
2813 // chand->waiting_for_resolver_result_closures, and it may not be invoked
2814 // until after this call has been destroyed. We mark the operation as
Mark D. Rothb2b9a0f2017-09-01 09:06:47 -07002815 // finished, so that when pick_after_resolver_result_done_locked()
Mark D. Roth76e264b2017-08-25 09:03:33 -07002816 // is called, it will be a no-op. We also immediately invoke
Mark D. Rothb2929602017-09-11 09:31:11 -07002817 // async_pick_done_locked() to propagate the error back to the caller.
2818 args->finished = true;
Craig Tillerbaa14a92017-11-03 09:09:36 -07002819 grpc_call_element* elem = args->elem;
Noah Eisenbe82e642018-02-09 09:16:55 -08002820 channel_data* chand = static_cast<channel_data*>(elem->channel_data);
2821 call_data* calld = static_cast<call_data*>(elem->call_data);
Craig Tiller6014e8a2017-10-16 13:50:29 -07002822 if (grpc_client_channel_trace.enabled()) {
Mark D. Roth48854d22018-04-25 13:05:26 -07002823 gpr_log(GPR_INFO,
Mark D. Rothb2b9a0f2017-09-01 09:06:47 -07002824 "chand=%p calld=%p: cancelling pick waiting for resolver result",
2825 chand, calld);
Mark D. Roth76e264b2017-08-25 09:03:33 -07002826 }
Mark D. Rothb2b9a0f2017-09-01 09:06:47 -07002827 // Note: Although we are not in the call combiner here, we are
2828 // basically stealing the call combiner from the pending pick, so
Mark D. Rothb2929602017-09-11 09:31:11 -07002829 // it's safe to call async_pick_done_locked() here -- we are
Mark D. Rothb2b9a0f2017-09-01 09:06:47 -07002830 // essentially calling it here instead of calling it in
2831 // pick_after_resolver_result_done_locked().
Yash Tibrewal8cf14702017-12-06 09:47:54 -08002832 async_pick_done_locked(elem, GRPC_ERROR_CREATE_REFERENCING_FROM_STATIC_STRING(
2833 "Pick cancelled", &error, 1));
Mark D. Roth76e264b2017-08-25 09:03:33 -07002834}
2835
Yash Tibrewal8cf14702017-12-06 09:47:54 -08002836static void pick_after_resolver_result_done_locked(void* arg,
Craig Tillerbaa14a92017-11-03 09:09:36 -07002837 grpc_error* error) {
Noah Eisen4d20a662018-02-09 09:34:04 -08002838 pick_after_resolver_result_args* args =
2839 static_cast<pick_after_resolver_result_args*>(arg);
Yash Tibrewal8c5b8832018-05-14 11:44:18 -07002840 if (GPR_UNLIKELY(args->finished)) {
Craig Tiller577c9b22015-11-02 14:11:15 -08002841 /* cancelled, do nothing */
Craig Tiller6014e8a2017-10-16 13:50:29 -07002842 if (grpc_client_channel_trace.enabled()) {
Mark D. Roth48854d22018-04-25 13:05:26 -07002843 gpr_log(GPR_INFO, "call cancelled before resolver result");
Mark D. Roth60751fe2017-07-07 12:50:33 -07002844 }
Mark D. Roth66f3d2b2017-09-01 09:02:17 -07002845 gpr_free(args);
Mark D. Rothb2b9a0f2017-09-01 09:06:47 -07002846 return;
2847 }
2848 args->finished = true;
Craig Tillerbaa14a92017-11-03 09:09:36 -07002849 grpc_call_element* elem = args->elem;
Noah Eisenbe82e642018-02-09 09:16:55 -08002850 channel_data* chand = static_cast<channel_data*>(elem->channel_data);
2851 call_data* calld = static_cast<call_data*>(elem->call_data);
Yash Tibrewal8c5b8832018-05-14 11:44:18 -07002852 if (GPR_UNLIKELY(error != GRPC_ERROR_NONE)) {
Craig Tiller6014e8a2017-10-16 13:50:29 -07002853 if (grpc_client_channel_trace.enabled()) {
Mark D. Roth48854d22018-04-25 13:05:26 -07002854 gpr_log(GPR_INFO, "chand=%p calld=%p: resolver failed to return data",
Mark D. Rothb2b9a0f2017-09-01 09:06:47 -07002855 chand, calld);
2856 }
Yash Tibrewal8cf14702017-12-06 09:47:54 -08002857 async_pick_done_locked(elem, GRPC_ERROR_REF(error));
Yash Tibrewal8c5b8832018-05-14 11:44:18 -07002858 } else if (GPR_UNLIKELY(chand->resolver == nullptr)) {
Mark D. Rothe63e06d2018-03-23 08:12:11 -07002859 // Shutting down.
2860 if (grpc_client_channel_trace.enabled()) {
Mark D. Roth48854d22018-04-25 13:05:26 -07002861 gpr_log(GPR_INFO, "chand=%p calld=%p: resolver disconnected", chand,
Mark D. Rothe63e06d2018-03-23 08:12:11 -07002862 calld);
2863 }
2864 async_pick_done_locked(
2865 elem, GRPC_ERROR_CREATE_FROM_STATIC_STRING("Disconnected"));
Yash Tibrewal8c5b8832018-05-14 11:44:18 -07002866 } else if (GPR_UNLIKELY(chand->lb_policy == nullptr)) {
Mark D. Rothe63e06d2018-03-23 08:12:11 -07002867 // Transient resolver failure.
2868 // If call has wait_for_ready=true, try again; otherwise, fail.
2869 uint32_t send_initial_metadata_flags =
2870 calld->seen_send_initial_metadata
2871 ? calld->send_initial_metadata_flags
2872 : calld->pending_batches[0]
2873 .batch->payload->send_initial_metadata
2874 .send_initial_metadata_flags;
2875 if (send_initial_metadata_flags & GRPC_INITIAL_METADATA_WAIT_FOR_READY) {
2876 if (grpc_client_channel_trace.enabled()) {
Mark D. Roth48854d22018-04-25 13:05:26 -07002877 gpr_log(GPR_INFO,
Mark D. Rothe63e06d2018-03-23 08:12:11 -07002878 "chand=%p calld=%p: resolver returned but no LB policy; "
2879 "wait_for_ready=true; trying again",
2880 chand, calld);
2881 }
2882 pick_after_resolver_result_start_locked(elem);
2883 } else {
2884 if (grpc_client_channel_trace.enabled()) {
Mark D. Roth48854d22018-04-25 13:05:26 -07002885 gpr_log(GPR_INFO,
Mark D. Rothe63e06d2018-03-23 08:12:11 -07002886 "chand=%p calld=%p: resolver returned but no LB policy; "
2887 "wait_for_ready=false; failing",
2888 chand, calld);
2889 }
2890 async_pick_done_locked(
2891 elem,
2892 grpc_error_set_int(
2893 GRPC_ERROR_CREATE_FROM_STATIC_STRING("Name resolution failure"),
2894 GRPC_ERROR_INT_GRPC_STATUS, GRPC_STATUS_UNAVAILABLE));
2895 }
2896 } else {
Craig Tiller6014e8a2017-10-16 13:50:29 -07002897 if (grpc_client_channel_trace.enabled()) {
Mark D. Roth48854d22018-04-25 13:05:26 -07002898 gpr_log(GPR_INFO, "chand=%p calld=%p: resolver returned, doing pick",
Mark D. Rothb2b9a0f2017-09-01 09:06:47 -07002899 chand, calld);
2900 }
Yash Tibrewal8c5b8832018-05-14 11:44:18 -07002901 if (GPR_LIKELY(pick_callback_start_locked(elem))) {
Mark D. Rothb2929602017-09-11 09:31:11 -07002902 // Even if the LB policy returns a result synchronously, we have
2903 // already added our polling entity to chand->interested_parties
2904 // in order to wait for the resolver result, so we need to
2905 // remove it here. Therefore, we call async_pick_done_locked()
2906 // instead of pick_done_locked().
Yash Tibrewal8cf14702017-12-06 09:47:54 -08002907 async_pick_done_locked(elem, GRPC_ERROR_NONE);
Mark D. Roth9dab7d52016-10-07 07:48:03 -07002908 }
Craig Tiller577c9b22015-11-02 14:11:15 -08002909 }
Craig Tiller577c9b22015-11-02 14:11:15 -08002910}
2911
Yash Tibrewal8cf14702017-12-06 09:47:54 -08002912static void pick_after_resolver_result_start_locked(grpc_call_element* elem) {
Noah Eisenbe82e642018-02-09 09:16:55 -08002913 channel_data* chand = static_cast<channel_data*>(elem->channel_data);
2914 call_data* calld = static_cast<call_data*>(elem->call_data);
Craig Tiller6014e8a2017-10-16 13:50:29 -07002915 if (grpc_client_channel_trace.enabled()) {
Mark D. Roth48854d22018-04-25 13:05:26 -07002916 gpr_log(GPR_INFO,
Mark D. Roth60751fe2017-07-07 12:50:33 -07002917 "chand=%p calld=%p: deferring pick pending resolver result", chand,
2918 calld);
Mark D. Roth64a317c2017-05-02 08:27:08 -07002919 }
Craig Tillerbaa14a92017-11-03 09:09:36 -07002920 pick_after_resolver_result_args* args =
Noah Eisenbe82e642018-02-09 09:16:55 -08002921 static_cast<pick_after_resolver_result_args*>(gpr_zalloc(sizeof(*args)));
Mark D. Roth60751fe2017-07-07 12:50:33 -07002922 args->elem = elem;
2923 GRPC_CLOSURE_INIT(&args->closure, pick_after_resolver_result_done_locked,
2924 args, grpc_combiner_scheduler(chand->combiner));
2925 grpc_closure_list_append(&chand->waiting_for_resolver_result_closures,
2926 &args->closure, GRPC_ERROR_NONE);
Mark D. Roth76e264b2017-08-25 09:03:33 -07002927 grpc_call_combiner_set_notify_on_cancel(
Yash Tibrewal8cf14702017-12-06 09:47:54 -08002928 calld->call_combiner,
Mark D. Roth66f3d2b2017-09-01 09:02:17 -07002929 GRPC_CLOSURE_INIT(&args->cancel_closure,
2930 pick_after_resolver_result_cancel_locked, args,
Mark D. Roth76e264b2017-08-25 09:03:33 -07002931 grpc_combiner_scheduler(chand->combiner)));
Mark D. Roth60751fe2017-07-07 12:50:33 -07002932}
2933
Yash Tibrewal8cf14702017-12-06 09:47:54 -08002934static void start_pick_locked(void* arg, grpc_error* ignored) {
Noah Eisenbe82e642018-02-09 09:16:55 -08002935 grpc_call_element* elem = static_cast<grpc_call_element*>(arg);
2936 call_data* calld = static_cast<call_data*>(elem->call_data);
2937 channel_data* chand = static_cast<channel_data*>(elem->channel_data);
David Garcia Quintasbe1b7f92018-01-12 14:01:38 -08002938 GPR_ASSERT(calld->pick.connected_subchannel == nullptr);
Mark D. Roth718c8342018-02-28 13:00:04 -08002939 GPR_ASSERT(calld->subchannel_call == nullptr);
Yash Tibrewal8c5b8832018-05-14 11:44:18 -07002940 if (GPR_LIKELY(chand->lb_policy != nullptr)) {
Mark D. Rothb2929602017-09-11 09:31:11 -07002941 // We already have an LB policy, so ask it for a pick.
Yash Tibrewal8c5b8832018-05-14 11:44:18 -07002942 if (GPR_LIKELY(pick_callback_start_locked(elem))) {
Mark D. Rothb2929602017-09-11 09:31:11 -07002943 // Pick completed synchronously.
Yash Tibrewal8cf14702017-12-06 09:47:54 -08002944 pick_done_locked(elem, GRPC_ERROR_NONE);
Mark D. Rothb2929602017-09-11 09:31:11 -07002945 return;
Mark D. Roth2a5959f2016-09-01 08:20:27 -07002946 }
Mark D. Roth76e264b2017-08-25 09:03:33 -07002947 } else {
Mark D. Rothb2929602017-09-11 09:31:11 -07002948 // We do not yet have an LB policy, so wait for a resolver result.
Yash Tibrewal8c5b8832018-05-14 11:44:18 -07002949 if (GPR_UNLIKELY(chand->resolver == nullptr)) {
Yash Tibrewal8cf14702017-12-06 09:47:54 -08002950 pick_done_locked(elem,
Mark D. Rothb2929602017-09-11 09:31:11 -07002951 GRPC_ERROR_CREATE_FROM_STATIC_STRING("Disconnected"));
2952 return;
2953 }
2954 if (!chand->started_resolving) {
Yash Tibrewal8cf14702017-12-06 09:47:54 -08002955 start_resolving_locked(chand);
Mark D. Rothb2929602017-09-11 09:31:11 -07002956 }
Yash Tibrewal8cf14702017-12-06 09:47:54 -08002957 pick_after_resolver_result_start_locked(elem);
Mark D. Roth2a5959f2016-09-01 08:20:27 -07002958 }
Mark D. Rothb2929602017-09-11 09:31:11 -07002959 // We need to wait for either a resolver result or for an async result
2960 // from the LB policy. Add the polling entity from call_data to the
2961 // channel_data's interested_parties, so that the I/O of the LB policy
2962 // and resolver can be done under it. The polling entity will be
2963 // removed in async_pick_done_locked().
Yash Tibrewal8cf14702017-12-06 09:47:54 -08002964 grpc_polling_entity_add_to_pollset_set(calld->pollent,
Mark D. Rothb2929602017-09-11 09:31:11 -07002965 chand->interested_parties);
Craig Tillera11bfc82017-02-14 09:56:33 -08002966}
2967
Mark D. Roth718c8342018-02-28 13:00:04 -08002968//
2969// filter call vtable functions
2970//
Mark D. Rothd6d192d2017-02-23 08:58:42 -08002971
Craig Tillere1b51da2017-03-31 15:44:33 -07002972static void cc_start_transport_stream_op_batch(
Yash Tibrewal8cf14702017-12-06 09:47:54 -08002973 grpc_call_element* elem, grpc_transport_stream_op_batch* batch) {
yang-gce1cfea2018-01-31 15:59:50 -08002974 GPR_TIMER_SCOPE("cc_start_transport_stream_op_batch", 0);
Noah Eisenbe82e642018-02-09 09:16:55 -08002975 call_data* calld = static_cast<call_data*>(elem->call_data);
2976 channel_data* chand = static_cast<channel_data*>(elem->channel_data);
Craig Tiller3be7dd02017-04-03 14:30:03 -07002977 if (chand->deadline_checking_enabled) {
Yash Tibrewal8cf14702017-12-06 09:47:54 -08002978 grpc_deadline_state_client_start_transport_stream_op_batch(elem, batch);
Craig Tiller3be7dd02017-04-03 14:30:03 -07002979 }
Mark D. Roth76e264b2017-08-25 09:03:33 -07002980 // If we've previously been cancelled, immediately fail any new batches.
Yash Tibrewal8c5b8832018-05-14 11:44:18 -07002981 if (GPR_UNLIKELY(calld->cancel_error != GRPC_ERROR_NONE)) {
Craig Tiller6014e8a2017-10-16 13:50:29 -07002982 if (grpc_client_channel_trace.enabled()) {
Mark D. Roth48854d22018-04-25 13:05:26 -07002983 gpr_log(GPR_INFO, "chand=%p calld=%p: failing batch with error: %s",
Mark D. Roth718c8342018-02-28 13:00:04 -08002984 chand, calld, grpc_error_string(calld->cancel_error));
Mark D. Roth76e264b2017-08-25 09:03:33 -07002985 }
Mark D. Roth718c8342018-02-28 13:00:04 -08002986 // Note: This will release the call combiner.
Mark D. Roth76e264b2017-08-25 09:03:33 -07002987 grpc_transport_stream_op_batch_finish_with_failure(
Mark D. Roth718c8342018-02-28 13:00:04 -08002988 batch, GRPC_ERROR_REF(calld->cancel_error), calld->call_combiner);
yang-gce1cfea2018-01-31 15:59:50 -08002989 return;
Mark D. Roth76e264b2017-08-25 09:03:33 -07002990 }
Mark D. Roth718c8342018-02-28 13:00:04 -08002991 // Handle cancellation.
Mark D. Roth76e264b2017-08-25 09:03:33 -07002992 if (batch->cancel_stream) {
2993 // Stash a copy of cancel_error in our call data, so that we can use
2994 // it for subsequent operations. This ensures that if the call is
2995 // cancelled before any batches are passed down (e.g., if the deadline
2996 // is in the past when the call starts), we can return the right
2997 // error to the caller when the first batch does get passed down.
Mark D. Roth718c8342018-02-28 13:00:04 -08002998 GRPC_ERROR_UNREF(calld->cancel_error);
2999 calld->cancel_error =
3000 GRPC_ERROR_REF(batch->payload->cancel_stream.cancel_error);
Craig Tiller6014e8a2017-10-16 13:50:29 -07003001 if (grpc_client_channel_trace.enabled()) {
Mark D. Roth48854d22018-04-25 13:05:26 -07003002 gpr_log(GPR_INFO, "chand=%p calld=%p: recording cancel_error=%s", chand,
Mark D. Roth718c8342018-02-28 13:00:04 -08003003 calld, grpc_error_string(calld->cancel_error));
Mark D. Roth76e264b2017-08-25 09:03:33 -07003004 }
Mark D. Roth718c8342018-02-28 13:00:04 -08003005 // If we do not have a subchannel call (i.e., a pick has not yet
3006 // been started), fail all pending batches. Otherwise, send the
3007 // cancellation down to the subchannel call.
3008 if (calld->subchannel_call == nullptr) {
3009 pending_batches_fail(elem, GRPC_ERROR_REF(calld->cancel_error),
3010 false /* yield_call_combiner */);
3011 // Note: This will release the call combiner.
3012 grpc_transport_stream_op_batch_finish_with_failure(
3013 batch, GRPC_ERROR_REF(calld->cancel_error), calld->call_combiner);
Mark D. Roth76e264b2017-08-25 09:03:33 -07003014 } else {
Mark D. Roth718c8342018-02-28 13:00:04 -08003015 // Note: This will release the call combiner.
3016 grpc_subchannel_call_process_op(calld->subchannel_call, batch);
Mark D. Roth76e264b2017-08-25 09:03:33 -07003017 }
yang-gce1cfea2018-01-31 15:59:50 -08003018 return;
Mark D. Roth76e264b2017-08-25 09:03:33 -07003019 }
Mark D. Roth718c8342018-02-28 13:00:04 -08003020 // Add the batch to the pending list.
3021 pending_batches_add(elem, batch);
Mark D. Roth76e264b2017-08-25 09:03:33 -07003022 // Check if we've already gotten a subchannel call.
3023 // Note that once we have completed the pick, we do not need to enter
3024 // the channel combiner, which is more efficient (especially for
3025 // streaming calls).
Craig Tiller4782d922017-11-10 09:53:21 -08003026 if (calld->subchannel_call != nullptr) {
Craig Tiller6014e8a2017-10-16 13:50:29 -07003027 if (grpc_client_channel_trace.enabled()) {
Mark D. Roth48854d22018-04-25 13:05:26 -07003028 gpr_log(GPR_INFO,
Mark D. Roth718c8342018-02-28 13:00:04 -08003029 "chand=%p calld=%p: starting batch on subchannel_call=%p", chand,
Mark D. Roth76e264b2017-08-25 09:03:33 -07003030 calld, calld->subchannel_call);
Mark D. Roth60751fe2017-07-07 12:50:33 -07003031 }
Mark D. Roth718c8342018-02-28 13:00:04 -08003032 pending_batches_resume(elem);
yang-gce1cfea2018-01-31 15:59:50 -08003033 return;
Mark D. Roth2a5959f2016-09-01 08:20:27 -07003034 }
Mark D. Roth76e264b2017-08-25 09:03:33 -07003035 // We do not yet have a subchannel call.
Mark D. Roth76e264b2017-08-25 09:03:33 -07003036 // For batches containing a send_initial_metadata op, enter the channel
3037 // combiner to start a pick.
Yash Tibrewal8c5b8832018-05-14 11:44:18 -07003038 if (GPR_LIKELY(batch->send_initial_metadata)) {
Craig Tiller6014e8a2017-10-16 13:50:29 -07003039 if (grpc_client_channel_trace.enabled()) {
Mark D. Roth48854d22018-04-25 13:05:26 -07003040 gpr_log(GPR_INFO, "chand=%p calld=%p: entering client_channel combiner",
Mark D. Rothb2929602017-09-11 09:31:11 -07003041 chand, calld);
Mark D. Roth76e264b2017-08-25 09:03:33 -07003042 }
3043 GRPC_CLOSURE_SCHED(
Mark D. Roth76e264b2017-08-25 09:03:33 -07003044 GRPC_CLOSURE_INIT(&batch->handler_private.closure, start_pick_locked,
3045 elem, grpc_combiner_scheduler(chand->combiner)),
3046 GRPC_ERROR_NONE);
3047 } else {
3048 // For all other batches, release the call combiner.
Craig Tiller6014e8a2017-10-16 13:50:29 -07003049 if (grpc_client_channel_trace.enabled()) {
Mark D. Roth48854d22018-04-25 13:05:26 -07003050 gpr_log(GPR_INFO,
Mark D. Roth76e264b2017-08-25 09:03:33 -07003051 "chand=%p calld=%p: saved batch, yeilding call combiner", chand,
3052 calld);
3053 }
Yash Tibrewal8cf14702017-12-06 09:47:54 -08003054 GRPC_CALL_COMBINER_STOP(calld->call_combiner,
Mark D. Roth76e264b2017-08-25 09:03:33 -07003055 "batch does not include send_initial_metadata");
Mark D. Roth60751fe2017-07-07 12:50:33 -07003056 }
Mark D. Roth2a5959f2016-09-01 08:20:27 -07003057}
3058
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08003059/* Constructor for call_data */
Yash Tibrewal8cf14702017-12-06 09:47:54 -08003060static grpc_error* cc_init_call_elem(grpc_call_element* elem,
Craig Tillerbaa14a92017-11-03 09:09:36 -07003061 const grpc_call_element_args* args) {
Noah Eisenbe82e642018-02-09 09:16:55 -08003062 call_data* calld = static_cast<call_data*>(elem->call_data);
3063 channel_data* chand = static_cast<channel_data*>(elem->channel_data);
Mark D. Rothe40dd292016-10-05 14:58:37 -07003064 // Initialize data members.
Craig Tiller7c70b6c2017-01-23 07:48:42 -08003065 calld->path = grpc_slice_ref_internal(args->path);
Mark D. Rothff08f332016-10-14 13:01:01 -07003066 calld->call_start_time = args->start_time;
Craig Tiller89c14282017-07-19 15:32:27 -07003067 calld->deadline = args->deadline;
Craig Tillerd426cac2017-03-13 12:30:45 -07003068 calld->arena = args->arena;
Mark D. Roth66f3d2b2017-09-01 09:02:17 -07003069 calld->owning_call = args->call_stack;
Mark D. Roth76e264b2017-08-25 09:03:33 -07003070 calld->call_combiner = args->call_combiner;
Craig Tiller3be7dd02017-04-03 14:30:03 -07003071 if (chand->deadline_checking_enabled) {
Yash Tibrewal8cf14702017-12-06 09:47:54 -08003072 grpc_deadline_state_init(elem, args->call_stack, args->call_combiner,
3073 calld->deadline);
Craig Tiller3be7dd02017-04-03 14:30:03 -07003074 }
Mark D. Roth718c8342018-02-28 13:00:04 -08003075 calld->enable_retries = chand->enable_retries;
Mark D. Rothefcd45b2018-03-28 10:49:59 -07003076 calld->send_messages.Init();
Mark D. Roth0badbe82016-06-23 10:15:12 -07003077 return GRPC_ERROR_NONE;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08003078}
3079
3080/* Destructor for call_data */
Yash Tibrewal8cf14702017-12-06 09:47:54 -08003081static void cc_destroy_call_elem(grpc_call_element* elem,
Craig Tillerbaa14a92017-11-03 09:09:36 -07003082 const grpc_call_final_info* final_info,
3083 grpc_closure* then_schedule_closure) {
Noah Eisenbe82e642018-02-09 09:16:55 -08003084 call_data* calld = static_cast<call_data*>(elem->call_data);
3085 channel_data* chand = static_cast<channel_data*>(elem->channel_data);
Craig Tiller3be7dd02017-04-03 14:30:03 -07003086 if (chand->deadline_checking_enabled) {
Yash Tibrewal8cf14702017-12-06 09:47:54 -08003087 grpc_deadline_state_destroy(elem);
Craig Tiller3be7dd02017-04-03 14:30:03 -07003088 }
Yash Tibrewal8cf14702017-12-06 09:47:54 -08003089 grpc_slice_unref_internal(calld->path);
Mark D. Roth9db86fc2018-03-28 07:42:20 -07003090 calld->retry_throttle_data.reset();
Mark D. Roth3e7f2df2018-02-26 13:17:06 -08003091 calld->method_params.reset();
Mark D. Roth718c8342018-02-28 13:00:04 -08003092 GRPC_ERROR_UNREF(calld->cancel_error);
Yash Tibrewal8c5b8832018-05-14 11:44:18 -07003093 if (GPR_LIKELY(calld->subchannel_call != nullptr)) {
Mark D. Roth76e264b2017-08-25 09:03:33 -07003094 grpc_subchannel_call_set_cleanup_closure(calld->subchannel_call,
Craig Tillerf7c8c9f2017-05-17 15:22:05 -07003095 then_schedule_closure);
Craig Tiller4782d922017-11-10 09:53:21 -08003096 then_schedule_closure = nullptr;
Yash Tibrewal8cf14702017-12-06 09:47:54 -08003097 GRPC_SUBCHANNEL_CALL_UNREF(calld->subchannel_call,
Craig Tillerf7c8c9f2017-05-17 15:22:05 -07003098 "client_channel_destroy_call");
Mark D. Roth4c0fe492016-08-31 13:51:55 -07003099 }
Mark D. Roth718c8342018-02-28 13:00:04 -08003100 for (size_t i = 0; i < GPR_ARRAY_SIZE(calld->pending_batches); ++i) {
3101 GPR_ASSERT(calld->pending_batches[i].batch == nullptr);
3102 }
Yash Tibrewal8c5b8832018-05-14 11:44:18 -07003103 if (GPR_LIKELY(calld->pick.connected_subchannel != nullptr)) {
David Garcia Quintasdfa28512018-01-11 18:31:13 -08003104 calld->pick.connected_subchannel.reset();
Craig Tiller693d3942016-10-27 16:51:25 -07003105 }
Mark D. Roth09e458c2017-05-02 08:13:26 -07003106 for (size_t i = 0; i < GRPC_CONTEXT_COUNT; ++i) {
Mark D. Rothc0febd32018-01-09 10:25:24 -08003107 if (calld->pick.subchannel_call_context[i].value != nullptr) {
3108 calld->pick.subchannel_call_context[i].destroy(
3109 calld->pick.subchannel_call_context[i].value);
Mark D. Roth09e458c2017-05-02 08:13:26 -07003110 }
3111 }
Mark D. Rothefcd45b2018-03-28 10:49:59 -07003112 calld->send_messages.Destroy();
Yash Tibrewal8cf14702017-12-06 09:47:54 -08003113 GRPC_CLOSURE_SCHED(then_schedule_closure, GRPC_ERROR_NONE);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08003114}
3115
Yash Tibrewal8cf14702017-12-06 09:47:54 -08003116static void cc_set_pollset_or_pollset_set(grpc_call_element* elem,
Craig Tillerbaa14a92017-11-03 09:09:36 -07003117 grpc_polling_entity* pollent) {
Noah Eisenbe82e642018-02-09 09:16:55 -08003118 call_data* calld = static_cast<call_data*>(elem->call_data);
David Garcia Quintas2a50dfe2016-05-31 15:09:12 -07003119 calld->pollent = pollent;
Craig Tiller577c9b22015-11-02 14:11:15 -08003120}
3121
Mark D. Roth2a5959f2016-09-01 08:20:27 -07003122/*************************************************************************
3123 * EXPORTED SYMBOLS
3124 */
3125
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08003126const grpc_channel_filter grpc_client_channel_filter = {
Craig Tillera0f3abd2017-03-31 15:42:16 -07003127 cc_start_transport_stream_op_batch,
Craig Tillerf40df232016-03-25 13:38:14 -07003128 cc_start_transport_op,
3129 sizeof(call_data),
Mark D. Roth2a5959f2016-09-01 08:20:27 -07003130 cc_init_call_elem,
David Garcia Quintas4afce7e2016-04-18 16:25:17 -07003131 cc_set_pollset_or_pollset_set,
Mark D. Roth2a5959f2016-09-01 08:20:27 -07003132 cc_destroy_call_elem,
Craig Tillerf40df232016-03-25 13:38:14 -07003133 sizeof(channel_data),
Mark D. Roth2a5959f2016-09-01 08:20:27 -07003134 cc_init_channel_elem,
3135 cc_destroy_channel_elem,
Mark D. Rothb2d24882016-10-27 15:44:07 -07003136 cc_get_channel_info,
Craig Tillerf40df232016-03-25 13:38:14 -07003137 "client-channel",
Craig Tiller87d5b192015-04-16 14:37:57 -07003138};
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08003139
Yash Tibrewal8cf14702017-12-06 09:47:54 -08003140static void try_to_connect_locked(void* arg, grpc_error* error_ignored) {
Noah Eisenbe82e642018-02-09 09:16:55 -08003141 channel_data* chand = static_cast<channel_data*>(arg);
Craig Tiller4782d922017-11-10 09:53:21 -08003142 if (chand->lb_policy != nullptr) {
Mark D. Rothc8875492018-02-20 08:33:48 -08003143 chand->lb_policy->ExitIdleLocked();
Craig Tiller613dafa2017-02-09 12:00:43 -08003144 } else {
3145 chand->exit_idle_when_lb_policy_arrives = true;
Craig Tiller4782d922017-11-10 09:53:21 -08003146 if (!chand->started_resolving && chand->resolver != nullptr) {
Yash Tibrewal8cf14702017-12-06 09:47:54 -08003147 start_resolving_locked(chand);
Craig Tiller613dafa2017-02-09 12:00:43 -08003148 }
3149 }
Yash Tibrewal8cf14702017-12-06 09:47:54 -08003150 GRPC_CHANNEL_STACK_UNREF(chand->owning_stack, "try_to_connect");
Craig Tiller613dafa2017-02-09 12:00:43 -08003151}
3152
Craig Tillera82950e2015-09-22 12:33:20 -07003153grpc_connectivity_state grpc_client_channel_check_connectivity_state(
Yash Tibrewal8cf14702017-12-06 09:47:54 -08003154 grpc_channel_element* elem, int try_to_connect) {
Noah Eisenbe82e642018-02-09 09:16:55 -08003155 channel_data* chand = static_cast<channel_data*>(elem->channel_data);
Craig Tillera8610c02017-02-14 10:05:11 -08003156 grpc_connectivity_state out =
3157 grpc_connectivity_state_check(&chand->state_tracker);
Craig Tillera82950e2015-09-22 12:33:20 -07003158 if (out == GRPC_CHANNEL_IDLE && try_to_connect) {
Craig Tillerd2e5cfc2017-02-09 13:02:20 -08003159 GRPC_CHANNEL_STACK_REF(chand->owning_stack, "try_to_connect");
ncteisen274bbbe2017-06-08 14:57:11 -07003160 GRPC_CLOSURE_SCHED(
Yash Tibrewal0ee75742017-10-13 16:07:13 -07003161 GRPC_CLOSURE_CREATE(try_to_connect_locked, chand,
3162 grpc_combiner_scheduler(chand->combiner)),
Craig Tiller613dafa2017-02-09 12:00:43 -08003163 GRPC_ERROR_NONE);
Craig Tillera82950e2015-09-22 12:33:20 -07003164 }
Craig Tiller48cb07c2015-07-15 16:16:15 -07003165 return out;
3166}
3167
Alexander Polcync3b1f182017-04-18 13:51:36 -07003168typedef struct external_connectivity_watcher {
Craig Tillerbaa14a92017-11-03 09:09:36 -07003169 channel_data* chand;
David Garcia Quintas87d5a312017-06-06 19:45:58 -07003170 grpc_polling_entity pollent;
Craig Tillerbaa14a92017-11-03 09:09:36 -07003171 grpc_closure* on_complete;
3172 grpc_closure* watcher_timer_init;
3173 grpc_connectivity_state* state;
Craig Tiller86c99582015-11-25 15:22:26 -08003174 grpc_closure my_closure;
Craig Tillerbaa14a92017-11-03 09:09:36 -07003175 struct external_connectivity_watcher* next;
Craig Tiller86c99582015-11-25 15:22:26 -08003176} external_connectivity_watcher;
3177
Craig Tillerbaa14a92017-11-03 09:09:36 -07003178static external_connectivity_watcher* lookup_external_connectivity_watcher(
3179 channel_data* chand, grpc_closure* on_complete) {
Alexander Polcync3b1f182017-04-18 13:51:36 -07003180 gpr_mu_lock(&chand->external_connectivity_watcher_list_mu);
Craig Tillerbaa14a92017-11-03 09:09:36 -07003181 external_connectivity_watcher* w =
Alexander Polcync3b1f182017-04-18 13:51:36 -07003182 chand->external_connectivity_watcher_list_head;
Craig Tiller4782d922017-11-10 09:53:21 -08003183 while (w != nullptr && w->on_complete != on_complete) {
Alexander Polcync3b1f182017-04-18 13:51:36 -07003184 w = w->next;
3185 }
3186 gpr_mu_unlock(&chand->external_connectivity_watcher_list_mu);
3187 return w;
3188}
3189
3190static void external_connectivity_watcher_list_append(
Craig Tillerbaa14a92017-11-03 09:09:36 -07003191 channel_data* chand, external_connectivity_watcher* w) {
Alexander Polcync3b1f182017-04-18 13:51:36 -07003192 GPR_ASSERT(!lookup_external_connectivity_watcher(chand, w->on_complete));
3193
3194 gpr_mu_lock(&w->chand->external_connectivity_watcher_list_mu);
3195 GPR_ASSERT(!w->next);
3196 w->next = chand->external_connectivity_watcher_list_head;
3197 chand->external_connectivity_watcher_list_head = w;
3198 gpr_mu_unlock(&w->chand->external_connectivity_watcher_list_mu);
3199}
3200
3201static void external_connectivity_watcher_list_remove(
Craig Tillerbaa14a92017-11-03 09:09:36 -07003202 channel_data* chand, external_connectivity_watcher* too_remove) {
Alexander Polcync3b1f182017-04-18 13:51:36 -07003203 GPR_ASSERT(
3204 lookup_external_connectivity_watcher(chand, too_remove->on_complete));
3205 gpr_mu_lock(&chand->external_connectivity_watcher_list_mu);
3206 if (too_remove == chand->external_connectivity_watcher_list_head) {
3207 chand->external_connectivity_watcher_list_head = too_remove->next;
3208 gpr_mu_unlock(&chand->external_connectivity_watcher_list_mu);
3209 return;
3210 }
Craig Tillerbaa14a92017-11-03 09:09:36 -07003211 external_connectivity_watcher* w =
Alexander Polcync3b1f182017-04-18 13:51:36 -07003212 chand->external_connectivity_watcher_list_head;
Craig Tiller4782d922017-11-10 09:53:21 -08003213 while (w != nullptr) {
Alexander Polcync3b1f182017-04-18 13:51:36 -07003214 if (w->next == too_remove) {
3215 w->next = w->next->next;
3216 gpr_mu_unlock(&chand->external_connectivity_watcher_list_mu);
3217 return;
3218 }
3219 w = w->next;
3220 }
3221 GPR_UNREACHABLE_CODE(return );
3222}
3223
3224int grpc_client_channel_num_external_connectivity_watchers(
Craig Tillerbaa14a92017-11-03 09:09:36 -07003225 grpc_channel_element* elem) {
Noah Eisenbe82e642018-02-09 09:16:55 -08003226 channel_data* chand = static_cast<channel_data*>(elem->channel_data);
Alexander Polcync3b1f182017-04-18 13:51:36 -07003227 int count = 0;
3228
3229 gpr_mu_lock(&chand->external_connectivity_watcher_list_mu);
Craig Tillerbaa14a92017-11-03 09:09:36 -07003230 external_connectivity_watcher* w =
Alexander Polcync3b1f182017-04-18 13:51:36 -07003231 chand->external_connectivity_watcher_list_head;
Craig Tiller4782d922017-11-10 09:53:21 -08003232 while (w != nullptr) {
Alexander Polcync3b1f182017-04-18 13:51:36 -07003233 count++;
3234 w = w->next;
3235 }
3236 gpr_mu_unlock(&chand->external_connectivity_watcher_list_mu);
3237
3238 return count;
3239}
3240
Yash Tibrewal8cf14702017-12-06 09:47:54 -08003241static void on_external_watch_complete_locked(void* arg, grpc_error* error) {
Noah Eisen4d20a662018-02-09 09:34:04 -08003242 external_connectivity_watcher* w =
3243 static_cast<external_connectivity_watcher*>(arg);
Craig Tillerbaa14a92017-11-03 09:09:36 -07003244 grpc_closure* follow_up = w->on_complete;
Yash Tibrewal8cf14702017-12-06 09:47:54 -08003245 grpc_polling_entity_del_from_pollset_set(&w->pollent,
David Garcia Quintas87d5a312017-06-06 19:45:58 -07003246 w->chand->interested_parties);
Yash Tibrewal8cf14702017-12-06 09:47:54 -08003247 GRPC_CHANNEL_STACK_UNREF(w->chand->owning_stack,
Craig Tiller1d881fb2015-12-01 07:39:04 -08003248 "external_connectivity_watcher");
Alexander Polcync3b1f182017-04-18 13:51:36 -07003249 external_connectivity_watcher_list_remove(w->chand, w);
Craig Tiller86c99582015-11-25 15:22:26 -08003250 gpr_free(w);
Yash Tibrewal2629f462018-04-30 14:52:31 -07003251 GRPC_CLOSURE_SCHED(follow_up, GRPC_ERROR_REF(error));
Craig Tiller613dafa2017-02-09 12:00:43 -08003252}
3253
Yash Tibrewal8cf14702017-12-06 09:47:54 -08003254static void watch_connectivity_state_locked(void* arg,
Craig Tillerbaa14a92017-11-03 09:09:36 -07003255 grpc_error* error_ignored) {
Noah Eisen4d20a662018-02-09 09:34:04 -08003256 external_connectivity_watcher* w =
3257 static_cast<external_connectivity_watcher*>(arg);
Craig Tiller4782d922017-11-10 09:53:21 -08003258 external_connectivity_watcher* found = nullptr;
3259 if (w->state != nullptr) {
Alexander Polcync3b1f182017-04-18 13:51:36 -07003260 external_connectivity_watcher_list_append(w->chand, w);
Yash Tibrewal446d1ea2018-04-30 16:58:21 -07003261 // An assumption is being made that the closure is scheduled on the exec ctx
3262 // scheduler and that GRPC_CLOSURE_RUN would run the closure immediately.
Yash Tibrewal8cf14702017-12-06 09:47:54 -08003263 GRPC_CLOSURE_RUN(w->watcher_timer_init, GRPC_ERROR_NONE);
Alexander Polcyn2004e392017-10-16 15:14:46 -07003264 GRPC_CLOSURE_INIT(&w->my_closure, on_external_watch_complete_locked, w,
3265 grpc_combiner_scheduler(w->chand->combiner));
Yash Tibrewal8cf14702017-12-06 09:47:54 -08003266 grpc_connectivity_state_notify_on_state_change(&w->chand->state_tracker,
3267 w->state, &w->my_closure);
Alexander Polcync3b1f182017-04-18 13:51:36 -07003268 } else {
Craig Tiller4782d922017-11-10 09:53:21 -08003269 GPR_ASSERT(w->watcher_timer_init == nullptr);
Alexander Polcync3b1f182017-04-18 13:51:36 -07003270 found = lookup_external_connectivity_watcher(w->chand, w->on_complete);
3271 if (found) {
3272 GPR_ASSERT(found->on_complete == w->on_complete);
3273 grpc_connectivity_state_notify_on_state_change(
Yash Tibrewal8cf14702017-12-06 09:47:54 -08003274 &found->chand->state_tracker, nullptr, &found->my_closure);
Alexander Polcync3b1f182017-04-18 13:51:36 -07003275 }
Yash Tibrewal8cf14702017-12-06 09:47:54 -08003276 grpc_polling_entity_del_from_pollset_set(&w->pollent,
David Garcia Quintas87d5a312017-06-06 19:45:58 -07003277 w->chand->interested_parties);
Yash Tibrewal8cf14702017-12-06 09:47:54 -08003278 GRPC_CHANNEL_STACK_UNREF(w->chand->owning_stack,
Alexander Polcync3b1f182017-04-18 13:51:36 -07003279 "external_connectivity_watcher");
3280 gpr_free(w);
3281 }
Craig Tiller86c99582015-11-25 15:22:26 -08003282}
3283
Craig Tillera82950e2015-09-22 12:33:20 -07003284void grpc_client_channel_watch_connectivity_state(
Yash Tibrewal8cf14702017-12-06 09:47:54 -08003285 grpc_channel_element* elem, grpc_polling_entity pollent,
3286 grpc_connectivity_state* state, grpc_closure* closure,
3287 grpc_closure* watcher_timer_init) {
Noah Eisenbe82e642018-02-09 09:16:55 -08003288 channel_data* chand = static_cast<channel_data*>(elem->channel_data);
Craig Tillerbaa14a92017-11-03 09:09:36 -07003289 external_connectivity_watcher* w =
Noah Eisenbe82e642018-02-09 09:16:55 -08003290 static_cast<external_connectivity_watcher*>(gpr_zalloc(sizeof(*w)));
Craig Tiller86c99582015-11-25 15:22:26 -08003291 w->chand = chand;
David Garcia Quintas87d5a312017-06-06 19:45:58 -07003292 w->pollent = pollent;
Mark D. Roth92210832017-05-02 15:04:39 -07003293 w->on_complete = closure;
Craig Tiller613dafa2017-02-09 12:00:43 -08003294 w->state = state;
Alexander Polcync3b1f182017-04-18 13:51:36 -07003295 w->watcher_timer_init = watcher_timer_init;
Yash Tibrewal8cf14702017-12-06 09:47:54 -08003296 grpc_polling_entity_add_to_pollset_set(&w->pollent,
David Garcia Quintas87d5a312017-06-06 19:45:58 -07003297 chand->interested_parties);
Craig Tiller1d881fb2015-12-01 07:39:04 -08003298 GRPC_CHANNEL_STACK_REF(w->chand->owning_stack,
3299 "external_connectivity_watcher");
ncteisen274bbbe2017-06-08 14:57:11 -07003300 GRPC_CLOSURE_SCHED(
ncteisen274bbbe2017-06-08 14:57:11 -07003301 GRPC_CLOSURE_INIT(&w->my_closure, watch_connectivity_state_locked, w,
Craig Tilleree4b1452017-05-12 10:56:03 -07003302 grpc_combiner_scheduler(chand->combiner)),
Craig Tiller613dafa2017-02-09 12:00:43 -08003303 GRPC_ERROR_NONE);
Craig Tiller48cb07c2015-07-15 16:16:15 -07003304}
Mark D. Roth718c8342018-02-28 13:00:04 -08003305
3306grpc_subchannel_call* grpc_client_channel_get_subchannel_call(
3307 grpc_call_element* elem) {
3308 call_data* calld = static_cast<call_data*>(elem->call_data);
3309 return calld->subchannel_call;
3310}