Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 3 | * Copyright 2015 gRPC authors. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 4 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 5 | * 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 Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 8 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 10 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 11 | * 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 Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 16 | * |
| 17 | */ |
| 18 | |
Yash Tibrewal | 37fdb73 | 2017-09-25 16:45:02 -0700 | [diff] [blame] | 19 | #include <grpc/support/port_platform.h> |
| 20 | |
Craig Tiller | 9eb0fde | 2017-03-31 16:59:30 -0700 | [diff] [blame] | 21 | #include "src/core/ext/filters/client_channel/client_channel.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 22 | |
Yash Tibrewal | fcd26bc | 2017-09-25 15:08:28 -0700 | [diff] [blame] | 23 | #include <inttypes.h> |
Mark D. Roth | 718c834 | 2018-02-28 13:00:04 -0800 | [diff] [blame] | 24 | #include <limits.h> |
Mark D. Roth | 4c0fe49 | 2016-08-31 13:51:55 -0700 | [diff] [blame] | 25 | #include <stdbool.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 26 | #include <stdio.h> |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 27 | #include <string.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 28 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 29 | #include <grpc/support/alloc.h> |
| 30 | #include <grpc/support/log.h> |
Mark D. Roth | b2d2488 | 2016-10-27 15:44:07 -0700 | [diff] [blame] | 31 | #include <grpc/support/string_util.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 32 | #include <grpc/support/sync.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 33 | |
Yuchen Zeng | 0bad30a | 2017-10-05 21:47:39 -0700 | [diff] [blame] | 34 | #include "src/core/ext/filters/client_channel/backup_poller.h" |
Craig Tiller | 9eb0fde | 2017-03-31 16:59:30 -0700 | [diff] [blame] | 35 | #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. Roth | 3e7f2df | 2018-02-26 13:17:06 -0800 | [diff] [blame] | 37 | #include "src/core/ext/filters/client_channel/method_params.h" |
Craig Tiller | 9eb0fde | 2017-03-31 16:59:30 -0700 | [diff] [blame] | 38 | #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" |
Mark D. Roth | 718c834 | 2018-02-28 13:00:04 -0800 | [diff] [blame] | 41 | #include "src/core/ext/filters/client_channel/status_util.h" |
Craig Tiller | 9eb0fde | 2017-03-31 16:59:30 -0700 | [diff] [blame] | 42 | #include "src/core/ext/filters/client_channel/subchannel.h" |
Craig Tiller | 3be7dd0 | 2017-04-03 14:30:03 -0700 | [diff] [blame] | 43 | #include "src/core/ext/filters/deadline/deadline_filter.h" |
Mark D. Roth | 718c834 | 2018-02-28 13:00:04 -0800 | [diff] [blame] | 44 | #include "src/core/lib/backoff/backoff.h" |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 45 | #include "src/core/lib/channel/channel_args.h" |
| 46 | #include "src/core/lib/channel/connected_channel.h" |
Mark D. Roth | dbdf495 | 2018-01-18 11:21:12 -0800 | [diff] [blame] | 47 | #include "src/core/lib/gpr/string.h" |
Mark D. Roth | 718c834 | 2018-02-28 13:00:04 -0800 | [diff] [blame] | 48 | #include "src/core/lib/gprpp/inlined_vector.h" |
| 49 | #include "src/core/lib/gprpp/manual_constructor.h" |
Craig Tiller | befafe6 | 2017-02-09 11:30:54 -0800 | [diff] [blame] | 50 | #include "src/core/lib/iomgr/combiner.h" |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 51 | #include "src/core/lib/iomgr/iomgr.h" |
Mark D. Roth | 4c0fe49 | 2016-08-31 13:51:55 -0700 | [diff] [blame] | 52 | #include "src/core/lib/iomgr/polling_entity.h" |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 53 | #include "src/core/lib/profiling/timers.h" |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 54 | #include "src/core/lib/slice/slice_internal.h" |
Mark D. Roth | 718c834 | 2018-02-28 13:00:04 -0800 | [diff] [blame] | 55 | #include "src/core/lib/slice/slice_string_helpers.h" |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 56 | #include "src/core/lib/surface/channel.h" |
| 57 | #include "src/core/lib/transport/connectivity_state.h" |
Mark D. Roth | 718c834 | 2018-02-28 13:00:04 -0800 | [diff] [blame] | 58 | #include "src/core/lib/transport/error_utils.h" |
Mark D. Roth | 9fe284e | 2016-09-12 11:22:27 -0700 | [diff] [blame] | 59 | #include "src/core/lib/transport/metadata.h" |
| 60 | #include "src/core/lib/transport/metadata_batch.h" |
Mark D. Roth | ea846a0 | 2016-11-03 11:32:54 -0700 | [diff] [blame] | 61 | #include "src/core/lib/transport/service_config.h" |
Mark D. Roth | 9fe284e | 2016-09-12 11:22:27 -0700 | [diff] [blame] | 62 | #include "src/core/lib/transport/static_metadata.h" |
Mark D. Roth | 718c834 | 2018-02-28 13:00:04 -0800 | [diff] [blame] | 63 | #include "src/core/lib/transport/status_metadata.h" |
Craig Tiller | 8910ac6 | 2015-10-08 16:49:15 -0700 | [diff] [blame] | 64 | |
Mark D. Roth | 3e7f2df | 2018-02-26 13:17:06 -0800 | [diff] [blame] | 65 | using grpc_core::internal::ClientChannelMethodParams; |
| 66 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 67 | /* Client channel implementation */ |
| 68 | |
Mark D. Roth | 718c834 | 2018-02-28 13:00:04 -0800 | [diff] [blame] | 69 | // By default, we buffer 256 KiB per RPC for retries. |
| 70 | // TODO(roth): Do we have any data to suggest a better value? |
| 71 | #define DEFAULT_PER_RPC_RETRY_BUFFER_SIZE (256 << 10) |
| 72 | |
| 73 | // This value was picked arbitrarily. It can be changed if there is |
| 74 | // any even moderately compelling reason to do so. |
| 75 | #define RETRY_BACKOFF_JITTER 0.2 |
| 76 | |
Craig Tiller | 694580f | 2017-10-18 14:48:14 -0700 | [diff] [blame] | 77 | grpc_core::TraceFlag grpc_client_channel_trace(false, "client_channel"); |
Mark D. Roth | 60751fe | 2017-07-07 12:50:33 -0700 | [diff] [blame] | 78 | |
Mark D. Roth | 26b7be4 | 2016-10-24 10:08:07 -0700 | [diff] [blame] | 79 | /************************************************************************* |
Mark D. Roth | 3e7f2df | 2018-02-26 13:17:06 -0800 | [diff] [blame] | 80 | * CHANNEL-WIDE FUNCTIONS |
Mark D. Roth | 26b7be4 | 2016-10-24 10:08:07 -0700 | [diff] [blame] | 81 | */ |
| 82 | |
Alexander Polcyn | c3b1f18 | 2017-04-18 13:51:36 -0700 | [diff] [blame] | 83 | struct external_connectivity_watcher; |
| 84 | |
Mark D. Roth | 3e7f2df | 2018-02-26 13:17:06 -0800 | [diff] [blame] | 85 | typedef grpc_core::SliceHashTable< |
| 86 | grpc_core::RefCountedPtr<ClientChannelMethodParams>> |
| 87 | MethodParamsTable; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 88 | |
Craig Tiller | 800dacb | 2015-10-06 09:10:26 -0700 | [diff] [blame] | 89 | typedef struct client_channel_channel_data { |
Mark D. Roth | 209f644 | 2018-02-08 10:26:46 -0800 | [diff] [blame] | 90 | grpc_core::OrphanablePtr<grpc_core::Resolver> resolver; |
Mark D. Roth | 4c0fe49 | 2016-08-31 13:51:55 -0700 | [diff] [blame] | 91 | bool started_resolving; |
Craig Tiller | 3be7dd0 | 2017-04-03 14:30:03 -0700 | [diff] [blame] | 92 | bool deadline_checking_enabled; |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 93 | grpc_client_channel_factory* client_channel_factory; |
Mark D. Roth | 718c834 | 2018-02-28 13:00:04 -0800 | [diff] [blame] | 94 | bool enable_retries; |
| 95 | size_t per_rpc_retry_buffer_size; |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 96 | |
Craig Tiller | befafe6 | 2017-02-09 11:30:54 -0800 | [diff] [blame] | 97 | /** combiner protecting all variables below in this data structure */ |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 98 | grpc_combiner* combiner; |
Mark D. Roth | 046cf76 | 2016-09-26 11:13:51 -0700 | [diff] [blame] | 99 | /** currently active load balancer */ |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 100 | grpc_core::OrphanablePtr<grpc_core::LoadBalancingPolicy> lb_policy; |
Mark D. Roth | d6d192d | 2017-02-23 08:58:42 -0800 | [diff] [blame] | 101 | /** retry throttle data */ |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 102 | grpc_server_retry_throttle_data* retry_throttle_data; |
Mark D. Roth | 9d48094 | 2016-10-19 14:18:05 -0700 | [diff] [blame] | 103 | /** maps method names to method_parameters structs */ |
Mark D. Roth | 3e7f2df | 2018-02-26 13:17:06 -0800 | [diff] [blame] | 104 | grpc_core::RefCountedPtr<MethodParamsTable> method_params_table; |
Mark D. Roth | 046cf76 | 2016-09-26 11:13:51 -0700 | [diff] [blame] | 105 | /** incoming resolver result - set by resolver.next() */ |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 106 | grpc_channel_args* resolver_result; |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 107 | /** a list of closures that are all waiting for resolver result to come in */ |
| 108 | grpc_closure_list waiting_for_resolver_result_closures; |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 109 | /** resolver callback */ |
Mark D. Roth | ff4df06 | 2016-08-22 15:02:49 -0700 | [diff] [blame] | 110 | grpc_closure on_resolver_result_changed; |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 111 | /** connectivity state being tracked */ |
Craig Tiller | ca3e9d3 | 2015-06-27 18:37:27 -0700 | [diff] [blame] | 112 | grpc_connectivity_state_tracker state_tracker; |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 113 | /** when an lb_policy arrives, should we try to exit idle */ |
Mark D. Roth | 4c0fe49 | 2016-08-31 13:51:55 -0700 | [diff] [blame] | 114 | bool exit_idle_when_lb_policy_arrives; |
Craig Tiller | 906e3bc | 2015-11-24 07:31:31 -0800 | [diff] [blame] | 115 | /** owning stack */ |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 116 | grpc_channel_stack* owning_stack; |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 117 | /** interested parties (owned) */ |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 118 | grpc_pollset_set* interested_parties; |
Craig Tiller | 613dafa | 2017-02-09 12:00:43 -0800 | [diff] [blame] | 119 | |
Alexander Polcyn | c3b1f18 | 2017-04-18 13:51:36 -0700 | [diff] [blame] | 120 | /* external_connectivity_watcher_list head is guarded by its own mutex, since |
| 121 | * counts need to be grabbed immediately without polling on a cq */ |
| 122 | gpr_mu external_connectivity_watcher_list_mu; |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 123 | struct external_connectivity_watcher* external_connectivity_watcher_list_head; |
Alexander Polcyn | c3b1f18 | 2017-04-18 13:51:36 -0700 | [diff] [blame] | 124 | |
Mark D. Roth | 718c834 | 2018-02-28 13:00:04 -0800 | [diff] [blame] | 125 | /* the following properties are guarded by a mutex since APIs require them |
Craig Tiller | 46dd790 | 2017-02-23 09:42:16 -0800 | [diff] [blame] | 126 | to be instantaneously available */ |
Craig Tiller | 613dafa | 2017-02-09 12:00:43 -0800 | [diff] [blame] | 127 | gpr_mu info_mu; |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 128 | char* info_lb_policy_name; |
Craig Tiller | 613dafa | 2017-02-09 12:00:43 -0800 | [diff] [blame] | 129 | /** service config in JSON form */ |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 130 | char* info_service_config_json; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 131 | } channel_data; |
| 132 | |
Juanli Shen | 592cf34 | 2017-12-04 20:52:01 -0800 | [diff] [blame] | 133 | typedef struct { |
| 134 | channel_data* chand; |
| 135 | /** used as an identifier, don't dereference it because the LB policy may be |
| 136 | * non-existing when the callback is run */ |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 137 | grpc_core::LoadBalancingPolicy* lb_policy; |
Juanli Shen | 592cf34 | 2017-12-04 20:52:01 -0800 | [diff] [blame] | 138 | grpc_closure closure; |
| 139 | } reresolution_request_args; |
| 140 | |
Craig Tiller | d6c98df | 2015-08-18 09:33:44 -0700 | [diff] [blame] | 141 | /** We create one watcher for each new lb_policy that is returned from a |
Mark D. Roth | 4c0fe49 | 2016-08-31 13:51:55 -0700 | [diff] [blame] | 142 | resolver, to watch for state changes from the lb_policy. When a state |
| 143 | change is seen, we update the channel, and create a new watcher. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 144 | typedef struct { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 145 | channel_data* chand; |
Craig Tiller | 3382511 | 2015-09-18 07:44:19 -0700 | [diff] [blame] | 146 | grpc_closure on_changed; |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 147 | grpc_connectivity_state state; |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 148 | grpc_core::LoadBalancingPolicy* lb_policy; |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 149 | } lb_policy_connectivity_watcher; |
| 150 | |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 151 | static void watch_lb_policy_locked(channel_data* chand, |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 152 | grpc_core::LoadBalancingPolicy* lb_policy, |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 153 | grpc_connectivity_state current_state); |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 154 | |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 155 | static void set_channel_connectivity_state_locked(channel_data* chand, |
Craig Tiller | 8c0d96f | 2016-03-11 14:27:52 -0800 | [diff] [blame] | 156 | grpc_connectivity_state state, |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 157 | grpc_error* error, |
| 158 | const char* reason) { |
David Garcia Quintas | 3725128 | 2017-04-14 13:46:03 -0700 | [diff] [blame] | 159 | /* TODO: Improve failure handling: |
| 160 | * - Make it possible for policies to return GRPC_CHANNEL_TRANSIENT_FAILURE. |
| 161 | * - Hand over pending picks from old policies during the switch that happens |
| 162 | * when resolver provides an update. */ |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 163 | if (chand->lb_policy != nullptr) { |
David Garcia Quintas | 956f700 | 2017-04-13 15:40:06 -0700 | [diff] [blame] | 164 | if (state == GRPC_CHANNEL_TRANSIENT_FAILURE) { |
| 165 | /* cancel picks with wait_for_ready=false */ |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 166 | chand->lb_policy->CancelMatchingPicksLocked( |
David Garcia Quintas | 956f700 | 2017-04-13 15:40:06 -0700 | [diff] [blame] | 167 | /* mask= */ GRPC_INITIAL_METADATA_WAIT_FOR_READY, |
| 168 | /* check= */ 0, GRPC_ERROR_REF(error)); |
| 169 | } else if (state == GRPC_CHANNEL_SHUTDOWN) { |
| 170 | /* cancel all picks */ |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 171 | chand->lb_policy->CancelMatchingPicksLocked(/* mask= */ 0, /* check= */ 0, |
| 172 | GRPC_ERROR_REF(error)); |
David Garcia Quintas | 956f700 | 2017-04-13 15:40:06 -0700 | [diff] [blame] | 173 | } |
Craig Tiller | 8c0d96f | 2016-03-11 14:27:52 -0800 | [diff] [blame] | 174 | } |
Craig Tiller | 6014e8a | 2017-10-16 13:50:29 -0700 | [diff] [blame] | 175 | if (grpc_client_channel_trace.enabled()) { |
Mark D. Roth | 60751fe | 2017-07-07 12:50:33 -0700 | [diff] [blame] | 176 | gpr_log(GPR_DEBUG, "chand=%p: setting connectivity state to %s", chand, |
| 177 | grpc_connectivity_state_name(state)); |
| 178 | } |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 179 | grpc_connectivity_state_set(&chand->state_tracker, state, error, reason); |
Craig Tiller | 8c0d96f | 2016-03-11 14:27:52 -0800 | [diff] [blame] | 180 | } |
| 181 | |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 182 | static void on_lb_policy_state_changed_locked(void* arg, grpc_error* error) { |
Noah Eisen | 4d20a66 | 2018-02-09 09:34:04 -0800 | [diff] [blame] | 183 | lb_policy_connectivity_watcher* w = |
| 184 | static_cast<lb_policy_connectivity_watcher*>(arg); |
Craig Tiller | c5de835 | 2017-02-09 14:08:05 -0800 | [diff] [blame] | 185 | /* check if the notification is for the latest policy */ |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 186 | if (w->lb_policy == w->chand->lb_policy.get()) { |
Craig Tiller | 6014e8a | 2017-10-16 13:50:29 -0700 | [diff] [blame] | 187 | if (grpc_client_channel_trace.enabled()) { |
Mark D. Roth | 60751fe | 2017-07-07 12:50:33 -0700 | [diff] [blame] | 188 | gpr_log(GPR_DEBUG, "chand=%p: lb_policy=%p state changed to %s", w->chand, |
| 189 | w->lb_policy, grpc_connectivity_state_name(w->state)); |
| 190 | } |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 191 | set_channel_connectivity_state_locked(w->chand, w->state, |
Craig Tiller | c5de835 | 2017-02-09 14:08:05 -0800 | [diff] [blame] | 192 | GRPC_ERROR_REF(error), "lb_changed"); |
| 193 | if (w->state != GRPC_CHANNEL_SHUTDOWN) { |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 194 | watch_lb_policy_locked(w->chand, w->lb_policy, w->state); |
Craig Tiller | c5de835 | 2017-02-09 14:08:05 -0800 | [diff] [blame] | 195 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 196 | } |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 197 | GRPC_CHANNEL_STACK_UNREF(w->chand->owning_stack, "watch_lb_policy"); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 198 | gpr_free(w); |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 199 | } |
| 200 | |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 201 | static void watch_lb_policy_locked(channel_data* chand, |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 202 | grpc_core::LoadBalancingPolicy* lb_policy, |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 203 | grpc_connectivity_state current_state) { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 204 | lb_policy_connectivity_watcher* w = |
Noah Eisen | be82e64 | 2018-02-09 09:16:55 -0800 | [diff] [blame] | 205 | static_cast<lb_policy_connectivity_watcher*>(gpr_malloc(sizeof(*w))); |
Craig Tiller | 906e3bc | 2015-11-24 07:31:31 -0800 | [diff] [blame] | 206 | GRPC_CHANNEL_STACK_REF(chand->owning_stack, "watch_lb_policy"); |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 207 | w->chand = chand; |
ncteisen | 274bbbe | 2017-06-08 14:57:11 -0700 | [diff] [blame] | 208 | GRPC_CLOSURE_INIT(&w->on_changed, on_lb_policy_state_changed_locked, w, |
Craig Tiller | ee4b145 | 2017-05-12 10:56:03 -0700 | [diff] [blame] | 209 | grpc_combiner_scheduler(chand->combiner)); |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 210 | w->state = current_state; |
| 211 | w->lb_policy = lb_policy; |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 212 | lb_policy->NotifyOnStateChangeLocked(&w->state, &w->on_changed); |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 213 | } |
| 214 | |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 215 | static void start_resolving_locked(channel_data* chand) { |
Craig Tiller | 6014e8a | 2017-10-16 13:50:29 -0700 | [diff] [blame] | 216 | if (grpc_client_channel_trace.enabled()) { |
Mark D. Roth | 60751fe | 2017-07-07 12:50:33 -0700 | [diff] [blame] | 217 | gpr_log(GPR_DEBUG, "chand=%p: starting name resolution", chand); |
| 218 | } |
| 219 | GPR_ASSERT(!chand->started_resolving); |
| 220 | chand->started_resolving = true; |
| 221 | GRPC_CHANNEL_STACK_REF(chand->owning_stack, "resolver"); |
Mark D. Roth | 209f644 | 2018-02-08 10:26:46 -0800 | [diff] [blame] | 222 | chand->resolver->NextLocked(&chand->resolver_result, |
| 223 | &chand->on_resolver_result_changed); |
Mark D. Roth | 60751fe | 2017-07-07 12:50:33 -0700 | [diff] [blame] | 224 | } |
| 225 | |
Mark D. Roth | d6d192d | 2017-02-23 08:58:42 -0800 | [diff] [blame] | 226 | typedef struct { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 227 | char* server_name; |
| 228 | grpc_server_retry_throttle_data* retry_throttle_data; |
Mark D. Roth | d6d192d | 2017-02-23 08:58:42 -0800 | [diff] [blame] | 229 | } service_config_parsing_state; |
| 230 | |
Mark D. Roth | 3e7f2df | 2018-02-26 13:17:06 -0800 | [diff] [blame] | 231 | static void parse_retry_throttle_params( |
| 232 | const grpc_json* field, service_config_parsing_state* parsing_state) { |
Mark D. Roth | d6d192d | 2017-02-23 08:58:42 -0800 | [diff] [blame] | 233 | if (strcmp(field->key, "retryThrottling") == 0) { |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 234 | if (parsing_state->retry_throttle_data != nullptr) return; // Duplicate. |
Mark D. Roth | d6d192d | 2017-02-23 08:58:42 -0800 | [diff] [blame] | 235 | if (field->type != GRPC_JSON_OBJECT) return; |
| 236 | int max_milli_tokens = 0; |
| 237 | int milli_token_ratio = 0; |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 238 | for (grpc_json* sub_field = field->child; sub_field != nullptr; |
Mark D. Roth | d6d192d | 2017-02-23 08:58:42 -0800 | [diff] [blame] | 239 | sub_field = sub_field->next) { |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 240 | if (sub_field->key == nullptr) return; |
Mark D. Roth | d6d192d | 2017-02-23 08:58:42 -0800 | [diff] [blame] | 241 | if (strcmp(sub_field->key, "maxTokens") == 0) { |
| 242 | if (max_milli_tokens != 0) return; // Duplicate. |
| 243 | if (sub_field->type != GRPC_JSON_NUMBER) return; |
| 244 | max_milli_tokens = gpr_parse_nonnegative_int(sub_field->value); |
| 245 | if (max_milli_tokens == -1) return; |
| 246 | max_milli_tokens *= 1000; |
| 247 | } else if (strcmp(sub_field->key, "tokenRatio") == 0) { |
| 248 | if (milli_token_ratio != 0) return; // Duplicate. |
| 249 | if (sub_field->type != GRPC_JSON_NUMBER) return; |
| 250 | // We support up to 3 decimal digits. |
| 251 | size_t whole_len = strlen(sub_field->value); |
| 252 | uint32_t multiplier = 1; |
| 253 | uint32_t decimal_value = 0; |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 254 | const char* decimal_point = strchr(sub_field->value, '.'); |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 255 | if (decimal_point != nullptr) { |
Noah Eisen | be82e64 | 2018-02-09 09:16:55 -0800 | [diff] [blame] | 256 | whole_len = static_cast<size_t>(decimal_point - sub_field->value); |
Mark D. Roth | d6d192d | 2017-02-23 08:58:42 -0800 | [diff] [blame] | 257 | multiplier = 1000; |
| 258 | size_t decimal_len = strlen(decimal_point + 1); |
| 259 | if (decimal_len > 3) decimal_len = 3; |
| 260 | if (!gpr_parse_bytes_to_uint32(decimal_point + 1, decimal_len, |
| 261 | &decimal_value)) { |
| 262 | return; |
| 263 | } |
| 264 | uint32_t decimal_multiplier = 1; |
| 265 | for (size_t i = 0; i < (3 - decimal_len); ++i) { |
| 266 | decimal_multiplier *= 10; |
| 267 | } |
| 268 | decimal_value *= decimal_multiplier; |
| 269 | } |
| 270 | uint32_t whole_value; |
| 271 | if (!gpr_parse_bytes_to_uint32(sub_field->value, whole_len, |
| 272 | &whole_value)) { |
| 273 | return; |
| 274 | } |
Noah Eisen | 4d20a66 | 2018-02-09 09:34:04 -0800 | [diff] [blame] | 275 | milli_token_ratio = |
| 276 | static_cast<int>((whole_value * multiplier) + decimal_value); |
Mark D. Roth | b332256 | 2017-02-23 14:38:02 -0800 | [diff] [blame] | 277 | if (milli_token_ratio <= 0) return; |
Mark D. Roth | d6d192d | 2017-02-23 08:58:42 -0800 | [diff] [blame] | 278 | } |
| 279 | } |
| 280 | parsing_state->retry_throttle_data = |
| 281 | grpc_retry_throttle_map_get_data_for_server( |
| 282 | parsing_state->server_name, max_milli_tokens, milli_token_ratio); |
| 283 | } |
| 284 | } |
| 285 | |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 286 | static void request_reresolution_locked(void* arg, grpc_error* error) { |
Noah Eisen | 4d20a66 | 2018-02-09 09:34:04 -0800 | [diff] [blame] | 287 | reresolution_request_args* args = |
| 288 | static_cast<reresolution_request_args*>(arg); |
Juanli Shen | 592cf34 | 2017-12-04 20:52:01 -0800 | [diff] [blame] | 289 | channel_data* chand = args->chand; |
| 290 | // If this invocation is for a stale LB policy, treat it as an LB shutdown |
| 291 | // signal. |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 292 | if (args->lb_policy != chand->lb_policy.get() || error != GRPC_ERROR_NONE || |
Juanli Shen | 592cf34 | 2017-12-04 20:52:01 -0800 | [diff] [blame] | 293 | chand->resolver == nullptr) { |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 294 | GRPC_CHANNEL_STACK_UNREF(chand->owning_stack, "re-resolution"); |
Juanli Shen | 592cf34 | 2017-12-04 20:52:01 -0800 | [diff] [blame] | 295 | gpr_free(args); |
| 296 | return; |
| 297 | } |
| 298 | if (grpc_client_channel_trace.enabled()) { |
| 299 | gpr_log(GPR_DEBUG, "chand=%p: started name re-resolving", chand); |
| 300 | } |
Mark D. Roth | 209f644 | 2018-02-08 10:26:46 -0800 | [diff] [blame] | 301 | chand->resolver->RequestReresolutionLocked(); |
Juanli Shen | 592cf34 | 2017-12-04 20:52:01 -0800 | [diff] [blame] | 302 | // Give back the closure to the LB policy. |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 303 | chand->lb_policy->SetReresolutionClosureLocked(&args->closure); |
Juanli Shen | 592cf34 | 2017-12-04 20:52:01 -0800 | [diff] [blame] | 304 | } |
| 305 | |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 306 | static void on_resolver_result_changed_locked(void* arg, grpc_error* error) { |
Noah Eisen | be82e64 | 2018-02-09 09:16:55 -0800 | [diff] [blame] | 307 | channel_data* chand = static_cast<channel_data*>(arg); |
Craig Tiller | 6014e8a | 2017-10-16 13:50:29 -0700 | [diff] [blame] | 308 | if (grpc_client_channel_trace.enabled()) { |
Mark D. Roth | 60751fe | 2017-07-07 12:50:33 -0700 | [diff] [blame] | 309 | gpr_log(GPR_DEBUG, "chand=%p: got resolver result: error=%s", chand, |
| 310 | grpc_error_string(error)); |
| 311 | } |
Mark D. Roth | 718c834 | 2018-02-28 13:00:04 -0800 | [diff] [blame] | 312 | // Extract the following fields from the resolver result, if non-nullptr. |
Mark D. Roth | 15494b5 | 2017-07-12 15:26:55 -0700 | [diff] [blame] | 313 | bool lb_policy_updated = false; |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 314 | bool lb_policy_created = false; |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 315 | char* lb_policy_name_dup = nullptr; |
Mark D. Roth | 60751fe | 2017-07-07 12:50:33 -0700 | [diff] [blame] | 316 | bool lb_policy_name_changed = false; |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 317 | grpc_core::OrphanablePtr<grpc_core::LoadBalancingPolicy> new_lb_policy; |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 318 | char* service_config_json = nullptr; |
| 319 | grpc_server_retry_throttle_data* retry_throttle_data = nullptr; |
Mark D. Roth | 3e7f2df | 2018-02-26 13:17:06 -0800 | [diff] [blame] | 320 | grpc_core::RefCountedPtr<MethodParamsTable> method_params_table; |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 321 | if (chand->resolver_result != nullptr) { |
Juanli Shen | 592cf34 | 2017-12-04 20:52:01 -0800 | [diff] [blame] | 322 | if (chand->resolver != nullptr) { |
| 323 | // Find LB policy name. |
Juanli Shen | 592cf34 | 2017-12-04 20:52:01 -0800 | [diff] [blame] | 324 | const grpc_arg* channel_arg = grpc_channel_args_find( |
| 325 | chand->resolver_result, GRPC_ARG_LB_POLICY_NAME); |
ncteisen | bf323a9 | 2018-02-14 17:34:05 -0800 | [diff] [blame] | 326 | const char* lb_policy_name = grpc_channel_arg_get_string(channel_arg); |
Juanli Shen | 592cf34 | 2017-12-04 20:52:01 -0800 | [diff] [blame] | 327 | // Special case: If at least one balancer address is present, we use |
| 328 | // the grpclb policy, regardless of what the resolver actually specified. |
| 329 | channel_arg = |
| 330 | grpc_channel_args_find(chand->resolver_result, GRPC_ARG_LB_ADDRESSES); |
| 331 | if (channel_arg != nullptr && channel_arg->type == GRPC_ARG_POINTER) { |
| 332 | grpc_lb_addresses* addresses = |
Noah Eisen | be82e64 | 2018-02-09 09:16:55 -0800 | [diff] [blame] | 333 | static_cast<grpc_lb_addresses*>(channel_arg->value.pointer.p); |
Juanli Shen | 592cf34 | 2017-12-04 20:52:01 -0800 | [diff] [blame] | 334 | bool found_balancer_address = false; |
| 335 | for (size_t i = 0; i < addresses->num_addresses; ++i) { |
| 336 | if (addresses->addresses[i].is_balancer) { |
| 337 | found_balancer_address = true; |
| 338 | break; |
| 339 | } |
| 340 | } |
| 341 | if (found_balancer_address) { |
| 342 | if (lb_policy_name != nullptr && |
| 343 | strcmp(lb_policy_name, "grpclb") != 0) { |
| 344 | gpr_log(GPR_INFO, |
| 345 | "resolver requested LB policy %s but provided at least one " |
| 346 | "balancer address -- forcing use of grpclb LB policy", |
| 347 | lb_policy_name); |
| 348 | } |
| 349 | lb_policy_name = "grpclb"; |
| 350 | } |
| 351 | } |
| 352 | // Use pick_first if nothing was specified and we didn't select grpclb |
| 353 | // above. |
| 354 | if (lb_policy_name == nullptr) lb_policy_name = "pick_first"; |
Juanli Shen | 592cf34 | 2017-12-04 20:52:01 -0800 | [diff] [blame] | 355 | // Check to see if we're already using the right LB policy. |
| 356 | // Note: It's safe to use chand->info_lb_policy_name here without |
| 357 | // taking a lock on chand->info_mu, because this function is the |
| 358 | // only thing that modifies its value, and it can only be invoked |
| 359 | // once at any given time. |
| 360 | lb_policy_name_changed = |
| 361 | chand->info_lb_policy_name == nullptr || |
| 362 | gpr_stricmp(chand->info_lb_policy_name, lb_policy_name) != 0; |
| 363 | if (chand->lb_policy != nullptr && !lb_policy_name_changed) { |
| 364 | // Continue using the same LB policy. Update with new addresses. |
| 365 | lb_policy_updated = true; |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 366 | chand->lb_policy->UpdateLocked(*chand->resolver_result); |
Juanli Shen | 592cf34 | 2017-12-04 20:52:01 -0800 | [diff] [blame] | 367 | } else { |
| 368 | // Instantiate new LB policy. |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 369 | grpc_core::LoadBalancingPolicy::Args lb_policy_args; |
| 370 | lb_policy_args.combiner = chand->combiner; |
| 371 | lb_policy_args.client_channel_factory = chand->client_channel_factory; |
| 372 | lb_policy_args.args = chand->resolver_result; |
| 373 | new_lb_policy = |
| 374 | grpc_core::LoadBalancingPolicyRegistry::CreateLoadBalancingPolicy( |
| 375 | lb_policy_name, lb_policy_args); |
Juanli Shen | 592cf34 | 2017-12-04 20:52:01 -0800 | [diff] [blame] | 376 | if (new_lb_policy == nullptr) { |
| 377 | gpr_log(GPR_ERROR, "could not create LB policy \"%s\"", |
| 378 | lb_policy_name); |
| 379 | } else { |
Mark D. Roth | 3ef4af2 | 2018-02-21 07:53:26 -0800 | [diff] [blame] | 380 | lb_policy_created = true; |
Juanli Shen | 592cf34 | 2017-12-04 20:52:01 -0800 | [diff] [blame] | 381 | reresolution_request_args* args = |
Noah Eisen | 4d20a66 | 2018-02-09 09:34:04 -0800 | [diff] [blame] | 382 | static_cast<reresolution_request_args*>( |
| 383 | gpr_zalloc(sizeof(*args))); |
Juanli Shen | 592cf34 | 2017-12-04 20:52:01 -0800 | [diff] [blame] | 384 | args->chand = chand; |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 385 | args->lb_policy = new_lb_policy.get(); |
Juanli Shen | 592cf34 | 2017-12-04 20:52:01 -0800 | [diff] [blame] | 386 | GRPC_CLOSURE_INIT(&args->closure, request_reresolution_locked, args, |
| 387 | grpc_combiner_scheduler(chand->combiner)); |
| 388 | GRPC_CHANNEL_STACK_REF(chand->owning_stack, "re-resolution"); |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 389 | new_lb_policy->SetReresolutionClosureLocked(&args->closure); |
Juanli Shen | 592cf34 | 2017-12-04 20:52:01 -0800 | [diff] [blame] | 390 | } |
| 391 | } |
Mark D. Roth | 718c834 | 2018-02-28 13:00:04 -0800 | [diff] [blame] | 392 | // Before we clean up, save a copy of lb_policy_name, since it might |
| 393 | // be pointing to data inside chand->resolver_result. |
| 394 | // The copy will be saved in chand->lb_policy_name below. |
| 395 | lb_policy_name_dup = gpr_strdup(lb_policy_name); |
Juanli Shen | 592cf34 | 2017-12-04 20:52:01 -0800 | [diff] [blame] | 396 | // Find service config. |
| 397 | channel_arg = grpc_channel_args_find(chand->resolver_result, |
| 398 | GRPC_ARG_SERVICE_CONFIG); |
ncteisen | bf323a9 | 2018-02-14 17:34:05 -0800 | [diff] [blame] | 399 | service_config_json = |
| 400 | gpr_strdup(grpc_channel_arg_get_string(channel_arg)); |
| 401 | if (service_config_json != nullptr) { |
Mark D. Roth | 3e7f2df | 2018-02-26 13:17:06 -0800 | [diff] [blame] | 402 | grpc_core::UniquePtr<grpc_core::ServiceConfig> service_config = |
| 403 | grpc_core::ServiceConfig::Create(service_config_json); |
Juanli Shen | 592cf34 | 2017-12-04 20:52:01 -0800 | [diff] [blame] | 404 | if (service_config != nullptr) { |
Mark D. Roth | 718c834 | 2018-02-28 13:00:04 -0800 | [diff] [blame] | 405 | if (chand->enable_retries) { |
| 406 | channel_arg = grpc_channel_args_find(chand->resolver_result, |
| 407 | GRPC_ARG_SERVER_URI); |
| 408 | const char* server_uri = grpc_channel_arg_get_string(channel_arg); |
| 409 | GPR_ASSERT(server_uri != nullptr); |
| 410 | grpc_uri* uri = grpc_uri_parse(server_uri, true); |
| 411 | GPR_ASSERT(uri->path[0] != '\0'); |
| 412 | service_config_parsing_state parsing_state; |
| 413 | memset(&parsing_state, 0, sizeof(parsing_state)); |
| 414 | parsing_state.server_name = |
| 415 | uri->path[0] == '/' ? uri->path + 1 : uri->path; |
| 416 | service_config->ParseGlobalParams(parse_retry_throttle_params, |
| 417 | &parsing_state); |
| 418 | grpc_uri_destroy(uri); |
| 419 | retry_throttle_data = parsing_state.retry_throttle_data; |
| 420 | } |
Mark D. Roth | 3e7f2df | 2018-02-26 13:17:06 -0800 | [diff] [blame] | 421 | method_params_table = service_config->CreateMethodConfigTable( |
| 422 | ClientChannelMethodParams::CreateFromJson); |
Juanli Shen | 592cf34 | 2017-12-04 20:52:01 -0800 | [diff] [blame] | 423 | } |
| 424 | } |
Mark D. Roth | 9fe284e | 2016-09-12 11:22:27 -0700 | [diff] [blame] | 425 | } |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 426 | grpc_channel_args_destroy(chand->resolver_result); |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 427 | chand->resolver_result = nullptr; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 428 | } |
Craig Tiller | 6014e8a | 2017-10-16 13:50:29 -0700 | [diff] [blame] | 429 | if (grpc_client_channel_trace.enabled()) { |
Mark D. Roth | 60751fe | 2017-07-07 12:50:33 -0700 | [diff] [blame] | 430 | gpr_log(GPR_DEBUG, |
| 431 | "chand=%p: resolver result: lb_policy_name=\"%s\"%s, " |
| 432 | "service_config=\"%s\"", |
Yash Tibrewal | 9eb8672 | 2017-09-17 23:43:30 -0700 | [diff] [blame] | 433 | chand, lb_policy_name_dup, |
| 434 | lb_policy_name_changed ? " (changed)" : "", service_config_json); |
Mark D. Roth | 60751fe | 2017-07-07 12:50:33 -0700 | [diff] [blame] | 435 | } |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 436 | // Now swap out fields in chand. Note that the new values may still |
Mark D. Roth | 718c834 | 2018-02-28 13:00:04 -0800 | [diff] [blame] | 437 | // be nullptr if (e.g.) the resolver failed to return results or the |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 438 | // results did not contain the necessary data. |
| 439 | // |
| 440 | // First, swap out the data used by cc_get_channel_info(). |
Craig Tiller | 613dafa | 2017-02-09 12:00:43 -0800 | [diff] [blame] | 441 | gpr_mu_lock(&chand->info_mu); |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 442 | if (lb_policy_name_dup != nullptr) { |
Craig Tiller | 613dafa | 2017-02-09 12:00:43 -0800 | [diff] [blame] | 443 | gpr_free(chand->info_lb_policy_name); |
Yash Tibrewal | 9eb8672 | 2017-09-17 23:43:30 -0700 | [diff] [blame] | 444 | chand->info_lb_policy_name = lb_policy_name_dup; |
Mark D. Roth | b2d2488 | 2016-10-27 15:44:07 -0700 | [diff] [blame] | 445 | } |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 446 | if (service_config_json != nullptr) { |
Craig Tiller | 613dafa | 2017-02-09 12:00:43 -0800 | [diff] [blame] | 447 | gpr_free(chand->info_service_config_json); |
| 448 | chand->info_service_config_json = service_config_json; |
Mark D. Roth | c625c7a | 2016-11-09 14:12:37 -0800 | [diff] [blame] | 449 | } |
Craig Tiller | 613dafa | 2017-02-09 12:00:43 -0800 | [diff] [blame] | 450 | gpr_mu_unlock(&chand->info_mu); |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 451 | // Swap out the retry throttle data. |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 452 | if (chand->retry_throttle_data != nullptr) { |
Mark D. Roth | d6d192d | 2017-02-23 08:58:42 -0800 | [diff] [blame] | 453 | grpc_server_retry_throttle_data_unref(chand->retry_throttle_data); |
| 454 | } |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 455 | chand->retry_throttle_data = retry_throttle_data; |
| 456 | // Swap out the method params table. |
Mark D. Roth | 3e7f2df | 2018-02-26 13:17:06 -0800 | [diff] [blame] | 457 | chand->method_params_table = std::move(method_params_table); |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 458 | // If we have a new LB policy or are shutting down (in which case |
Mark D. Roth | 718c834 | 2018-02-28 13:00:04 -0800 | [diff] [blame] | 459 | // new_lb_policy will be nullptr), swap out the LB policy, unreffing the |
| 460 | // old one and removing its fds from chand->interested_parties. |
| 461 | // Note that we do NOT do this if either (a) we updated the existing |
| 462 | // LB policy above or (b) we failed to create the new LB policy (in |
| 463 | // which case we want to continue using the most recent one we had). |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 464 | if (new_lb_policy != nullptr || error != GRPC_ERROR_NONE || |
| 465 | chand->resolver == nullptr) { |
| 466 | if (chand->lb_policy != nullptr) { |
Craig Tiller | 6014e8a | 2017-10-16 13:50:29 -0700 | [diff] [blame] | 467 | if (grpc_client_channel_trace.enabled()) { |
Mark D. Roth | 60751fe | 2017-07-07 12:50:33 -0700 | [diff] [blame] | 468 | gpr_log(GPR_DEBUG, "chand=%p: unreffing lb_policy=%p", chand, |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 469 | chand->lb_policy.get()); |
Mark D. Roth | 60751fe | 2017-07-07 12:50:33 -0700 | [diff] [blame] | 470 | } |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 471 | grpc_pollset_set_del_pollset_set(chand->lb_policy->interested_parties(), |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 472 | chand->interested_parties); |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 473 | chand->lb_policy->HandOffPendingPicksLocked(new_lb_policy.get()); |
| 474 | chand->lb_policy.reset(); |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 475 | } |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 476 | chand->lb_policy = std::move(new_lb_policy); |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 477 | } |
| 478 | // Now that we've swapped out the relevant fields of chand, check for |
| 479 | // error or shutdown. |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 480 | if (error != GRPC_ERROR_NONE || chand->resolver == nullptr) { |
Craig Tiller | 6014e8a | 2017-10-16 13:50:29 -0700 | [diff] [blame] | 481 | if (grpc_client_channel_trace.enabled()) { |
Mark D. Roth | 60751fe | 2017-07-07 12:50:33 -0700 | [diff] [blame] | 482 | gpr_log(GPR_DEBUG, "chand=%p: shutting down", chand); |
| 483 | } |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 484 | if (chand->resolver != nullptr) { |
Craig Tiller | 6014e8a | 2017-10-16 13:50:29 -0700 | [diff] [blame] | 485 | if (grpc_client_channel_trace.enabled()) { |
Mark D. Roth | 60751fe | 2017-07-07 12:50:33 -0700 | [diff] [blame] | 486 | gpr_log(GPR_DEBUG, "chand=%p: shutting down resolver", chand); |
| 487 | } |
Mark D. Roth | 209f644 | 2018-02-08 10:26:46 -0800 | [diff] [blame] | 488 | chand->resolver.reset(); |
Craig Tiller | 76a5c0e | 2016-03-09 09:05:30 -0800 | [diff] [blame] | 489 | } |
Craig Tiller | 8c0d96f | 2016-03-11 14:27:52 -0800 | [diff] [blame] | 490 | set_channel_connectivity_state_locked( |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 491 | chand, GRPC_CHANNEL_SHUTDOWN, |
ncteisen | 4b36a3d | 2017-03-13 19:08:06 -0700 | [diff] [blame] | 492 | GRPC_ERROR_CREATE_REFERENCING_FROM_STATIC_STRING( |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 493 | "Got resolver result after disconnection", &error, 1), |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 494 | "resolver_gone"); |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 495 | grpc_closure_list_fail_all(&chand->waiting_for_resolver_result_closures, |
| 496 | GRPC_ERROR_CREATE_REFERENCING_FROM_STATIC_STRING( |
| 497 | "Channel disconnected", &error, 1)); |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 498 | GRPC_CLOSURE_LIST_SCHED(&chand->waiting_for_resolver_result_closures); |
Mark D. Roth | 1b95f47 | 2018-02-15 12:54:02 -0800 | [diff] [blame] | 499 | GRPC_CHANNEL_STACK_UNREF(chand->owning_stack, "resolver"); |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 500 | } else { // Not shutting down. |
| 501 | grpc_connectivity_state state = GRPC_CHANNEL_TRANSIENT_FAILURE; |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 502 | grpc_error* state_error = |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 503 | GRPC_ERROR_CREATE_FROM_STATIC_STRING("No load balancing policy"); |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 504 | if (lb_policy_created) { |
Craig Tiller | 6014e8a | 2017-10-16 13:50:29 -0700 | [diff] [blame] | 505 | if (grpc_client_channel_trace.enabled()) { |
Mark D. Roth | 60751fe | 2017-07-07 12:50:33 -0700 | [diff] [blame] | 506 | gpr_log(GPR_DEBUG, "chand=%p: initializing new LB policy", chand); |
| 507 | } |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 508 | GRPC_ERROR_UNREF(state_error); |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 509 | state = chand->lb_policy->CheckConnectivityLocked(&state_error); |
| 510 | grpc_pollset_set_add_pollset_set(chand->lb_policy->interested_parties(), |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 511 | chand->interested_parties); |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 512 | GRPC_CLOSURE_LIST_SCHED(&chand->waiting_for_resolver_result_closures); |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 513 | if (chand->exit_idle_when_lb_policy_arrives) { |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 514 | chand->lb_policy->ExitIdleLocked(); |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 515 | chand->exit_idle_when_lb_policy_arrives = false; |
| 516 | } |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 517 | watch_lb_policy_locked(chand, chand->lb_policy.get(), state); |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 518 | } |
Mark D. Roth | 15494b5 | 2017-07-12 15:26:55 -0700 | [diff] [blame] | 519 | if (!lb_policy_updated) { |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 520 | set_channel_connectivity_state_locked( |
| 521 | chand, state, GRPC_ERROR_REF(state_error), "new_lb+resolver"); |
Mark D. Roth | 15494b5 | 2017-07-12 15:26:55 -0700 | [diff] [blame] | 522 | } |
Mark D. Roth | 209f644 | 2018-02-08 10:26:46 -0800 | [diff] [blame] | 523 | chand->resolver->NextLocked(&chand->resolver_result, |
| 524 | &chand->on_resolver_result_changed); |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 525 | GRPC_ERROR_UNREF(state_error); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 526 | } |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 527 | } |
| 528 | |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 529 | static void start_transport_op_locked(void* arg, grpc_error* error_ignored) { |
Noah Eisen | be82e64 | 2018-02-09 09:16:55 -0800 | [diff] [blame] | 530 | grpc_transport_op* op = static_cast<grpc_transport_op*>(arg); |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 531 | grpc_channel_element* elem = |
Noah Eisen | be82e64 | 2018-02-09 09:16:55 -0800 | [diff] [blame] | 532 | static_cast<grpc_channel_element*>(op->handler_private.extra_arg); |
| 533 | channel_data* chand = static_cast<channel_data*>(elem->channel_data); |
Craig Tiller | 000cd8f | 2015-09-18 07:20:29 -0700 | [diff] [blame] | 534 | |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 535 | if (op->on_connectivity_state_change != nullptr) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 536 | grpc_connectivity_state_notify_on_state_change( |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 537 | &chand->state_tracker, op->connectivity_state, |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 538 | op->on_connectivity_state_change); |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 539 | op->on_connectivity_state_change = nullptr; |
| 540 | op->connectivity_state = nullptr; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 541 | } |
| 542 | |
Yuchen Zeng | c272dd7 | 2017-12-05 12:18:34 -0800 | [diff] [blame] | 543 | if (op->send_ping.on_initiate != nullptr || op->send_ping.on_ack != nullptr) { |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 544 | if (chand->lb_policy == nullptr) { |
ncteisen | 274bbbe | 2017-06-08 14:57:11 -0700 | [diff] [blame] | 545 | GRPC_CLOSURE_SCHED( |
Yash Tibrewal | d6c292f | 2017-12-07 19:38:43 -0800 | [diff] [blame] | 546 | op->send_ping.on_initiate, |
Yuchen Zeng | c272dd7 | 2017-12-05 12:18:34 -0800 | [diff] [blame] | 547 | GRPC_ERROR_CREATE_FROM_STATIC_STRING("Ping with no load balancing")); |
| 548 | GRPC_CLOSURE_SCHED( |
Yash Tibrewal | d6c292f | 2017-12-07 19:38:43 -0800 | [diff] [blame] | 549 | op->send_ping.on_ack, |
ncteisen | 4b36a3d | 2017-03-13 19:08:06 -0700 | [diff] [blame] | 550 | GRPC_ERROR_CREATE_FROM_STATIC_STRING("Ping with no load balancing")); |
Craig Tiller | 26dab31 | 2015-12-07 14:43:47 -0800 | [diff] [blame] | 551 | } else { |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 552 | chand->lb_policy->PingOneLocked(op->send_ping.on_initiate, |
| 553 | op->send_ping.on_ack); |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 554 | op->bind_pollset = nullptr; |
Craig Tiller | 26dab31 | 2015-12-07 14:43:47 -0800 | [diff] [blame] | 555 | } |
Yuchen Zeng | c272dd7 | 2017-12-05 12:18:34 -0800 | [diff] [blame] | 556 | op->send_ping.on_initiate = nullptr; |
| 557 | op->send_ping.on_ack = nullptr; |
Craig Tiller | 26dab31 | 2015-12-07 14:43:47 -0800 | [diff] [blame] | 558 | } |
| 559 | |
Craig Tiller | 1c51edc | 2016-05-07 16:18:43 -0700 | [diff] [blame] | 560 | if (op->disconnect_with_error != GRPC_ERROR_NONE) { |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 561 | if (chand->resolver != nullptr) { |
Craig Tiller | 1c51edc | 2016-05-07 16:18:43 -0700 | [diff] [blame] | 562 | set_channel_connectivity_state_locked( |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 563 | chand, GRPC_CHANNEL_SHUTDOWN, |
Craig Tiller | 1c51edc | 2016-05-07 16:18:43 -0700 | [diff] [blame] | 564 | GRPC_ERROR_REF(op->disconnect_with_error), "disconnect"); |
Mark D. Roth | 209f644 | 2018-02-08 10:26:46 -0800 | [diff] [blame] | 565 | chand->resolver.reset(); |
Craig Tiller | 1c51edc | 2016-05-07 16:18:43 -0700 | [diff] [blame] | 566 | if (!chand->started_resolving) { |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 567 | grpc_closure_list_fail_all(&chand->waiting_for_resolver_result_closures, |
Craig Tiller | 1c51edc | 2016-05-07 16:18:43 -0700 | [diff] [blame] | 568 | GRPC_ERROR_REF(op->disconnect_with_error)); |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 569 | GRPC_CLOSURE_LIST_SCHED(&chand->waiting_for_resolver_result_closures); |
Craig Tiller | 1c51edc | 2016-05-07 16:18:43 -0700 | [diff] [blame] | 570 | } |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 571 | if (chand->lb_policy != nullptr) { |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 572 | grpc_pollset_set_del_pollset_set(chand->lb_policy->interested_parties(), |
Craig Tiller | 1c51edc | 2016-05-07 16:18:43 -0700 | [diff] [blame] | 573 | chand->interested_parties); |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 574 | chand->lb_policy.reset(); |
Craig Tiller | 1c51edc | 2016-05-07 16:18:43 -0700 | [diff] [blame] | 575 | } |
Craig Tiller | b12d22a | 2016-04-23 12:50:21 -0700 | [diff] [blame] | 576 | } |
Craig Tiller | 1c51edc | 2016-05-07 16:18:43 -0700 | [diff] [blame] | 577 | GRPC_ERROR_UNREF(op->disconnect_with_error); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 578 | } |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 579 | GRPC_CHANNEL_STACK_UNREF(chand->owning_stack, "start_transport_op"); |
Craig Tiller | d2e5cfc | 2017-02-09 13:02:20 -0800 | [diff] [blame] | 580 | |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 581 | GRPC_CLOSURE_SCHED(op->on_consumed, GRPC_ERROR_NONE); |
Craig Tiller | befafe6 | 2017-02-09 11:30:54 -0800 | [diff] [blame] | 582 | } |
| 583 | |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 584 | static void cc_start_transport_op(grpc_channel_element* elem, |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 585 | grpc_transport_op* op) { |
Noah Eisen | be82e64 | 2018-02-09 09:16:55 -0800 | [diff] [blame] | 586 | channel_data* chand = static_cast<channel_data*>(elem->channel_data); |
Craig Tiller | befafe6 | 2017-02-09 11:30:54 -0800 | [diff] [blame] | 587 | |
Craig Tiller | befafe6 | 2017-02-09 11:30:54 -0800 | [diff] [blame] | 588 | GPR_ASSERT(op->set_accept_stream == false); |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 589 | if (op->bind_pollset != nullptr) { |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 590 | grpc_pollset_set_add_pollset(chand->interested_parties, op->bind_pollset); |
Craig Tiller | befafe6 | 2017-02-09 11:30:54 -0800 | [diff] [blame] | 591 | } |
| 592 | |
Craig Tiller | c55c102 | 2017-03-10 10:26:42 -0800 | [diff] [blame] | 593 | op->handler_private.extra_arg = elem; |
Craig Tiller | d2e5cfc | 2017-02-09 13:02:20 -0800 | [diff] [blame] | 594 | GRPC_CHANNEL_STACK_REF(chand->owning_stack, "start_transport_op"); |
ncteisen | 274bbbe | 2017-06-08 14:57:11 -0700 | [diff] [blame] | 595 | GRPC_CLOSURE_SCHED( |
ncteisen | 274bbbe | 2017-06-08 14:57:11 -0700 | [diff] [blame] | 596 | GRPC_CLOSURE_INIT(&op->handler_private.closure, start_transport_op_locked, |
Craig Tiller | ee4b145 | 2017-05-12 10:56:03 -0700 | [diff] [blame] | 597 | op, grpc_combiner_scheduler(chand->combiner)), |
Craig Tiller | befafe6 | 2017-02-09 11:30:54 -0800 | [diff] [blame] | 598 | GRPC_ERROR_NONE); |
Craig Tiller | ca3e9d3 | 2015-06-27 18:37:27 -0700 | [diff] [blame] | 599 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 600 | |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 601 | static void cc_get_channel_info(grpc_channel_element* elem, |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 602 | const grpc_channel_info* info) { |
Noah Eisen | be82e64 | 2018-02-09 09:16:55 -0800 | [diff] [blame] | 603 | channel_data* chand = static_cast<channel_data*>(elem->channel_data); |
Craig Tiller | 613dafa | 2017-02-09 12:00:43 -0800 | [diff] [blame] | 604 | gpr_mu_lock(&chand->info_mu); |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 605 | if (info->lb_policy_name != nullptr) { |
| 606 | *info->lb_policy_name = chand->info_lb_policy_name == nullptr |
| 607 | ? nullptr |
Craig Tiller | 613dafa | 2017-02-09 12:00:43 -0800 | [diff] [blame] | 608 | : gpr_strdup(chand->info_lb_policy_name); |
Mark D. Roth | b2d2488 | 2016-10-27 15:44:07 -0700 | [diff] [blame] | 609 | } |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 610 | if (info->service_config_json != nullptr) { |
Craig Tiller | 613dafa | 2017-02-09 12:00:43 -0800 | [diff] [blame] | 611 | *info->service_config_json = |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 612 | chand->info_service_config_json == nullptr |
| 613 | ? nullptr |
Craig Tiller | 613dafa | 2017-02-09 12:00:43 -0800 | [diff] [blame] | 614 | : gpr_strdup(chand->info_service_config_json); |
Mark D. Roth | c625c7a | 2016-11-09 14:12:37 -0800 | [diff] [blame] | 615 | } |
Craig Tiller | 613dafa | 2017-02-09 12:00:43 -0800 | [diff] [blame] | 616 | gpr_mu_unlock(&chand->info_mu); |
Mark D. Roth | b2d2488 | 2016-10-27 15:44:07 -0700 | [diff] [blame] | 617 | } |
| 618 | |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 619 | /* Constructor for channel_data */ |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 620 | static grpc_error* cc_init_channel_elem(grpc_channel_element* elem, |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 621 | grpc_channel_element_args* args) { |
Noah Eisen | be82e64 | 2018-02-09 09:16:55 -0800 | [diff] [blame] | 622 | channel_data* chand = static_cast<channel_data*>(elem->channel_data); |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 623 | GPR_ASSERT(args->is_last); |
| 624 | GPR_ASSERT(elem->filter == &grpc_client_channel_filter); |
Mark D. Roth | 21d4b2d | 2016-11-18 09:53:41 -0800 | [diff] [blame] | 625 | // Initialize data members. |
Craig Tiller | ee4b145 | 2017-05-12 10:56:03 -0700 | [diff] [blame] | 626 | chand->combiner = grpc_combiner_create(); |
Craig Tiller | d8547751 | 2017-02-09 12:02:39 -0800 | [diff] [blame] | 627 | gpr_mu_init(&chand->info_mu); |
Alexander Polcyn | c3b1f18 | 2017-04-18 13:51:36 -0700 | [diff] [blame] | 628 | gpr_mu_init(&chand->external_connectivity_watcher_list_mu); |
| 629 | |
| 630 | gpr_mu_lock(&chand->external_connectivity_watcher_list_mu); |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 631 | chand->external_connectivity_watcher_list_head = nullptr; |
Alexander Polcyn | c3b1f18 | 2017-04-18 13:51:36 -0700 | [diff] [blame] | 632 | gpr_mu_unlock(&chand->external_connectivity_watcher_list_mu); |
| 633 | |
Mark D. Roth | 21d4b2d | 2016-11-18 09:53:41 -0800 | [diff] [blame] | 634 | chand->owning_stack = args->channel_stack; |
ncteisen | 274bbbe | 2017-06-08 14:57:11 -0700 | [diff] [blame] | 635 | GRPC_CLOSURE_INIT(&chand->on_resolver_result_changed, |
Craig Tiller | befafe6 | 2017-02-09 11:30:54 -0800 | [diff] [blame] | 636 | on_resolver_result_changed_locked, chand, |
Craig Tiller | ee4b145 | 2017-05-12 10:56:03 -0700 | [diff] [blame] | 637 | grpc_combiner_scheduler(chand->combiner)); |
Mark D. Roth | 21d4b2d | 2016-11-18 09:53:41 -0800 | [diff] [blame] | 638 | chand->interested_parties = grpc_pollset_set_create(); |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 639 | grpc_connectivity_state_init(&chand->state_tracker, GRPC_CHANNEL_IDLE, |
| 640 | "client_channel"); |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 641 | grpc_client_channel_start_backup_polling(chand->interested_parties); |
Mark D. Roth | 718c834 | 2018-02-28 13:00:04 -0800 | [diff] [blame] | 642 | // Record max per-RPC retry buffer size. |
| 643 | const grpc_arg* arg = grpc_channel_args_find( |
| 644 | args->channel_args, GRPC_ARG_PER_RPC_RETRY_BUFFER_SIZE); |
| 645 | chand->per_rpc_retry_buffer_size = (size_t)grpc_channel_arg_get_integer( |
| 646 | arg, {DEFAULT_PER_RPC_RETRY_BUFFER_SIZE, 0, INT_MAX}); |
| 647 | // Record enable_retries. |
| 648 | arg = grpc_channel_args_find(args->channel_args, GRPC_ARG_ENABLE_RETRIES); |
| 649 | chand->enable_retries = grpc_channel_arg_get_bool(arg, true); |
Mark D. Roth | 21d4b2d | 2016-11-18 09:53:41 -0800 | [diff] [blame] | 650 | // Record client channel factory. |
Mark D. Roth | 718c834 | 2018-02-28 13:00:04 -0800 | [diff] [blame] | 651 | arg = grpc_channel_args_find(args->channel_args, |
| 652 | GRPC_ARG_CLIENT_CHANNEL_FACTORY); |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 653 | if (arg == nullptr) { |
David Garcia Quintas | 228a514 | 2017-03-30 19:43:00 -0700 | [diff] [blame] | 654 | return GRPC_ERROR_CREATE_FROM_STATIC_STRING( |
| 655 | "Missing client channel factory in args for client channel filter"); |
| 656 | } |
| 657 | if (arg->type != GRPC_ARG_POINTER) { |
| 658 | return GRPC_ERROR_CREATE_FROM_STATIC_STRING( |
| 659 | "client channel factory arg must be a pointer"); |
| 660 | } |
Yash Tibrewal | bc130da | 2017-09-12 22:44:08 -0700 | [diff] [blame] | 661 | grpc_client_channel_factory_ref( |
Noah Eisen | be82e64 | 2018-02-09 09:16:55 -0800 | [diff] [blame] | 662 | static_cast<grpc_client_channel_factory*>(arg->value.pointer.p)); |
Yash Tibrewal | ca3c1c0 | 2017-09-07 22:47:16 -0700 | [diff] [blame] | 663 | chand->client_channel_factory = |
Noah Eisen | be82e64 | 2018-02-09 09:16:55 -0800 | [diff] [blame] | 664 | static_cast<grpc_client_channel_factory*>(arg->value.pointer.p); |
Mark D. Roth | dc9bee7 | 2017-02-07 12:29:14 -0800 | [diff] [blame] | 665 | // Get server name to resolve, using proxy mapper if needed. |
Mark D. Roth | 86e9059 | 2016-11-18 09:56:40 -0800 | [diff] [blame] | 666 | arg = grpc_channel_args_find(args->channel_args, GRPC_ARG_SERVER_URI); |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 667 | if (arg == nullptr) { |
David Garcia Quintas | 228a514 | 2017-03-30 19:43:00 -0700 | [diff] [blame] | 668 | return GRPC_ERROR_CREATE_FROM_STATIC_STRING( |
| 669 | "Missing server uri in args for client channel filter"); |
| 670 | } |
| 671 | if (arg->type != GRPC_ARG_STRING) { |
| 672 | return GRPC_ERROR_CREATE_FROM_STATIC_STRING( |
| 673 | "server uri arg must be a string"); |
| 674 | } |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 675 | char* proxy_name = nullptr; |
| 676 | grpc_channel_args* new_args = nullptr; |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 677 | grpc_proxy_mappers_map_name(arg->value.string, args->channel_args, |
Mark D. Roth | dc9bee7 | 2017-02-07 12:29:14 -0800 | [diff] [blame] | 678 | &proxy_name, &new_args); |
| 679 | // Instantiate resolver. |
Mark D. Roth | 209f644 | 2018-02-08 10:26:46 -0800 | [diff] [blame] | 680 | chand->resolver = grpc_core::ResolverRegistry::CreateResolver( |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 681 | proxy_name != nullptr ? proxy_name : arg->value.string, |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 682 | new_args != nullptr ? new_args : args->channel_args, |
Craig Tiller | 972470b | 2017-02-09 15:05:36 -0800 | [diff] [blame] | 683 | chand->interested_parties, chand->combiner); |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 684 | if (proxy_name != nullptr) gpr_free(proxy_name); |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 685 | if (new_args != nullptr) grpc_channel_args_destroy(new_args); |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 686 | if (chand->resolver == nullptr) { |
ncteisen | 4b36a3d | 2017-03-13 19:08:06 -0700 | [diff] [blame] | 687 | return GRPC_ERROR_CREATE_FROM_STATIC_STRING("resolver creation failed"); |
Mark D. Roth | 5e2566e | 2016-11-18 10:53:13 -0800 | [diff] [blame] | 688 | } |
Craig Tiller | 3be7dd0 | 2017-04-03 14:30:03 -0700 | [diff] [blame] | 689 | chand->deadline_checking_enabled = |
| 690 | grpc_deadline_checking_enabled(args->channel_args); |
Mark D. Roth | 5e2566e | 2016-11-18 10:53:13 -0800 | [diff] [blame] | 691 | return GRPC_ERROR_NONE; |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 692 | } |
| 693 | |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 694 | static void shutdown_resolver_locked(void* arg, grpc_error* error) { |
Mark D. Roth | 209f644 | 2018-02-08 10:26:46 -0800 | [diff] [blame] | 695 | grpc_core::Resolver* resolver = static_cast<grpc_core::Resolver*>(arg); |
| 696 | resolver->Orphan(); |
Craig Tiller | 972470b | 2017-02-09 15:05:36 -0800 | [diff] [blame] | 697 | } |
| 698 | |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 699 | /* Destructor for channel_data */ |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 700 | static void cc_destroy_channel_elem(grpc_channel_element* elem) { |
Noah Eisen | be82e64 | 2018-02-09 09:16:55 -0800 | [diff] [blame] | 701 | channel_data* chand = static_cast<channel_data*>(elem->channel_data); |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 702 | if (chand->resolver != nullptr) { |
ncteisen | 274bbbe | 2017-06-08 14:57:11 -0700 | [diff] [blame] | 703 | GRPC_CLOSURE_SCHED( |
Mark D. Roth | 209f644 | 2018-02-08 10:26:46 -0800 | [diff] [blame] | 704 | GRPC_CLOSURE_CREATE(shutdown_resolver_locked, chand->resolver.release(), |
Yash Tibrewal | 0ee7574 | 2017-10-13 16:07:13 -0700 | [diff] [blame] | 705 | grpc_combiner_scheduler(chand->combiner)), |
Craig Tiller | 972470b | 2017-02-09 15:05:36 -0800 | [diff] [blame] | 706 | GRPC_ERROR_NONE); |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 707 | } |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 708 | if (chand->client_channel_factory != nullptr) { |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 709 | grpc_client_channel_factory_unref(chand->client_channel_factory); |
Mark D. Roth | 0e48a9a | 2016-09-08 14:14:39 -0700 | [diff] [blame] | 710 | } |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 711 | if (chand->lb_policy != nullptr) { |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 712 | grpc_pollset_set_del_pollset_set(chand->lb_policy->interested_parties(), |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 713 | chand->interested_parties); |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 714 | chand->lb_policy.reset(); |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 715 | } |
Craig Tiller | 613dafa | 2017-02-09 12:00:43 -0800 | [diff] [blame] | 716 | gpr_free(chand->info_lb_policy_name); |
| 717 | gpr_free(chand->info_service_config_json); |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 718 | if (chand->retry_throttle_data != nullptr) { |
Mark D. Roth | d6d192d | 2017-02-23 08:58:42 -0800 | [diff] [blame] | 719 | grpc_server_retry_throttle_data_unref(chand->retry_throttle_data); |
| 720 | } |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 721 | if (chand->method_params_table != nullptr) { |
Mark D. Roth | 3e7f2df | 2018-02-26 13:17:06 -0800 | [diff] [blame] | 722 | chand->method_params_table.reset(); |
Mark D. Roth | 9fe284e | 2016-09-12 11:22:27 -0700 | [diff] [blame] | 723 | } |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 724 | grpc_client_channel_stop_backup_polling(chand->interested_parties); |
| 725 | grpc_connectivity_state_destroy(&chand->state_tracker); |
| 726 | grpc_pollset_set_destroy(chand->interested_parties); |
| 727 | GRPC_COMBINER_UNREF(chand->combiner, "client_channel"); |
Craig Tiller | d8547751 | 2017-02-09 12:02:39 -0800 | [diff] [blame] | 728 | gpr_mu_destroy(&chand->info_mu); |
Alexander Polcyn | c3b1f18 | 2017-04-18 13:51:36 -0700 | [diff] [blame] | 729 | gpr_mu_destroy(&chand->external_connectivity_watcher_list_mu); |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 730 | } |
| 731 | |
| 732 | /************************************************************************* |
| 733 | * PER-CALL FUNCTIONS |
| 734 | */ |
| 735 | |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 736 | // Max number of batches that can be pending on a call at any given |
Mark D. Roth | 718c834 | 2018-02-28 13:00:04 -0800 | [diff] [blame] | 737 | // time. This includes one batch for each of the following ops: |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 738 | // recv_initial_metadata |
| 739 | // send_initial_metadata |
| 740 | // recv_message |
| 741 | // send_message |
| 742 | // recv_trailing_metadata |
| 743 | // send_trailing_metadata |
Mark D. Roth | 718c834 | 2018-02-28 13:00:04 -0800 | [diff] [blame] | 744 | #define MAX_PENDING_BATCHES 6 |
| 745 | |
| 746 | // Retry support: |
| 747 | // |
| 748 | // In order to support retries, we act as a proxy for stream op batches. |
| 749 | // When we get a batch from the surface, we add it to our list of pending |
| 750 | // batches, and we then use those batches to construct separate "child" |
| 751 | // batches to be started on the subchannel call. When the child batches |
| 752 | // return, we then decide which pending batches have been completed and |
| 753 | // schedule their callbacks accordingly. If a subchannel call fails and |
| 754 | // we want to retry it, we do a new pick and start again, constructing |
| 755 | // new "child" batches for the new subchannel call. |
| 756 | // |
| 757 | // Note that retries are committed when receiving data from the server |
| 758 | // (except for Trailers-Only responses). However, there may be many |
| 759 | // send ops started before receiving any data, so we may have already |
| 760 | // completed some number of send ops (and returned the completions up to |
| 761 | // the surface) by the time we realize that we need to retry. To deal |
| 762 | // with this, we cache data for send ops, so that we can replay them on a |
| 763 | // different subchannel call even after we have completed the original |
| 764 | // batches. |
| 765 | // |
| 766 | // There are two sets of data to maintain: |
| 767 | // - In call_data (in the parent channel), we maintain a list of pending |
| 768 | // ops and cached data for send ops. |
| 769 | // - In the subchannel call, we maintain state to indicate what ops have |
| 770 | // already been sent down to that call. |
| 771 | // |
| 772 | // When constructing the "child" batches, we compare those two sets of |
| 773 | // data to see which batches need to be sent to the subchannel call. |
| 774 | |
| 775 | // TODO(roth): In subsequent PRs: |
| 776 | // - add support for transparent retries (including initial metadata) |
| 777 | // - figure out how to record stats in census for retries |
| 778 | // (census filter is on top of this one) |
| 779 | // - add census stats for retries |
| 780 | |
| 781 | // State used for starting a retryable batch on a subchannel call. |
| 782 | // This provides its own grpc_transport_stream_op_batch and other data |
| 783 | // structures needed to populate the ops in the batch. |
| 784 | // We allocate one struct on the arena for each attempt at starting a |
| 785 | // batch on a given subchannel call. |
| 786 | typedef struct { |
| 787 | gpr_refcount refs; |
| 788 | grpc_call_element* elem; |
| 789 | grpc_subchannel_call* subchannel_call; // Holds a ref. |
| 790 | // The batch to use in the subchannel call. |
| 791 | // Its payload field points to subchannel_call_retry_state.batch_payload. |
| 792 | grpc_transport_stream_op_batch batch; |
| 793 | // For send_initial_metadata. |
| 794 | // Note that we need to make a copy of the initial metadata for each |
| 795 | // subchannel call instead of just referring to the copy in call_data, |
| 796 | // because filters in the subchannel stack will probably add entries, |
| 797 | // so we need to start in a pristine state for each attempt of the call. |
| 798 | grpc_linked_mdelem* send_initial_metadata_storage; |
| 799 | grpc_metadata_batch send_initial_metadata; |
| 800 | // For send_message. |
Mark D. Roth | 3d8b32d | 2018-03-09 13:25:40 -0800 | [diff] [blame^] | 801 | grpc_core::ManualConstructor<grpc_core::ByteStreamCache::CachingByteStream> |
| 802 | send_message; |
Mark D. Roth | 718c834 | 2018-02-28 13:00:04 -0800 | [diff] [blame] | 803 | // For send_trailing_metadata. |
| 804 | grpc_linked_mdelem* send_trailing_metadata_storage; |
| 805 | grpc_metadata_batch send_trailing_metadata; |
| 806 | // For intercepting recv_initial_metadata. |
| 807 | grpc_metadata_batch recv_initial_metadata; |
| 808 | grpc_closure recv_initial_metadata_ready; |
| 809 | bool trailing_metadata_available; |
| 810 | // For intercepting recv_message. |
| 811 | grpc_closure recv_message_ready; |
Mark D. Roth | 3d8b32d | 2018-03-09 13:25:40 -0800 | [diff] [blame^] | 812 | grpc_core::OrphanablePtr<grpc_core::ByteStream> recv_message; |
Mark D. Roth | 718c834 | 2018-02-28 13:00:04 -0800 | [diff] [blame] | 813 | // For intercepting recv_trailing_metadata. |
| 814 | grpc_metadata_batch recv_trailing_metadata; |
| 815 | grpc_transport_stream_stats collect_stats; |
| 816 | // For intercepting on_complete. |
| 817 | grpc_closure on_complete; |
| 818 | } subchannel_batch_data; |
| 819 | |
| 820 | // Retry state associated with a subchannel call. |
| 821 | // Stored in the parent_data of the subchannel call object. |
| 822 | typedef struct { |
| 823 | // subchannel_batch_data.batch.payload points to this. |
| 824 | grpc_transport_stream_op_batch_payload batch_payload; |
| 825 | // These fields indicate which ops have been started and completed on |
| 826 | // this subchannel call. |
| 827 | size_t started_send_message_count; |
| 828 | size_t completed_send_message_count; |
| 829 | size_t started_recv_message_count; |
| 830 | size_t completed_recv_message_count; |
| 831 | bool started_send_initial_metadata : 1; |
| 832 | bool completed_send_initial_metadata : 1; |
| 833 | bool started_send_trailing_metadata : 1; |
| 834 | bool completed_send_trailing_metadata : 1; |
| 835 | bool started_recv_initial_metadata : 1; |
| 836 | bool completed_recv_initial_metadata : 1; |
| 837 | bool started_recv_trailing_metadata : 1; |
| 838 | bool completed_recv_trailing_metadata : 1; |
| 839 | // State for callback processing. |
| 840 | bool retry_dispatched : 1; |
| 841 | bool recv_initial_metadata_ready_deferred : 1; |
| 842 | bool recv_message_ready_deferred : 1; |
| 843 | grpc_error* recv_initial_metadata_error; |
| 844 | grpc_error* recv_message_error; |
| 845 | } subchannel_call_retry_state; |
| 846 | |
| 847 | // Pending batches stored in call data. |
| 848 | typedef struct { |
| 849 | // The pending batch. If nullptr, this slot is empty. |
| 850 | grpc_transport_stream_op_batch* batch; |
| 851 | // Indicates whether payload for send ops has been cached in call data. |
| 852 | bool send_ops_cached; |
| 853 | } pending_batch; |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 854 | |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 855 | /** Call data. Holds a pointer to grpc_subchannel_call and the |
| 856 | associated machinery to create such a pointer. |
| 857 | Handles queueing of stream ops until a call object is ready, waiting |
| 858 | for initial metadata before trying to create a call object, |
| 859 | and handling cancellation gracefully. */ |
| 860 | typedef struct client_channel_call_data { |
Mark D. Roth | 72f6da8 | 2016-09-02 13:42:38 -0700 | [diff] [blame] | 861 | // State for handling deadlines. |
| 862 | // The code in deadline_filter.c requires this to be the first field. |
Mark D. Roth | 72f6da8 | 2016-09-02 13:42:38 -0700 | [diff] [blame] | 863 | // TODO(roth): This is slightly sub-optimal in that grpc_deadline_state |
Mark D. Roth | 66f3d2b | 2017-09-01 09:02:17 -0700 | [diff] [blame] | 864 | // and this struct both independently store pointers to the call stack |
| 865 | // and call combiner. If/when we have time, find a way to avoid this |
| 866 | // without breaking the grpc_deadline_state abstraction. |
Mark D. Roth | 72f6da8 | 2016-09-02 13:42:38 -0700 | [diff] [blame] | 867 | grpc_deadline_state deadline_state; |
Mark D. Roth | f28763c | 2016-09-14 15:18:40 -0700 | [diff] [blame] | 868 | |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 869 | grpc_slice path; // Request path. |
Mark D. Roth | e40dd29 | 2016-10-05 14:58:37 -0700 | [diff] [blame] | 870 | gpr_timespec call_start_time; |
Craig Tiller | 89c1428 | 2017-07-19 15:32:27 -0700 | [diff] [blame] | 871 | grpc_millis deadline; |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 872 | gpr_arena* arena; |
| 873 | grpc_call_stack* owning_call; |
| 874 | grpc_call_combiner* call_combiner; |
Mark D. Roth | 76e264b | 2017-08-25 09:03:33 -0700 | [diff] [blame] | 875 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 876 | grpc_server_retry_throttle_data* retry_throttle_data; |
Mark D. Roth | 3e7f2df | 2018-02-26 13:17:06 -0800 | [diff] [blame] | 877 | grpc_core::RefCountedPtr<ClientChannelMethodParams> method_params; |
Mark D. Roth | aa850a7 | 2016-09-26 13:38:02 -0700 | [diff] [blame] | 878 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 879 | grpc_subchannel_call* subchannel_call; |
Mark D. Roth | 718c834 | 2018-02-28 13:00:04 -0800 | [diff] [blame] | 880 | |
| 881 | // Set when we get a cancel_stream op. |
| 882 | grpc_error* cancel_error; |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 883 | |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 884 | grpc_core::LoadBalancingPolicy::PickState pick; |
Mark D. Roth | 718c834 | 2018-02-28 13:00:04 -0800 | [diff] [blame] | 885 | grpc_closure pick_closure; |
| 886 | grpc_closure pick_cancel_closure; |
Mark D. Roth | 60751fe | 2017-07-07 12:50:33 -0700 | [diff] [blame] | 887 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 888 | grpc_polling_entity* pollent; |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 889 | |
Mark D. Roth | 718c834 | 2018-02-28 13:00:04 -0800 | [diff] [blame] | 890 | // Batches are added to this list when received from above. |
| 891 | // They are removed when we are done handling the batch (i.e., when |
| 892 | // either we have invoked all of the batch's callbacks or we have |
| 893 | // passed the batch down to the subchannel call and are not |
| 894 | // intercepting any of its callbacks). |
| 895 | pending_batch pending_batches[MAX_PENDING_BATCHES]; |
| 896 | bool pending_send_initial_metadata : 1; |
| 897 | bool pending_send_message : 1; |
| 898 | bool pending_send_trailing_metadata : 1; |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 899 | |
Mark D. Roth | 718c834 | 2018-02-28 13:00:04 -0800 | [diff] [blame] | 900 | // Retry state. |
| 901 | bool enable_retries : 1; |
| 902 | bool retry_committed : 1; |
| 903 | bool last_attempt_got_server_pushback : 1; |
| 904 | int num_attempts_completed; |
| 905 | size_t bytes_buffered_for_retry; |
| 906 | grpc_core::ManualConstructor<grpc_core::BackOff> retry_backoff; |
| 907 | grpc_timer retry_timer; |
David Garcia Quintas | d1a47f1 | 2016-09-02 12:46:44 +0200 | [diff] [blame] | 908 | |
Mark D. Roth | 718c834 | 2018-02-28 13:00:04 -0800 | [diff] [blame] | 909 | // Cached data for retrying send ops. |
| 910 | // send_initial_metadata |
| 911 | bool seen_send_initial_metadata; |
| 912 | grpc_linked_mdelem* send_initial_metadata_storage; |
| 913 | grpc_metadata_batch send_initial_metadata; |
| 914 | uint32_t send_initial_metadata_flags; |
| 915 | gpr_atm* peer_string; |
| 916 | // send_message |
| 917 | // When we get a send_message op, we replace the original byte stream |
Mark D. Roth | 3d8b32d | 2018-03-09 13:25:40 -0800 | [diff] [blame^] | 918 | // with a CachingByteStream that caches the slices to a local buffer for |
| 919 | // use in retries. |
Mark D. Roth | 718c834 | 2018-02-28 13:00:04 -0800 | [diff] [blame] | 920 | // Note: We inline the cache for the first 3 send_message ops and use |
| 921 | // dynamic allocation after that. This number was essentially picked |
| 922 | // at random; it could be changed in the future to tune performance. |
Mark D. Roth | 3d8b32d | 2018-03-09 13:25:40 -0800 | [diff] [blame^] | 923 | grpc_core::InlinedVector<grpc_core::ByteStreamCache*, 3> send_messages; |
Mark D. Roth | 718c834 | 2018-02-28 13:00:04 -0800 | [diff] [blame] | 924 | // send_trailing_metadata |
| 925 | bool seen_send_trailing_metadata; |
| 926 | grpc_linked_mdelem* send_trailing_metadata_storage; |
| 927 | grpc_metadata_batch send_trailing_metadata; |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 928 | } call_data; |
| 929 | |
Mark D. Roth | 718c834 | 2018-02-28 13:00:04 -0800 | [diff] [blame] | 930 | // Forward declarations. |
| 931 | static void retry_commit(grpc_call_element* elem, |
| 932 | subchannel_call_retry_state* retry_state); |
| 933 | static void start_internal_recv_trailing_metadata(grpc_call_element* elem); |
| 934 | static void on_complete(void* arg, grpc_error* error); |
| 935 | static void start_retriable_subchannel_batches(void* arg, grpc_error* ignored); |
| 936 | static void pick_after_resolver_result_start_locked(grpc_call_element* elem); |
| 937 | static void start_pick_locked(void* arg, grpc_error* ignored); |
Craig Tiller | 8b1d59c | 2016-12-27 15:15:30 -0800 | [diff] [blame] | 938 | |
Mark D. Roth | 718c834 | 2018-02-28 13:00:04 -0800 | [diff] [blame] | 939 | // |
| 940 | // send op data caching |
| 941 | // |
| 942 | |
| 943 | // Caches data for send ops so that it can be retried later, if not |
| 944 | // already cached. |
| 945 | static void maybe_cache_send_ops_for_batch(call_data* calld, |
| 946 | pending_batch* pending) { |
| 947 | if (pending->send_ops_cached) return; |
| 948 | pending->send_ops_cached = true; |
| 949 | grpc_transport_stream_op_batch* batch = pending->batch; |
| 950 | // Save a copy of metadata for send_initial_metadata ops. |
Mark D. Roth | 76e264b | 2017-08-25 09:03:33 -0700 | [diff] [blame] | 951 | if (batch->send_initial_metadata) { |
Mark D. Roth | 718c834 | 2018-02-28 13:00:04 -0800 | [diff] [blame] | 952 | calld->seen_send_initial_metadata = true; |
| 953 | GPR_ASSERT(calld->send_initial_metadata_storage == nullptr); |
| 954 | grpc_metadata_batch* send_initial_metadata = |
| 955 | batch->payload->send_initial_metadata.send_initial_metadata; |
| 956 | calld->send_initial_metadata_storage = (grpc_linked_mdelem*)gpr_arena_alloc( |
| 957 | calld->arena, |
| 958 | sizeof(grpc_linked_mdelem) * send_initial_metadata->list.count); |
| 959 | grpc_metadata_batch_copy(send_initial_metadata, |
| 960 | &calld->send_initial_metadata, |
| 961 | calld->send_initial_metadata_storage); |
| 962 | calld->send_initial_metadata_flags = |
| 963 | batch->payload->send_initial_metadata.send_initial_metadata_flags; |
| 964 | calld->peer_string = batch->payload->send_initial_metadata.peer_string; |
| 965 | } |
| 966 | // Set up cache for send_message ops. |
| 967 | if (batch->send_message) { |
Mark D. Roth | 3d8b32d | 2018-03-09 13:25:40 -0800 | [diff] [blame^] | 968 | grpc_core::ByteStreamCache* cache = |
| 969 | static_cast<grpc_core::ByteStreamCache*>( |
| 970 | gpr_arena_alloc(calld->arena, sizeof(grpc_core::ByteStreamCache))); |
| 971 | new (cache) grpc_core::ByteStreamCache( |
| 972 | std::move(batch->payload->send_message.send_message)); |
Mark D. Roth | 718c834 | 2018-02-28 13:00:04 -0800 | [diff] [blame] | 973 | calld->send_messages.push_back(cache); |
| 974 | } |
| 975 | // Save metadata batch for send_trailing_metadata ops. |
| 976 | if (batch->send_trailing_metadata) { |
| 977 | calld->seen_send_trailing_metadata = true; |
| 978 | GPR_ASSERT(calld->send_trailing_metadata_storage == nullptr); |
| 979 | grpc_metadata_batch* send_trailing_metadata = |
| 980 | batch->payload->send_trailing_metadata.send_trailing_metadata; |
| 981 | calld->send_trailing_metadata_storage = |
| 982 | (grpc_linked_mdelem*)gpr_arena_alloc( |
| 983 | calld->arena, |
| 984 | sizeof(grpc_linked_mdelem) * send_trailing_metadata->list.count); |
| 985 | grpc_metadata_batch_copy(send_trailing_metadata, |
| 986 | &calld->send_trailing_metadata, |
| 987 | calld->send_trailing_metadata_storage); |
Mark D. Roth | 76e264b | 2017-08-25 09:03:33 -0700 | [diff] [blame] | 988 | } |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 989 | } |
| 990 | |
Mark D. Roth | 718c834 | 2018-02-28 13:00:04 -0800 | [diff] [blame] | 991 | // Frees cached send ops that have already been completed after |
| 992 | // committing the call. |
| 993 | static void free_cached_send_op_data_after_commit( |
| 994 | grpc_call_element* elem, subchannel_call_retry_state* retry_state) { |
Noah Eisen | be82e64 | 2018-02-09 09:16:55 -0800 | [diff] [blame] | 995 | channel_data* chand = static_cast<channel_data*>(elem->channel_data); |
| 996 | call_data* calld = static_cast<call_data*>(elem->call_data); |
Mark D. Roth | 718c834 | 2018-02-28 13:00:04 -0800 | [diff] [blame] | 997 | if (retry_state->completed_send_initial_metadata) { |
| 998 | grpc_metadata_batch_destroy(&calld->send_initial_metadata); |
Mark D. Roth | 60751fe | 2017-07-07 12:50:33 -0700 | [diff] [blame] | 999 | } |
Mark D. Roth | 718c834 | 2018-02-28 13:00:04 -0800 | [diff] [blame] | 1000 | for (size_t i = 0; i < retry_state->completed_send_message_count; ++i) { |
| 1001 | if (grpc_client_channel_trace.enabled()) { |
| 1002 | gpr_log(GPR_DEBUG, |
| 1003 | "chand=%p calld=%p: destroying calld->send_messages[%" PRIuPTR |
| 1004 | "]", |
| 1005 | chand, calld, i); |
| 1006 | } |
Mark D. Roth | 3d8b32d | 2018-03-09 13:25:40 -0800 | [diff] [blame^] | 1007 | calld->send_messages[i]->Destroy(); |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 1008 | } |
Mark D. Roth | 718c834 | 2018-02-28 13:00:04 -0800 | [diff] [blame] | 1009 | if (retry_state->completed_send_trailing_metadata) { |
| 1010 | grpc_metadata_batch_destroy(&calld->send_trailing_metadata); |
| 1011 | } |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 1012 | } |
| 1013 | |
Mark D. Roth | 718c834 | 2018-02-28 13:00:04 -0800 | [diff] [blame] | 1014 | // Frees cached send ops that were completed by the completed batch in |
| 1015 | // batch_data. Used when batches are completed after the call is committed. |
| 1016 | static void free_cached_send_op_data_for_completed_batch( |
| 1017 | grpc_call_element* elem, subchannel_batch_data* batch_data, |
| 1018 | subchannel_call_retry_state* retry_state) { |
Noah Eisen | be82e64 | 2018-02-09 09:16:55 -0800 | [diff] [blame] | 1019 | channel_data* chand = static_cast<channel_data*>(elem->channel_data); |
| 1020 | call_data* calld = static_cast<call_data*>(elem->call_data); |
Mark D. Roth | 718c834 | 2018-02-28 13:00:04 -0800 | [diff] [blame] | 1021 | if (batch_data->batch.send_initial_metadata) { |
| 1022 | grpc_metadata_batch_destroy(&calld->send_initial_metadata); |
| 1023 | } |
| 1024 | if (batch_data->batch.send_message) { |
| 1025 | if (grpc_client_channel_trace.enabled()) { |
| 1026 | gpr_log(GPR_DEBUG, |
| 1027 | "chand=%p calld=%p: destroying calld->send_messages[%" PRIuPTR |
| 1028 | "]", |
| 1029 | chand, calld, retry_state->completed_send_message_count - 1); |
| 1030 | } |
Mark D. Roth | 3d8b32d | 2018-03-09 13:25:40 -0800 | [diff] [blame^] | 1031 | calld->send_messages[retry_state->completed_send_message_count - 1] |
| 1032 | ->Destroy(); |
Mark D. Roth | 718c834 | 2018-02-28 13:00:04 -0800 | [diff] [blame] | 1033 | } |
| 1034 | if (batch_data->batch.send_trailing_metadata) { |
| 1035 | grpc_metadata_batch_destroy(&calld->send_trailing_metadata); |
| 1036 | } |
| 1037 | } |
| 1038 | |
| 1039 | // |
| 1040 | // pending_batches management |
| 1041 | // |
| 1042 | |
| 1043 | // Returns the index into calld->pending_batches to be used for batch. |
| 1044 | static size_t get_batch_index(grpc_transport_stream_op_batch* batch) { |
| 1045 | // Note: It is important the send_initial_metadata be the first entry |
| 1046 | // here, since the code in pick_subchannel_locked() assumes it will be. |
| 1047 | if (batch->send_initial_metadata) return 0; |
| 1048 | if (batch->send_message) return 1; |
| 1049 | if (batch->send_trailing_metadata) return 2; |
| 1050 | if (batch->recv_initial_metadata) return 3; |
| 1051 | if (batch->recv_message) return 4; |
| 1052 | if (batch->recv_trailing_metadata) return 5; |
| 1053 | GPR_UNREACHABLE_CODE(return (size_t)-1); |
| 1054 | } |
| 1055 | |
| 1056 | // This is called via the call combiner, so access to calld is synchronized. |
| 1057 | static void pending_batches_add(grpc_call_element* elem, |
| 1058 | grpc_transport_stream_op_batch* batch) { |
| 1059 | channel_data* chand = static_cast<channel_data*>(elem->channel_data); |
| 1060 | call_data* calld = static_cast<call_data*>(elem->call_data); |
| 1061 | const size_t idx = get_batch_index(batch); |
Craig Tiller | 6014e8a | 2017-10-16 13:50:29 -0700 | [diff] [blame] | 1062 | if (grpc_client_channel_trace.enabled()) { |
Mark D. Roth | 718c834 | 2018-02-28 13:00:04 -0800 | [diff] [blame] | 1063 | gpr_log(GPR_DEBUG, |
| 1064 | "chand=%p calld=%p: adding pending batch at index %" PRIuPTR, chand, |
| 1065 | calld, idx); |
Mark D. Roth | 60751fe | 2017-07-07 12:50:33 -0700 | [diff] [blame] | 1066 | } |
Mark D. Roth | 718c834 | 2018-02-28 13:00:04 -0800 | [diff] [blame] | 1067 | pending_batch* pending = &calld->pending_batches[idx]; |
| 1068 | GPR_ASSERT(pending->batch == nullptr); |
| 1069 | pending->batch = batch; |
| 1070 | pending->send_ops_cached = false; |
| 1071 | if (calld->enable_retries) { |
| 1072 | // Update state in calld about pending batches. |
| 1073 | // Also check if the batch takes us over the retry buffer limit. |
| 1074 | // Note: We don't check the size of trailing metadata here, because |
| 1075 | // gRPC clients do not send trailing metadata. |
| 1076 | if (batch->send_initial_metadata) { |
| 1077 | calld->pending_send_initial_metadata = true; |
| 1078 | calld->bytes_buffered_for_retry += grpc_metadata_batch_size( |
| 1079 | batch->payload->send_initial_metadata.send_initial_metadata); |
| 1080 | } |
| 1081 | if (batch->send_message) { |
| 1082 | calld->pending_send_message = true; |
| 1083 | calld->bytes_buffered_for_retry += |
Mark D. Roth | 3d8b32d | 2018-03-09 13:25:40 -0800 | [diff] [blame^] | 1084 | batch->payload->send_message.send_message->length(); |
Mark D. Roth | 718c834 | 2018-02-28 13:00:04 -0800 | [diff] [blame] | 1085 | } |
| 1086 | if (batch->send_trailing_metadata) { |
| 1087 | calld->pending_send_trailing_metadata = true; |
| 1088 | } |
| 1089 | if (calld->bytes_buffered_for_retry > chand->per_rpc_retry_buffer_size) { |
| 1090 | if (grpc_client_channel_trace.enabled()) { |
| 1091 | gpr_log(GPR_DEBUG, |
| 1092 | "chand=%p calld=%p: exceeded retry buffer size, committing", |
| 1093 | chand, calld); |
| 1094 | } |
| 1095 | subchannel_call_retry_state* retry_state = |
| 1096 | calld->subchannel_call == nullptr |
| 1097 | ? nullptr |
| 1098 | : static_cast<subchannel_call_retry_state*>( |
| 1099 | grpc_connected_subchannel_call_get_parent_data( |
| 1100 | calld->subchannel_call)); |
| 1101 | retry_commit(elem, retry_state); |
| 1102 | // If we are not going to retry and have not yet started, pretend |
| 1103 | // retries are disabled so that we don't bother with retry overhead. |
| 1104 | if (calld->num_attempts_completed == 0) { |
| 1105 | if (grpc_client_channel_trace.enabled()) { |
| 1106 | gpr_log(GPR_DEBUG, |
| 1107 | "chand=%p calld=%p: disabling retries before first attempt", |
| 1108 | chand, calld); |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 1109 | } |
Mark D. Roth | 718c834 | 2018-02-28 13:00:04 -0800 | [diff] [blame] | 1110 | calld->enable_retries = false; |
Craig Tiller | 11c17d4 | 2017-03-13 13:36:34 -0700 | [diff] [blame] | 1111 | } |
| 1112 | } |
| 1113 | } |
Craig Tiller | 11c17d4 | 2017-03-13 13:36:34 -0700 | [diff] [blame] | 1114 | } |
Craig Tiller | ea4a4f1 | 2017-03-13 13:36:52 -0700 | [diff] [blame] | 1115 | |
Mark D. Roth | 718c834 | 2018-02-28 13:00:04 -0800 | [diff] [blame] | 1116 | static void pending_batch_clear(call_data* calld, pending_batch* pending) { |
| 1117 | if (calld->enable_retries) { |
| 1118 | if (pending->batch->send_initial_metadata) { |
| 1119 | calld->pending_send_initial_metadata = false; |
| 1120 | } |
| 1121 | if (pending->batch->send_message) { |
| 1122 | calld->pending_send_message = false; |
| 1123 | } |
| 1124 | if (pending->batch->send_trailing_metadata) { |
| 1125 | calld->pending_send_trailing_metadata = false; |
| 1126 | } |
| 1127 | } |
| 1128 | pending->batch = nullptr; |
| 1129 | } |
| 1130 | |
| 1131 | // This is called via the call combiner, so access to calld is synchronized. |
| 1132 | static void fail_pending_batch_in_call_combiner(void* arg, grpc_error* error) { |
| 1133 | grpc_transport_stream_op_batch* batch = |
| 1134 | static_cast<grpc_transport_stream_op_batch*>(arg); |
| 1135 | call_data* calld = static_cast<call_data*>(batch->handler_private.extra_arg); |
| 1136 | // Note: This will release the call combiner. |
| 1137 | grpc_transport_stream_op_batch_finish_with_failure( |
| 1138 | batch, GRPC_ERROR_REF(error), calld->call_combiner); |
| 1139 | } |
| 1140 | |
| 1141 | // This is called via the call combiner, so access to calld is synchronized. |
| 1142 | // If yield_call_combiner is true, assumes responsibility for yielding |
| 1143 | // the call combiner. |
| 1144 | static void pending_batches_fail(grpc_call_element* elem, grpc_error* error, |
| 1145 | bool yield_call_combiner) { |
| 1146 | GPR_ASSERT(error != GRPC_ERROR_NONE); |
| 1147 | call_data* calld = static_cast<call_data*>(elem->call_data); |
| 1148 | if (grpc_client_channel_trace.enabled()) { |
| 1149 | size_t num_batches = 0; |
| 1150 | for (size_t i = 0; i < GPR_ARRAY_SIZE(calld->pending_batches); ++i) { |
| 1151 | if (calld->pending_batches[i].batch != nullptr) ++num_batches; |
| 1152 | } |
| 1153 | gpr_log(GPR_DEBUG, |
| 1154 | "chand=%p calld=%p: failing %" PRIuPTR " pending batches: %s", |
| 1155 | elem->channel_data, calld, num_batches, grpc_error_string(error)); |
| 1156 | } |
| 1157 | grpc_transport_stream_op_batch* |
| 1158 | batches[GPR_ARRAY_SIZE(calld->pending_batches)]; |
| 1159 | size_t num_batches = 0; |
| 1160 | for (size_t i = 0; i < GPR_ARRAY_SIZE(calld->pending_batches); ++i) { |
| 1161 | pending_batch* pending = &calld->pending_batches[i]; |
| 1162 | grpc_transport_stream_op_batch* batch = pending->batch; |
| 1163 | if (batch != nullptr) { |
| 1164 | batches[num_batches++] = batch; |
| 1165 | pending_batch_clear(calld, pending); |
| 1166 | } |
| 1167 | } |
| 1168 | for (size_t i = yield_call_combiner ? 1 : 0; i < num_batches; ++i) { |
| 1169 | grpc_transport_stream_op_batch* batch = batches[i]; |
| 1170 | batch->handler_private.extra_arg = calld; |
| 1171 | GRPC_CLOSURE_INIT(&batch->handler_private.closure, |
| 1172 | fail_pending_batch_in_call_combiner, batch, |
| 1173 | grpc_schedule_on_exec_ctx); |
| 1174 | GRPC_CALL_COMBINER_START(calld->call_combiner, |
| 1175 | &batch->handler_private.closure, |
| 1176 | GRPC_ERROR_REF(error), "pending_batches_fail"); |
| 1177 | } |
| 1178 | if (yield_call_combiner) { |
| 1179 | if (num_batches > 0) { |
| 1180 | // Note: This will release the call combiner. |
| 1181 | grpc_transport_stream_op_batch_finish_with_failure( |
| 1182 | batches[0], GRPC_ERROR_REF(error), calld->call_combiner); |
| 1183 | } else { |
| 1184 | GRPC_CALL_COMBINER_STOP(calld->call_combiner, "pending_batches_fail"); |
| 1185 | } |
| 1186 | } |
| 1187 | GRPC_ERROR_UNREF(error); |
| 1188 | } |
| 1189 | |
| 1190 | // This is called via the call combiner, so access to calld is synchronized. |
| 1191 | static void resume_pending_batch_in_call_combiner(void* arg, |
| 1192 | grpc_error* ignored) { |
| 1193 | grpc_transport_stream_op_batch* batch = |
| 1194 | static_cast<grpc_transport_stream_op_batch*>(arg); |
| 1195 | grpc_subchannel_call* subchannel_call = |
| 1196 | static_cast<grpc_subchannel_call*>(batch->handler_private.extra_arg); |
| 1197 | // Note: This will release the call combiner. |
| 1198 | grpc_subchannel_call_process_op(subchannel_call, batch); |
| 1199 | } |
| 1200 | |
| 1201 | // This is called via the call combiner, so access to calld is synchronized. |
| 1202 | static void pending_batches_resume(grpc_call_element* elem) { |
Noah Eisen | be82e64 | 2018-02-09 09:16:55 -0800 | [diff] [blame] | 1203 | channel_data* chand = static_cast<channel_data*>(elem->channel_data); |
| 1204 | call_data* calld = static_cast<call_data*>(elem->call_data); |
Mark D. Roth | 718c834 | 2018-02-28 13:00:04 -0800 | [diff] [blame] | 1205 | if (calld->enable_retries) { |
| 1206 | start_retriable_subchannel_batches(elem, GRPC_ERROR_NONE); |
| 1207 | return; |
| 1208 | } |
| 1209 | // Retries not enabled; send down batches as-is. |
| 1210 | if (grpc_client_channel_trace.enabled()) { |
| 1211 | size_t num_batches = 0; |
| 1212 | for (size_t i = 0; i < GPR_ARRAY_SIZE(calld->pending_batches); ++i) { |
| 1213 | if (calld->pending_batches[i].batch != nullptr) ++num_batches; |
| 1214 | } |
| 1215 | gpr_log(GPR_DEBUG, |
| 1216 | "chand=%p calld=%p: starting %" PRIuPTR |
| 1217 | " pending batches on subchannel_call=%p", |
| 1218 | chand, calld, num_batches, calld->subchannel_call); |
| 1219 | } |
| 1220 | grpc_transport_stream_op_batch* |
| 1221 | batches[GPR_ARRAY_SIZE(calld->pending_batches)]; |
| 1222 | size_t num_batches = 0; |
| 1223 | for (size_t i = 0; i < GPR_ARRAY_SIZE(calld->pending_batches); ++i) { |
| 1224 | pending_batch* pending = &calld->pending_batches[i]; |
| 1225 | grpc_transport_stream_op_batch* batch = pending->batch; |
| 1226 | if (batch != nullptr) { |
| 1227 | batches[num_batches++] = batch; |
| 1228 | pending_batch_clear(calld, pending); |
| 1229 | } |
| 1230 | } |
| 1231 | for (size_t i = 1; i < num_batches; ++i) { |
| 1232 | grpc_transport_stream_op_batch* batch = batches[i]; |
| 1233 | batch->handler_private.extra_arg = calld->subchannel_call; |
| 1234 | GRPC_CLOSURE_INIT(&batch->handler_private.closure, |
| 1235 | resume_pending_batch_in_call_combiner, batch, |
| 1236 | grpc_schedule_on_exec_ctx); |
| 1237 | GRPC_CALL_COMBINER_START(calld->call_combiner, |
| 1238 | &batch->handler_private.closure, GRPC_ERROR_NONE, |
| 1239 | "pending_batches_resume"); |
| 1240 | } |
| 1241 | GPR_ASSERT(num_batches > 0); |
| 1242 | // Note: This will release the call combiner. |
| 1243 | grpc_subchannel_call_process_op(calld->subchannel_call, batches[0]); |
| 1244 | } |
| 1245 | |
| 1246 | static void maybe_clear_pending_batch(grpc_call_element* elem, |
| 1247 | pending_batch* pending) { |
| 1248 | channel_data* chand = static_cast<channel_data*>(elem->channel_data); |
| 1249 | call_data* calld = static_cast<call_data*>(elem->call_data); |
| 1250 | grpc_transport_stream_op_batch* batch = pending->batch; |
| 1251 | // We clear the pending batch if all of its callbacks have been |
| 1252 | // scheduled and reset to nullptr. |
| 1253 | if (batch->on_complete == nullptr && |
| 1254 | (!batch->recv_initial_metadata || |
| 1255 | batch->payload->recv_initial_metadata.recv_initial_metadata_ready == |
| 1256 | nullptr) && |
| 1257 | (!batch->recv_message || |
| 1258 | batch->payload->recv_message.recv_message_ready == nullptr)) { |
| 1259 | if (grpc_client_channel_trace.enabled()) { |
| 1260 | gpr_log(GPR_DEBUG, "chand=%p calld=%p: clearing pending batch", chand, |
| 1261 | calld); |
| 1262 | } |
| 1263 | pending_batch_clear(calld, pending); |
| 1264 | } |
| 1265 | } |
| 1266 | |
| 1267 | // Returns true if all ops in the pending batch have been completed. |
| 1268 | static bool pending_batch_is_completed( |
| 1269 | pending_batch* pending, call_data* calld, |
| 1270 | subchannel_call_retry_state* retry_state) { |
| 1271 | if (pending->batch == nullptr || pending->batch->on_complete == nullptr) { |
| 1272 | return false; |
| 1273 | } |
| 1274 | if (pending->batch->send_initial_metadata && |
| 1275 | !retry_state->completed_send_initial_metadata) { |
| 1276 | return false; |
| 1277 | } |
| 1278 | if (pending->batch->send_message && |
| 1279 | retry_state->completed_send_message_count < calld->send_messages.size()) { |
| 1280 | return false; |
| 1281 | } |
| 1282 | if (pending->batch->send_trailing_metadata && |
| 1283 | !retry_state->completed_send_trailing_metadata) { |
| 1284 | return false; |
| 1285 | } |
| 1286 | if (pending->batch->recv_initial_metadata && |
| 1287 | !retry_state->completed_recv_initial_metadata) { |
| 1288 | return false; |
| 1289 | } |
| 1290 | if (pending->batch->recv_message && |
| 1291 | retry_state->completed_recv_message_count < |
| 1292 | retry_state->started_recv_message_count) { |
| 1293 | return false; |
| 1294 | } |
| 1295 | if (pending->batch->recv_trailing_metadata && |
| 1296 | !retry_state->completed_recv_trailing_metadata) { |
| 1297 | return false; |
| 1298 | } |
| 1299 | return true; |
| 1300 | } |
| 1301 | |
| 1302 | // Returns true if any op in the batch was not yet started. |
| 1303 | static bool pending_batch_is_unstarted( |
| 1304 | pending_batch* pending, call_data* calld, |
| 1305 | subchannel_call_retry_state* retry_state) { |
| 1306 | if (pending->batch == nullptr || pending->batch->on_complete == nullptr) { |
| 1307 | return false; |
| 1308 | } |
| 1309 | if (pending->batch->send_initial_metadata && |
| 1310 | !retry_state->started_send_initial_metadata) { |
| 1311 | return true; |
| 1312 | } |
| 1313 | if (pending->batch->send_message && |
| 1314 | retry_state->started_send_message_count < calld->send_messages.size()) { |
| 1315 | return true; |
| 1316 | } |
| 1317 | if (pending->batch->send_trailing_metadata && |
| 1318 | !retry_state->started_send_trailing_metadata) { |
| 1319 | return true; |
| 1320 | } |
| 1321 | if (pending->batch->recv_initial_metadata && |
| 1322 | !retry_state->started_recv_initial_metadata) { |
| 1323 | return true; |
| 1324 | } |
| 1325 | if (pending->batch->recv_message && |
| 1326 | retry_state->completed_recv_message_count == |
| 1327 | retry_state->started_recv_message_count) { |
| 1328 | return true; |
| 1329 | } |
| 1330 | if (pending->batch->recv_trailing_metadata && |
| 1331 | !retry_state->started_recv_trailing_metadata) { |
| 1332 | return true; |
| 1333 | } |
| 1334 | return false; |
| 1335 | } |
| 1336 | |
| 1337 | // |
| 1338 | // retry code |
| 1339 | // |
| 1340 | |
| 1341 | // Commits the call so that no further retry attempts will be performed. |
| 1342 | static void retry_commit(grpc_call_element* elem, |
| 1343 | subchannel_call_retry_state* retry_state) { |
| 1344 | channel_data* chand = static_cast<channel_data*>(elem->channel_data); |
| 1345 | call_data* calld = static_cast<call_data*>(elem->call_data); |
| 1346 | if (calld->retry_committed) return; |
| 1347 | calld->retry_committed = true; |
| 1348 | if (grpc_client_channel_trace.enabled()) { |
| 1349 | gpr_log(GPR_DEBUG, "chand=%p calld=%p: committing retries", chand, calld); |
| 1350 | } |
| 1351 | if (retry_state != nullptr) { |
| 1352 | free_cached_send_op_data_after_commit(elem, retry_state); |
| 1353 | } |
| 1354 | } |
| 1355 | |
| 1356 | // Starts a retry after appropriate back-off. |
| 1357 | static void do_retry(grpc_call_element* elem, |
| 1358 | subchannel_call_retry_state* retry_state, |
| 1359 | grpc_millis server_pushback_ms) { |
| 1360 | channel_data* chand = static_cast<channel_data*>(elem->channel_data); |
| 1361 | call_data* calld = static_cast<call_data*>(elem->call_data); |
| 1362 | GPR_ASSERT(calld->method_params != nullptr); |
| 1363 | const ClientChannelMethodParams::RetryPolicy* retry_policy = |
| 1364 | calld->method_params->retry_policy(); |
| 1365 | GPR_ASSERT(retry_policy != nullptr); |
| 1366 | // Reset subchannel call and connected subchannel. |
| 1367 | if (calld->subchannel_call != nullptr) { |
| 1368 | GRPC_SUBCHANNEL_CALL_UNREF(calld->subchannel_call, |
| 1369 | "client_channel_call_retry"); |
| 1370 | calld->subchannel_call = nullptr; |
| 1371 | } |
| 1372 | if (calld->pick.connected_subchannel != nullptr) { |
| 1373 | calld->pick.connected_subchannel.reset(); |
| 1374 | } |
| 1375 | // Compute backoff delay. |
| 1376 | grpc_millis next_attempt_time; |
| 1377 | if (server_pushback_ms >= 0) { |
| 1378 | next_attempt_time = grpc_core::ExecCtx::Get()->Now() + server_pushback_ms; |
| 1379 | calld->last_attempt_got_server_pushback = true; |
| 1380 | } else { |
| 1381 | if (calld->num_attempts_completed == 1 || |
| 1382 | calld->last_attempt_got_server_pushback) { |
| 1383 | calld->retry_backoff.Init( |
| 1384 | grpc_core::BackOff::Options() |
| 1385 | .set_initial_backoff(retry_policy->initial_backoff) |
| 1386 | .set_multiplier(retry_policy->backoff_multiplier) |
| 1387 | .set_jitter(RETRY_BACKOFF_JITTER) |
| 1388 | .set_max_backoff(retry_policy->max_backoff)); |
| 1389 | calld->last_attempt_got_server_pushback = false; |
| 1390 | } |
| 1391 | next_attempt_time = calld->retry_backoff->NextAttemptTime(); |
| 1392 | } |
| 1393 | if (grpc_client_channel_trace.enabled()) { |
| 1394 | gpr_log(GPR_DEBUG, |
| 1395 | "chand=%p calld=%p: retrying failed call in %" PRIuPTR " ms", chand, |
| 1396 | calld, next_attempt_time - grpc_core::ExecCtx::Get()->Now()); |
| 1397 | } |
| 1398 | // Schedule retry after computed delay. |
| 1399 | GRPC_CLOSURE_INIT(&calld->pick_closure, start_pick_locked, elem, |
| 1400 | grpc_combiner_scheduler(chand->combiner)); |
| 1401 | grpc_timer_init(&calld->retry_timer, next_attempt_time, &calld->pick_closure); |
| 1402 | // Update bookkeeping. |
| 1403 | if (retry_state != nullptr) retry_state->retry_dispatched = true; |
| 1404 | } |
| 1405 | |
| 1406 | // Returns true if the call is being retried. |
| 1407 | static bool maybe_retry(grpc_call_element* elem, |
| 1408 | subchannel_batch_data* batch_data, |
| 1409 | grpc_status_code status, |
| 1410 | grpc_mdelem* server_pushback_md) { |
| 1411 | channel_data* chand = static_cast<channel_data*>(elem->channel_data); |
| 1412 | call_data* calld = static_cast<call_data*>(elem->call_data); |
| 1413 | // Get retry policy. |
| 1414 | if (calld->method_params == nullptr) return false; |
| 1415 | const ClientChannelMethodParams::RetryPolicy* retry_policy = |
| 1416 | calld->method_params->retry_policy(); |
| 1417 | if (retry_policy == nullptr) return false; |
| 1418 | // If we've already dispatched a retry from this call, return true. |
| 1419 | // This catches the case where the batch has multiple callbacks |
| 1420 | // (i.e., it includes either recv_message or recv_initial_metadata). |
| 1421 | subchannel_call_retry_state* retry_state = nullptr; |
| 1422 | if (batch_data != nullptr) { |
| 1423 | retry_state = static_cast<subchannel_call_retry_state*>( |
| 1424 | grpc_connected_subchannel_call_get_parent_data( |
| 1425 | batch_data->subchannel_call)); |
| 1426 | if (retry_state->retry_dispatched) { |
| 1427 | if (grpc_client_channel_trace.enabled()) { |
| 1428 | gpr_log(GPR_DEBUG, "chand=%p calld=%p: retry already dispatched", chand, |
| 1429 | calld); |
| 1430 | } |
| 1431 | return true; |
| 1432 | } |
| 1433 | } |
| 1434 | // Check status. |
| 1435 | if (status == GRPC_STATUS_OK) { |
| 1436 | grpc_server_retry_throttle_data_record_success(calld->retry_throttle_data); |
| 1437 | if (grpc_client_channel_trace.enabled()) { |
| 1438 | gpr_log(GPR_DEBUG, "chand=%p calld=%p: call succeeded", chand, calld); |
| 1439 | } |
| 1440 | return false; |
| 1441 | } |
| 1442 | // Status is not OK. Check whether the status is retryable. |
| 1443 | if (!retry_policy->retryable_status_codes.Contains(status)) { |
| 1444 | if (grpc_client_channel_trace.enabled()) { |
| 1445 | gpr_log(GPR_DEBUG, |
| 1446 | "chand=%p calld=%p: status %s not configured as retryable", chand, |
| 1447 | calld, grpc_status_code_to_string(status)); |
| 1448 | } |
| 1449 | return false; |
| 1450 | } |
| 1451 | // Record the failure and check whether retries are throttled. |
| 1452 | // Note that it's important for this check to come after the status |
| 1453 | // code check above, since we should only record failures whose statuses |
| 1454 | // match the configured retryable status codes, so that we don't count |
| 1455 | // things like failures due to malformed requests (INVALID_ARGUMENT). |
| 1456 | // Conversely, it's important for this to come before the remaining |
| 1457 | // checks, so that we don't fail to record failures due to other factors. |
| 1458 | if (!grpc_server_retry_throttle_data_record_failure( |
| 1459 | calld->retry_throttle_data)) { |
| 1460 | if (grpc_client_channel_trace.enabled()) { |
| 1461 | gpr_log(GPR_DEBUG, "chand=%p calld=%p: retries throttled", chand, calld); |
| 1462 | } |
| 1463 | return false; |
| 1464 | } |
| 1465 | // Check whether the call is committed. |
| 1466 | if (calld->retry_committed) { |
| 1467 | if (grpc_client_channel_trace.enabled()) { |
| 1468 | gpr_log(GPR_DEBUG, "chand=%p calld=%p: retries already committed", chand, |
| 1469 | calld); |
| 1470 | } |
| 1471 | return false; |
| 1472 | } |
| 1473 | // Check whether we have retries remaining. |
| 1474 | ++calld->num_attempts_completed; |
| 1475 | if (calld->num_attempts_completed >= retry_policy->max_attempts) { |
| 1476 | if (grpc_client_channel_trace.enabled()) { |
| 1477 | gpr_log(GPR_DEBUG, "chand=%p calld=%p: exceeded %d retry attempts", chand, |
| 1478 | calld, retry_policy->max_attempts); |
| 1479 | } |
| 1480 | return false; |
| 1481 | } |
| 1482 | // If the call was cancelled from the surface, don't retry. |
| 1483 | if (calld->cancel_error != GRPC_ERROR_NONE) { |
| 1484 | if (grpc_client_channel_trace.enabled()) { |
| 1485 | gpr_log(GPR_DEBUG, |
| 1486 | "chand=%p calld=%p: call cancelled from surface, not retrying", |
| 1487 | chand, calld); |
| 1488 | } |
| 1489 | return false; |
| 1490 | } |
| 1491 | // Check server push-back. |
| 1492 | grpc_millis server_pushback_ms = -1; |
| 1493 | if (server_pushback_md != nullptr) { |
| 1494 | // If the value is "-1" or any other unparseable string, we do not retry. |
| 1495 | uint32_t ms; |
| 1496 | if (!grpc_parse_slice_to_uint32(GRPC_MDVALUE(*server_pushback_md), &ms)) { |
| 1497 | if (grpc_client_channel_trace.enabled()) { |
| 1498 | gpr_log(GPR_DEBUG, |
| 1499 | "chand=%p calld=%p: not retrying due to server push-back", |
| 1500 | chand, calld); |
| 1501 | } |
| 1502 | return false; |
| 1503 | } else { |
| 1504 | if (grpc_client_channel_trace.enabled()) { |
| 1505 | gpr_log(GPR_DEBUG, |
| 1506 | "chand=%p calld=%p: server push-back: retry in %u ms", chand, |
| 1507 | calld, ms); |
| 1508 | } |
| 1509 | server_pushback_ms = (grpc_millis)ms; |
| 1510 | } |
| 1511 | } |
| 1512 | do_retry(elem, retry_state, server_pushback_ms); |
| 1513 | return true; |
| 1514 | } |
| 1515 | |
| 1516 | // |
| 1517 | // subchannel_batch_data |
| 1518 | // |
| 1519 | |
| 1520 | static subchannel_batch_data* batch_data_create(grpc_call_element* elem, |
| 1521 | int refcount) { |
| 1522 | call_data* calld = static_cast<call_data*>(elem->call_data); |
| 1523 | subchannel_call_retry_state* retry_state = |
| 1524 | static_cast<subchannel_call_retry_state*>( |
| 1525 | grpc_connected_subchannel_call_get_parent_data( |
| 1526 | calld->subchannel_call)); |
| 1527 | subchannel_batch_data* batch_data = static_cast<subchannel_batch_data*>( |
| 1528 | gpr_arena_alloc(calld->arena, sizeof(*batch_data))); |
| 1529 | batch_data->elem = elem; |
| 1530 | batch_data->subchannel_call = |
| 1531 | GRPC_SUBCHANNEL_CALL_REF(calld->subchannel_call, "batch_data_create"); |
| 1532 | batch_data->batch.payload = &retry_state->batch_payload; |
| 1533 | gpr_ref_init(&batch_data->refs, refcount); |
| 1534 | GRPC_CLOSURE_INIT(&batch_data->on_complete, on_complete, batch_data, |
| 1535 | grpc_schedule_on_exec_ctx); |
| 1536 | batch_data->batch.on_complete = &batch_data->on_complete; |
| 1537 | GRPC_CALL_STACK_REF(calld->owning_call, "batch_data"); |
| 1538 | return batch_data; |
| 1539 | } |
| 1540 | |
| 1541 | static void batch_data_unref(subchannel_batch_data* batch_data) { |
| 1542 | if (gpr_unref(&batch_data->refs)) { |
| 1543 | if (batch_data->send_initial_metadata_storage != nullptr) { |
| 1544 | grpc_metadata_batch_destroy(&batch_data->send_initial_metadata); |
| 1545 | } |
| 1546 | if (batch_data->send_trailing_metadata_storage != nullptr) { |
| 1547 | grpc_metadata_batch_destroy(&batch_data->send_trailing_metadata); |
| 1548 | } |
| 1549 | if (batch_data->batch.recv_initial_metadata) { |
| 1550 | grpc_metadata_batch_destroy(&batch_data->recv_initial_metadata); |
| 1551 | } |
| 1552 | if (batch_data->batch.recv_trailing_metadata) { |
| 1553 | grpc_metadata_batch_destroy(&batch_data->recv_trailing_metadata); |
| 1554 | } |
| 1555 | GRPC_SUBCHANNEL_CALL_UNREF(batch_data->subchannel_call, "batch_data_unref"); |
| 1556 | call_data* calld = static_cast<call_data*>(batch_data->elem->call_data); |
| 1557 | GRPC_CALL_STACK_UNREF(calld->owning_call, "batch_data"); |
| 1558 | } |
| 1559 | } |
| 1560 | |
| 1561 | // |
| 1562 | // recv_initial_metadata callback handling |
| 1563 | // |
| 1564 | |
| 1565 | // Invokes recv_initial_metadata_ready for a subchannel batch. |
| 1566 | static void invoke_recv_initial_metadata_callback(void* arg, |
| 1567 | grpc_error* error) { |
| 1568 | subchannel_batch_data* batch_data = static_cast<subchannel_batch_data*>(arg); |
| 1569 | channel_data* chand = |
| 1570 | static_cast<channel_data*>(batch_data->elem->channel_data); |
| 1571 | call_data* calld = static_cast<call_data*>(batch_data->elem->call_data); |
| 1572 | // Find pending batch. |
| 1573 | pending_batch* pending = nullptr; |
| 1574 | for (size_t i = 0; i < GPR_ARRAY_SIZE(calld->pending_batches); ++i) { |
| 1575 | grpc_transport_stream_op_batch* batch = calld->pending_batches[i].batch; |
| 1576 | if (batch != nullptr && batch->recv_initial_metadata && |
| 1577 | batch->payload->recv_initial_metadata.recv_initial_metadata_ready != |
| 1578 | nullptr) { |
| 1579 | if (grpc_client_channel_trace.enabled()) { |
| 1580 | gpr_log(GPR_DEBUG, |
| 1581 | "chand=%p calld=%p: invoking recv_initial_metadata_ready for " |
| 1582 | "pending batch at index %" PRIuPTR, |
| 1583 | chand, calld, i); |
| 1584 | } |
| 1585 | pending = &calld->pending_batches[i]; |
| 1586 | break; |
| 1587 | } |
| 1588 | } |
| 1589 | GPR_ASSERT(pending != nullptr); |
| 1590 | // Return metadata. |
| 1591 | grpc_metadata_batch_move( |
| 1592 | &batch_data->recv_initial_metadata, |
| 1593 | pending->batch->payload->recv_initial_metadata.recv_initial_metadata); |
| 1594 | // Update bookkeeping. |
| 1595 | // Note: Need to do this before invoking the callback, since invoking |
| 1596 | // the callback will result in yielding the call combiner. |
| 1597 | grpc_closure* recv_initial_metadata_ready = |
| 1598 | pending->batch->payload->recv_initial_metadata |
| 1599 | .recv_initial_metadata_ready; |
| 1600 | pending->batch->payload->recv_initial_metadata.recv_initial_metadata_ready = |
| 1601 | nullptr; |
| 1602 | maybe_clear_pending_batch(batch_data->elem, pending); |
| 1603 | batch_data_unref(batch_data); |
| 1604 | // Invoke callback. |
| 1605 | GRPC_CLOSURE_RUN(recv_initial_metadata_ready, GRPC_ERROR_REF(error)); |
| 1606 | } |
| 1607 | |
| 1608 | // Intercepts recv_initial_metadata_ready callback for retries. |
| 1609 | // Commits the call and returns the initial metadata up the stack. |
| 1610 | static void recv_initial_metadata_ready(void* arg, grpc_error* error) { |
| 1611 | subchannel_batch_data* batch_data = static_cast<subchannel_batch_data*>(arg); |
| 1612 | grpc_call_element* elem = batch_data->elem; |
| 1613 | channel_data* chand = static_cast<channel_data*>(elem->channel_data); |
| 1614 | call_data* calld = static_cast<call_data*>(elem->call_data); |
| 1615 | if (grpc_client_channel_trace.enabled()) { |
| 1616 | gpr_log(GPR_DEBUG, |
| 1617 | "chand=%p calld=%p: got recv_initial_metadata_ready, error=%s", |
| 1618 | chand, calld, grpc_error_string(error)); |
| 1619 | } |
| 1620 | subchannel_call_retry_state* retry_state = |
| 1621 | static_cast<subchannel_call_retry_state*>( |
| 1622 | grpc_connected_subchannel_call_get_parent_data( |
| 1623 | batch_data->subchannel_call)); |
| 1624 | // If we got an error or a Trailers-Only response and have not yet gotten |
| 1625 | // the recv_trailing_metadata on_complete callback, then defer |
| 1626 | // propagating this callback back to the surface. We can evaluate whether |
| 1627 | // to retry when recv_trailing_metadata comes back. |
| 1628 | if ((batch_data->trailing_metadata_available || error != GRPC_ERROR_NONE) && |
| 1629 | !retry_state->completed_recv_trailing_metadata) { |
| 1630 | if (grpc_client_channel_trace.enabled()) { |
| 1631 | gpr_log(GPR_DEBUG, |
| 1632 | "chand=%p calld=%p: deferring recv_initial_metadata_ready " |
| 1633 | "(Trailers-Only)", |
| 1634 | chand, calld); |
| 1635 | } |
| 1636 | retry_state->recv_initial_metadata_ready_deferred = true; |
| 1637 | retry_state->recv_initial_metadata_error = GRPC_ERROR_REF(error); |
| 1638 | if (!retry_state->started_recv_trailing_metadata) { |
| 1639 | // recv_trailing_metadata not yet started by application; start it |
| 1640 | // ourselves to get status. |
| 1641 | start_internal_recv_trailing_metadata(elem); |
| 1642 | } else { |
| 1643 | GRPC_CALL_COMBINER_STOP( |
| 1644 | calld->call_combiner, |
| 1645 | "recv_initial_metadata_ready trailers-only or error"); |
| 1646 | } |
| 1647 | return; |
| 1648 | } |
| 1649 | // Received valid initial metadata, so commit the call. |
| 1650 | retry_commit(elem, retry_state); |
| 1651 | // Manually invoking a callback function; it does not take ownership of error. |
| 1652 | invoke_recv_initial_metadata_callback(batch_data, error); |
| 1653 | GRPC_ERROR_UNREF(error); |
| 1654 | } |
| 1655 | |
| 1656 | // |
| 1657 | // recv_message callback handling |
| 1658 | // |
| 1659 | |
| 1660 | // Invokes recv_message_ready for a subchannel batch. |
| 1661 | static void invoke_recv_message_callback(void* arg, grpc_error* error) { |
| 1662 | subchannel_batch_data* batch_data = static_cast<subchannel_batch_data*>(arg); |
| 1663 | channel_data* chand = |
| 1664 | static_cast<channel_data*>(batch_data->elem->channel_data); |
| 1665 | call_data* calld = static_cast<call_data*>(batch_data->elem->call_data); |
| 1666 | // Find pending op. |
| 1667 | pending_batch* pending = nullptr; |
| 1668 | for (size_t i = 0; i < GPR_ARRAY_SIZE(calld->pending_batches); ++i) { |
| 1669 | grpc_transport_stream_op_batch* batch = calld->pending_batches[i].batch; |
| 1670 | if (batch != nullptr && batch->recv_message && |
| 1671 | batch->payload->recv_message.recv_message_ready != nullptr) { |
| 1672 | if (grpc_client_channel_trace.enabled()) { |
| 1673 | gpr_log(GPR_DEBUG, |
| 1674 | "chand=%p calld=%p: invoking recv_message_ready for " |
| 1675 | "pending batch at index %" PRIuPTR, |
| 1676 | chand, calld, i); |
| 1677 | } |
| 1678 | pending = &calld->pending_batches[i]; |
| 1679 | break; |
| 1680 | } |
| 1681 | } |
| 1682 | GPR_ASSERT(pending != nullptr); |
| 1683 | // Return payload. |
| 1684 | *pending->batch->payload->recv_message.recv_message = |
Mark D. Roth | 3d8b32d | 2018-03-09 13:25:40 -0800 | [diff] [blame^] | 1685 | std::move(batch_data->recv_message); |
Mark D. Roth | 718c834 | 2018-02-28 13:00:04 -0800 | [diff] [blame] | 1686 | // Update bookkeeping. |
| 1687 | // Note: Need to do this before invoking the callback, since invoking |
| 1688 | // the callback will result in yielding the call combiner. |
| 1689 | grpc_closure* recv_message_ready = |
| 1690 | pending->batch->payload->recv_message.recv_message_ready; |
| 1691 | pending->batch->payload->recv_message.recv_message_ready = nullptr; |
| 1692 | maybe_clear_pending_batch(batch_data->elem, pending); |
| 1693 | batch_data_unref(batch_data); |
| 1694 | // Invoke callback. |
| 1695 | GRPC_CLOSURE_RUN(recv_message_ready, GRPC_ERROR_REF(error)); |
| 1696 | } |
| 1697 | |
| 1698 | // Intercepts recv_message_ready callback for retries. |
| 1699 | // Commits the call and returns the message up the stack. |
| 1700 | static void recv_message_ready(void* arg, grpc_error* error) { |
| 1701 | subchannel_batch_data* batch_data = static_cast<subchannel_batch_data*>(arg); |
| 1702 | grpc_call_element* elem = batch_data->elem; |
| 1703 | channel_data* chand = static_cast<channel_data*>(elem->channel_data); |
| 1704 | call_data* calld = static_cast<call_data*>(elem->call_data); |
| 1705 | if (grpc_client_channel_trace.enabled()) { |
| 1706 | gpr_log(GPR_DEBUG, "chand=%p calld=%p: got recv_message_ready, error=%s", |
| 1707 | chand, calld, grpc_error_string(error)); |
| 1708 | } |
| 1709 | subchannel_call_retry_state* retry_state = |
| 1710 | static_cast<subchannel_call_retry_state*>( |
| 1711 | grpc_connected_subchannel_call_get_parent_data( |
| 1712 | batch_data->subchannel_call)); |
| 1713 | // If we got an error or the payload was nullptr and we have not yet gotten |
| 1714 | // the recv_trailing_metadata on_complete callback, then defer |
| 1715 | // propagating this callback back to the surface. We can evaluate whether |
| 1716 | // to retry when recv_trailing_metadata comes back. |
| 1717 | if ((batch_data->recv_message == nullptr || error != GRPC_ERROR_NONE) && |
| 1718 | !retry_state->completed_recv_trailing_metadata) { |
| 1719 | if (grpc_client_channel_trace.enabled()) { |
| 1720 | gpr_log(GPR_DEBUG, |
| 1721 | "chand=%p calld=%p: deferring recv_message_ready (nullptr " |
| 1722 | "message and recv_trailing_metadata pending)", |
| 1723 | chand, calld); |
| 1724 | } |
| 1725 | retry_state->recv_message_ready_deferred = true; |
| 1726 | retry_state->recv_message_error = GRPC_ERROR_REF(error); |
| 1727 | if (!retry_state->started_recv_trailing_metadata) { |
| 1728 | // recv_trailing_metadata not yet started by application; start it |
| 1729 | // ourselves to get status. |
| 1730 | start_internal_recv_trailing_metadata(elem); |
| 1731 | } else { |
| 1732 | GRPC_CALL_COMBINER_STOP(calld->call_combiner, "recv_message_ready null"); |
| 1733 | } |
| 1734 | return; |
| 1735 | } |
| 1736 | // Received a valid message, so commit the call. |
| 1737 | retry_commit(elem, retry_state); |
| 1738 | // Manually invoking a callback function; it does not take ownership of error. |
| 1739 | invoke_recv_message_callback(batch_data, error); |
| 1740 | GRPC_ERROR_UNREF(error); |
| 1741 | } |
| 1742 | |
| 1743 | // |
| 1744 | // on_complete callback handling |
| 1745 | // |
| 1746 | |
| 1747 | // Updates retry_state to reflect the ops completed in batch_data. |
| 1748 | static void update_retry_state_for_completed_batch( |
| 1749 | subchannel_batch_data* batch_data, |
| 1750 | subchannel_call_retry_state* retry_state) { |
| 1751 | if (batch_data->batch.send_initial_metadata) { |
| 1752 | retry_state->completed_send_initial_metadata = true; |
| 1753 | } |
| 1754 | if (batch_data->batch.send_message) { |
| 1755 | ++retry_state->completed_send_message_count; |
| 1756 | } |
| 1757 | if (batch_data->batch.send_trailing_metadata) { |
| 1758 | retry_state->completed_send_trailing_metadata = true; |
| 1759 | } |
| 1760 | if (batch_data->batch.recv_initial_metadata) { |
| 1761 | retry_state->completed_recv_initial_metadata = true; |
| 1762 | } |
| 1763 | if (batch_data->batch.recv_message) { |
| 1764 | ++retry_state->completed_recv_message_count; |
| 1765 | } |
| 1766 | if (batch_data->batch.recv_trailing_metadata) { |
| 1767 | retry_state->completed_recv_trailing_metadata = true; |
| 1768 | } |
| 1769 | } |
| 1770 | |
| 1771 | // Represents a closure that needs to run as a result of a completed batch. |
| 1772 | typedef struct { |
| 1773 | grpc_closure* closure; |
| 1774 | grpc_error* error; |
| 1775 | const char* reason; |
| 1776 | } closure_to_execute; |
| 1777 | |
| 1778 | // Adds any necessary closures for deferred recv_initial_metadata and |
| 1779 | // recv_message callbacks to closures, updating *num_closures as needed. |
| 1780 | static void add_closures_for_deferred_recv_callbacks( |
| 1781 | subchannel_batch_data* batch_data, subchannel_call_retry_state* retry_state, |
| 1782 | closure_to_execute* closures, size_t* num_closures) { |
| 1783 | if (batch_data->batch.recv_trailing_metadata && |
| 1784 | retry_state->recv_initial_metadata_ready_deferred) { |
| 1785 | closure_to_execute* closure = &closures[(*num_closures)++]; |
| 1786 | closure->closure = |
| 1787 | GRPC_CLOSURE_INIT(&batch_data->recv_initial_metadata_ready, |
| 1788 | invoke_recv_initial_metadata_callback, batch_data, |
| 1789 | grpc_schedule_on_exec_ctx); |
| 1790 | closure->error = retry_state->recv_initial_metadata_error; |
| 1791 | closure->reason = "resuming recv_initial_metadata_ready"; |
| 1792 | } |
| 1793 | if (batch_data->batch.recv_trailing_metadata && |
| 1794 | retry_state->recv_message_ready_deferred) { |
| 1795 | closure_to_execute* closure = &closures[(*num_closures)++]; |
| 1796 | closure->closure = GRPC_CLOSURE_INIT(&batch_data->recv_message_ready, |
| 1797 | invoke_recv_message_callback, |
| 1798 | batch_data, grpc_schedule_on_exec_ctx); |
| 1799 | closure->error = retry_state->recv_message_error; |
| 1800 | closure->reason = "resuming recv_message_ready"; |
| 1801 | } |
| 1802 | } |
| 1803 | |
| 1804 | // If there are any cached ops to replay or pending ops to start on the |
| 1805 | // subchannel call, adds a closure to closures to invoke |
| 1806 | // start_retriable_subchannel_batches(), updating *num_closures as needed. |
| 1807 | static void add_closures_for_replay_or_pending_send_ops( |
| 1808 | grpc_call_element* elem, subchannel_batch_data* batch_data, |
| 1809 | subchannel_call_retry_state* retry_state, closure_to_execute* closures, |
| 1810 | size_t* num_closures) { |
| 1811 | channel_data* chand = static_cast<channel_data*>(elem->channel_data); |
| 1812 | call_data* calld = static_cast<call_data*>(elem->call_data); |
| 1813 | bool have_pending_send_message_ops = |
| 1814 | retry_state->started_send_message_count < calld->send_messages.size(); |
| 1815 | bool have_pending_send_trailing_metadata_op = |
| 1816 | calld->seen_send_trailing_metadata && |
| 1817 | !retry_state->started_send_trailing_metadata; |
| 1818 | if (!have_pending_send_message_ops && |
| 1819 | !have_pending_send_trailing_metadata_op) { |
| 1820 | for (size_t i = 0; i < GPR_ARRAY_SIZE(calld->pending_batches); ++i) { |
| 1821 | pending_batch* pending = &calld->pending_batches[i]; |
| 1822 | grpc_transport_stream_op_batch* batch = pending->batch; |
| 1823 | if (batch == nullptr || pending->send_ops_cached) continue; |
| 1824 | if (batch->send_message) have_pending_send_message_ops = true; |
| 1825 | if (batch->send_trailing_metadata) { |
| 1826 | have_pending_send_trailing_metadata_op = true; |
| 1827 | } |
| 1828 | } |
| 1829 | } |
| 1830 | if (have_pending_send_message_ops || have_pending_send_trailing_metadata_op) { |
| 1831 | if (grpc_client_channel_trace.enabled()) { |
| 1832 | gpr_log(GPR_DEBUG, |
| 1833 | "chand=%p calld=%p: starting next batch for pending send op(s)", |
| 1834 | chand, calld); |
| 1835 | } |
| 1836 | closure_to_execute* closure = &closures[(*num_closures)++]; |
| 1837 | closure->closure = GRPC_CLOSURE_INIT( |
| 1838 | &batch_data->batch.handler_private.closure, |
| 1839 | start_retriable_subchannel_batches, elem, grpc_schedule_on_exec_ctx); |
| 1840 | closure->error = GRPC_ERROR_NONE; |
| 1841 | closure->reason = "starting next batch for send_* op(s)"; |
| 1842 | } |
| 1843 | } |
| 1844 | |
| 1845 | // For any pending batch completed in batch_data, adds the necessary |
| 1846 | // completion closures to closures, updating *num_closures as needed. |
| 1847 | static void add_closures_for_completed_pending_batches( |
| 1848 | grpc_call_element* elem, subchannel_batch_data* batch_data, |
| 1849 | subchannel_call_retry_state* retry_state, grpc_error* error, |
| 1850 | closure_to_execute* closures, size_t* num_closures) { |
| 1851 | channel_data* chand = static_cast<channel_data*>(elem->channel_data); |
| 1852 | call_data* calld = static_cast<call_data*>(elem->call_data); |
| 1853 | for (size_t i = 0; i < GPR_ARRAY_SIZE(calld->pending_batches); ++i) { |
| 1854 | pending_batch* pending = &calld->pending_batches[i]; |
| 1855 | if (pending_batch_is_completed(pending, calld, retry_state)) { |
| 1856 | if (grpc_client_channel_trace.enabled()) { |
| 1857 | gpr_log(GPR_DEBUG, |
| 1858 | "chand=%p calld=%p: pending batch completed at index %" PRIuPTR, |
| 1859 | chand, calld, i); |
| 1860 | } |
| 1861 | // Copy the trailing metadata to return it to the surface. |
| 1862 | if (batch_data->batch.recv_trailing_metadata) { |
| 1863 | grpc_metadata_batch_move(&batch_data->recv_trailing_metadata, |
| 1864 | pending->batch->payload->recv_trailing_metadata |
| 1865 | .recv_trailing_metadata); |
| 1866 | } |
| 1867 | closure_to_execute* closure = &closures[(*num_closures)++]; |
| 1868 | closure->closure = pending->batch->on_complete; |
| 1869 | closure->error = GRPC_ERROR_REF(error); |
| 1870 | closure->reason = "on_complete for pending batch"; |
| 1871 | pending->batch->on_complete = nullptr; |
| 1872 | maybe_clear_pending_batch(elem, pending); |
| 1873 | } |
| 1874 | } |
| 1875 | GRPC_ERROR_UNREF(error); |
| 1876 | } |
| 1877 | |
| 1878 | // For any pending batch containing an op that has not yet been started, |
| 1879 | // adds the pending batch's completion closures to closures, updating |
| 1880 | // *num_closures as needed. |
| 1881 | static void add_closures_to_fail_unstarted_pending_batches( |
| 1882 | grpc_call_element* elem, subchannel_call_retry_state* retry_state, |
| 1883 | grpc_error* error, closure_to_execute* closures, size_t* num_closures) { |
| 1884 | channel_data* chand = static_cast<channel_data*>(elem->channel_data); |
| 1885 | call_data* calld = static_cast<call_data*>(elem->call_data); |
| 1886 | for (size_t i = 0; i < GPR_ARRAY_SIZE(calld->pending_batches); ++i) { |
| 1887 | pending_batch* pending = &calld->pending_batches[i]; |
| 1888 | if (pending_batch_is_unstarted(pending, calld, retry_state)) { |
| 1889 | if (grpc_client_channel_trace.enabled()) { |
| 1890 | gpr_log(GPR_DEBUG, |
| 1891 | "chand=%p calld=%p: failing unstarted pending batch at index " |
| 1892 | "%" PRIuPTR, |
| 1893 | chand, calld, i); |
| 1894 | } |
| 1895 | if (pending->batch->recv_initial_metadata) { |
| 1896 | closure_to_execute* closure = &closures[(*num_closures)++]; |
| 1897 | closure->closure = pending->batch->payload->recv_initial_metadata |
| 1898 | .recv_initial_metadata_ready; |
| 1899 | closure->error = GRPC_ERROR_REF(error); |
| 1900 | closure->reason = |
| 1901 | "failing recv_initial_metadata_ready for pending batch"; |
| 1902 | pending->batch->payload->recv_initial_metadata |
| 1903 | .recv_initial_metadata_ready = nullptr; |
| 1904 | } |
| 1905 | if (pending->batch->recv_message) { |
| 1906 | *pending->batch->payload->recv_message.recv_message = nullptr; |
| 1907 | closure_to_execute* closure = &closures[(*num_closures)++]; |
| 1908 | closure->closure = |
| 1909 | pending->batch->payload->recv_message.recv_message_ready; |
| 1910 | closure->error = GRPC_ERROR_REF(error); |
| 1911 | closure->reason = "failing recv_message_ready for pending batch"; |
| 1912 | pending->batch->payload->recv_message.recv_message_ready = nullptr; |
| 1913 | } |
| 1914 | closure_to_execute* closure = &closures[(*num_closures)++]; |
| 1915 | closure->closure = pending->batch->on_complete; |
| 1916 | closure->error = GRPC_ERROR_REF(error); |
| 1917 | closure->reason = "failing on_complete for pending batch"; |
| 1918 | pending->batch->on_complete = nullptr; |
| 1919 | maybe_clear_pending_batch(elem, pending); |
| 1920 | } |
| 1921 | } |
| 1922 | GRPC_ERROR_UNREF(error); |
| 1923 | } |
| 1924 | |
| 1925 | // Callback used to intercept on_complete from subchannel calls. |
| 1926 | // Called only when retries are enabled. |
| 1927 | static void on_complete(void* arg, grpc_error* error) { |
| 1928 | subchannel_batch_data* batch_data = static_cast<subchannel_batch_data*>(arg); |
| 1929 | grpc_call_element* elem = batch_data->elem; |
| 1930 | channel_data* chand = static_cast<channel_data*>(elem->channel_data); |
| 1931 | call_data* calld = static_cast<call_data*>(elem->call_data); |
| 1932 | if (grpc_client_channel_trace.enabled()) { |
| 1933 | char* batch_str = grpc_transport_stream_op_batch_string(&batch_data->batch); |
| 1934 | gpr_log(GPR_DEBUG, "chand=%p calld=%p: got on_complete, error=%s, batch=%s", |
| 1935 | chand, calld, grpc_error_string(error), batch_str); |
| 1936 | gpr_free(batch_str); |
| 1937 | } |
| 1938 | subchannel_call_retry_state* retry_state = |
| 1939 | static_cast<subchannel_call_retry_state*>( |
| 1940 | grpc_connected_subchannel_call_get_parent_data( |
| 1941 | batch_data->subchannel_call)); |
| 1942 | // If we have previously completed recv_trailing_metadata, then the |
| 1943 | // call is finished. |
| 1944 | bool call_finished = retry_state->completed_recv_trailing_metadata; |
| 1945 | // Update bookkeeping in retry_state. |
| 1946 | update_retry_state_for_completed_batch(batch_data, retry_state); |
| 1947 | if (call_finished) { |
| 1948 | if (grpc_client_channel_trace.enabled()) { |
| 1949 | gpr_log(GPR_DEBUG, "chand=%p calld=%p: call already finished", chand, |
| 1950 | calld); |
| 1951 | } |
| 1952 | } else { |
| 1953 | // Check if this batch finished the call, and if so, get its status. |
| 1954 | // The call is finished if either (a) this callback was invoked with |
| 1955 | // an error or (b) we receive status. |
| 1956 | grpc_status_code status = GRPC_STATUS_OK; |
| 1957 | grpc_mdelem* server_pushback_md = nullptr; |
| 1958 | if (error != GRPC_ERROR_NONE) { // Case (a). |
| 1959 | call_finished = true; |
| 1960 | grpc_error_get_status(error, calld->deadline, &status, nullptr, nullptr, |
| 1961 | nullptr); |
| 1962 | } else if (batch_data->batch.recv_trailing_metadata) { // Case (b). |
| 1963 | call_finished = true; |
| 1964 | grpc_metadata_batch* md_batch = |
| 1965 | batch_data->batch.payload->recv_trailing_metadata |
| 1966 | .recv_trailing_metadata; |
| 1967 | GPR_ASSERT(md_batch->idx.named.grpc_status != nullptr); |
| 1968 | status = grpc_get_status_code_from_metadata( |
| 1969 | md_batch->idx.named.grpc_status->md); |
| 1970 | if (md_batch->idx.named.grpc_retry_pushback_ms != nullptr) { |
| 1971 | server_pushback_md = &md_batch->idx.named.grpc_retry_pushback_ms->md; |
| 1972 | } |
| 1973 | } else if (retry_state->completed_recv_trailing_metadata) { |
| 1974 | call_finished = true; |
| 1975 | } |
| 1976 | if (call_finished && grpc_client_channel_trace.enabled()) { |
| 1977 | gpr_log(GPR_DEBUG, "chand=%p calld=%p: call finished, status=%s", chand, |
| 1978 | calld, grpc_status_code_to_string(status)); |
| 1979 | } |
| 1980 | // If the call is finished, check if we should retry. |
| 1981 | if (call_finished && |
| 1982 | maybe_retry(elem, batch_data, status, server_pushback_md)) { |
| 1983 | // Unref batch_data for deferred recv_initial_metadata_ready or |
| 1984 | // recv_message_ready callbacks, if any. |
| 1985 | if (batch_data->batch.recv_trailing_metadata && |
| 1986 | retry_state->recv_initial_metadata_ready_deferred) { |
| 1987 | batch_data_unref(batch_data); |
| 1988 | GRPC_ERROR_UNREF(retry_state->recv_initial_metadata_error); |
| 1989 | } |
| 1990 | if (batch_data->batch.recv_trailing_metadata && |
| 1991 | retry_state->recv_message_ready_deferred) { |
| 1992 | batch_data_unref(batch_data); |
| 1993 | GRPC_ERROR_UNREF(retry_state->recv_message_error); |
| 1994 | } |
| 1995 | batch_data_unref(batch_data); |
| 1996 | return; |
| 1997 | } |
| 1998 | } |
| 1999 | // If the call is finished or retries are committed, free cached data for |
| 2000 | // send ops that we've just completed. |
| 2001 | if (call_finished || calld->retry_committed) { |
| 2002 | free_cached_send_op_data_for_completed_batch(elem, batch_data, retry_state); |
| 2003 | } |
| 2004 | // Call not being retried. |
| 2005 | // Construct list of closures to execute. |
| 2006 | // Max number of closures is number of pending batches plus one for |
| 2007 | // each of: |
| 2008 | // - recv_initial_metadata_ready (either deferred or unstarted) |
| 2009 | // - recv_message_ready (either deferred or unstarted) |
| 2010 | // - starting a new batch for pending send ops |
| 2011 | closure_to_execute closures[GPR_ARRAY_SIZE(calld->pending_batches) + 3]; |
| 2012 | size_t num_closures = 0; |
| 2013 | // If there are deferred recv_initial_metadata_ready or recv_message_ready |
| 2014 | // callbacks, add them to closures. |
| 2015 | add_closures_for_deferred_recv_callbacks(batch_data, retry_state, closures, |
| 2016 | &num_closures); |
| 2017 | // Find pending batches whose ops are now complete and add their |
| 2018 | // on_complete callbacks to closures. |
| 2019 | add_closures_for_completed_pending_batches(elem, batch_data, retry_state, |
| 2020 | GRPC_ERROR_REF(error), closures, |
| 2021 | &num_closures); |
| 2022 | // Add closures to handle any pending batches that have not yet been started. |
| 2023 | // If the call is finished, we fail these batches; otherwise, we add a |
| 2024 | // callback to start_retriable_subchannel_batches() to start them on |
| 2025 | // the subchannel call. |
| 2026 | if (call_finished) { |
| 2027 | add_closures_to_fail_unstarted_pending_batches( |
| 2028 | elem, retry_state, GRPC_ERROR_REF(error), closures, &num_closures); |
| 2029 | } else { |
| 2030 | add_closures_for_replay_or_pending_send_ops(elem, batch_data, retry_state, |
| 2031 | closures, &num_closures); |
| 2032 | } |
| 2033 | // Don't need batch_data anymore. |
| 2034 | batch_data_unref(batch_data); |
| 2035 | // Schedule all of the closures identified above. |
| 2036 | // Note that the call combiner will be yielded for each closure that |
| 2037 | // we schedule. We're already running in the call combiner, so one of |
| 2038 | // the closures can be scheduled directly, but the others will |
| 2039 | // have to re-enter the call combiner. |
| 2040 | if (num_closures > 0) { |
| 2041 | GRPC_CLOSURE_SCHED(closures[0].closure, closures[0].error); |
| 2042 | for (size_t i = 1; i < num_closures; ++i) { |
| 2043 | GRPC_CALL_COMBINER_START(calld->call_combiner, closures[i].closure, |
| 2044 | closures[i].error, closures[i].reason); |
| 2045 | } |
| 2046 | } else { |
| 2047 | GRPC_CALL_COMBINER_STOP(calld->call_combiner, |
| 2048 | "no closures to run for on_complete"); |
| 2049 | } |
| 2050 | } |
| 2051 | |
| 2052 | // |
| 2053 | // subchannel batch construction |
| 2054 | // |
| 2055 | |
| 2056 | // Helper function used to start a subchannel batch in the call combiner. |
| 2057 | static void start_batch_in_call_combiner(void* arg, grpc_error* ignored) { |
| 2058 | grpc_transport_stream_op_batch* batch = |
| 2059 | static_cast<grpc_transport_stream_op_batch*>(arg); |
| 2060 | grpc_subchannel_call* subchannel_call = |
| 2061 | static_cast<grpc_subchannel_call*>(batch->handler_private.extra_arg); |
| 2062 | // Note: This will release the call combiner. |
| 2063 | grpc_subchannel_call_process_op(subchannel_call, batch); |
| 2064 | } |
| 2065 | |
| 2066 | // Adds retriable send_initial_metadata op to batch_data. |
| 2067 | static void add_retriable_send_initial_metadata_op( |
| 2068 | call_data* calld, subchannel_call_retry_state* retry_state, |
| 2069 | subchannel_batch_data* batch_data) { |
| 2070 | // Maps the number of retries to the corresponding metadata value slice. |
| 2071 | static const grpc_slice* retry_count_strings[] = { |
| 2072 | &GRPC_MDSTR_1, &GRPC_MDSTR_2, &GRPC_MDSTR_3, &GRPC_MDSTR_4}; |
| 2073 | // We need to make a copy of the metadata batch for each attempt, since |
| 2074 | // the filters in the subchannel stack may modify this batch, and we don't |
| 2075 | // want those modifications to be passed forward to subsequent attempts. |
| 2076 | // |
| 2077 | // If we've already completed one or more attempts, add the |
| 2078 | // grpc-retry-attempts header. |
| 2079 | batch_data->send_initial_metadata_storage = |
| 2080 | static_cast<grpc_linked_mdelem*>(gpr_arena_alloc( |
| 2081 | calld->arena, sizeof(grpc_linked_mdelem) * |
| 2082 | (calld->send_initial_metadata.list.count + |
| 2083 | (calld->num_attempts_completed > 0)))); |
| 2084 | grpc_metadata_batch_copy(&calld->send_initial_metadata, |
| 2085 | &batch_data->send_initial_metadata, |
| 2086 | batch_data->send_initial_metadata_storage); |
| 2087 | if (batch_data->send_initial_metadata.idx.named.grpc_previous_rpc_attempts != |
| 2088 | nullptr) { |
| 2089 | grpc_metadata_batch_remove( |
| 2090 | &batch_data->send_initial_metadata, |
| 2091 | batch_data->send_initial_metadata.idx.named.grpc_previous_rpc_attempts); |
| 2092 | } |
| 2093 | if (calld->num_attempts_completed > 0) { |
| 2094 | grpc_mdelem retry_md = grpc_mdelem_from_slices( |
| 2095 | GRPC_MDSTR_GRPC_PREVIOUS_RPC_ATTEMPTS, |
| 2096 | *retry_count_strings[calld->num_attempts_completed - 1]); |
| 2097 | grpc_error* error = grpc_metadata_batch_add_tail( |
| 2098 | &batch_data->send_initial_metadata, |
| 2099 | &batch_data->send_initial_metadata_storage[calld->send_initial_metadata |
| 2100 | .list.count], |
| 2101 | retry_md); |
| 2102 | if (error != GRPC_ERROR_NONE) { |
| 2103 | gpr_log(GPR_ERROR, "error adding retry metadata: %s", |
| 2104 | grpc_error_string(error)); |
| 2105 | GPR_ASSERT(false); |
| 2106 | } |
| 2107 | } |
| 2108 | retry_state->started_send_initial_metadata = true; |
| 2109 | batch_data->batch.send_initial_metadata = true; |
| 2110 | batch_data->batch.payload->send_initial_metadata.send_initial_metadata = |
| 2111 | &batch_data->send_initial_metadata; |
| 2112 | batch_data->batch.payload->send_initial_metadata.send_initial_metadata_flags = |
| 2113 | calld->send_initial_metadata_flags; |
| 2114 | batch_data->batch.payload->send_initial_metadata.peer_string = |
| 2115 | calld->peer_string; |
| 2116 | } |
| 2117 | |
| 2118 | // Adds retriable send_message op to batch_data. |
| 2119 | static void add_retriable_send_message_op( |
| 2120 | grpc_call_element* elem, subchannel_call_retry_state* retry_state, |
| 2121 | subchannel_batch_data* batch_data) { |
| 2122 | channel_data* chand = static_cast<channel_data*>(elem->channel_data); |
| 2123 | call_data* calld = static_cast<call_data*>(elem->call_data); |
| 2124 | if (grpc_client_channel_trace.enabled()) { |
| 2125 | gpr_log(GPR_DEBUG, |
| 2126 | "chand=%p calld=%p: starting calld->send_messages[%" PRIuPTR "]", |
| 2127 | chand, calld, retry_state->started_send_message_count); |
| 2128 | } |
Mark D. Roth | 3d8b32d | 2018-03-09 13:25:40 -0800 | [diff] [blame^] | 2129 | grpc_core::ByteStreamCache* cache = |
Mark D. Roth | 718c834 | 2018-02-28 13:00:04 -0800 | [diff] [blame] | 2130 | calld->send_messages[retry_state->started_send_message_count]; |
| 2131 | ++retry_state->started_send_message_count; |
Mark D. Roth | 3d8b32d | 2018-03-09 13:25:40 -0800 | [diff] [blame^] | 2132 | batch_data->send_message.Init(cache); |
Mark D. Roth | 718c834 | 2018-02-28 13:00:04 -0800 | [diff] [blame] | 2133 | batch_data->batch.send_message = true; |
Mark D. Roth | 3d8b32d | 2018-03-09 13:25:40 -0800 | [diff] [blame^] | 2134 | batch_data->batch.payload->send_message.send_message.reset( |
| 2135 | batch_data->send_message.get()); |
Mark D. Roth | 718c834 | 2018-02-28 13:00:04 -0800 | [diff] [blame] | 2136 | } |
| 2137 | |
| 2138 | // Adds retriable send_trailing_metadata op to batch_data. |
| 2139 | static void add_retriable_send_trailing_metadata_op( |
| 2140 | call_data* calld, subchannel_call_retry_state* retry_state, |
| 2141 | subchannel_batch_data* batch_data) { |
| 2142 | // We need to make a copy of the metadata batch for each attempt, since |
| 2143 | // the filters in the subchannel stack may modify this batch, and we don't |
| 2144 | // want those modifications to be passed forward to subsequent attempts. |
| 2145 | batch_data->send_trailing_metadata_storage = |
| 2146 | static_cast<grpc_linked_mdelem*>(gpr_arena_alloc( |
| 2147 | calld->arena, sizeof(grpc_linked_mdelem) * |
| 2148 | calld->send_trailing_metadata.list.count)); |
| 2149 | grpc_metadata_batch_copy(&calld->send_trailing_metadata, |
| 2150 | &batch_data->send_trailing_metadata, |
| 2151 | batch_data->send_trailing_metadata_storage); |
| 2152 | retry_state->started_send_trailing_metadata = true; |
| 2153 | batch_data->batch.send_trailing_metadata = true; |
| 2154 | batch_data->batch.payload->send_trailing_metadata.send_trailing_metadata = |
| 2155 | &batch_data->send_trailing_metadata; |
| 2156 | } |
| 2157 | |
| 2158 | // Adds retriable recv_initial_metadata op to batch_data. |
| 2159 | static void add_retriable_recv_initial_metadata_op( |
| 2160 | call_data* calld, subchannel_call_retry_state* retry_state, |
| 2161 | subchannel_batch_data* batch_data) { |
| 2162 | retry_state->started_recv_initial_metadata = true; |
| 2163 | batch_data->batch.recv_initial_metadata = true; |
| 2164 | grpc_metadata_batch_init(&batch_data->recv_initial_metadata); |
| 2165 | batch_data->batch.payload->recv_initial_metadata.recv_initial_metadata = |
| 2166 | &batch_data->recv_initial_metadata; |
| 2167 | batch_data->batch.payload->recv_initial_metadata.trailing_metadata_available = |
| 2168 | &batch_data->trailing_metadata_available; |
| 2169 | GRPC_CLOSURE_INIT(&batch_data->recv_initial_metadata_ready, |
| 2170 | recv_initial_metadata_ready, batch_data, |
| 2171 | grpc_schedule_on_exec_ctx); |
| 2172 | batch_data->batch.payload->recv_initial_metadata.recv_initial_metadata_ready = |
| 2173 | &batch_data->recv_initial_metadata_ready; |
| 2174 | } |
| 2175 | |
| 2176 | // Adds retriable recv_message op to batch_data. |
| 2177 | static void add_retriable_recv_message_op( |
| 2178 | call_data* calld, subchannel_call_retry_state* retry_state, |
| 2179 | subchannel_batch_data* batch_data) { |
| 2180 | ++retry_state->started_recv_message_count; |
| 2181 | batch_data->batch.recv_message = true; |
| 2182 | batch_data->batch.payload->recv_message.recv_message = |
| 2183 | &batch_data->recv_message; |
| 2184 | GRPC_CLOSURE_INIT(&batch_data->recv_message_ready, recv_message_ready, |
| 2185 | batch_data, grpc_schedule_on_exec_ctx); |
| 2186 | batch_data->batch.payload->recv_message.recv_message_ready = |
| 2187 | &batch_data->recv_message_ready; |
| 2188 | } |
| 2189 | |
| 2190 | // Adds retriable recv_trailing_metadata op to batch_data. |
| 2191 | static void add_retriable_recv_trailing_metadata_op( |
| 2192 | call_data* calld, subchannel_call_retry_state* retry_state, |
| 2193 | subchannel_batch_data* batch_data) { |
| 2194 | retry_state->started_recv_trailing_metadata = true; |
| 2195 | batch_data->batch.recv_trailing_metadata = true; |
| 2196 | grpc_metadata_batch_init(&batch_data->recv_trailing_metadata); |
| 2197 | batch_data->batch.payload->recv_trailing_metadata.recv_trailing_metadata = |
| 2198 | &batch_data->recv_trailing_metadata; |
| 2199 | batch_data->batch.collect_stats = true; |
| 2200 | batch_data->batch.payload->collect_stats.collect_stats = |
| 2201 | &batch_data->collect_stats; |
| 2202 | } |
| 2203 | |
| 2204 | // Helper function used to start a recv_trailing_metadata batch. This |
| 2205 | // is used in the case where a recv_initial_metadata or recv_message |
| 2206 | // op fails in a way that we know the call is over but when the application |
| 2207 | // has not yet started its own recv_trailing_metadata op. |
| 2208 | static void start_internal_recv_trailing_metadata(grpc_call_element* elem) { |
| 2209 | channel_data* chand = static_cast<channel_data*>(elem->channel_data); |
| 2210 | call_data* calld = static_cast<call_data*>(elem->call_data); |
| 2211 | if (grpc_client_channel_trace.enabled()) { |
| 2212 | gpr_log(GPR_DEBUG, |
| 2213 | "chand=%p calld=%p: call failed but recv_trailing_metadata not " |
| 2214 | "started; starting it internally", |
| 2215 | chand, calld); |
| 2216 | } |
| 2217 | subchannel_call_retry_state* retry_state = |
| 2218 | static_cast<subchannel_call_retry_state*>( |
| 2219 | grpc_connected_subchannel_call_get_parent_data( |
| 2220 | calld->subchannel_call)); |
| 2221 | subchannel_batch_data* batch_data = batch_data_create(elem, 1); |
| 2222 | add_retriable_recv_trailing_metadata_op(calld, retry_state, batch_data); |
| 2223 | // Note: This will release the call combiner. |
| 2224 | grpc_subchannel_call_process_op(calld->subchannel_call, &batch_data->batch); |
| 2225 | } |
| 2226 | |
| 2227 | // If there are any cached send ops that need to be replayed on the |
| 2228 | // current subchannel call, creates and returns a new subchannel batch |
| 2229 | // to replay those ops. Otherwise, returns nullptr. |
| 2230 | static subchannel_batch_data* maybe_create_subchannel_batch_for_replay( |
| 2231 | grpc_call_element* elem, subchannel_call_retry_state* retry_state) { |
| 2232 | channel_data* chand = static_cast<channel_data*>(elem->channel_data); |
| 2233 | call_data* calld = static_cast<call_data*>(elem->call_data); |
| 2234 | subchannel_batch_data* replay_batch_data = nullptr; |
| 2235 | // send_initial_metadata. |
| 2236 | if (calld->seen_send_initial_metadata && |
| 2237 | !retry_state->started_send_initial_metadata && |
| 2238 | !calld->pending_send_initial_metadata) { |
| 2239 | if (grpc_client_channel_trace.enabled()) { |
| 2240 | gpr_log(GPR_DEBUG, |
| 2241 | "chand=%p calld=%p: replaying previously completed " |
| 2242 | "send_initial_metadata op", |
| 2243 | chand, calld); |
| 2244 | } |
| 2245 | replay_batch_data = batch_data_create(elem, 1); |
| 2246 | add_retriable_send_initial_metadata_op(calld, retry_state, |
| 2247 | replay_batch_data); |
| 2248 | } |
| 2249 | // send_message. |
| 2250 | // Note that we can only have one send_message op in flight at a time. |
| 2251 | if (retry_state->started_send_message_count < calld->send_messages.size() && |
| 2252 | retry_state->started_send_message_count == |
| 2253 | retry_state->completed_send_message_count && |
| 2254 | !calld->pending_send_message) { |
| 2255 | if (grpc_client_channel_trace.enabled()) { |
| 2256 | gpr_log(GPR_DEBUG, |
| 2257 | "chand=%p calld=%p: replaying previously completed " |
| 2258 | "send_message op", |
| 2259 | chand, calld); |
| 2260 | } |
| 2261 | if (replay_batch_data == nullptr) { |
| 2262 | replay_batch_data = batch_data_create(elem, 1); |
| 2263 | } |
| 2264 | add_retriable_send_message_op(elem, retry_state, replay_batch_data); |
| 2265 | } |
| 2266 | // send_trailing_metadata. |
| 2267 | // Note that we only add this op if we have no more send_message ops |
| 2268 | // to start, since we can't send down any more send_message ops after |
| 2269 | // send_trailing_metadata. |
| 2270 | if (calld->seen_send_trailing_metadata && |
| 2271 | retry_state->started_send_message_count == calld->send_messages.size() && |
| 2272 | !retry_state->started_send_trailing_metadata && |
| 2273 | !calld->pending_send_trailing_metadata) { |
| 2274 | if (grpc_client_channel_trace.enabled()) { |
| 2275 | gpr_log(GPR_DEBUG, |
| 2276 | "chand=%p calld=%p: replaying previously completed " |
| 2277 | "send_trailing_metadata op", |
| 2278 | chand, calld); |
| 2279 | } |
| 2280 | if (replay_batch_data == nullptr) { |
| 2281 | replay_batch_data = batch_data_create(elem, 1); |
| 2282 | } |
| 2283 | add_retriable_send_trailing_metadata_op(calld, retry_state, |
| 2284 | replay_batch_data); |
| 2285 | } |
| 2286 | return replay_batch_data; |
| 2287 | } |
| 2288 | |
| 2289 | // Adds subchannel batches for pending batches to batches, updating |
| 2290 | // *num_batches as needed. |
| 2291 | static void add_subchannel_batches_for_pending_batches( |
| 2292 | grpc_call_element* elem, subchannel_call_retry_state* retry_state, |
| 2293 | grpc_transport_stream_op_batch** batches, size_t* num_batches) { |
| 2294 | call_data* calld = static_cast<call_data*>(elem->call_data); |
| 2295 | for (size_t i = 0; i < GPR_ARRAY_SIZE(calld->pending_batches); ++i) { |
| 2296 | pending_batch* pending = &calld->pending_batches[i]; |
| 2297 | grpc_transport_stream_op_batch* batch = pending->batch; |
| 2298 | if (batch == nullptr) continue; |
| 2299 | // Skip any batch that either (a) has already been started on this |
| 2300 | // subchannel call or (b) we can't start yet because we're still |
| 2301 | // replaying send ops that need to be completed first. |
| 2302 | // TODO(roth): Note that if any one op in the batch can't be sent |
| 2303 | // yet due to ops that we're replaying, we don't start any of the ops |
| 2304 | // in the batch. This is probably okay, but it could conceivably |
| 2305 | // lead to increased latency in some cases -- e.g., we could delay |
| 2306 | // starting a recv op due to it being in the same batch with a send |
| 2307 | // op. If/when we revamp the callback protocol in |
| 2308 | // transport_stream_op_batch, we may be able to fix this. |
| 2309 | if (batch->send_initial_metadata && |
| 2310 | retry_state->started_send_initial_metadata) { |
| 2311 | continue; |
| 2312 | } |
| 2313 | if (batch->send_message && retry_state->completed_send_message_count < |
| 2314 | retry_state->started_send_message_count) { |
| 2315 | continue; |
| 2316 | } |
| 2317 | // Note that we only start send_trailing_metadata if we have no more |
| 2318 | // send_message ops to start, since we can't send down any more |
| 2319 | // send_message ops after send_trailing_metadata. |
| 2320 | if (batch->send_trailing_metadata && |
| 2321 | (retry_state->started_send_message_count + batch->send_message < |
| 2322 | calld->send_messages.size() || |
| 2323 | retry_state->started_send_trailing_metadata)) { |
| 2324 | continue; |
| 2325 | } |
| 2326 | if (batch->recv_initial_metadata && |
| 2327 | retry_state->started_recv_initial_metadata) { |
| 2328 | continue; |
| 2329 | } |
| 2330 | if (batch->recv_message && retry_state->completed_recv_message_count < |
| 2331 | retry_state->started_recv_message_count) { |
| 2332 | continue; |
| 2333 | } |
| 2334 | if (batch->recv_trailing_metadata && |
| 2335 | retry_state->started_recv_trailing_metadata) { |
| 2336 | continue; |
| 2337 | } |
| 2338 | // If we're not retrying, just send the batch as-is. |
| 2339 | if (calld->method_params == nullptr || |
| 2340 | calld->method_params->retry_policy() == nullptr || |
| 2341 | calld->retry_committed) { |
| 2342 | batches[(*num_batches)++] = batch; |
| 2343 | pending_batch_clear(calld, pending); |
| 2344 | continue; |
| 2345 | } |
| 2346 | // Create batch with the right number of callbacks. |
| 2347 | const int num_callbacks = |
| 2348 | 1 + batch->recv_initial_metadata + batch->recv_message; |
| 2349 | subchannel_batch_data* batch_data = batch_data_create(elem, num_callbacks); |
| 2350 | // Cache send ops if needed. |
| 2351 | maybe_cache_send_ops_for_batch(calld, pending); |
| 2352 | // send_initial_metadata. |
| 2353 | if (batch->send_initial_metadata) { |
| 2354 | add_retriable_send_initial_metadata_op(calld, retry_state, batch_data); |
| 2355 | } |
| 2356 | // send_message. |
| 2357 | if (batch->send_message) { |
| 2358 | add_retriable_send_message_op(elem, retry_state, batch_data); |
| 2359 | } |
| 2360 | // send_trailing_metadata. |
| 2361 | if (batch->send_trailing_metadata) { |
| 2362 | add_retriable_send_trailing_metadata_op(calld, retry_state, batch_data); |
| 2363 | } |
| 2364 | // recv_initial_metadata. |
| 2365 | if (batch->recv_initial_metadata) { |
| 2366 | // recv_flags is only used on the server side. |
| 2367 | GPR_ASSERT(batch->payload->recv_initial_metadata.recv_flags == nullptr); |
| 2368 | add_retriable_recv_initial_metadata_op(calld, retry_state, batch_data); |
| 2369 | } |
| 2370 | // recv_message. |
| 2371 | if (batch->recv_message) { |
| 2372 | add_retriable_recv_message_op(calld, retry_state, batch_data); |
| 2373 | } |
| 2374 | // recv_trailing_metadata. |
| 2375 | if (batch->recv_trailing_metadata) { |
| 2376 | GPR_ASSERT(batch->collect_stats); |
| 2377 | add_retriable_recv_trailing_metadata_op(calld, retry_state, batch_data); |
| 2378 | } |
| 2379 | batches[(*num_batches)++] = &batch_data->batch; |
| 2380 | } |
| 2381 | } |
| 2382 | |
| 2383 | // Constructs and starts whatever subchannel batches are needed on the |
| 2384 | // subchannel call. |
| 2385 | static void start_retriable_subchannel_batches(void* arg, grpc_error* ignored) { |
| 2386 | grpc_call_element* elem = static_cast<grpc_call_element*>(arg); |
| 2387 | channel_data* chand = static_cast<channel_data*>(elem->channel_data); |
| 2388 | call_data* calld = static_cast<call_data*>(elem->call_data); |
| 2389 | if (grpc_client_channel_trace.enabled()) { |
| 2390 | gpr_log(GPR_DEBUG, "chand=%p calld=%p: constructing retriable batches", |
| 2391 | chand, calld); |
| 2392 | } |
| 2393 | subchannel_call_retry_state* retry_state = |
| 2394 | static_cast<subchannel_call_retry_state*>( |
| 2395 | grpc_connected_subchannel_call_get_parent_data( |
| 2396 | calld->subchannel_call)); |
| 2397 | // We can start up to 6 batches. |
| 2398 | grpc_transport_stream_op_batch* |
| 2399 | batches[GPR_ARRAY_SIZE(calld->pending_batches)]; |
| 2400 | size_t num_batches = 0; |
| 2401 | // Replay previously-returned send_* ops if needed. |
| 2402 | subchannel_batch_data* replay_batch_data = |
| 2403 | maybe_create_subchannel_batch_for_replay(elem, retry_state); |
| 2404 | if (replay_batch_data != nullptr) { |
| 2405 | batches[num_batches++] = &replay_batch_data->batch; |
| 2406 | } |
| 2407 | // Now add pending batches. |
| 2408 | add_subchannel_batches_for_pending_batches(elem, retry_state, batches, |
| 2409 | &num_batches); |
| 2410 | // Start batches on subchannel call. |
| 2411 | // Note that the call combiner will be yielded for each batch that we |
| 2412 | // send down. We're already running in the call combiner, so one of |
| 2413 | // the batches can be started directly, but the others will have to |
| 2414 | // re-enter the call combiner. |
| 2415 | if (grpc_client_channel_trace.enabled()) { |
| 2416 | gpr_log(GPR_DEBUG, |
| 2417 | "chand=%p calld=%p: starting %" PRIuPTR |
| 2418 | " retriable batches on subchannel_call=%p", |
| 2419 | chand, calld, num_batches, calld->subchannel_call); |
| 2420 | } |
| 2421 | if (num_batches == 0) { |
| 2422 | // This should be fairly rare, but it can happen when (e.g.) an |
| 2423 | // attempt completes before it has finished replaying all |
| 2424 | // previously sent messages. |
| 2425 | GRPC_CALL_COMBINER_STOP(calld->call_combiner, |
| 2426 | "no retriable subchannel batches to start"); |
| 2427 | } else { |
| 2428 | for (size_t i = 1; i < num_batches; ++i) { |
| 2429 | if (grpc_client_channel_trace.enabled()) { |
| 2430 | char* batch_str = grpc_transport_stream_op_batch_string(batches[i]); |
| 2431 | gpr_log(GPR_DEBUG, |
| 2432 | "chand=%p calld=%p: starting batch in call combiner: %s", chand, |
| 2433 | calld, batch_str); |
| 2434 | gpr_free(batch_str); |
| 2435 | } |
| 2436 | batches[i]->handler_private.extra_arg = calld->subchannel_call; |
| 2437 | GRPC_CLOSURE_INIT(&batches[i]->handler_private.closure, |
| 2438 | start_batch_in_call_combiner, batches[i], |
| 2439 | grpc_schedule_on_exec_ctx); |
| 2440 | GRPC_CALL_COMBINER_START(calld->call_combiner, |
| 2441 | &batches[i]->handler_private.closure, |
| 2442 | GRPC_ERROR_NONE, "start_subchannel_batch"); |
| 2443 | } |
| 2444 | if (grpc_client_channel_trace.enabled()) { |
| 2445 | char* batch_str = grpc_transport_stream_op_batch_string(batches[0]); |
| 2446 | gpr_log(GPR_DEBUG, "chand=%p calld=%p: starting batch: %s", chand, calld, |
| 2447 | batch_str); |
| 2448 | gpr_free(batch_str); |
| 2449 | } |
| 2450 | // Note: This will release the call combiner. |
| 2451 | grpc_subchannel_call_process_op(calld->subchannel_call, batches[0]); |
| 2452 | } |
| 2453 | } |
| 2454 | |
| 2455 | // |
| 2456 | // LB pick |
| 2457 | // |
| 2458 | |
| 2459 | static void create_subchannel_call(grpc_call_element* elem, grpc_error* error) { |
| 2460 | channel_data* chand = static_cast<channel_data*>(elem->channel_data); |
| 2461 | call_data* calld = static_cast<call_data*>(elem->call_data); |
| 2462 | const size_t parent_data_size = |
| 2463 | calld->enable_retries ? sizeof(subchannel_call_retry_state) : 0; |
David Garcia Quintas | baf1ac7 | 2018-01-09 14:24:32 -0800 | [diff] [blame] | 2464 | const grpc_core::ConnectedSubchannel::CallArgs call_args = { |
Mark D. Roth | c0febd3 | 2018-01-09 10:25:24 -0800 | [diff] [blame] | 2465 | calld->pollent, // pollent |
| 2466 | calld->path, // path |
| 2467 | calld->call_start_time, // start_time |
| 2468 | calld->deadline, // deadline |
| 2469 | calld->arena, // arena |
| 2470 | calld->pick.subchannel_call_context, // context |
Mark D. Roth | 718c834 | 2018-02-28 13:00:04 -0800 | [diff] [blame] | 2471 | calld->call_combiner, // call_combiner |
| 2472 | parent_data_size // parent_data_size |
Yash Tibrewal | d8b84a2 | 2017-09-25 13:38:03 -0700 | [diff] [blame] | 2473 | }; |
David Garcia Quintas | 70fbe62 | 2018-01-09 19:27:46 -0800 | [diff] [blame] | 2474 | grpc_error* new_error = calld->pick.connected_subchannel->CreateCall( |
David Garcia Quintas | baf1ac7 | 2018-01-09 14:24:32 -0800 | [diff] [blame] | 2475 | call_args, &calld->subchannel_call); |
Craig Tiller | 6014e8a | 2017-10-16 13:50:29 -0700 | [diff] [blame] | 2476 | if (grpc_client_channel_trace.enabled()) { |
Mark D. Roth | 60751fe | 2017-07-07 12:50:33 -0700 | [diff] [blame] | 2477 | gpr_log(GPR_DEBUG, "chand=%p calld=%p: create subchannel_call=%p: error=%s", |
Mark D. Roth | 76e264b | 2017-08-25 09:03:33 -0700 | [diff] [blame] | 2478 | chand, calld, calld->subchannel_call, grpc_error_string(new_error)); |
Mark D. Roth | 60751fe | 2017-07-07 12:50:33 -0700 | [diff] [blame] | 2479 | } |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 2480 | if (new_error != GRPC_ERROR_NONE) { |
| 2481 | new_error = grpc_error_add_child(new_error, error); |
Mark D. Roth | 718c834 | 2018-02-28 13:00:04 -0800 | [diff] [blame] | 2482 | pending_batches_fail(elem, new_error, true /* yield_call_combiner */); |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 2483 | } else { |
Mark D. Roth | 718c834 | 2018-02-28 13:00:04 -0800 | [diff] [blame] | 2484 | if (parent_data_size > 0) { |
| 2485 | subchannel_call_retry_state* retry_state = |
| 2486 | static_cast<subchannel_call_retry_state*>( |
| 2487 | grpc_connected_subchannel_call_get_parent_data( |
| 2488 | calld->subchannel_call)); |
| 2489 | retry_state->batch_payload.context = calld->pick.subchannel_call_context; |
| 2490 | } |
| 2491 | pending_batches_resume(elem); |
Craig Tiller | 11c17d4 | 2017-03-13 13:36:34 -0700 | [diff] [blame] | 2492 | } |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 2493 | GRPC_ERROR_UNREF(error); |
Craig Tiller | 11c17d4 | 2017-03-13 13:36:34 -0700 | [diff] [blame] | 2494 | } |
| 2495 | |
Mark D. Roth | b292960 | 2017-09-11 09:31:11 -0700 | [diff] [blame] | 2496 | // Invoked when a pick is completed, on both success or failure. |
Mark D. Roth | 718c834 | 2018-02-28 13:00:04 -0800 | [diff] [blame] | 2497 | static void pick_done(void* arg, grpc_error* error) { |
| 2498 | grpc_call_element* elem = static_cast<grpc_call_element*>(arg); |
Noah Eisen | be82e64 | 2018-02-09 09:16:55 -0800 | [diff] [blame] | 2499 | channel_data* chand = static_cast<channel_data*>(elem->channel_data); |
Mark D. Roth | 718c834 | 2018-02-28 13:00:04 -0800 | [diff] [blame] | 2500 | call_data* calld = static_cast<call_data*>(elem->call_data); |
David Garcia Quintas | be1b7f9 | 2018-01-12 14:01:38 -0800 | [diff] [blame] | 2501 | if (calld->pick.connected_subchannel == nullptr) { |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 2502 | // Failed to create subchannel. |
Mark D. Roth | 718c834 | 2018-02-28 13:00:04 -0800 | [diff] [blame] | 2503 | // If there was no error, this is an LB policy drop, in which case |
| 2504 | // we return an error; otherwise, we may retry. |
| 2505 | grpc_status_code status = GRPC_STATUS_OK; |
| 2506 | grpc_error_get_status(error, calld->deadline, &status, nullptr, nullptr, |
| 2507 | nullptr); |
| 2508 | if (error == GRPC_ERROR_NONE || !calld->enable_retries || |
| 2509 | !maybe_retry(elem, nullptr /* batch_data */, status, |
| 2510 | nullptr /* server_pushback_md */)) { |
| 2511 | grpc_error* new_error = |
| 2512 | error == GRPC_ERROR_NONE |
| 2513 | ? GRPC_ERROR_CREATE_FROM_STATIC_STRING( |
| 2514 | "Call dropped by load balancing policy") |
| 2515 | : GRPC_ERROR_CREATE_REFERENCING_FROM_STATIC_STRING( |
| 2516 | "Failed to create subchannel", &error, 1); |
| 2517 | if (grpc_client_channel_trace.enabled()) { |
| 2518 | gpr_log(GPR_DEBUG, |
| 2519 | "chand=%p calld=%p: failed to create subchannel: error=%s", |
| 2520 | chand, calld, grpc_error_string(new_error)); |
| 2521 | } |
| 2522 | pending_batches_fail(elem, new_error, true /* yield_call_combiner */); |
Mark D. Roth | 60751fe | 2017-07-07 12:50:33 -0700 | [diff] [blame] | 2523 | } |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 2524 | } else { |
Mark D. Roth | 9fe284e | 2016-09-12 11:22:27 -0700 | [diff] [blame] | 2525 | /* Create call on subchannel. */ |
Mark D. Roth | 718c834 | 2018-02-28 13:00:04 -0800 | [diff] [blame] | 2526 | create_subchannel_call(elem, GRPC_ERROR_REF(error)); |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 2527 | } |
Mark D. Roth | 718c834 | 2018-02-28 13:00:04 -0800 | [diff] [blame] | 2528 | } |
| 2529 | |
| 2530 | // Invoked when a pick is completed to leave the client_channel combiner |
| 2531 | // and continue processing in the call combiner. |
| 2532 | static void pick_done_locked(grpc_call_element* elem, grpc_error* error) { |
| 2533 | call_data* calld = static_cast<call_data*>(elem->call_data); |
| 2534 | GRPC_CLOSURE_INIT(&calld->pick_closure, pick_done, elem, |
| 2535 | grpc_schedule_on_exec_ctx); |
| 2536 | GRPC_CLOSURE_SCHED(&calld->pick_closure, error); |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 2537 | } |
| 2538 | |
Mark D. Roth | b292960 | 2017-09-11 09:31:11 -0700 | [diff] [blame] | 2539 | // A wrapper around pick_done_locked() that is used in cases where |
| 2540 | // either (a) the pick was deferred pending a resolver result or (b) the |
| 2541 | // pick was done asynchronously. Removes the call's polling entity from |
| 2542 | // chand->interested_parties before invoking pick_done_locked(). |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 2543 | static void async_pick_done_locked(grpc_call_element* elem, grpc_error* error) { |
Noah Eisen | be82e64 | 2018-02-09 09:16:55 -0800 | [diff] [blame] | 2544 | channel_data* chand = static_cast<channel_data*>(elem->channel_data); |
| 2545 | call_data* calld = static_cast<call_data*>(elem->call_data); |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 2546 | grpc_polling_entity_del_from_pollset_set(calld->pollent, |
Mark D. Roth | b292960 | 2017-09-11 09:31:11 -0700 | [diff] [blame] | 2547 | chand->interested_parties); |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 2548 | pick_done_locked(elem, error); |
Mark D. Roth | b292960 | 2017-09-11 09:31:11 -0700 | [diff] [blame] | 2549 | } |
| 2550 | |
| 2551 | // Note: This runs under the client_channel combiner, but will NOT be |
| 2552 | // holding the call combiner. |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 2553 | static void pick_callback_cancel_locked(void* arg, grpc_error* error) { |
Noah Eisen | be82e64 | 2018-02-09 09:16:55 -0800 | [diff] [blame] | 2554 | grpc_call_element* elem = static_cast<grpc_call_element*>(arg); |
| 2555 | channel_data* chand = static_cast<channel_data*>(elem->channel_data); |
| 2556 | call_data* calld = static_cast<call_data*>(elem->call_data); |
Mark D. Roth | c0febd3 | 2018-01-09 10:25:24 -0800 | [diff] [blame] | 2557 | // Note: chand->lb_policy may have changed since we started our pick, |
| 2558 | // in which case we will be cancelling the pick on a policy other than |
| 2559 | // the one we started it on. However, this will just be a no-op. |
| 2560 | if (error != GRPC_ERROR_NONE && chand->lb_policy != nullptr) { |
Craig Tiller | 6014e8a | 2017-10-16 13:50:29 -0700 | [diff] [blame] | 2561 | if (grpc_client_channel_trace.enabled()) { |
Mark D. Roth | b292960 | 2017-09-11 09:31:11 -0700 | [diff] [blame] | 2562 | gpr_log(GPR_DEBUG, "chand=%p calld=%p: cancelling pick from LB policy %p", |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 2563 | chand, calld, chand->lb_policy.get()); |
Mark D. Roth | b292960 | 2017-09-11 09:31:11 -0700 | [diff] [blame] | 2564 | } |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 2565 | chand->lb_policy->CancelPickLocked(&calld->pick, GRPC_ERROR_REF(error)); |
Mark D. Roth | b292960 | 2017-09-11 09:31:11 -0700 | [diff] [blame] | 2566 | } |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 2567 | GRPC_CALL_STACK_UNREF(calld->owning_call, "pick_callback_cancel"); |
Mark D. Roth | b292960 | 2017-09-11 09:31:11 -0700 | [diff] [blame] | 2568 | } |
| 2569 | |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 2570 | // Callback invoked by LoadBalancingPolicy::PickLocked() for async picks. |
Mark D. Roth | b292960 | 2017-09-11 09:31:11 -0700 | [diff] [blame] | 2571 | // Unrefs the LB policy and invokes async_pick_done_locked(). |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 2572 | static void pick_callback_done_locked(void* arg, grpc_error* error) { |
Noah Eisen | be82e64 | 2018-02-09 09:16:55 -0800 | [diff] [blame] | 2573 | grpc_call_element* elem = static_cast<grpc_call_element*>(arg); |
| 2574 | channel_data* chand = static_cast<channel_data*>(elem->channel_data); |
| 2575 | call_data* calld = static_cast<call_data*>(elem->call_data); |
Craig Tiller | 6014e8a | 2017-10-16 13:50:29 -0700 | [diff] [blame] | 2576 | if (grpc_client_channel_trace.enabled()) { |
Mark D. Roth | b292960 | 2017-09-11 09:31:11 -0700 | [diff] [blame] | 2577 | gpr_log(GPR_DEBUG, "chand=%p calld=%p: pick completed asynchronously", |
| 2578 | chand, calld); |
| 2579 | } |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 2580 | async_pick_done_locked(elem, GRPC_ERROR_REF(error)); |
Ken Payson | f069dd4 | 2018-02-05 09:15:05 -0800 | [diff] [blame] | 2581 | GRPC_CALL_STACK_UNREF(calld->owning_call, "pick_callback"); |
Mark D. Roth | b292960 | 2017-09-11 09:31:11 -0700 | [diff] [blame] | 2582 | } |
| 2583 | |
Mark D. Roth | 718c834 | 2018-02-28 13:00:04 -0800 | [diff] [blame] | 2584 | // Applies service config to the call. Must be invoked once we know |
| 2585 | // that the resolver has returned results to the channel. |
| 2586 | static void apply_service_config_to_call_locked(grpc_call_element* elem) { |
| 2587 | channel_data* chand = static_cast<channel_data*>(elem->channel_data); |
| 2588 | call_data* calld = static_cast<call_data*>(elem->call_data); |
| 2589 | if (grpc_client_channel_trace.enabled()) { |
| 2590 | gpr_log(GPR_DEBUG, "chand=%p calld=%p: applying service config to call", |
| 2591 | chand, calld); |
| 2592 | } |
| 2593 | if (chand->retry_throttle_data != nullptr) { |
| 2594 | calld->retry_throttle_data = |
| 2595 | grpc_server_retry_throttle_data_ref(chand->retry_throttle_data); |
| 2596 | } |
| 2597 | if (chand->method_params_table != nullptr) { |
| 2598 | calld->method_params = grpc_core::ServiceConfig::MethodConfigTableLookup( |
| 2599 | *chand->method_params_table, calld->path); |
| 2600 | if (calld->method_params != nullptr) { |
| 2601 | // If the deadline from the service config is shorter than the one |
| 2602 | // from the client API, reset the deadline timer. |
| 2603 | if (chand->deadline_checking_enabled && |
| 2604 | calld->method_params->timeout() != 0) { |
| 2605 | const grpc_millis per_method_deadline = |
| 2606 | grpc_timespec_to_millis_round_up(calld->call_start_time) + |
| 2607 | calld->method_params->timeout(); |
| 2608 | if (per_method_deadline < calld->deadline) { |
| 2609 | calld->deadline = per_method_deadline; |
| 2610 | grpc_deadline_state_reset(elem, calld->deadline); |
| 2611 | } |
| 2612 | } |
| 2613 | } |
| 2614 | } |
| 2615 | // If no retry policy, disable retries. |
| 2616 | // TODO(roth): Remove this when adding support for transparent retries. |
| 2617 | if (calld->method_params == nullptr || |
| 2618 | calld->method_params->retry_policy() == nullptr) { |
| 2619 | calld->enable_retries = false; |
| 2620 | } |
| 2621 | } |
| 2622 | |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 2623 | // Starts a pick on chand->lb_policy. |
| 2624 | // Returns true if pick is completed synchronously. |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 2625 | static bool pick_callback_start_locked(grpc_call_element* elem) { |
Noah Eisen | be82e64 | 2018-02-09 09:16:55 -0800 | [diff] [blame] | 2626 | channel_data* chand = static_cast<channel_data*>(elem->channel_data); |
| 2627 | call_data* calld = static_cast<call_data*>(elem->call_data); |
Craig Tiller | 6014e8a | 2017-10-16 13:50:29 -0700 | [diff] [blame] | 2628 | if (grpc_client_channel_trace.enabled()) { |
Mark D. Roth | b292960 | 2017-09-11 09:31:11 -0700 | [diff] [blame] | 2629 | gpr_log(GPR_DEBUG, "chand=%p calld=%p: starting pick on lb_policy=%p", |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 2630 | chand, calld, chand->lb_policy.get()); |
Mark D. Roth | b292960 | 2017-09-11 09:31:11 -0700 | [diff] [blame] | 2631 | } |
Mark D. Roth | 718c834 | 2018-02-28 13:00:04 -0800 | [diff] [blame] | 2632 | // Only get service config data on the first attempt. |
| 2633 | if (calld->num_attempts_completed == 0) { |
| 2634 | apply_service_config_to_call_locked(elem); |
| 2635 | } |
Mark D. Roth | b292960 | 2017-09-11 09:31:11 -0700 | [diff] [blame] | 2636 | // If the application explicitly set wait_for_ready, use that. |
| 2637 | // Otherwise, if the service config specified a value for this |
| 2638 | // method, use that. |
Mark D. Roth | 718c834 | 2018-02-28 13:00:04 -0800 | [diff] [blame] | 2639 | // |
| 2640 | // The send_initial_metadata batch will be the first one in the list, |
| 2641 | // as set by get_batch_index() above. |
| 2642 | calld->pick.initial_metadata = |
| 2643 | calld->seen_send_initial_metadata |
| 2644 | ? &calld->send_initial_metadata |
| 2645 | : calld->pending_batches[0] |
| 2646 | .batch->payload->send_initial_metadata.send_initial_metadata; |
| 2647 | uint32_t send_initial_metadata_flags = |
| 2648 | calld->seen_send_initial_metadata |
| 2649 | ? calld->send_initial_metadata_flags |
| 2650 | : calld->pending_batches[0] |
| 2651 | .batch->payload->send_initial_metadata |
| 2652 | .send_initial_metadata_flags; |
Mark D. Roth | b292960 | 2017-09-11 09:31:11 -0700 | [diff] [blame] | 2653 | const bool wait_for_ready_set_from_api = |
Mark D. Roth | 718c834 | 2018-02-28 13:00:04 -0800 | [diff] [blame] | 2654 | send_initial_metadata_flags & |
Mark D. Roth | b292960 | 2017-09-11 09:31:11 -0700 | [diff] [blame] | 2655 | GRPC_INITIAL_METADATA_WAIT_FOR_READY_EXPLICITLY_SET; |
| 2656 | const bool wait_for_ready_set_from_service_config = |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 2657 | calld->method_params != nullptr && |
Mark D. Roth | 3e7f2df | 2018-02-26 13:17:06 -0800 | [diff] [blame] | 2658 | calld->method_params->wait_for_ready() != |
| 2659 | ClientChannelMethodParams::WAIT_FOR_READY_UNSET; |
Mark D. Roth | b292960 | 2017-09-11 09:31:11 -0700 | [diff] [blame] | 2660 | if (!wait_for_ready_set_from_api && wait_for_ready_set_from_service_config) { |
Mark D. Roth | 3e7f2df | 2018-02-26 13:17:06 -0800 | [diff] [blame] | 2661 | if (calld->method_params->wait_for_ready() == |
| 2662 | ClientChannelMethodParams::WAIT_FOR_READY_TRUE) { |
Mark D. Roth | 718c834 | 2018-02-28 13:00:04 -0800 | [diff] [blame] | 2663 | send_initial_metadata_flags |= GRPC_INITIAL_METADATA_WAIT_FOR_READY; |
Mark D. Roth | b292960 | 2017-09-11 09:31:11 -0700 | [diff] [blame] | 2664 | } else { |
Mark D. Roth | 718c834 | 2018-02-28 13:00:04 -0800 | [diff] [blame] | 2665 | send_initial_metadata_flags &= ~GRPC_INITIAL_METADATA_WAIT_FOR_READY; |
Mark D. Roth | b292960 | 2017-09-11 09:31:11 -0700 | [diff] [blame] | 2666 | } |
| 2667 | } |
Mark D. Roth | 718c834 | 2018-02-28 13:00:04 -0800 | [diff] [blame] | 2668 | calld->pick.initial_metadata_flags = send_initial_metadata_flags; |
| 2669 | GRPC_CLOSURE_INIT(&calld->pick_closure, pick_callback_done_locked, elem, |
Mark D. Roth | b292960 | 2017-09-11 09:31:11 -0700 | [diff] [blame] | 2670 | grpc_combiner_scheduler(chand->combiner)); |
Mark D. Roth | 718c834 | 2018-02-28 13:00:04 -0800 | [diff] [blame] | 2671 | calld->pick.on_complete = &calld->pick_closure; |
Ken Payson | f069dd4 | 2018-02-05 09:15:05 -0800 | [diff] [blame] | 2672 | GRPC_CALL_STACK_REF(calld->owning_call, "pick_callback"); |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 2673 | const bool pick_done = chand->lb_policy->PickLocked(&calld->pick); |
Mark D. Roth | b292960 | 2017-09-11 09:31:11 -0700 | [diff] [blame] | 2674 | if (pick_done) { |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 2675 | // Pick completed synchronously. |
Craig Tiller | 6014e8a | 2017-10-16 13:50:29 -0700 | [diff] [blame] | 2676 | if (grpc_client_channel_trace.enabled()) { |
Mark D. Roth | b292960 | 2017-09-11 09:31:11 -0700 | [diff] [blame] | 2677 | gpr_log(GPR_DEBUG, "chand=%p calld=%p: pick completed synchronously", |
| 2678 | chand, calld); |
| 2679 | } |
Ken Payson | f069dd4 | 2018-02-05 09:15:05 -0800 | [diff] [blame] | 2680 | GRPC_CALL_STACK_UNREF(calld->owning_call, "pick_callback"); |
Mark D. Roth | b292960 | 2017-09-11 09:31:11 -0700 | [diff] [blame] | 2681 | } else { |
| 2682 | GRPC_CALL_STACK_REF(calld->owning_call, "pick_callback_cancel"); |
| 2683 | grpc_call_combiner_set_notify_on_cancel( |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 2684 | calld->call_combiner, |
Mark D. Roth | 718c834 | 2018-02-28 13:00:04 -0800 | [diff] [blame] | 2685 | GRPC_CLOSURE_INIT(&calld->pick_cancel_closure, |
Mark D. Roth | b292960 | 2017-09-11 09:31:11 -0700 | [diff] [blame] | 2686 | pick_callback_cancel_locked, elem, |
| 2687 | grpc_combiner_scheduler(chand->combiner))); |
| 2688 | } |
| 2689 | return pick_done; |
| 2690 | } |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 2691 | |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 2692 | typedef struct { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 2693 | grpc_call_element* elem; |
Mark D. Roth | 66f3d2b | 2017-09-01 09:02:17 -0700 | [diff] [blame] | 2694 | bool finished; |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 2695 | grpc_closure closure; |
Mark D. Roth | 66f3d2b | 2017-09-01 09:02:17 -0700 | [diff] [blame] | 2696 | grpc_closure cancel_closure; |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 2697 | } pick_after_resolver_result_args; |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 2698 | |
Mark D. Roth | 76e264b | 2017-08-25 09:03:33 -0700 | [diff] [blame] | 2699 | // Note: This runs under the client_channel combiner, but will NOT be |
| 2700 | // holding the call combiner. |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 2701 | static void pick_after_resolver_result_cancel_locked(void* arg, |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 2702 | grpc_error* error) { |
Noah Eisen | 4d20a66 | 2018-02-09 09:34:04 -0800 | [diff] [blame] | 2703 | pick_after_resolver_result_args* args = |
| 2704 | static_cast<pick_after_resolver_result_args*>(arg); |
Mark D. Roth | 66f3d2b | 2017-09-01 09:02:17 -0700 | [diff] [blame] | 2705 | if (args->finished) { |
| 2706 | gpr_free(args); |
Mark D. Roth | b2b9a0f | 2017-09-01 09:06:47 -0700 | [diff] [blame] | 2707 | return; |
Mark D. Roth | 764cf04 | 2017-09-01 09:00:06 -0700 | [diff] [blame] | 2708 | } |
Mark D. Roth | 76e264b | 2017-08-25 09:03:33 -0700 | [diff] [blame] | 2709 | // If we don't yet have a resolver result, then a closure for |
| 2710 | // pick_after_resolver_result_done_locked() will have been added to |
| 2711 | // chand->waiting_for_resolver_result_closures, and it may not be invoked |
| 2712 | // until after this call has been destroyed. We mark the operation as |
Mark D. Roth | b2b9a0f | 2017-09-01 09:06:47 -0700 | [diff] [blame] | 2713 | // finished, so that when pick_after_resolver_result_done_locked() |
Mark D. Roth | 76e264b | 2017-08-25 09:03:33 -0700 | [diff] [blame] | 2714 | // is called, it will be a no-op. We also immediately invoke |
Mark D. Roth | b292960 | 2017-09-11 09:31:11 -0700 | [diff] [blame] | 2715 | // async_pick_done_locked() to propagate the error back to the caller. |
| 2716 | args->finished = true; |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 2717 | grpc_call_element* elem = args->elem; |
Noah Eisen | be82e64 | 2018-02-09 09:16:55 -0800 | [diff] [blame] | 2718 | channel_data* chand = static_cast<channel_data*>(elem->channel_data); |
| 2719 | call_data* calld = static_cast<call_data*>(elem->call_data); |
Craig Tiller | 6014e8a | 2017-10-16 13:50:29 -0700 | [diff] [blame] | 2720 | if (grpc_client_channel_trace.enabled()) { |
Mark D. Roth | b2b9a0f | 2017-09-01 09:06:47 -0700 | [diff] [blame] | 2721 | gpr_log(GPR_DEBUG, |
| 2722 | "chand=%p calld=%p: cancelling pick waiting for resolver result", |
| 2723 | chand, calld); |
Mark D. Roth | 76e264b | 2017-08-25 09:03:33 -0700 | [diff] [blame] | 2724 | } |
Mark D. Roth | b2b9a0f | 2017-09-01 09:06:47 -0700 | [diff] [blame] | 2725 | // Note: Although we are not in the call combiner here, we are |
| 2726 | // basically stealing the call combiner from the pending pick, so |
Mark D. Roth | b292960 | 2017-09-11 09:31:11 -0700 | [diff] [blame] | 2727 | // it's safe to call async_pick_done_locked() here -- we are |
Mark D. Roth | b2b9a0f | 2017-09-01 09:06:47 -0700 | [diff] [blame] | 2728 | // essentially calling it here instead of calling it in |
| 2729 | // pick_after_resolver_result_done_locked(). |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 2730 | async_pick_done_locked(elem, GRPC_ERROR_CREATE_REFERENCING_FROM_STATIC_STRING( |
| 2731 | "Pick cancelled", &error, 1)); |
Mark D. Roth | 76e264b | 2017-08-25 09:03:33 -0700 | [diff] [blame] | 2732 | } |
| 2733 | |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 2734 | static void pick_after_resolver_result_done_locked(void* arg, |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 2735 | grpc_error* error) { |
Noah Eisen | 4d20a66 | 2018-02-09 09:34:04 -0800 | [diff] [blame] | 2736 | pick_after_resolver_result_args* args = |
| 2737 | static_cast<pick_after_resolver_result_args*>(arg); |
Mark D. Roth | 66f3d2b | 2017-09-01 09:02:17 -0700 | [diff] [blame] | 2738 | if (args->finished) { |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 2739 | /* cancelled, do nothing */ |
Craig Tiller | 6014e8a | 2017-10-16 13:50:29 -0700 | [diff] [blame] | 2740 | if (grpc_client_channel_trace.enabled()) { |
Mark D. Roth | 60751fe | 2017-07-07 12:50:33 -0700 | [diff] [blame] | 2741 | gpr_log(GPR_DEBUG, "call cancelled before resolver result"); |
| 2742 | } |
Mark D. Roth | 66f3d2b | 2017-09-01 09:02:17 -0700 | [diff] [blame] | 2743 | gpr_free(args); |
Mark D. Roth | b2b9a0f | 2017-09-01 09:06:47 -0700 | [diff] [blame] | 2744 | return; |
| 2745 | } |
| 2746 | args->finished = true; |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 2747 | grpc_call_element* elem = args->elem; |
Noah Eisen | be82e64 | 2018-02-09 09:16:55 -0800 | [diff] [blame] | 2748 | channel_data* chand = static_cast<channel_data*>(elem->channel_data); |
| 2749 | call_data* calld = static_cast<call_data*>(elem->call_data); |
Mark D. Roth | b2b9a0f | 2017-09-01 09:06:47 -0700 | [diff] [blame] | 2750 | if (error != GRPC_ERROR_NONE) { |
Craig Tiller | 6014e8a | 2017-10-16 13:50:29 -0700 | [diff] [blame] | 2751 | if (grpc_client_channel_trace.enabled()) { |
Mark D. Roth | b2b9a0f | 2017-09-01 09:06:47 -0700 | [diff] [blame] | 2752 | gpr_log(GPR_DEBUG, "chand=%p calld=%p: resolver failed to return data", |
| 2753 | chand, calld); |
| 2754 | } |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 2755 | async_pick_done_locked(elem, GRPC_ERROR_REF(error)); |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 2756 | } else if (chand->lb_policy != nullptr) { |
Craig Tiller | 6014e8a | 2017-10-16 13:50:29 -0700 | [diff] [blame] | 2757 | if (grpc_client_channel_trace.enabled()) { |
Mark D. Roth | b2b9a0f | 2017-09-01 09:06:47 -0700 | [diff] [blame] | 2758 | gpr_log(GPR_DEBUG, "chand=%p calld=%p: resolver returned, doing pick", |
| 2759 | chand, calld); |
| 2760 | } |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 2761 | if (pick_callback_start_locked(elem)) { |
Mark D. Roth | b292960 | 2017-09-11 09:31:11 -0700 | [diff] [blame] | 2762 | // Even if the LB policy returns a result synchronously, we have |
| 2763 | // already added our polling entity to chand->interested_parties |
| 2764 | // in order to wait for the resolver result, so we need to |
| 2765 | // remove it here. Therefore, we call async_pick_done_locked() |
| 2766 | // instead of pick_done_locked(). |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 2767 | async_pick_done_locked(elem, GRPC_ERROR_NONE); |
Mark D. Roth | 9dab7d5 | 2016-10-07 07:48:03 -0700 | [diff] [blame] | 2768 | } |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 2769 | } |
Mark D. Roth | 718c834 | 2018-02-28 13:00:04 -0800 | [diff] [blame] | 2770 | // TODO(roth): It should be impossible for chand->lb_policy to be nullptr |
Mark D. Roth | 48be9de | 2017-10-23 12:27:37 -0700 | [diff] [blame] | 2771 | // here, so the rest of this code should never actually be executed. |
| 2772 | // However, we have reports of a crash on iOS that triggers this case, |
| 2773 | // so we are temporarily adding this to restore branches that were |
| 2774 | // removed in https://github.com/grpc/grpc/pull/12297. Need to figure |
| 2775 | // out what is actually causing this to occur and then figure out the |
| 2776 | // right way to deal with it. |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 2777 | else if (chand->resolver != nullptr) { |
Mark D. Roth | 48be9de | 2017-10-23 12:27:37 -0700 | [diff] [blame] | 2778 | // No LB policy, so try again. |
ncteisen | 72afb76 | 2017-11-10 12:23:12 -0800 | [diff] [blame] | 2779 | if (grpc_client_channel_trace.enabled()) { |
Mark D. Roth | 48be9de | 2017-10-23 12:27:37 -0700 | [diff] [blame] | 2780 | gpr_log(GPR_DEBUG, |
| 2781 | "chand=%p calld=%p: resolver returned but no LB policy, " |
| 2782 | "trying again", |
| 2783 | chand, calld); |
| 2784 | } |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 2785 | pick_after_resolver_result_start_locked(elem); |
Mark D. Roth | 48be9de | 2017-10-23 12:27:37 -0700 | [diff] [blame] | 2786 | } else { |
ncteisen | 72afb76 | 2017-11-10 12:23:12 -0800 | [diff] [blame] | 2787 | if (grpc_client_channel_trace.enabled()) { |
Mark D. Roth | 48be9de | 2017-10-23 12:27:37 -0700 | [diff] [blame] | 2788 | gpr_log(GPR_DEBUG, "chand=%p calld=%p: resolver disconnected", chand, |
| 2789 | calld); |
| 2790 | } |
| 2791 | async_pick_done_locked( |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 2792 | elem, GRPC_ERROR_CREATE_FROM_STATIC_STRING("Disconnected")); |
Mark D. Roth | 48be9de | 2017-10-23 12:27:37 -0700 | [diff] [blame] | 2793 | } |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 2794 | } |
| 2795 | |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 2796 | static void pick_after_resolver_result_start_locked(grpc_call_element* elem) { |
Noah Eisen | be82e64 | 2018-02-09 09:16:55 -0800 | [diff] [blame] | 2797 | channel_data* chand = static_cast<channel_data*>(elem->channel_data); |
| 2798 | call_data* calld = static_cast<call_data*>(elem->call_data); |
Craig Tiller | 6014e8a | 2017-10-16 13:50:29 -0700 | [diff] [blame] | 2799 | if (grpc_client_channel_trace.enabled()) { |
Mark D. Roth | 60751fe | 2017-07-07 12:50:33 -0700 | [diff] [blame] | 2800 | gpr_log(GPR_DEBUG, |
| 2801 | "chand=%p calld=%p: deferring pick pending resolver result", chand, |
| 2802 | calld); |
Mark D. Roth | 64a317c | 2017-05-02 08:27:08 -0700 | [diff] [blame] | 2803 | } |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 2804 | pick_after_resolver_result_args* args = |
Noah Eisen | be82e64 | 2018-02-09 09:16:55 -0800 | [diff] [blame] | 2805 | static_cast<pick_after_resolver_result_args*>(gpr_zalloc(sizeof(*args))); |
Mark D. Roth | 60751fe | 2017-07-07 12:50:33 -0700 | [diff] [blame] | 2806 | args->elem = elem; |
| 2807 | GRPC_CLOSURE_INIT(&args->closure, pick_after_resolver_result_done_locked, |
| 2808 | args, grpc_combiner_scheduler(chand->combiner)); |
| 2809 | grpc_closure_list_append(&chand->waiting_for_resolver_result_closures, |
| 2810 | &args->closure, GRPC_ERROR_NONE); |
Mark D. Roth | 76e264b | 2017-08-25 09:03:33 -0700 | [diff] [blame] | 2811 | grpc_call_combiner_set_notify_on_cancel( |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 2812 | calld->call_combiner, |
Mark D. Roth | 66f3d2b | 2017-09-01 09:02:17 -0700 | [diff] [blame] | 2813 | GRPC_CLOSURE_INIT(&args->cancel_closure, |
| 2814 | pick_after_resolver_result_cancel_locked, args, |
Mark D. Roth | 76e264b | 2017-08-25 09:03:33 -0700 | [diff] [blame] | 2815 | grpc_combiner_scheduler(chand->combiner))); |
Mark D. Roth | 60751fe | 2017-07-07 12:50:33 -0700 | [diff] [blame] | 2816 | } |
| 2817 | |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 2818 | static void start_pick_locked(void* arg, grpc_error* ignored) { |
Noah Eisen | be82e64 | 2018-02-09 09:16:55 -0800 | [diff] [blame] | 2819 | grpc_call_element* elem = static_cast<grpc_call_element*>(arg); |
| 2820 | call_data* calld = static_cast<call_data*>(elem->call_data); |
| 2821 | channel_data* chand = static_cast<channel_data*>(elem->channel_data); |
David Garcia Quintas | be1b7f9 | 2018-01-12 14:01:38 -0800 | [diff] [blame] | 2822 | GPR_ASSERT(calld->pick.connected_subchannel == nullptr); |
Mark D. Roth | 718c834 | 2018-02-28 13:00:04 -0800 | [diff] [blame] | 2823 | GPR_ASSERT(calld->subchannel_call == nullptr); |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 2824 | if (chand->lb_policy != nullptr) { |
Mark D. Roth | b292960 | 2017-09-11 09:31:11 -0700 | [diff] [blame] | 2825 | // We already have an LB policy, so ask it for a pick. |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 2826 | if (pick_callback_start_locked(elem)) { |
Mark D. Roth | b292960 | 2017-09-11 09:31:11 -0700 | [diff] [blame] | 2827 | // Pick completed synchronously. |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 2828 | pick_done_locked(elem, GRPC_ERROR_NONE); |
Mark D. Roth | b292960 | 2017-09-11 09:31:11 -0700 | [diff] [blame] | 2829 | return; |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 2830 | } |
Mark D. Roth | 76e264b | 2017-08-25 09:03:33 -0700 | [diff] [blame] | 2831 | } else { |
Mark D. Roth | b292960 | 2017-09-11 09:31:11 -0700 | [diff] [blame] | 2832 | // We do not yet have an LB policy, so wait for a resolver result. |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 2833 | if (chand->resolver == nullptr) { |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 2834 | pick_done_locked(elem, |
Mark D. Roth | b292960 | 2017-09-11 09:31:11 -0700 | [diff] [blame] | 2835 | GRPC_ERROR_CREATE_FROM_STATIC_STRING("Disconnected")); |
| 2836 | return; |
| 2837 | } |
| 2838 | if (!chand->started_resolving) { |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 2839 | start_resolving_locked(chand); |
Mark D. Roth | b292960 | 2017-09-11 09:31:11 -0700 | [diff] [blame] | 2840 | } |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 2841 | pick_after_resolver_result_start_locked(elem); |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 2842 | } |
Mark D. Roth | b292960 | 2017-09-11 09:31:11 -0700 | [diff] [blame] | 2843 | // We need to wait for either a resolver result or for an async result |
| 2844 | // from the LB policy. Add the polling entity from call_data to the |
| 2845 | // channel_data's interested_parties, so that the I/O of the LB policy |
| 2846 | // and resolver can be done under it. The polling entity will be |
| 2847 | // removed in async_pick_done_locked(). |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 2848 | grpc_polling_entity_add_to_pollset_set(calld->pollent, |
Mark D. Roth | b292960 | 2017-09-11 09:31:11 -0700 | [diff] [blame] | 2849 | chand->interested_parties); |
Craig Tiller | a11bfc8 | 2017-02-14 09:56:33 -0800 | [diff] [blame] | 2850 | } |
| 2851 | |
Mark D. Roth | 718c834 | 2018-02-28 13:00:04 -0800 | [diff] [blame] | 2852 | // |
| 2853 | // filter call vtable functions |
| 2854 | // |
Mark D. Roth | d6d192d | 2017-02-23 08:58:42 -0800 | [diff] [blame] | 2855 | |
Craig Tiller | e1b51da | 2017-03-31 15:44:33 -0700 | [diff] [blame] | 2856 | static void cc_start_transport_stream_op_batch( |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 2857 | grpc_call_element* elem, grpc_transport_stream_op_batch* batch) { |
yang-g | ce1cfea | 2018-01-31 15:59:50 -0800 | [diff] [blame] | 2858 | GPR_TIMER_SCOPE("cc_start_transport_stream_op_batch", 0); |
Noah Eisen | be82e64 | 2018-02-09 09:16:55 -0800 | [diff] [blame] | 2859 | call_data* calld = static_cast<call_data*>(elem->call_data); |
| 2860 | channel_data* chand = static_cast<channel_data*>(elem->channel_data); |
Craig Tiller | 3be7dd0 | 2017-04-03 14:30:03 -0700 | [diff] [blame] | 2861 | if (chand->deadline_checking_enabled) { |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 2862 | grpc_deadline_state_client_start_transport_stream_op_batch(elem, batch); |
Craig Tiller | 3be7dd0 | 2017-04-03 14:30:03 -0700 | [diff] [blame] | 2863 | } |
Mark D. Roth | 76e264b | 2017-08-25 09:03:33 -0700 | [diff] [blame] | 2864 | // If we've previously been cancelled, immediately fail any new batches. |
Mark D. Roth | 718c834 | 2018-02-28 13:00:04 -0800 | [diff] [blame] | 2865 | if (calld->cancel_error != GRPC_ERROR_NONE) { |
Craig Tiller | 6014e8a | 2017-10-16 13:50:29 -0700 | [diff] [blame] | 2866 | if (grpc_client_channel_trace.enabled()) { |
Mark D. Roth | 76e264b | 2017-08-25 09:03:33 -0700 | [diff] [blame] | 2867 | gpr_log(GPR_DEBUG, "chand=%p calld=%p: failing batch with error: %s", |
Mark D. Roth | 718c834 | 2018-02-28 13:00:04 -0800 | [diff] [blame] | 2868 | chand, calld, grpc_error_string(calld->cancel_error)); |
Mark D. Roth | 76e264b | 2017-08-25 09:03:33 -0700 | [diff] [blame] | 2869 | } |
Mark D. Roth | 718c834 | 2018-02-28 13:00:04 -0800 | [diff] [blame] | 2870 | // Note: This will release the call combiner. |
Mark D. Roth | 76e264b | 2017-08-25 09:03:33 -0700 | [diff] [blame] | 2871 | grpc_transport_stream_op_batch_finish_with_failure( |
Mark D. Roth | 718c834 | 2018-02-28 13:00:04 -0800 | [diff] [blame] | 2872 | batch, GRPC_ERROR_REF(calld->cancel_error), calld->call_combiner); |
yang-g | ce1cfea | 2018-01-31 15:59:50 -0800 | [diff] [blame] | 2873 | return; |
Mark D. Roth | 76e264b | 2017-08-25 09:03:33 -0700 | [diff] [blame] | 2874 | } |
Mark D. Roth | 718c834 | 2018-02-28 13:00:04 -0800 | [diff] [blame] | 2875 | // Handle cancellation. |
Mark D. Roth | 76e264b | 2017-08-25 09:03:33 -0700 | [diff] [blame] | 2876 | if (batch->cancel_stream) { |
| 2877 | // Stash a copy of cancel_error in our call data, so that we can use |
| 2878 | // it for subsequent operations. This ensures that if the call is |
| 2879 | // cancelled before any batches are passed down (e.g., if the deadline |
| 2880 | // is in the past when the call starts), we can return the right |
| 2881 | // error to the caller when the first batch does get passed down. |
Mark D. Roth | 718c834 | 2018-02-28 13:00:04 -0800 | [diff] [blame] | 2882 | GRPC_ERROR_UNREF(calld->cancel_error); |
| 2883 | calld->cancel_error = |
| 2884 | GRPC_ERROR_REF(batch->payload->cancel_stream.cancel_error); |
Craig Tiller | 6014e8a | 2017-10-16 13:50:29 -0700 | [diff] [blame] | 2885 | if (grpc_client_channel_trace.enabled()) { |
Mark D. Roth | 76e264b | 2017-08-25 09:03:33 -0700 | [diff] [blame] | 2886 | gpr_log(GPR_DEBUG, "chand=%p calld=%p: recording cancel_error=%s", chand, |
Mark D. Roth | 718c834 | 2018-02-28 13:00:04 -0800 | [diff] [blame] | 2887 | calld, grpc_error_string(calld->cancel_error)); |
Mark D. Roth | 76e264b | 2017-08-25 09:03:33 -0700 | [diff] [blame] | 2888 | } |
Mark D. Roth | 718c834 | 2018-02-28 13:00:04 -0800 | [diff] [blame] | 2889 | // If we do not have a subchannel call (i.e., a pick has not yet |
| 2890 | // been started), fail all pending batches. Otherwise, send the |
| 2891 | // cancellation down to the subchannel call. |
| 2892 | if (calld->subchannel_call == nullptr) { |
| 2893 | pending_batches_fail(elem, GRPC_ERROR_REF(calld->cancel_error), |
| 2894 | false /* yield_call_combiner */); |
| 2895 | // Note: This will release the call combiner. |
| 2896 | grpc_transport_stream_op_batch_finish_with_failure( |
| 2897 | batch, GRPC_ERROR_REF(calld->cancel_error), calld->call_combiner); |
Mark D. Roth | 76e264b | 2017-08-25 09:03:33 -0700 | [diff] [blame] | 2898 | } else { |
Mark D. Roth | 718c834 | 2018-02-28 13:00:04 -0800 | [diff] [blame] | 2899 | // Note: This will release the call combiner. |
| 2900 | grpc_subchannel_call_process_op(calld->subchannel_call, batch); |
Mark D. Roth | 76e264b | 2017-08-25 09:03:33 -0700 | [diff] [blame] | 2901 | } |
yang-g | ce1cfea | 2018-01-31 15:59:50 -0800 | [diff] [blame] | 2902 | return; |
Mark D. Roth | 76e264b | 2017-08-25 09:03:33 -0700 | [diff] [blame] | 2903 | } |
Mark D. Roth | 718c834 | 2018-02-28 13:00:04 -0800 | [diff] [blame] | 2904 | // Add the batch to the pending list. |
| 2905 | pending_batches_add(elem, batch); |
Mark D. Roth | 76e264b | 2017-08-25 09:03:33 -0700 | [diff] [blame] | 2906 | // Check if we've already gotten a subchannel call. |
| 2907 | // Note that once we have completed the pick, we do not need to enter |
| 2908 | // the channel combiner, which is more efficient (especially for |
| 2909 | // streaming calls). |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 2910 | if (calld->subchannel_call != nullptr) { |
Craig Tiller | 6014e8a | 2017-10-16 13:50:29 -0700 | [diff] [blame] | 2911 | if (grpc_client_channel_trace.enabled()) { |
Mark D. Roth | 60751fe | 2017-07-07 12:50:33 -0700 | [diff] [blame] | 2912 | gpr_log(GPR_DEBUG, |
Mark D. Roth | 718c834 | 2018-02-28 13:00:04 -0800 | [diff] [blame] | 2913 | "chand=%p calld=%p: starting batch on subchannel_call=%p", chand, |
Mark D. Roth | 76e264b | 2017-08-25 09:03:33 -0700 | [diff] [blame] | 2914 | calld, calld->subchannel_call); |
Mark D. Roth | 60751fe | 2017-07-07 12:50:33 -0700 | [diff] [blame] | 2915 | } |
Mark D. Roth | 718c834 | 2018-02-28 13:00:04 -0800 | [diff] [blame] | 2916 | pending_batches_resume(elem); |
yang-g | ce1cfea | 2018-01-31 15:59:50 -0800 | [diff] [blame] | 2917 | return; |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 2918 | } |
Mark D. Roth | 76e264b | 2017-08-25 09:03:33 -0700 | [diff] [blame] | 2919 | // We do not yet have a subchannel call. |
Mark D. Roth | 76e264b | 2017-08-25 09:03:33 -0700 | [diff] [blame] | 2920 | // For batches containing a send_initial_metadata op, enter the channel |
| 2921 | // combiner to start a pick. |
| 2922 | if (batch->send_initial_metadata) { |
Craig Tiller | 6014e8a | 2017-10-16 13:50:29 -0700 | [diff] [blame] | 2923 | if (grpc_client_channel_trace.enabled()) { |
Mark D. Roth | b292960 | 2017-09-11 09:31:11 -0700 | [diff] [blame] | 2924 | gpr_log(GPR_DEBUG, "chand=%p calld=%p: entering client_channel combiner", |
| 2925 | chand, calld); |
Mark D. Roth | 76e264b | 2017-08-25 09:03:33 -0700 | [diff] [blame] | 2926 | } |
| 2927 | GRPC_CLOSURE_SCHED( |
Mark D. Roth | 76e264b | 2017-08-25 09:03:33 -0700 | [diff] [blame] | 2928 | GRPC_CLOSURE_INIT(&batch->handler_private.closure, start_pick_locked, |
| 2929 | elem, grpc_combiner_scheduler(chand->combiner)), |
| 2930 | GRPC_ERROR_NONE); |
| 2931 | } else { |
| 2932 | // For all other batches, release the call combiner. |
Craig Tiller | 6014e8a | 2017-10-16 13:50:29 -0700 | [diff] [blame] | 2933 | if (grpc_client_channel_trace.enabled()) { |
Mark D. Roth | 76e264b | 2017-08-25 09:03:33 -0700 | [diff] [blame] | 2934 | gpr_log(GPR_DEBUG, |
| 2935 | "chand=%p calld=%p: saved batch, yeilding call combiner", chand, |
| 2936 | calld); |
| 2937 | } |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 2938 | GRPC_CALL_COMBINER_STOP(calld->call_combiner, |
Mark D. Roth | 76e264b | 2017-08-25 09:03:33 -0700 | [diff] [blame] | 2939 | "batch does not include send_initial_metadata"); |
Mark D. Roth | 60751fe | 2017-07-07 12:50:33 -0700 | [diff] [blame] | 2940 | } |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 2941 | } |
| 2942 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 2943 | /* Constructor for call_data */ |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 2944 | static grpc_error* cc_init_call_elem(grpc_call_element* elem, |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 2945 | const grpc_call_element_args* args) { |
Noah Eisen | be82e64 | 2018-02-09 09:16:55 -0800 | [diff] [blame] | 2946 | call_data* calld = static_cast<call_data*>(elem->call_data); |
| 2947 | channel_data* chand = static_cast<channel_data*>(elem->channel_data); |
Mark D. Roth | e40dd29 | 2016-10-05 14:58:37 -0700 | [diff] [blame] | 2948 | // Initialize data members. |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 2949 | calld->path = grpc_slice_ref_internal(args->path); |
Mark D. Roth | ff08f33 | 2016-10-14 13:01:01 -0700 | [diff] [blame] | 2950 | calld->call_start_time = args->start_time; |
Craig Tiller | 89c1428 | 2017-07-19 15:32:27 -0700 | [diff] [blame] | 2951 | calld->deadline = args->deadline; |
Craig Tiller | d426cac | 2017-03-13 12:30:45 -0700 | [diff] [blame] | 2952 | calld->arena = args->arena; |
Mark D. Roth | 66f3d2b | 2017-09-01 09:02:17 -0700 | [diff] [blame] | 2953 | calld->owning_call = args->call_stack; |
Mark D. Roth | 76e264b | 2017-08-25 09:03:33 -0700 | [diff] [blame] | 2954 | calld->call_combiner = args->call_combiner; |
Craig Tiller | 3be7dd0 | 2017-04-03 14:30:03 -0700 | [diff] [blame] | 2955 | if (chand->deadline_checking_enabled) { |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 2956 | grpc_deadline_state_init(elem, args->call_stack, args->call_combiner, |
| 2957 | calld->deadline); |
Craig Tiller | 3be7dd0 | 2017-04-03 14:30:03 -0700 | [diff] [blame] | 2958 | } |
Mark D. Roth | 718c834 | 2018-02-28 13:00:04 -0800 | [diff] [blame] | 2959 | calld->enable_retries = chand->enable_retries; |
Mark D. Roth | 0badbe8 | 2016-06-23 10:15:12 -0700 | [diff] [blame] | 2960 | return GRPC_ERROR_NONE; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 2961 | } |
| 2962 | |
| 2963 | /* Destructor for call_data */ |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 2964 | static void cc_destroy_call_elem(grpc_call_element* elem, |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 2965 | const grpc_call_final_info* final_info, |
| 2966 | grpc_closure* then_schedule_closure) { |
Noah Eisen | be82e64 | 2018-02-09 09:16:55 -0800 | [diff] [blame] | 2967 | call_data* calld = static_cast<call_data*>(elem->call_data); |
| 2968 | channel_data* chand = static_cast<channel_data*>(elem->channel_data); |
Craig Tiller | 3be7dd0 | 2017-04-03 14:30:03 -0700 | [diff] [blame] | 2969 | if (chand->deadline_checking_enabled) { |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 2970 | grpc_deadline_state_destroy(elem); |
Craig Tiller | 3be7dd0 | 2017-04-03 14:30:03 -0700 | [diff] [blame] | 2971 | } |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 2972 | grpc_slice_unref_internal(calld->path); |
Mark D. Roth | 3e7f2df | 2018-02-26 13:17:06 -0800 | [diff] [blame] | 2973 | calld->method_params.reset(); |
Mark D. Roth | 718c834 | 2018-02-28 13:00:04 -0800 | [diff] [blame] | 2974 | GRPC_ERROR_UNREF(calld->cancel_error); |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 2975 | if (calld->subchannel_call != nullptr) { |
Mark D. Roth | 76e264b | 2017-08-25 09:03:33 -0700 | [diff] [blame] | 2976 | grpc_subchannel_call_set_cleanup_closure(calld->subchannel_call, |
Craig Tiller | f7c8c9f | 2017-05-17 15:22:05 -0700 | [diff] [blame] | 2977 | then_schedule_closure); |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 2978 | then_schedule_closure = nullptr; |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 2979 | GRPC_SUBCHANNEL_CALL_UNREF(calld->subchannel_call, |
Craig Tiller | f7c8c9f | 2017-05-17 15:22:05 -0700 | [diff] [blame] | 2980 | "client_channel_destroy_call"); |
Mark D. Roth | 4c0fe49 | 2016-08-31 13:51:55 -0700 | [diff] [blame] | 2981 | } |
Mark D. Roth | 718c834 | 2018-02-28 13:00:04 -0800 | [diff] [blame] | 2982 | for (size_t i = 0; i < GPR_ARRAY_SIZE(calld->pending_batches); ++i) { |
| 2983 | GPR_ASSERT(calld->pending_batches[i].batch == nullptr); |
| 2984 | } |
David Garcia Quintas | be1b7f9 | 2018-01-12 14:01:38 -0800 | [diff] [blame] | 2985 | if (calld->pick.connected_subchannel != nullptr) { |
David Garcia Quintas | dfa2851 | 2018-01-11 18:31:13 -0800 | [diff] [blame] | 2986 | calld->pick.connected_subchannel.reset(); |
Craig Tiller | 693d394 | 2016-10-27 16:51:25 -0700 | [diff] [blame] | 2987 | } |
Mark D. Roth | 09e458c | 2017-05-02 08:13:26 -0700 | [diff] [blame] | 2988 | for (size_t i = 0; i < GRPC_CONTEXT_COUNT; ++i) { |
Mark D. Roth | c0febd3 | 2018-01-09 10:25:24 -0800 | [diff] [blame] | 2989 | if (calld->pick.subchannel_call_context[i].value != nullptr) { |
| 2990 | calld->pick.subchannel_call_context[i].destroy( |
| 2991 | calld->pick.subchannel_call_context[i].value); |
Mark D. Roth | 09e458c | 2017-05-02 08:13:26 -0700 | [diff] [blame] | 2992 | } |
| 2993 | } |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 2994 | GRPC_CLOSURE_SCHED(then_schedule_closure, GRPC_ERROR_NONE); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 2995 | } |
| 2996 | |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 2997 | static void cc_set_pollset_or_pollset_set(grpc_call_element* elem, |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 2998 | grpc_polling_entity* pollent) { |
Noah Eisen | be82e64 | 2018-02-09 09:16:55 -0800 | [diff] [blame] | 2999 | call_data* calld = static_cast<call_data*>(elem->call_data); |
David Garcia Quintas | 2a50dfe | 2016-05-31 15:09:12 -0700 | [diff] [blame] | 3000 | calld->pollent = pollent; |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 3001 | } |
| 3002 | |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 3003 | /************************************************************************* |
| 3004 | * EXPORTED SYMBOLS |
| 3005 | */ |
| 3006 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 3007 | const grpc_channel_filter grpc_client_channel_filter = { |
Craig Tiller | a0f3abd | 2017-03-31 15:42:16 -0700 | [diff] [blame] | 3008 | cc_start_transport_stream_op_batch, |
Craig Tiller | f40df23 | 2016-03-25 13:38:14 -0700 | [diff] [blame] | 3009 | cc_start_transport_op, |
| 3010 | sizeof(call_data), |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 3011 | cc_init_call_elem, |
David Garcia Quintas | 4afce7e | 2016-04-18 16:25:17 -0700 | [diff] [blame] | 3012 | cc_set_pollset_or_pollset_set, |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 3013 | cc_destroy_call_elem, |
Craig Tiller | f40df23 | 2016-03-25 13:38:14 -0700 | [diff] [blame] | 3014 | sizeof(channel_data), |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 3015 | cc_init_channel_elem, |
| 3016 | cc_destroy_channel_elem, |
Mark D. Roth | b2d2488 | 2016-10-27 15:44:07 -0700 | [diff] [blame] | 3017 | cc_get_channel_info, |
Craig Tiller | f40df23 | 2016-03-25 13:38:14 -0700 | [diff] [blame] | 3018 | "client-channel", |
Craig Tiller | 87d5b19 | 2015-04-16 14:37:57 -0700 | [diff] [blame] | 3019 | }; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 3020 | |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 3021 | static void try_to_connect_locked(void* arg, grpc_error* error_ignored) { |
Noah Eisen | be82e64 | 2018-02-09 09:16:55 -0800 | [diff] [blame] | 3022 | channel_data* chand = static_cast<channel_data*>(arg); |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 3023 | if (chand->lb_policy != nullptr) { |
Mark D. Roth | c887549 | 2018-02-20 08:33:48 -0800 | [diff] [blame] | 3024 | chand->lb_policy->ExitIdleLocked(); |
Craig Tiller | 613dafa | 2017-02-09 12:00:43 -0800 | [diff] [blame] | 3025 | } else { |
| 3026 | chand->exit_idle_when_lb_policy_arrives = true; |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 3027 | if (!chand->started_resolving && chand->resolver != nullptr) { |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 3028 | start_resolving_locked(chand); |
Craig Tiller | 613dafa | 2017-02-09 12:00:43 -0800 | [diff] [blame] | 3029 | } |
| 3030 | } |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 3031 | GRPC_CHANNEL_STACK_UNREF(chand->owning_stack, "try_to_connect"); |
Craig Tiller | 613dafa | 2017-02-09 12:00:43 -0800 | [diff] [blame] | 3032 | } |
| 3033 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 3034 | grpc_connectivity_state grpc_client_channel_check_connectivity_state( |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 3035 | grpc_channel_element* elem, int try_to_connect) { |
Noah Eisen | be82e64 | 2018-02-09 09:16:55 -0800 | [diff] [blame] | 3036 | channel_data* chand = static_cast<channel_data*>(elem->channel_data); |
Craig Tiller | a8610c0 | 2017-02-14 10:05:11 -0800 | [diff] [blame] | 3037 | grpc_connectivity_state out = |
| 3038 | grpc_connectivity_state_check(&chand->state_tracker); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 3039 | if (out == GRPC_CHANNEL_IDLE && try_to_connect) { |
Craig Tiller | d2e5cfc | 2017-02-09 13:02:20 -0800 | [diff] [blame] | 3040 | GRPC_CHANNEL_STACK_REF(chand->owning_stack, "try_to_connect"); |
ncteisen | 274bbbe | 2017-06-08 14:57:11 -0700 | [diff] [blame] | 3041 | GRPC_CLOSURE_SCHED( |
Yash Tibrewal | 0ee7574 | 2017-10-13 16:07:13 -0700 | [diff] [blame] | 3042 | GRPC_CLOSURE_CREATE(try_to_connect_locked, chand, |
| 3043 | grpc_combiner_scheduler(chand->combiner)), |
Craig Tiller | 613dafa | 2017-02-09 12:00:43 -0800 | [diff] [blame] | 3044 | GRPC_ERROR_NONE); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 3045 | } |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 3046 | return out; |
| 3047 | } |
| 3048 | |
Alexander Polcyn | c3b1f18 | 2017-04-18 13:51:36 -0700 | [diff] [blame] | 3049 | typedef struct external_connectivity_watcher { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 3050 | channel_data* chand; |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 3051 | grpc_polling_entity pollent; |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 3052 | grpc_closure* on_complete; |
| 3053 | grpc_closure* watcher_timer_init; |
| 3054 | grpc_connectivity_state* state; |
Craig Tiller | 86c9958 | 2015-11-25 15:22:26 -0800 | [diff] [blame] | 3055 | grpc_closure my_closure; |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 3056 | struct external_connectivity_watcher* next; |
Craig Tiller | 86c9958 | 2015-11-25 15:22:26 -0800 | [diff] [blame] | 3057 | } external_connectivity_watcher; |
| 3058 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 3059 | static external_connectivity_watcher* lookup_external_connectivity_watcher( |
| 3060 | channel_data* chand, grpc_closure* on_complete) { |
Alexander Polcyn | c3b1f18 | 2017-04-18 13:51:36 -0700 | [diff] [blame] | 3061 | gpr_mu_lock(&chand->external_connectivity_watcher_list_mu); |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 3062 | external_connectivity_watcher* w = |
Alexander Polcyn | c3b1f18 | 2017-04-18 13:51:36 -0700 | [diff] [blame] | 3063 | chand->external_connectivity_watcher_list_head; |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 3064 | while (w != nullptr && w->on_complete != on_complete) { |
Alexander Polcyn | c3b1f18 | 2017-04-18 13:51:36 -0700 | [diff] [blame] | 3065 | w = w->next; |
| 3066 | } |
| 3067 | gpr_mu_unlock(&chand->external_connectivity_watcher_list_mu); |
| 3068 | return w; |
| 3069 | } |
| 3070 | |
| 3071 | static void external_connectivity_watcher_list_append( |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 3072 | channel_data* chand, external_connectivity_watcher* w) { |
Alexander Polcyn | c3b1f18 | 2017-04-18 13:51:36 -0700 | [diff] [blame] | 3073 | GPR_ASSERT(!lookup_external_connectivity_watcher(chand, w->on_complete)); |
| 3074 | |
| 3075 | gpr_mu_lock(&w->chand->external_connectivity_watcher_list_mu); |
| 3076 | GPR_ASSERT(!w->next); |
| 3077 | w->next = chand->external_connectivity_watcher_list_head; |
| 3078 | chand->external_connectivity_watcher_list_head = w; |
| 3079 | gpr_mu_unlock(&w->chand->external_connectivity_watcher_list_mu); |
| 3080 | } |
| 3081 | |
| 3082 | static void external_connectivity_watcher_list_remove( |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 3083 | channel_data* chand, external_connectivity_watcher* too_remove) { |
Alexander Polcyn | c3b1f18 | 2017-04-18 13:51:36 -0700 | [diff] [blame] | 3084 | GPR_ASSERT( |
| 3085 | lookup_external_connectivity_watcher(chand, too_remove->on_complete)); |
| 3086 | gpr_mu_lock(&chand->external_connectivity_watcher_list_mu); |
| 3087 | if (too_remove == chand->external_connectivity_watcher_list_head) { |
| 3088 | chand->external_connectivity_watcher_list_head = too_remove->next; |
| 3089 | gpr_mu_unlock(&chand->external_connectivity_watcher_list_mu); |
| 3090 | return; |
| 3091 | } |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 3092 | external_connectivity_watcher* w = |
Alexander Polcyn | c3b1f18 | 2017-04-18 13:51:36 -0700 | [diff] [blame] | 3093 | chand->external_connectivity_watcher_list_head; |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 3094 | while (w != nullptr) { |
Alexander Polcyn | c3b1f18 | 2017-04-18 13:51:36 -0700 | [diff] [blame] | 3095 | if (w->next == too_remove) { |
| 3096 | w->next = w->next->next; |
| 3097 | gpr_mu_unlock(&chand->external_connectivity_watcher_list_mu); |
| 3098 | return; |
| 3099 | } |
| 3100 | w = w->next; |
| 3101 | } |
| 3102 | GPR_UNREACHABLE_CODE(return ); |
| 3103 | } |
| 3104 | |
| 3105 | int grpc_client_channel_num_external_connectivity_watchers( |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 3106 | grpc_channel_element* elem) { |
Noah Eisen | be82e64 | 2018-02-09 09:16:55 -0800 | [diff] [blame] | 3107 | channel_data* chand = static_cast<channel_data*>(elem->channel_data); |
Alexander Polcyn | c3b1f18 | 2017-04-18 13:51:36 -0700 | [diff] [blame] | 3108 | int count = 0; |
| 3109 | |
| 3110 | gpr_mu_lock(&chand->external_connectivity_watcher_list_mu); |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 3111 | external_connectivity_watcher* w = |
Alexander Polcyn | c3b1f18 | 2017-04-18 13:51:36 -0700 | [diff] [blame] | 3112 | chand->external_connectivity_watcher_list_head; |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 3113 | while (w != nullptr) { |
Alexander Polcyn | c3b1f18 | 2017-04-18 13:51:36 -0700 | [diff] [blame] | 3114 | count++; |
| 3115 | w = w->next; |
| 3116 | } |
| 3117 | gpr_mu_unlock(&chand->external_connectivity_watcher_list_mu); |
| 3118 | |
| 3119 | return count; |
| 3120 | } |
| 3121 | |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 3122 | static void on_external_watch_complete_locked(void* arg, grpc_error* error) { |
Noah Eisen | 4d20a66 | 2018-02-09 09:34:04 -0800 | [diff] [blame] | 3123 | external_connectivity_watcher* w = |
| 3124 | static_cast<external_connectivity_watcher*>(arg); |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 3125 | grpc_closure* follow_up = w->on_complete; |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 3126 | grpc_polling_entity_del_from_pollset_set(&w->pollent, |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 3127 | w->chand->interested_parties); |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 3128 | GRPC_CHANNEL_STACK_UNREF(w->chand->owning_stack, |
Craig Tiller | 1d881fb | 2015-12-01 07:39:04 -0800 | [diff] [blame] | 3129 | "external_connectivity_watcher"); |
Alexander Polcyn | c3b1f18 | 2017-04-18 13:51:36 -0700 | [diff] [blame] | 3130 | external_connectivity_watcher_list_remove(w->chand, w); |
Craig Tiller | 86c9958 | 2015-11-25 15:22:26 -0800 | [diff] [blame] | 3131 | gpr_free(w); |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 3132 | GRPC_CLOSURE_RUN(follow_up, GRPC_ERROR_REF(error)); |
Craig Tiller | 613dafa | 2017-02-09 12:00:43 -0800 | [diff] [blame] | 3133 | } |
| 3134 | |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 3135 | static void watch_connectivity_state_locked(void* arg, |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 3136 | grpc_error* error_ignored) { |
Noah Eisen | 4d20a66 | 2018-02-09 09:34:04 -0800 | [diff] [blame] | 3137 | external_connectivity_watcher* w = |
| 3138 | static_cast<external_connectivity_watcher*>(arg); |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 3139 | external_connectivity_watcher* found = nullptr; |
| 3140 | if (w->state != nullptr) { |
Alexander Polcyn | c3b1f18 | 2017-04-18 13:51:36 -0700 | [diff] [blame] | 3141 | external_connectivity_watcher_list_append(w->chand, w); |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 3142 | GRPC_CLOSURE_RUN(w->watcher_timer_init, GRPC_ERROR_NONE); |
Alexander Polcyn | 2004e39 | 2017-10-16 15:14:46 -0700 | [diff] [blame] | 3143 | GRPC_CLOSURE_INIT(&w->my_closure, on_external_watch_complete_locked, w, |
| 3144 | grpc_combiner_scheduler(w->chand->combiner)); |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 3145 | grpc_connectivity_state_notify_on_state_change(&w->chand->state_tracker, |
| 3146 | w->state, &w->my_closure); |
Alexander Polcyn | c3b1f18 | 2017-04-18 13:51:36 -0700 | [diff] [blame] | 3147 | } else { |
Craig Tiller | 4782d92 | 2017-11-10 09:53:21 -0800 | [diff] [blame] | 3148 | GPR_ASSERT(w->watcher_timer_init == nullptr); |
Alexander Polcyn | c3b1f18 | 2017-04-18 13:51:36 -0700 | [diff] [blame] | 3149 | found = lookup_external_connectivity_watcher(w->chand, w->on_complete); |
| 3150 | if (found) { |
| 3151 | GPR_ASSERT(found->on_complete == w->on_complete); |
| 3152 | grpc_connectivity_state_notify_on_state_change( |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 3153 | &found->chand->state_tracker, nullptr, &found->my_closure); |
Alexander Polcyn | c3b1f18 | 2017-04-18 13:51:36 -0700 | [diff] [blame] | 3154 | } |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 3155 | grpc_polling_entity_del_from_pollset_set(&w->pollent, |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 3156 | w->chand->interested_parties); |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 3157 | GRPC_CHANNEL_STACK_UNREF(w->chand->owning_stack, |
Alexander Polcyn | c3b1f18 | 2017-04-18 13:51:36 -0700 | [diff] [blame] | 3158 | "external_connectivity_watcher"); |
| 3159 | gpr_free(w); |
| 3160 | } |
Craig Tiller | 86c9958 | 2015-11-25 15:22:26 -0800 | [diff] [blame] | 3161 | } |
| 3162 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 3163 | void grpc_client_channel_watch_connectivity_state( |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 3164 | grpc_channel_element* elem, grpc_polling_entity pollent, |
| 3165 | grpc_connectivity_state* state, grpc_closure* closure, |
| 3166 | grpc_closure* watcher_timer_init) { |
Noah Eisen | be82e64 | 2018-02-09 09:16:55 -0800 | [diff] [blame] | 3167 | channel_data* chand = static_cast<channel_data*>(elem->channel_data); |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 3168 | external_connectivity_watcher* w = |
Noah Eisen | be82e64 | 2018-02-09 09:16:55 -0800 | [diff] [blame] | 3169 | static_cast<external_connectivity_watcher*>(gpr_zalloc(sizeof(*w))); |
Craig Tiller | 86c9958 | 2015-11-25 15:22:26 -0800 | [diff] [blame] | 3170 | w->chand = chand; |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 3171 | w->pollent = pollent; |
Mark D. Roth | 9221083 | 2017-05-02 15:04:39 -0700 | [diff] [blame] | 3172 | w->on_complete = closure; |
Craig Tiller | 613dafa | 2017-02-09 12:00:43 -0800 | [diff] [blame] | 3173 | w->state = state; |
Alexander Polcyn | c3b1f18 | 2017-04-18 13:51:36 -0700 | [diff] [blame] | 3174 | w->watcher_timer_init = watcher_timer_init; |
Yash Tibrewal | 8cf1470 | 2017-12-06 09:47:54 -0800 | [diff] [blame] | 3175 | grpc_polling_entity_add_to_pollset_set(&w->pollent, |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 3176 | chand->interested_parties); |
Craig Tiller | 1d881fb | 2015-12-01 07:39:04 -0800 | [diff] [blame] | 3177 | GRPC_CHANNEL_STACK_REF(w->chand->owning_stack, |
| 3178 | "external_connectivity_watcher"); |
ncteisen | 274bbbe | 2017-06-08 14:57:11 -0700 | [diff] [blame] | 3179 | GRPC_CLOSURE_SCHED( |
ncteisen | 274bbbe | 2017-06-08 14:57:11 -0700 | [diff] [blame] | 3180 | GRPC_CLOSURE_INIT(&w->my_closure, watch_connectivity_state_locked, w, |
Craig Tiller | ee4b145 | 2017-05-12 10:56:03 -0700 | [diff] [blame] | 3181 | grpc_combiner_scheduler(chand->combiner)), |
Craig Tiller | 613dafa | 2017-02-09 12:00:43 -0800 | [diff] [blame] | 3182 | GRPC_ERROR_NONE); |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 3183 | } |
Mark D. Roth | 718c834 | 2018-02-28 13:00:04 -0800 | [diff] [blame] | 3184 | |
| 3185 | grpc_subchannel_call* grpc_client_channel_get_subchannel_call( |
| 3186 | grpc_call_element* elem) { |
| 3187 | call_data* calld = static_cast<call_data*>(elem->call_data); |
| 3188 | return calld->subchannel_call; |
| 3189 | } |