Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 6169d5f | 2016-03-31 07:46:18 -0700 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions are |
| 8 | * met: |
| 9 | * |
| 10 | * * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * * Redistributions in binary form must reproduce the above |
| 13 | * copyright notice, this list of conditions and the following disclaimer |
| 14 | * in the documentation and/or other materials provided with the |
| 15 | * distribution. |
| 16 | * * Neither the name of Google Inc. nor the names of its |
| 17 | * contributors may be used to endorse or promote products derived from |
| 18 | * this software without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | * |
| 32 | */ |
| 33 | |
Mark D. Roth | 2137cd8 | 2016-09-14 09:04:00 -0700 | [diff] [blame] | 34 | #include "src/core/ext/client_channel/client_channel.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 35 | |
Mark D. Roth | 4c0fe49 | 2016-08-31 13:51:55 -0700 | [diff] [blame] | 36 | #include <stdbool.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 37 | #include <stdio.h> |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 38 | #include <string.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 39 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 40 | #include <grpc/support/alloc.h> |
| 41 | #include <grpc/support/log.h> |
Mark D. Roth | b2d2488 | 2016-10-27 15:44:07 -0700 | [diff] [blame] | 42 | #include <grpc/support/string_util.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 43 | #include <grpc/support/sync.h> |
| 44 | #include <grpc/support/useful.h> |
| 45 | |
Mark D. Roth | d58a985 | 2017-01-18 08:28:57 -0800 | [diff] [blame] | 46 | #include "src/core/ext/client_channel/http_connect_handshaker.h" |
Mark D. Roth | 1519574 | 2016-10-07 09:02:28 -0700 | [diff] [blame] | 47 | #include "src/core/ext/client_channel/lb_policy_registry.h" |
Mark D. Roth | dc9bee7 | 2017-02-07 12:29:14 -0800 | [diff] [blame] | 48 | #include "src/core/ext/client_channel/proxy_mapper_registry.h" |
Mark D. Roth | 21d4b2d | 2016-11-18 09:53:41 -0800 | [diff] [blame] | 49 | #include "src/core/ext/client_channel/resolver_registry.h" |
Mark D. Roth | 2137cd8 | 2016-09-14 09:04:00 -0700 | [diff] [blame] | 50 | #include "src/core/ext/client_channel/subchannel.h" |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 51 | #include "src/core/lib/channel/channel_args.h" |
| 52 | #include "src/core/lib/channel/connected_channel.h" |
Mark D. Roth | 72f6da8 | 2016-09-02 13:42:38 -0700 | [diff] [blame] | 53 | #include "src/core/lib/channel/deadline_filter.h" |
Craig Tiller | befafe6 | 2017-02-09 11:30:54 -0800 | [diff] [blame] | 54 | #include "src/core/lib/iomgr/combiner.h" |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 55 | #include "src/core/lib/iomgr/iomgr.h" |
Mark D. Roth | 4c0fe49 | 2016-08-31 13:51:55 -0700 | [diff] [blame] | 56 | #include "src/core/lib/iomgr/polling_entity.h" |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 57 | #include "src/core/lib/profiling/timers.h" |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 58 | #include "src/core/lib/slice/slice_internal.h" |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 59 | #include "src/core/lib/support/string.h" |
| 60 | #include "src/core/lib/surface/channel.h" |
| 61 | #include "src/core/lib/transport/connectivity_state.h" |
Mark D. Roth | 9fe284e | 2016-09-12 11:22:27 -0700 | [diff] [blame] | 62 | #include "src/core/lib/transport/metadata.h" |
| 63 | #include "src/core/lib/transport/metadata_batch.h" |
Mark D. Roth | ea846a0 | 2016-11-03 11:32:54 -0700 | [diff] [blame] | 64 | #include "src/core/lib/transport/service_config.h" |
Mark D. Roth | 9fe284e | 2016-09-12 11:22:27 -0700 | [diff] [blame] | 65 | #include "src/core/lib/transport/static_metadata.h" |
Craig Tiller | 8910ac6 | 2015-10-08 16:49:15 -0700 | [diff] [blame] | 66 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 67 | /* Client channel implementation */ |
| 68 | |
Mark D. Roth | 26b7be4 | 2016-10-24 10:08:07 -0700 | [diff] [blame] | 69 | /************************************************************************* |
| 70 | * METHOD-CONFIG TABLE |
| 71 | */ |
| 72 | |
Mark D. Roth | 9d48094 | 2016-10-19 14:18:05 -0700 | [diff] [blame] | 73 | typedef enum { |
| 74 | WAIT_FOR_READY_UNSET, |
| 75 | WAIT_FOR_READY_FALSE, |
| 76 | WAIT_FOR_READY_TRUE |
| 77 | } wait_for_ready_value; |
| 78 | |
Mark D. Roth | 95b627b | 2017-02-24 11:02:58 -0800 | [diff] [blame] | 79 | typedef struct { |
| 80 | gpr_refcount refs; |
Mark D. Roth | 9d48094 | 2016-10-19 14:18:05 -0700 | [diff] [blame] | 81 | gpr_timespec timeout; |
| 82 | wait_for_ready_value wait_for_ready; |
| 83 | } method_parameters; |
| 84 | |
Mark D. Roth | 722de8d | 2017-02-27 10:50:44 -0800 | [diff] [blame] | 85 | static method_parameters *method_parameters_ref( |
Mark D. Roth | 95b627b | 2017-02-24 11:02:58 -0800 | [diff] [blame] | 86 | method_parameters *method_params) { |
| 87 | gpr_ref(&method_params->refs); |
| 88 | return method_params; |
Mark D. Roth | 9d48094 | 2016-10-19 14:18:05 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Mark D. Roth | 95b627b | 2017-02-24 11:02:58 -0800 | [diff] [blame] | 91 | static void method_parameters_unref(method_parameters *method_params) { |
| 92 | if (gpr_unref(&method_params->refs)) { |
| 93 | gpr_free(method_params); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | static void *method_parameters_copy(void *value) { |
| 98 | return method_parameters_ref(value); |
| 99 | } |
| 100 | |
| 101 | static void method_parameters_free(grpc_exec_ctx *exec_ctx, void *value) { |
| 102 | method_parameters_unref(value); |
Craig Tiller | 87a7e1f | 2016-11-09 09:42:19 -0800 | [diff] [blame] | 103 | } |
| 104 | |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 105 | static const grpc_slice_hash_table_vtable method_parameters_vtable = { |
Craig Tiller | b28c7e8 | 2016-11-18 10:29:04 -0800 | [diff] [blame] | 106 | method_parameters_free, method_parameters_copy}; |
Mark D. Roth | 9d48094 | 2016-10-19 14:18:05 -0700 | [diff] [blame] | 107 | |
Mark D. Roth | 95b627b | 2017-02-24 11:02:58 -0800 | [diff] [blame] | 108 | static bool parse_wait_for_ready(grpc_json *field, |
| 109 | wait_for_ready_value *wait_for_ready) { |
| 110 | if (field->type != GRPC_JSON_TRUE && field->type != GRPC_JSON_FALSE) { |
| 111 | return false; |
| 112 | } |
| 113 | *wait_for_ready = field->type == GRPC_JSON_TRUE ? WAIT_FOR_READY_TRUE |
| 114 | : WAIT_FOR_READY_FALSE; |
| 115 | return true; |
| 116 | } |
| 117 | |
Mark D. Roth | 722de8d | 2017-02-27 10:50:44 -0800 | [diff] [blame] | 118 | static bool parse_timeout(grpc_json *field, gpr_timespec *timeout) { |
Mark D. Roth | 95b627b | 2017-02-24 11:02:58 -0800 | [diff] [blame] | 119 | if (field->type != GRPC_JSON_STRING) return false; |
| 120 | size_t len = strlen(field->value); |
| 121 | if (field->value[len - 1] != 's') return false; |
| 122 | char *buf = gpr_strdup(field->value); |
| 123 | buf[len - 1] = '\0'; // Remove trailing 's'. |
| 124 | char *decimal_point = strchr(buf, '.'); |
| 125 | if (decimal_point != NULL) { |
| 126 | *decimal_point = '\0'; |
| 127 | timeout->tv_nsec = gpr_parse_nonnegative_int(decimal_point + 1); |
| 128 | if (timeout->tv_nsec == -1) { |
| 129 | gpr_free(buf); |
| 130 | return false; |
| 131 | } |
| 132 | // There should always be exactly 3, 6, or 9 fractional digits. |
| 133 | int multiplier = 1; |
| 134 | switch (strlen(decimal_point + 1)) { |
| 135 | case 9: |
| 136 | break; |
| 137 | case 6: |
| 138 | multiplier *= 1000; |
| 139 | break; |
| 140 | case 3: |
| 141 | multiplier *= 1000000; |
| 142 | break; |
| 143 | default: // Unsupported number of digits. |
| 144 | gpr_free(buf); |
| 145 | return false; |
| 146 | } |
| 147 | timeout->tv_nsec *= multiplier; |
| 148 | } |
| 149 | timeout->tv_sec = gpr_parse_nonnegative_int(buf); |
| 150 | gpr_free(buf); |
| 151 | if (timeout->tv_sec == -1) return false; |
| 152 | return true; |
| 153 | } |
| 154 | |
Mark D. Roth | e30baeb | 2016-11-03 08:16:19 -0700 | [diff] [blame] | 155 | static void *method_parameters_create_from_json(const grpc_json *json) { |
Mark D. Roth | c968e60 | 2016-11-02 14:07:36 -0700 | [diff] [blame] | 156 | wait_for_ready_value wait_for_ready = WAIT_FOR_READY_UNSET; |
Mark D. Roth | 47f1084 | 2016-11-03 08:45:27 -0700 | [diff] [blame] | 157 | gpr_timespec timeout = {0, 0, GPR_TIMESPAN}; |
| 158 | for (grpc_json *field = json->child; field != NULL; field = field->next) { |
Mark D. Roth | c968e60 | 2016-11-02 14:07:36 -0700 | [diff] [blame] | 159 | if (field->key == NULL) continue; |
Mark D. Roth | 84c8a02 | 2016-11-10 09:39:34 -0800 | [diff] [blame] | 160 | if (strcmp(field->key, "waitForReady") == 0) { |
Mark D. Roth | c968e60 | 2016-11-02 14:07:36 -0700 | [diff] [blame] | 161 | if (wait_for_ready != WAIT_FOR_READY_UNSET) return NULL; // Duplicate. |
Mark D. Roth | 95b627b | 2017-02-24 11:02:58 -0800 | [diff] [blame] | 162 | if (!parse_wait_for_ready(field, &wait_for_ready)) return NULL; |
Mark D. Roth | c968e60 | 2016-11-02 14:07:36 -0700 | [diff] [blame] | 163 | } else if (strcmp(field->key, "timeout") == 0) { |
| 164 | if (timeout.tv_sec > 0 || timeout.tv_nsec > 0) return NULL; // Duplicate. |
Mark D. Roth | 95b627b | 2017-02-24 11:02:58 -0800 | [diff] [blame] | 165 | if (!parse_timeout(field, &timeout)) return NULL; |
Mark D. Roth | c968e60 | 2016-11-02 14:07:36 -0700 | [diff] [blame] | 166 | } |
| 167 | } |
Mark D. Roth | 9d48094 | 2016-10-19 14:18:05 -0700 | [diff] [blame] | 168 | method_parameters *value = gpr_malloc(sizeof(method_parameters)); |
Mark D. Roth | 95b627b | 2017-02-24 11:02:58 -0800 | [diff] [blame] | 169 | gpr_ref_init(&value->refs, 1); |
Mark D. Roth | c968e60 | 2016-11-02 14:07:36 -0700 | [diff] [blame] | 170 | value->timeout = timeout; |
| 171 | value->wait_for_ready = wait_for_ready; |
Mark D. Roth | 9d48094 | 2016-10-19 14:18:05 -0700 | [diff] [blame] | 172 | return value; |
| 173 | } |
| 174 | |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 175 | /************************************************************************* |
| 176 | * CHANNEL-WIDE FUNCTIONS |
| 177 | */ |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 178 | |
Craig Tiller | 800dacb | 2015-10-06 09:10:26 -0700 | [diff] [blame] | 179 | typedef struct client_channel_channel_data { |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 180 | /** resolver for this channel */ |
| 181 | grpc_resolver *resolver; |
Craig Tiller | 20a3c35 | 2015-08-05 08:39:50 -0700 | [diff] [blame] | 182 | /** have we started resolving this channel */ |
Mark D. Roth | 4c0fe49 | 2016-08-31 13:51:55 -0700 | [diff] [blame] | 183 | bool started_resolving; |
Mark D. Roth | 0e48a9a | 2016-09-08 14:14:39 -0700 | [diff] [blame] | 184 | /** client channel factory */ |
| 185 | grpc_client_channel_factory *client_channel_factory; |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 186 | |
Craig Tiller | befafe6 | 2017-02-09 11:30:54 -0800 | [diff] [blame] | 187 | /** combiner protecting all variables below in this data structure */ |
| 188 | grpc_combiner *combiner; |
Mark D. Roth | 046cf76 | 2016-09-26 11:13:51 -0700 | [diff] [blame] | 189 | /** currently active load balancer */ |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 190 | grpc_lb_policy *lb_policy; |
Mark D. Roth | 9d48094 | 2016-10-19 14:18:05 -0700 | [diff] [blame] | 191 | /** maps method names to method_parameters structs */ |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 192 | grpc_slice_hash_table *method_params_table; |
Mark D. Roth | 046cf76 | 2016-09-26 11:13:51 -0700 | [diff] [blame] | 193 | /** incoming resolver result - set by resolver.next() */ |
Mark D. Roth | af84245 | 2016-10-21 15:05:15 -0700 | [diff] [blame] | 194 | grpc_channel_args *resolver_result; |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 195 | /** a list of closures that are all waiting for config to come in */ |
Craig Tiller | d9ccbbf | 2015-09-22 09:30:00 -0700 | [diff] [blame] | 196 | grpc_closure_list waiting_for_config_closures; |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 197 | /** resolver callback */ |
Mark D. Roth | ff4df06 | 2016-08-22 15:02:49 -0700 | [diff] [blame] | 198 | grpc_closure on_resolver_result_changed; |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 199 | /** connectivity state being tracked */ |
Craig Tiller | ca3e9d3 | 2015-06-27 18:37:27 -0700 | [diff] [blame] | 200 | grpc_connectivity_state_tracker state_tracker; |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 201 | /** when an lb_policy arrives, should we try to exit idle */ |
Mark D. Roth | 4c0fe49 | 2016-08-31 13:51:55 -0700 | [diff] [blame] | 202 | bool exit_idle_when_lb_policy_arrives; |
Craig Tiller | 906e3bc | 2015-11-24 07:31:31 -0800 | [diff] [blame] | 203 | /** owning stack */ |
| 204 | grpc_channel_stack *owning_stack; |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 205 | /** interested parties (owned) */ |
| 206 | grpc_pollset_set *interested_parties; |
Craig Tiller | 613dafa | 2017-02-09 12:00:43 -0800 | [diff] [blame] | 207 | |
| 208 | /* the following properties are guarded by a mutex since API's require them |
Craig Tiller | 46dd790 | 2017-02-23 09:42:16 -0800 | [diff] [blame] | 209 | to be instantaneously available */ |
Craig Tiller | 613dafa | 2017-02-09 12:00:43 -0800 | [diff] [blame] | 210 | gpr_mu info_mu; |
| 211 | char *info_lb_policy_name; |
| 212 | /** service config in JSON form */ |
| 213 | char *info_service_config_json; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 214 | } channel_data; |
| 215 | |
Craig Tiller | d6c98df | 2015-08-18 09:33:44 -0700 | [diff] [blame] | 216 | /** 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] | 217 | resolver, to watch for state changes from the lb_policy. When a state |
| 218 | change is seen, we update the channel, and create a new watcher. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 219 | typedef struct { |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 220 | channel_data *chand; |
Craig Tiller | 3382511 | 2015-09-18 07:44:19 -0700 | [diff] [blame] | 221 | grpc_closure on_changed; |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 222 | grpc_connectivity_state state; |
| 223 | grpc_lb_policy *lb_policy; |
| 224 | } lb_policy_connectivity_watcher; |
| 225 | |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 226 | static void watch_lb_policy_locked(grpc_exec_ctx *exec_ctx, channel_data *chand, |
| 227 | grpc_lb_policy *lb_policy, |
| 228 | grpc_connectivity_state current_state); |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 229 | |
Craig Tiller | 8c0d96f | 2016-03-11 14:27:52 -0800 | [diff] [blame] | 230 | static void set_channel_connectivity_state_locked(grpc_exec_ctx *exec_ctx, |
| 231 | channel_data *chand, |
| 232 | grpc_connectivity_state state, |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 233 | grpc_error *error, |
Craig Tiller | 8c0d96f | 2016-03-11 14:27:52 -0800 | [diff] [blame] | 234 | const char *reason) { |
| 235 | if ((state == GRPC_CHANNEL_TRANSIENT_FAILURE || |
Craig Tiller | 48ed92e | 2016-06-02 11:07:12 -0700 | [diff] [blame] | 236 | state == GRPC_CHANNEL_SHUTDOWN) && |
Craig Tiller | 8c0d96f | 2016-03-11 14:27:52 -0800 | [diff] [blame] | 237 | chand->lb_policy != NULL) { |
Mark D. Roth | 59c9f90 | 2016-09-28 13:33:21 -0700 | [diff] [blame] | 238 | /* cancel picks with wait_for_ready=false */ |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 239 | grpc_lb_policy_cancel_picks_locked( |
Craig Tiller | 8c0d96f | 2016-03-11 14:27:52 -0800 | [diff] [blame] | 240 | exec_ctx, chand->lb_policy, |
Mark D. Roth | 59c9f90 | 2016-09-28 13:33:21 -0700 | [diff] [blame] | 241 | /* mask= */ GRPC_INITIAL_METADATA_WAIT_FOR_READY, |
Mark D. Roth | 58f52b7 | 2016-09-09 13:55:18 -0700 | [diff] [blame] | 242 | /* check= */ 0, GRPC_ERROR_REF(error)); |
Craig Tiller | 8c0d96f | 2016-03-11 14:27:52 -0800 | [diff] [blame] | 243 | } |
Craig Tiller | 9ccf5f1 | 2016-05-07 21:41:01 -0700 | [diff] [blame] | 244 | grpc_connectivity_state_set(exec_ctx, &chand->state_tracker, state, error, |
| 245 | reason); |
Craig Tiller | 8c0d96f | 2016-03-11 14:27:52 -0800 | [diff] [blame] | 246 | } |
| 247 | |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 248 | static void on_lb_policy_state_changed_locked(grpc_exec_ctx *exec_ctx, |
Craig Tiller | befafe6 | 2017-02-09 11:30:54 -0800 | [diff] [blame] | 249 | void *arg, grpc_error *error) { |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 250 | lb_policy_connectivity_watcher *w = arg; |
Craig Tiller | 5d44c06 | 2015-07-01 08:55:28 -0700 | [diff] [blame] | 251 | grpc_connectivity_state publish_state = w->state; |
Craig Tiller | c5de835 | 2017-02-09 14:08:05 -0800 | [diff] [blame] | 252 | /* check if the notification is for the latest policy */ |
| 253 | if (w->lb_policy == w->chand->lb_policy) { |
| 254 | if (publish_state == GRPC_CHANNEL_SHUTDOWN && w->chand->resolver != NULL) { |
| 255 | publish_state = GRPC_CHANNEL_TRANSIENT_FAILURE; |
Craig Tiller | 972470b | 2017-02-09 15:05:36 -0800 | [diff] [blame] | 256 | grpc_resolver_channel_saw_error_locked(exec_ctx, w->chand->resolver); |
Craig Tiller | c5de835 | 2017-02-09 14:08:05 -0800 | [diff] [blame] | 257 | GRPC_LB_POLICY_UNREF(exec_ctx, w->chand->lb_policy, "channel"); |
| 258 | w->chand->lb_policy = NULL; |
| 259 | } |
| 260 | set_channel_connectivity_state_locked(exec_ctx, w->chand, publish_state, |
| 261 | GRPC_ERROR_REF(error), "lb_changed"); |
| 262 | if (w->state != GRPC_CHANNEL_SHUTDOWN) { |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 263 | watch_lb_policy_locked(exec_ctx, w->chand, w->lb_policy, w->state); |
Craig Tiller | c5de835 | 2017-02-09 14:08:05 -0800 | [diff] [blame] | 264 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 265 | } |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 266 | |
Craig Tiller | 906e3bc | 2015-11-24 07:31:31 -0800 | [diff] [blame] | 267 | GRPC_CHANNEL_STACK_UNREF(exec_ctx, w->chand->owning_stack, "watch_lb_policy"); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 268 | gpr_free(w); |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 269 | } |
| 270 | |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 271 | static void watch_lb_policy_locked(grpc_exec_ctx *exec_ctx, channel_data *chand, |
| 272 | grpc_lb_policy *lb_policy, |
| 273 | grpc_connectivity_state current_state) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 274 | lb_policy_connectivity_watcher *w = gpr_malloc(sizeof(*w)); |
Craig Tiller | 906e3bc | 2015-11-24 07:31:31 -0800 | [diff] [blame] | 275 | GRPC_CHANNEL_STACK_REF(chand->owning_stack, "watch_lb_policy"); |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 276 | |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 277 | w->chand = chand; |
Craig Tiller | befafe6 | 2017-02-09 11:30:54 -0800 | [diff] [blame] | 278 | grpc_closure_init(&w->on_changed, on_lb_policy_state_changed_locked, w, |
| 279 | grpc_combiner_scheduler(chand->combiner, false)); |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 280 | w->state = current_state; |
| 281 | w->lb_policy = lb_policy; |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 282 | grpc_lb_policy_notify_on_state_change_locked(exec_ctx, lb_policy, &w->state, |
| 283 | &w->on_changed); |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 284 | } |
| 285 | |
Craig Tiller | befafe6 | 2017-02-09 11:30:54 -0800 | [diff] [blame] | 286 | static void on_resolver_result_changed_locked(grpc_exec_ctx *exec_ctx, |
| 287 | void *arg, grpc_error *error) { |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 288 | channel_data *chand = arg; |
Mark D. Roth | b2d2488 | 2016-10-27 15:44:07 -0700 | [diff] [blame] | 289 | char *lb_policy_name = NULL; |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 290 | grpc_lb_policy *lb_policy = NULL; |
| 291 | grpc_lb_policy *old_lb_policy; |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 292 | grpc_slice_hash_table *method_params_table = NULL; |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 293 | grpc_connectivity_state state = GRPC_CHANNEL_TRANSIENT_FAILURE; |
Mark D. Roth | 4c0fe49 | 2016-08-31 13:51:55 -0700 | [diff] [blame] | 294 | bool exit_idle = false; |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 295 | grpc_error *state_error = GRPC_ERROR_CREATE("No load balancing policy"); |
Mark D. Roth | c625c7a | 2016-11-09 14:12:37 -0800 | [diff] [blame] | 296 | char *service_config_json = NULL; |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 297 | |
Mark D. Roth | 046cf76 | 2016-09-26 11:13:51 -0700 | [diff] [blame] | 298 | if (chand->resolver_result != NULL) { |
Mark D. Roth | 5bd7be0 | 2016-10-21 14:19:50 -0700 | [diff] [blame] | 299 | // Find LB policy name. |
Mark D. Roth | af84245 | 2016-10-21 15:05:15 -0700 | [diff] [blame] | 300 | const grpc_arg *channel_arg = |
Mark D. Roth | 4112499 | 2016-11-03 11:22:20 -0700 | [diff] [blame] | 301 | grpc_channel_args_find(chand->resolver_result, GRPC_ARG_LB_POLICY_NAME); |
Mark D. Roth | af84245 | 2016-10-21 15:05:15 -0700 | [diff] [blame] | 302 | if (channel_arg != NULL) { |
| 303 | GPR_ASSERT(channel_arg->type == GRPC_ARG_STRING); |
| 304 | lb_policy_name = channel_arg->value.string; |
Mark D. Roth | 5bd7be0 | 2016-10-21 14:19:50 -0700 | [diff] [blame] | 305 | } |
Mark D. Roth | 88405f7 | 2016-10-03 08:24:52 -0700 | [diff] [blame] | 306 | // Special case: If all of the addresses are balancer addresses, |
| 307 | // assume that we should use the grpclb policy, regardless of what the |
| 308 | // resolver actually specified. |
Mark D. Roth | af84245 | 2016-10-21 15:05:15 -0700 | [diff] [blame] | 309 | channel_arg = |
Mark D. Roth | 4112499 | 2016-11-03 11:22:20 -0700 | [diff] [blame] | 310 | grpc_channel_args_find(chand->resolver_result, GRPC_ARG_LB_ADDRESSES); |
Mark D. Roth | af84245 | 2016-10-21 15:05:15 -0700 | [diff] [blame] | 311 | if (channel_arg != NULL) { |
| 312 | GPR_ASSERT(channel_arg->type == GRPC_ARG_POINTER); |
Mark D. Roth | 557c990 | 2016-10-24 11:12:05 -0700 | [diff] [blame] | 313 | grpc_lb_addresses *addresses = channel_arg->value.pointer.p; |
Mark D. Roth | af84245 | 2016-10-21 15:05:15 -0700 | [diff] [blame] | 314 | bool found_backend_address = false; |
| 315 | for (size_t i = 0; i < addresses->num_addresses; ++i) { |
| 316 | if (!addresses->addresses[i].is_balancer) { |
| 317 | found_backend_address = true; |
| 318 | break; |
| 319 | } |
Mark D. Roth | 88405f7 | 2016-10-03 08:24:52 -0700 | [diff] [blame] | 320 | } |
Mark D. Roth | af84245 | 2016-10-21 15:05:15 -0700 | [diff] [blame] | 321 | if (!found_backend_address) { |
| 322 | if (lb_policy_name != NULL && strcmp(lb_policy_name, "grpclb") != 0) { |
| 323 | gpr_log(GPR_INFO, |
| 324 | "resolver requested LB policy %s but provided only balancer " |
| 325 | "addresses, no backend addresses -- forcing use of grpclb LB " |
| 326 | "policy", |
Mark D. Roth | 5f40e5d | 2016-10-24 13:09:05 -0700 | [diff] [blame] | 327 | lb_policy_name); |
Mark D. Roth | af84245 | 2016-10-21 15:05:15 -0700 | [diff] [blame] | 328 | } |
| 329 | lb_policy_name = "grpclb"; |
Mark D. Roth | 88405f7 | 2016-10-03 08:24:52 -0700 | [diff] [blame] | 330 | } |
Mark D. Roth | 88405f7 | 2016-10-03 08:24:52 -0700 | [diff] [blame] | 331 | } |
| 332 | // Use pick_first if nothing was specified and we didn't select grpclb |
| 333 | // above. |
| 334 | if (lb_policy_name == NULL) lb_policy_name = "pick_first"; |
Mark D. Roth | 4112499 | 2016-11-03 11:22:20 -0700 | [diff] [blame] | 335 | // Instantiate LB policy. |
| 336 | grpc_lb_policy_args lb_policy_args; |
| 337 | lb_policy_args.args = chand->resolver_result; |
| 338 | lb_policy_args.client_channel_factory = chand->client_channel_factory; |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 339 | lb_policy_args.combiner = chand->combiner; |
Mark D. Roth | 88405f7 | 2016-10-03 08:24:52 -0700 | [diff] [blame] | 340 | lb_policy = |
| 341 | grpc_lb_policy_create(exec_ctx, lb_policy_name, &lb_policy_args); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 342 | if (lb_policy != NULL) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 343 | GRPC_LB_POLICY_REF(lb_policy, "config_change"); |
Craig Tiller | f707d62 | 2016-05-06 14:26:12 -0700 | [diff] [blame] | 344 | GRPC_ERROR_UNREF(state_error); |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 345 | state = grpc_lb_policy_check_connectivity_locked(exec_ctx, lb_policy, |
| 346 | &state_error); |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 347 | } |
Mark D. Roth | 4112499 | 2016-11-03 11:22:20 -0700 | [diff] [blame] | 348 | // Find service config. |
Mark D. Roth | af84245 | 2016-10-21 15:05:15 -0700 | [diff] [blame] | 349 | channel_arg = |
Mark D. Roth | 4112499 | 2016-11-03 11:22:20 -0700 | [diff] [blame] | 350 | grpc_channel_args_find(chand->resolver_result, GRPC_ARG_SERVICE_CONFIG); |
Mark D. Roth | 046cf76 | 2016-09-26 11:13:51 -0700 | [diff] [blame] | 351 | if (channel_arg != NULL) { |
Mark D. Roth | 9ec28af | 2016-11-03 12:32:39 -0700 | [diff] [blame] | 352 | GPR_ASSERT(channel_arg->type == GRPC_ARG_STRING); |
Mark D. Roth | c625c7a | 2016-11-09 14:12:37 -0800 | [diff] [blame] | 353 | service_config_json = gpr_strdup(channel_arg->value.string); |
Mark D. Roth | 70a1abd | 2016-11-04 09:26:37 -0700 | [diff] [blame] | 354 | grpc_service_config *service_config = |
Mark D. Roth | c625c7a | 2016-11-09 14:12:37 -0800 | [diff] [blame] | 355 | grpc_service_config_create(service_config_json); |
Mark D. Roth | bdc58b2 | 2016-11-04 09:25:57 -0700 | [diff] [blame] | 356 | if (service_config != NULL) { |
| 357 | method_params_table = grpc_service_config_create_method_config_table( |
Craig Tiller | b28c7e8 | 2016-11-18 10:29:04 -0800 | [diff] [blame] | 358 | exec_ctx, service_config, method_parameters_create_from_json, |
Mark D. Roth | bdc58b2 | 2016-11-04 09:25:57 -0700 | [diff] [blame] | 359 | &method_parameters_vtable); |
| 360 | grpc_service_config_destroy(service_config); |
| 361 | } |
Mark D. Roth | 9fe284e | 2016-09-12 11:22:27 -0700 | [diff] [blame] | 362 | } |
Mark D. Roth | f79ce7d | 2016-11-04 08:43:36 -0700 | [diff] [blame] | 363 | // Before we clean up, save a copy of lb_policy_name, since it might |
| 364 | // be pointing to data inside chand->resolver_result. |
| 365 | // The copy will be saved in chand->lb_policy_name below. |
| 366 | lb_policy_name = gpr_strdup(lb_policy_name); |
Craig Tiller | 87a7e1f | 2016-11-09 09:42:19 -0800 | [diff] [blame] | 367 | grpc_channel_args_destroy(exec_ctx, chand->resolver_result); |
Mark D. Roth | 046cf76 | 2016-09-26 11:13:51 -0700 | [diff] [blame] | 368 | chand->resolver_result = NULL; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 369 | } |
| 370 | |
Craig Tiller | 86c9958 | 2015-11-25 15:22:26 -0800 | [diff] [blame] | 371 | if (lb_policy != NULL) { |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 372 | grpc_pollset_set_add_pollset_set(exec_ctx, lb_policy->interested_parties, |
| 373 | chand->interested_parties); |
Craig Tiller | 86c9958 | 2015-11-25 15:22:26 -0800 | [diff] [blame] | 374 | } |
| 375 | |
Craig Tiller | 613dafa | 2017-02-09 12:00:43 -0800 | [diff] [blame] | 376 | gpr_mu_lock(&chand->info_mu); |
Mark D. Roth | b2d2488 | 2016-10-27 15:44:07 -0700 | [diff] [blame] | 377 | if (lb_policy_name != NULL) { |
Craig Tiller | 613dafa | 2017-02-09 12:00:43 -0800 | [diff] [blame] | 378 | gpr_free(chand->info_lb_policy_name); |
| 379 | chand->info_lb_policy_name = lb_policy_name; |
Mark D. Roth | b2d2488 | 2016-10-27 15:44:07 -0700 | [diff] [blame] | 380 | } |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 381 | old_lb_policy = chand->lb_policy; |
| 382 | chand->lb_policy = lb_policy; |
Mark D. Roth | c625c7a | 2016-11-09 14:12:37 -0800 | [diff] [blame] | 383 | if (service_config_json != NULL) { |
Craig Tiller | 613dafa | 2017-02-09 12:00:43 -0800 | [diff] [blame] | 384 | gpr_free(chand->info_service_config_json); |
| 385 | chand->info_service_config_json = service_config_json; |
Mark D. Roth | c625c7a | 2016-11-09 14:12:37 -0800 | [diff] [blame] | 386 | } |
Craig Tiller | 613dafa | 2017-02-09 12:00:43 -0800 | [diff] [blame] | 387 | gpr_mu_unlock(&chand->info_mu); |
Mark D. Roth | 9d48094 | 2016-10-19 14:18:05 -0700 | [diff] [blame] | 388 | if (chand->method_params_table != NULL) { |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 389 | grpc_slice_hash_table_unref(exec_ctx, chand->method_params_table); |
Mark D. Roth | 046cf76 | 2016-09-26 11:13:51 -0700 | [diff] [blame] | 390 | } |
Mark D. Roth | 9d48094 | 2016-10-19 14:18:05 -0700 | [diff] [blame] | 391 | chand->method_params_table = method_params_table; |
Craig Tiller | 0ede545 | 2016-04-23 12:21:45 -0700 | [diff] [blame] | 392 | if (lb_policy != NULL) { |
Craig Tiller | 91031da | 2016-12-28 15:44:25 -0800 | [diff] [blame] | 393 | grpc_closure_list_sched(exec_ctx, &chand->waiting_for_config_closures); |
Craig Tiller | 0ede545 | 2016-04-23 12:21:45 -0700 | [diff] [blame] | 394 | } else if (chand->resolver == NULL /* disconnected */) { |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 395 | grpc_closure_list_fail_all( |
| 396 | &chand->waiting_for_config_closures, |
| 397 | GRPC_ERROR_CREATE_REFERENCING("Channel disconnected", &error, 1)); |
Craig Tiller | 91031da | 2016-12-28 15:44:25 -0800 | [diff] [blame] | 398 | grpc_closure_list_sched(exec_ctx, &chand->waiting_for_config_closures); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 399 | } |
| 400 | if (lb_policy != NULL && chand->exit_idle_when_lb_policy_arrives) { |
| 401 | GRPC_LB_POLICY_REF(lb_policy, "exit_idle"); |
Mark D. Roth | 4c0fe49 | 2016-08-31 13:51:55 -0700 | [diff] [blame] | 402 | exit_idle = true; |
| 403 | chand->exit_idle_when_lb_policy_arrives = false; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 404 | } |
Craig Tiller | 9846503 | 2015-06-29 14:36:42 -0700 | [diff] [blame] | 405 | |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 406 | if (error == GRPC_ERROR_NONE && chand->resolver) { |
Craig Tiller | 9ccf5f1 | 2016-05-07 21:41:01 -0700 | [diff] [blame] | 407 | set_channel_connectivity_state_locked( |
| 408 | exec_ctx, chand, state, GRPC_ERROR_REF(state_error), "new_lb+resolver"); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 409 | if (lb_policy != NULL) { |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 410 | watch_lb_policy_locked(exec_ctx, chand, lb_policy, state); |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 411 | } |
Craig Tiller | 906e3bc | 2015-11-24 07:31:31 -0800 | [diff] [blame] | 412 | GRPC_CHANNEL_STACK_REF(chand->owning_stack, "resolver"); |
Craig Tiller | 972470b | 2017-02-09 15:05:36 -0800 | [diff] [blame] | 413 | grpc_resolver_next_locked(exec_ctx, chand->resolver, |
| 414 | &chand->resolver_result, |
| 415 | &chand->on_resolver_result_changed); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 416 | } else { |
Craig Tiller | 76a5c0e | 2016-03-09 09:05:30 -0800 | [diff] [blame] | 417 | if (chand->resolver != NULL) { |
Craig Tiller | 972470b | 2017-02-09 15:05:36 -0800 | [diff] [blame] | 418 | grpc_resolver_shutdown_locked(exec_ctx, chand->resolver); |
Craig Tiller | 76a5c0e | 2016-03-09 09:05:30 -0800 | [diff] [blame] | 419 | GRPC_RESOLVER_UNREF(exec_ctx, chand->resolver, "channel"); |
| 420 | chand->resolver = NULL; |
| 421 | } |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 422 | grpc_error *refs[] = {error, state_error}; |
Craig Tiller | 8c0d96f | 2016-03-11 14:27:52 -0800 | [diff] [blame] | 423 | set_channel_connectivity_state_locked( |
Craig Tiller | d925c93 | 2016-06-06 08:38:50 -0700 | [diff] [blame] | 424 | exec_ctx, chand, GRPC_CHANNEL_SHUTDOWN, |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 425 | GRPC_ERROR_CREATE_REFERENCING("Got config after disconnection", refs, |
| 426 | GPR_ARRAY_SIZE(refs)), |
| 427 | "resolver_gone"); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 428 | } |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 429 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 430 | if (exit_idle) { |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 431 | grpc_lb_policy_exit_idle_locked(exec_ctx, lb_policy); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 432 | GRPC_LB_POLICY_UNREF(exec_ctx, lb_policy, "exit_idle"); |
| 433 | } |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 434 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 435 | if (old_lb_policy != NULL) { |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 436 | grpc_pollset_set_del_pollset_set( |
| 437 | exec_ctx, old_lb_policy->interested_parties, chand->interested_parties); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 438 | GRPC_LB_POLICY_UNREF(exec_ctx, old_lb_policy, "channel"); |
| 439 | } |
Craig Tiller | 000cd8f | 2015-09-18 07:20:29 -0700 | [diff] [blame] | 440 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 441 | if (lb_policy != NULL) { |
| 442 | GRPC_LB_POLICY_UNREF(exec_ctx, lb_policy, "config_change"); |
| 443 | } |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 444 | |
Craig Tiller | 906e3bc | 2015-11-24 07:31:31 -0800 | [diff] [blame] | 445 | GRPC_CHANNEL_STACK_UNREF(exec_ctx, chand->owning_stack, "resolver"); |
Craig Tiller | 9ccf5f1 | 2016-05-07 21:41:01 -0700 | [diff] [blame] | 446 | GRPC_ERROR_UNREF(state_error); |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 447 | } |
| 448 | |
Craig Tiller | a8610c0 | 2017-02-14 10:05:11 -0800 | [diff] [blame] | 449 | static void start_transport_op_locked(grpc_exec_ctx *exec_ctx, void *arg, |
| 450 | grpc_error *error_ignored) { |
Craig Tiller | befafe6 | 2017-02-09 11:30:54 -0800 | [diff] [blame] | 451 | grpc_transport_op *op = arg; |
| 452 | grpc_channel_element *elem = op->transport_private.args[0]; |
Craig Tiller | ca3e9d3 | 2015-06-27 18:37:27 -0700 | [diff] [blame] | 453 | channel_data *chand = elem->channel_data; |
Craig Tiller | 000cd8f | 2015-09-18 07:20:29 -0700 | [diff] [blame] | 454 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 455 | if (op->on_connectivity_state_change != NULL) { |
| 456 | grpc_connectivity_state_notify_on_state_change( |
| 457 | exec_ctx, &chand->state_tracker, op->connectivity_state, |
| 458 | op->on_connectivity_state_change); |
| 459 | op->on_connectivity_state_change = NULL; |
| 460 | op->connectivity_state = NULL; |
| 461 | } |
| 462 | |
Craig Tiller | 26dab31 | 2015-12-07 14:43:47 -0800 | [diff] [blame] | 463 | if (op->send_ping != NULL) { |
Craig Tiller | 87b71e2 | 2015-12-07 15:14:14 -0800 | [diff] [blame] | 464 | if (chand->lb_policy == NULL) { |
Craig Tiller | 91031da | 2016-12-28 15:44:25 -0800 | [diff] [blame] | 465 | grpc_closure_sched(exec_ctx, op->send_ping, |
| 466 | GRPC_ERROR_CREATE("Ping with no load balancing")); |
Craig Tiller | 26dab31 | 2015-12-07 14:43:47 -0800 | [diff] [blame] | 467 | } else { |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 468 | grpc_lb_policy_ping_one_locked(exec_ctx, chand->lb_policy, op->send_ping); |
Craig Tiller | 26dab31 | 2015-12-07 14:43:47 -0800 | [diff] [blame] | 469 | op->bind_pollset = NULL; |
| 470 | } |
| 471 | op->send_ping = NULL; |
| 472 | } |
| 473 | |
Craig Tiller | 1c51edc | 2016-05-07 16:18:43 -0700 | [diff] [blame] | 474 | if (op->disconnect_with_error != GRPC_ERROR_NONE) { |
| 475 | if (chand->resolver != NULL) { |
| 476 | set_channel_connectivity_state_locked( |
Craig Tiller | d925c93 | 2016-06-06 08:38:50 -0700 | [diff] [blame] | 477 | exec_ctx, chand, GRPC_CHANNEL_SHUTDOWN, |
Craig Tiller | 1c51edc | 2016-05-07 16:18:43 -0700 | [diff] [blame] | 478 | GRPC_ERROR_REF(op->disconnect_with_error), "disconnect"); |
Craig Tiller | 972470b | 2017-02-09 15:05:36 -0800 | [diff] [blame] | 479 | grpc_resolver_shutdown_locked(exec_ctx, chand->resolver); |
Craig Tiller | 1c51edc | 2016-05-07 16:18:43 -0700 | [diff] [blame] | 480 | GRPC_RESOLVER_UNREF(exec_ctx, chand->resolver, "channel"); |
| 481 | chand->resolver = NULL; |
| 482 | if (!chand->started_resolving) { |
| 483 | grpc_closure_list_fail_all(&chand->waiting_for_config_closures, |
| 484 | GRPC_ERROR_REF(op->disconnect_with_error)); |
Craig Tiller | 91031da | 2016-12-28 15:44:25 -0800 | [diff] [blame] | 485 | grpc_closure_list_sched(exec_ctx, &chand->waiting_for_config_closures); |
Craig Tiller | 1c51edc | 2016-05-07 16:18:43 -0700 | [diff] [blame] | 486 | } |
| 487 | if (chand->lb_policy != NULL) { |
| 488 | grpc_pollset_set_del_pollset_set(exec_ctx, |
| 489 | chand->lb_policy->interested_parties, |
| 490 | chand->interested_parties); |
| 491 | GRPC_LB_POLICY_UNREF(exec_ctx, chand->lb_policy, "channel"); |
| 492 | chand->lb_policy = NULL; |
| 493 | } |
Craig Tiller | b12d22a | 2016-04-23 12:50:21 -0700 | [diff] [blame] | 494 | } |
Craig Tiller | 1c51edc | 2016-05-07 16:18:43 -0700 | [diff] [blame] | 495 | GRPC_ERROR_UNREF(op->disconnect_with_error); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 496 | } |
Craig Tiller | d2e5cfc | 2017-02-09 13:02:20 -0800 | [diff] [blame] | 497 | GRPC_CHANNEL_STACK_UNREF(exec_ctx, chand->owning_stack, "start_transport_op"); |
| 498 | |
| 499 | grpc_closure_sched(exec_ctx, op->on_consumed, GRPC_ERROR_NONE); |
Craig Tiller | befafe6 | 2017-02-09 11:30:54 -0800 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | static void cc_start_transport_op(grpc_exec_ctx *exec_ctx, |
| 503 | grpc_channel_element *elem, |
| 504 | grpc_transport_op *op) { |
| 505 | channel_data *chand = elem->channel_data; |
| 506 | |
Craig Tiller | befafe6 | 2017-02-09 11:30:54 -0800 | [diff] [blame] | 507 | GPR_ASSERT(op->set_accept_stream == false); |
| 508 | if (op->bind_pollset != NULL) { |
| 509 | grpc_pollset_set_add_pollset(exec_ctx, chand->interested_parties, |
| 510 | op->bind_pollset); |
| 511 | } |
| 512 | |
| 513 | op->transport_private.args[0] = elem; |
Craig Tiller | d2e5cfc | 2017-02-09 13:02:20 -0800 | [diff] [blame] | 514 | GRPC_CHANNEL_STACK_REF(chand->owning_stack, "start_transport_op"); |
Craig Tiller | befafe6 | 2017-02-09 11:30:54 -0800 | [diff] [blame] | 515 | grpc_closure_sched( |
Craig Tiller | a8610c0 | 2017-02-14 10:05:11 -0800 | [diff] [blame] | 516 | exec_ctx, grpc_closure_init( |
| 517 | &op->transport_private.closure, start_transport_op_locked, |
| 518 | op, grpc_combiner_scheduler(chand->combiner, false)), |
Craig Tiller | befafe6 | 2017-02-09 11:30:54 -0800 | [diff] [blame] | 519 | GRPC_ERROR_NONE); |
Craig Tiller | ca3e9d3 | 2015-06-27 18:37:27 -0700 | [diff] [blame] | 520 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 521 | |
Mark D. Roth | b2d2488 | 2016-10-27 15:44:07 -0700 | [diff] [blame] | 522 | static void cc_get_channel_info(grpc_exec_ctx *exec_ctx, |
| 523 | grpc_channel_element *elem, |
Mark D. Roth | f79ce7d | 2016-11-04 08:43:36 -0700 | [diff] [blame] | 524 | const grpc_channel_info *info) { |
Mark D. Roth | b2d2488 | 2016-10-27 15:44:07 -0700 | [diff] [blame] | 525 | channel_data *chand = elem->channel_data; |
Craig Tiller | 613dafa | 2017-02-09 12:00:43 -0800 | [diff] [blame] | 526 | gpr_mu_lock(&chand->info_mu); |
Mark D. Roth | b2d2488 | 2016-10-27 15:44:07 -0700 | [diff] [blame] | 527 | if (info->lb_policy_name != NULL) { |
Craig Tiller | 613dafa | 2017-02-09 12:00:43 -0800 | [diff] [blame] | 528 | *info->lb_policy_name = chand->info_lb_policy_name == NULL |
Mark D. Roth | 78afd77 | 2016-11-04 12:49:49 -0700 | [diff] [blame] | 529 | ? NULL |
Craig Tiller | 613dafa | 2017-02-09 12:00:43 -0800 | [diff] [blame] | 530 | : gpr_strdup(chand->info_lb_policy_name); |
Mark D. Roth | b2d2488 | 2016-10-27 15:44:07 -0700 | [diff] [blame] | 531 | } |
Mark D. Roth | c625c7a | 2016-11-09 14:12:37 -0800 | [diff] [blame] | 532 | if (info->service_config_json != NULL) { |
Craig Tiller | 613dafa | 2017-02-09 12:00:43 -0800 | [diff] [blame] | 533 | *info->service_config_json = |
| 534 | chand->info_service_config_json == NULL |
| 535 | ? NULL |
| 536 | : gpr_strdup(chand->info_service_config_json); |
Mark D. Roth | c625c7a | 2016-11-09 14:12:37 -0800 | [diff] [blame] | 537 | } |
Craig Tiller | 613dafa | 2017-02-09 12:00:43 -0800 | [diff] [blame] | 538 | gpr_mu_unlock(&chand->info_mu); |
Mark D. Roth | b2d2488 | 2016-10-27 15:44:07 -0700 | [diff] [blame] | 539 | } |
| 540 | |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 541 | /* Constructor for channel_data */ |
Mark D. Roth | c108788 | 2016-11-18 10:54:45 -0800 | [diff] [blame] | 542 | static grpc_error *cc_init_channel_elem(grpc_exec_ctx *exec_ctx, |
Mark D. Roth | 5e2566e | 2016-11-18 10:53:13 -0800 | [diff] [blame] | 543 | grpc_channel_element *elem, |
| 544 | grpc_channel_element_args *args) { |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 545 | channel_data *chand = elem->channel_data; |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 546 | GPR_ASSERT(args->is_last); |
| 547 | GPR_ASSERT(elem->filter == &grpc_client_channel_filter); |
Mark D. Roth | 21d4b2d | 2016-11-18 09:53:41 -0800 | [diff] [blame] | 548 | // Initialize data members. |
Craig Tiller | befafe6 | 2017-02-09 11:30:54 -0800 | [diff] [blame] | 549 | chand->combiner = grpc_combiner_create(NULL); |
Craig Tiller | d8547751 | 2017-02-09 12:02:39 -0800 | [diff] [blame] | 550 | gpr_mu_init(&chand->info_mu); |
Mark D. Roth | 21d4b2d | 2016-11-18 09:53:41 -0800 | [diff] [blame] | 551 | chand->owning_stack = args->channel_stack; |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 552 | grpc_closure_init(&chand->on_resolver_result_changed, |
Craig Tiller | befafe6 | 2017-02-09 11:30:54 -0800 | [diff] [blame] | 553 | on_resolver_result_changed_locked, chand, |
| 554 | grpc_combiner_scheduler(chand->combiner, false)); |
Mark D. Roth | 21d4b2d | 2016-11-18 09:53:41 -0800 | [diff] [blame] | 555 | chand->interested_parties = grpc_pollset_set_create(); |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 556 | grpc_connectivity_state_init(&chand->state_tracker, GRPC_CHANNEL_IDLE, |
| 557 | "client_channel"); |
Mark D. Roth | 21d4b2d | 2016-11-18 09:53:41 -0800 | [diff] [blame] | 558 | // Record client channel factory. |
| 559 | const grpc_arg *arg = grpc_channel_args_find(args->channel_args, |
| 560 | GRPC_ARG_CLIENT_CHANNEL_FACTORY); |
| 561 | GPR_ASSERT(arg != NULL); |
| 562 | GPR_ASSERT(arg->type == GRPC_ARG_POINTER); |
| 563 | grpc_client_channel_factory_ref(arg->value.pointer.p); |
| 564 | chand->client_channel_factory = arg->value.pointer.p; |
Mark D. Roth | dc9bee7 | 2017-02-07 12:29:14 -0800 | [diff] [blame] | 565 | // Get server name to resolve, using proxy mapper if needed. |
Mark D. Roth | 86e9059 | 2016-11-18 09:56:40 -0800 | [diff] [blame] | 566 | arg = grpc_channel_args_find(args->channel_args, GRPC_ARG_SERVER_URI); |
Mark D. Roth | 21d4b2d | 2016-11-18 09:53:41 -0800 | [diff] [blame] | 567 | GPR_ASSERT(arg != NULL); |
| 568 | GPR_ASSERT(arg->type == GRPC_ARG_STRING); |
Mark D. Roth | dc9bee7 | 2017-02-07 12:29:14 -0800 | [diff] [blame] | 569 | char *proxy_name = NULL; |
| 570 | grpc_channel_args *new_args = NULL; |
| 571 | grpc_proxy_mappers_map_name(exec_ctx, arg->value.string, args->channel_args, |
| 572 | &proxy_name, &new_args); |
| 573 | // Instantiate resolver. |
Mark D. Roth | 45ccec5 | 2017-01-18 14:04:01 -0800 | [diff] [blame] | 574 | chand->resolver = grpc_resolver_create( |
Mark D. Roth | dc9bee7 | 2017-02-07 12:29:14 -0800 | [diff] [blame] | 575 | exec_ctx, proxy_name != NULL ? proxy_name : arg->value.string, |
| 576 | new_args != NULL ? new_args : args->channel_args, |
Craig Tiller | 972470b | 2017-02-09 15:05:36 -0800 | [diff] [blame] | 577 | chand->interested_parties, chand->combiner); |
Mark D. Roth | dc9bee7 | 2017-02-07 12:29:14 -0800 | [diff] [blame] | 578 | if (proxy_name != NULL) gpr_free(proxy_name); |
| 579 | if (new_args != NULL) grpc_channel_args_destroy(exec_ctx, new_args); |
Mark D. Roth | 5e2566e | 2016-11-18 10:53:13 -0800 | [diff] [blame] | 580 | if (chand->resolver == NULL) { |
| 581 | return GRPC_ERROR_CREATE("resolver creation failed"); |
| 582 | } |
| 583 | return GRPC_ERROR_NONE; |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 584 | } |
| 585 | |
Craig Tiller | 972470b | 2017-02-09 15:05:36 -0800 | [diff] [blame] | 586 | static void shutdown_resolver_locked(grpc_exec_ctx *exec_ctx, void *arg, |
| 587 | grpc_error *error) { |
| 588 | grpc_resolver *resolver = arg; |
| 589 | grpc_resolver_shutdown_locked(exec_ctx, resolver); |
| 590 | GRPC_RESOLVER_UNREF(exec_ctx, resolver, "channel"); |
| 591 | } |
| 592 | |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 593 | /* Destructor for channel_data */ |
| 594 | static void cc_destroy_channel_elem(grpc_exec_ctx *exec_ctx, |
| 595 | grpc_channel_element *elem) { |
| 596 | channel_data *chand = elem->channel_data; |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 597 | if (chand->resolver != NULL) { |
Craig Tiller | 972470b | 2017-02-09 15:05:36 -0800 | [diff] [blame] | 598 | grpc_closure_sched( |
| 599 | exec_ctx, |
| 600 | grpc_closure_create(shutdown_resolver_locked, chand->resolver, |
| 601 | grpc_combiner_scheduler(chand->combiner, false)), |
| 602 | GRPC_ERROR_NONE); |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 603 | } |
Mark D. Roth | 0e48a9a | 2016-09-08 14:14:39 -0700 | [diff] [blame] | 604 | if (chand->client_channel_factory != NULL) { |
| 605 | grpc_client_channel_factory_unref(exec_ctx, chand->client_channel_factory); |
| 606 | } |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 607 | if (chand->lb_policy != NULL) { |
| 608 | grpc_pollset_set_del_pollset_set(exec_ctx, |
| 609 | chand->lb_policy->interested_parties, |
| 610 | chand->interested_parties); |
| 611 | GRPC_LB_POLICY_UNREF(exec_ctx, chand->lb_policy, "channel"); |
| 612 | } |
Craig Tiller | 613dafa | 2017-02-09 12:00:43 -0800 | [diff] [blame] | 613 | gpr_free(chand->info_lb_policy_name); |
| 614 | gpr_free(chand->info_service_config_json); |
Mark D. Roth | 9d48094 | 2016-10-19 14:18:05 -0700 | [diff] [blame] | 615 | if (chand->method_params_table != NULL) { |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 616 | grpc_slice_hash_table_unref(exec_ctx, chand->method_params_table); |
Mark D. Roth | 9fe284e | 2016-09-12 11:22:27 -0700 | [diff] [blame] | 617 | } |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 618 | grpc_connectivity_state_destroy(exec_ctx, &chand->state_tracker); |
Craig Tiller | 9e5ac1b | 2017-02-14 22:25:50 -0800 | [diff] [blame] | 619 | grpc_pollset_set_destroy(exec_ctx, chand->interested_parties); |
Craig Tiller | f102167 | 2017-02-09 21:29:50 -0800 | [diff] [blame] | 620 | GRPC_COMBINER_UNREF(exec_ctx, chand->combiner, "client_channel"); |
Craig Tiller | d8547751 | 2017-02-09 12:02:39 -0800 | [diff] [blame] | 621 | gpr_mu_destroy(&chand->info_mu); |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 622 | } |
| 623 | |
| 624 | /************************************************************************* |
| 625 | * PER-CALL FUNCTIONS |
| 626 | */ |
| 627 | |
| 628 | #define GET_CALL(call_data) \ |
| 629 | ((grpc_subchannel_call *)(gpr_atm_acq_load(&(call_data)->subchannel_call))) |
| 630 | |
| 631 | #define CANCELLED_CALL ((grpc_subchannel_call *)1) |
| 632 | |
| 633 | typedef enum { |
| 634 | GRPC_SUBCHANNEL_CALL_HOLDER_NOT_CREATING, |
| 635 | GRPC_SUBCHANNEL_CALL_HOLDER_PICKING_SUBCHANNEL |
| 636 | } subchannel_creation_phase; |
| 637 | |
| 638 | /** Call data. Holds a pointer to grpc_subchannel_call and the |
| 639 | associated machinery to create such a pointer. |
| 640 | Handles queueing of stream ops until a call object is ready, waiting |
| 641 | for initial metadata before trying to create a call object, |
| 642 | and handling cancellation gracefully. */ |
| 643 | typedef struct client_channel_call_data { |
Mark D. Roth | 72f6da8 | 2016-09-02 13:42:38 -0700 | [diff] [blame] | 644 | // State for handling deadlines. |
| 645 | // 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] | 646 | // TODO(roth): This is slightly sub-optimal in that grpc_deadline_state |
| 647 | // and this struct both independently store a pointer to the call |
| 648 | // stack and each has its own mutex. If/when we have time, find a way |
Mark D. Roth | 6ad9917 | 2016-09-09 07:52:48 -0700 | [diff] [blame] | 649 | // to avoid this without breaking the grpc_deadline_state abstraction. |
Mark D. Roth | 72f6da8 | 2016-09-02 13:42:38 -0700 | [diff] [blame] | 650 | grpc_deadline_state deadline_state; |
Mark D. Roth | f28763c | 2016-09-14 15:18:40 -0700 | [diff] [blame] | 651 | |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 652 | grpc_slice path; // Request path. |
Mark D. Roth | e40dd29 | 2016-10-05 14:58:37 -0700 | [diff] [blame] | 653 | gpr_timespec call_start_time; |
| 654 | gpr_timespec deadline; |
Mark D. Roth | 95b627b | 2017-02-24 11:02:58 -0800 | [diff] [blame] | 655 | method_parameters *method_params; |
Mark D. Roth | e40dd29 | 2016-10-05 14:58:37 -0700 | [diff] [blame] | 656 | grpc_closure read_service_config; |
Mark D. Roth | aa850a7 | 2016-09-26 13:38:02 -0700 | [diff] [blame] | 657 | |
Mark D. Roth | f28763c | 2016-09-14 15:18:40 -0700 | [diff] [blame] | 658 | grpc_error *cancel_error; |
Mark D. Roth | 72f6da8 | 2016-09-02 13:42:38 -0700 | [diff] [blame] | 659 | |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 660 | /** either 0 for no call, 1 for cancelled, or a pointer to a |
| 661 | grpc_subchannel_call */ |
| 662 | gpr_atm subchannel_call; |
Craig Tiller | d426cac | 2017-03-13 12:30:45 -0700 | [diff] [blame^] | 663 | gpr_arena *arena; |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 664 | |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 665 | subchannel_creation_phase creation_phase; |
| 666 | grpc_connected_subchannel *connected_subchannel; |
| 667 | grpc_polling_entity *pollent; |
| 668 | |
Craig Tiller | 57726ca | 2016-09-12 11:59:45 -0700 | [diff] [blame] | 669 | grpc_transport_stream_op **waiting_ops; |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 670 | size_t waiting_ops_count; |
| 671 | size_t waiting_ops_capacity; |
| 672 | |
| 673 | grpc_closure next_step; |
| 674 | |
| 675 | grpc_call_stack *owning_call; |
David Garcia Quintas | d1a47f1 | 2016-09-02 12:46:44 +0200 | [diff] [blame] | 676 | |
| 677 | grpc_linked_mdelem lb_token_mdelem; |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 678 | } call_data; |
| 679 | |
Craig Tiller | 8b1d59c | 2016-12-27 15:15:30 -0800 | [diff] [blame] | 680 | grpc_subchannel_call *grpc_client_channel_get_subchannel_call( |
| 681 | grpc_call_element *call_elem) { |
| 682 | grpc_subchannel_call *scc = GET_CALL((call_data *)call_elem->call_data); |
| 683 | return scc == CANCELLED_CALL ? NULL : scc; |
| 684 | } |
| 685 | |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 686 | static void add_waiting_locked(call_data *calld, grpc_transport_stream_op *op) { |
| 687 | GPR_TIMER_BEGIN("add_waiting_locked", 0); |
| 688 | if (calld->waiting_ops_count == calld->waiting_ops_capacity) { |
| 689 | calld->waiting_ops_capacity = GPR_MAX(3, 2 * calld->waiting_ops_capacity); |
| 690 | calld->waiting_ops = |
| 691 | gpr_realloc(calld->waiting_ops, |
| 692 | calld->waiting_ops_capacity * sizeof(*calld->waiting_ops)); |
| 693 | } |
Craig Tiller | 57726ca | 2016-09-12 11:59:45 -0700 | [diff] [blame] | 694 | calld->waiting_ops[calld->waiting_ops_count++] = op; |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 695 | GPR_TIMER_END("add_waiting_locked", 0); |
| 696 | } |
| 697 | |
| 698 | static void fail_locked(grpc_exec_ctx *exec_ctx, call_data *calld, |
| 699 | grpc_error *error) { |
| 700 | size_t i; |
| 701 | for (i = 0; i < calld->waiting_ops_count; i++) { |
| 702 | grpc_transport_stream_op_finish_with_failure( |
Craig Tiller | 57726ca | 2016-09-12 11:59:45 -0700 | [diff] [blame] | 703 | exec_ctx, calld->waiting_ops[i], GRPC_ERROR_REF(error)); |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 704 | } |
| 705 | calld->waiting_ops_count = 0; |
| 706 | GRPC_ERROR_UNREF(error); |
| 707 | } |
| 708 | |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 709 | static void retry_waiting_locked(grpc_exec_ctx *exec_ctx, call_data *calld) { |
Craig Tiller | 57726ca | 2016-09-12 11:59:45 -0700 | [diff] [blame] | 710 | if (calld->waiting_ops_count == 0) { |
| 711 | return; |
| 712 | } |
| 713 | |
Craig Tiller | d2e5cfc | 2017-02-09 13:02:20 -0800 | [diff] [blame] | 714 | grpc_subchannel_call *call = GET_CALL(calld); |
| 715 | grpc_transport_stream_op **ops = calld->waiting_ops; |
| 716 | size_t nops = calld->waiting_ops_count; |
| 717 | if (call == CANCELLED_CALL) { |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 718 | fail_locked(exec_ctx, calld, GRPC_ERROR_CANCELLED); |
| 719 | return; |
| 720 | } |
| 721 | calld->waiting_ops = NULL; |
| 722 | calld->waiting_ops_count = 0; |
| 723 | calld->waiting_ops_capacity = 0; |
Craig Tiller | d2e5cfc | 2017-02-09 13:02:20 -0800 | [diff] [blame] | 724 | for (size_t i = 0; i < nops; i++) { |
| 725 | grpc_subchannel_call_process_op(exec_ctx, call, ops[i]); |
| 726 | } |
Craig Tiller | 9efea88 | 2017-02-09 13:06:52 -0800 | [diff] [blame] | 727 | gpr_free(ops); |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 728 | } |
| 729 | |
Craig Tiller | befafe6 | 2017-02-09 11:30:54 -0800 | [diff] [blame] | 730 | static void subchannel_ready_locked(grpc_exec_ctx *exec_ctx, void *arg, |
| 731 | grpc_error *error) { |
Yuchen Zeng | 19656b1 | 2016-09-01 18:00:45 -0700 | [diff] [blame] | 732 | grpc_call_element *elem = arg; |
| 733 | call_data *calld = elem->call_data; |
| 734 | channel_data *chand = elem->channel_data; |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 735 | GPR_ASSERT(calld->creation_phase == |
| 736 | GRPC_SUBCHANNEL_CALL_HOLDER_PICKING_SUBCHANNEL); |
Yuchen Zeng | 19656b1 | 2016-09-01 18:00:45 -0700 | [diff] [blame] | 737 | grpc_polling_entity_del_from_pollset_set(exec_ctx, calld->pollent, |
| 738 | chand->interested_parties); |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 739 | calld->creation_phase = GRPC_SUBCHANNEL_CALL_HOLDER_NOT_CREATING; |
| 740 | if (calld->connected_subchannel == NULL) { |
| 741 | gpr_atm_no_barrier_store(&calld->subchannel_call, 1); |
| 742 | fail_locked(exec_ctx, calld, GRPC_ERROR_CREATE_REFERENCING( |
| 743 | "Failed to create subchannel", &error, 1)); |
Mark D. Roth | 72f6da8 | 2016-09-02 13:42:38 -0700 | [diff] [blame] | 744 | } else if (GET_CALL(calld) == CANCELLED_CALL) { |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 745 | /* already cancelled before subchannel became ready */ |
David Garcia Quintas | 68a9e38 | 2016-12-13 10:50:40 -0800 | [diff] [blame] | 746 | grpc_error *cancellation_error = GRPC_ERROR_CREATE_REFERENCING( |
| 747 | "Cancelled before creating subchannel", &error, 1); |
| 748 | /* if due to deadline, attach the deadline exceeded status to the error */ |
| 749 | if (gpr_time_cmp(calld->deadline, gpr_now(GPR_CLOCK_MONOTONIC)) < 0) { |
| 750 | cancellation_error = |
| 751 | grpc_error_set_int(cancellation_error, GRPC_ERROR_INT_GRPC_STATUS, |
| 752 | GRPC_STATUS_DEADLINE_EXCEEDED); |
| 753 | } |
| 754 | fail_locked(exec_ctx, calld, cancellation_error); |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 755 | } else { |
Mark D. Roth | 9fe284e | 2016-09-12 11:22:27 -0700 | [diff] [blame] | 756 | /* Create call on subchannel. */ |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 757 | grpc_subchannel_call *subchannel_call = NULL; |
Craig Tiller | d426cac | 2017-03-13 12:30:45 -0700 | [diff] [blame^] | 758 | grpc_connected_subchannel_call_args call_args = { |
| 759 | .pollent = calld->pollent, |
| 760 | .path = calld->path, |
| 761 | .start_time = calld->call_start_time, |
| 762 | .deadline = calld->deadline, |
| 763 | .arena = calld->arena}; |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 764 | grpc_error *new_error = grpc_connected_subchannel_create_call( |
Craig Tiller | d426cac | 2017-03-13 12:30:45 -0700 | [diff] [blame^] | 765 | exec_ctx, calld->connected_subchannel, &call_args, &subchannel_call); |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 766 | if (new_error != GRPC_ERROR_NONE) { |
| 767 | new_error = grpc_error_add_child(new_error, error); |
| 768 | subchannel_call = CANCELLED_CALL; |
| 769 | fail_locked(exec_ctx, calld, new_error); |
| 770 | } |
| 771 | gpr_atm_rel_store(&calld->subchannel_call, |
| 772 | (gpr_atm)(uintptr_t)subchannel_call); |
| 773 | retry_waiting_locked(exec_ctx, calld); |
| 774 | } |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 775 | GRPC_CALL_STACK_UNREF(exec_ctx, calld->owning_call, "pick_subchannel"); |
| 776 | } |
| 777 | |
| 778 | static char *cc_get_peer(grpc_exec_ctx *exec_ctx, grpc_call_element *elem) { |
| 779 | call_data *calld = elem->call_data; |
| 780 | grpc_subchannel_call *subchannel_call = GET_CALL(calld); |
| 781 | if (subchannel_call == NULL || subchannel_call == CANCELLED_CALL) { |
| 782 | return NULL; |
| 783 | } else { |
| 784 | return grpc_subchannel_call_get_peer(exec_ctx, subchannel_call); |
| 785 | } |
| 786 | } |
| 787 | |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 788 | typedef struct { |
| 789 | grpc_metadata_batch *initial_metadata; |
Craig Tiller | 8c0d96f | 2016-03-11 14:27:52 -0800 | [diff] [blame] | 790 | uint32_t initial_metadata_flags; |
Craig Tiller | b5585d4 | 2015-11-17 07:18:31 -0800 | [diff] [blame] | 791 | grpc_connected_subchannel **connected_subchannel; |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 792 | grpc_closure *on_ready; |
| 793 | grpc_call_element *elem; |
| 794 | grpc_closure closure; |
| 795 | } continue_picking_args; |
| 796 | |
Yuchen Zeng | 144ce65 | 2016-09-01 18:19:34 -0700 | [diff] [blame] | 797 | /** Return true if subchannel is available immediately (in which case on_ready |
| 798 | should not be called), or false otherwise (in which case on_ready should be |
| 799 | called when the subchannel is available). */ |
Craig Tiller | befafe6 | 2017-02-09 11:30:54 -0800 | [diff] [blame] | 800 | static bool pick_subchannel_locked( |
| 801 | grpc_exec_ctx *exec_ctx, grpc_call_element *elem, |
| 802 | grpc_metadata_batch *initial_metadata, uint32_t initial_metadata_flags, |
| 803 | grpc_connected_subchannel **connected_subchannel, grpc_closure *on_ready, |
| 804 | grpc_error *error); |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 805 | |
Craig Tiller | befafe6 | 2017-02-09 11:30:54 -0800 | [diff] [blame] | 806 | static void continue_picking_locked(grpc_exec_ctx *exec_ctx, void *arg, |
| 807 | grpc_error *error) { |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 808 | continue_picking_args *cpa = arg; |
Craig Tiller | 0ede545 | 2016-04-23 12:21:45 -0700 | [diff] [blame] | 809 | if (cpa->connected_subchannel == NULL) { |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 810 | /* cancelled, do nothing */ |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 811 | } else if (error != GRPC_ERROR_NONE) { |
Craig Tiller | 91031da | 2016-12-28 15:44:25 -0800 | [diff] [blame] | 812 | grpc_closure_sched(exec_ctx, cpa->on_ready, GRPC_ERROR_REF(error)); |
Mark D. Roth | 9dab7d5 | 2016-10-07 07:48:03 -0700 | [diff] [blame] | 813 | } else { |
Craig Tiller | befafe6 | 2017-02-09 11:30:54 -0800 | [diff] [blame] | 814 | if (pick_subchannel_locked(exec_ctx, cpa->elem, cpa->initial_metadata, |
| 815 | cpa->initial_metadata_flags, |
| 816 | cpa->connected_subchannel, cpa->on_ready, |
| 817 | GRPC_ERROR_NONE)) { |
Craig Tiller | 91031da | 2016-12-28 15:44:25 -0800 | [diff] [blame] | 818 | grpc_closure_sched(exec_ctx, cpa->on_ready, GRPC_ERROR_NONE); |
Mark D. Roth | 9dab7d5 | 2016-10-07 07:48:03 -0700 | [diff] [blame] | 819 | } |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 820 | } |
| 821 | gpr_free(cpa); |
| 822 | } |
| 823 | |
Craig Tiller | befafe6 | 2017-02-09 11:30:54 -0800 | [diff] [blame] | 824 | static bool pick_subchannel_locked( |
| 825 | grpc_exec_ctx *exec_ctx, grpc_call_element *elem, |
| 826 | grpc_metadata_batch *initial_metadata, uint32_t initial_metadata_flags, |
| 827 | grpc_connected_subchannel **connected_subchannel, grpc_closure *on_ready, |
| 828 | grpc_error *error) { |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 829 | GPR_TIMER_BEGIN("pick_subchannel", 0); |
Craig Tiller | bfc9adc | 2016-06-27 13:16:22 -0700 | [diff] [blame] | 830 | |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 831 | channel_data *chand = elem->channel_data; |
| 832 | call_data *calld = elem->call_data; |
| 833 | continue_picking_args *cpa; |
| 834 | grpc_closure *closure; |
| 835 | |
Craig Tiller | b5585d4 | 2015-11-17 07:18:31 -0800 | [diff] [blame] | 836 | GPR_ASSERT(connected_subchannel); |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 837 | |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 838 | if (initial_metadata == NULL) { |
| 839 | if (chand->lb_policy != NULL) { |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 840 | grpc_lb_policy_cancel_pick_locked(exec_ctx, chand->lb_policy, |
| 841 | connected_subchannel, |
| 842 | GRPC_ERROR_REF(error)); |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 843 | } |
| 844 | for (closure = chand->waiting_for_config_closures.head; closure != NULL; |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 845 | closure = closure->next_data.next) { |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 846 | cpa = closure->cb_arg; |
Craig Tiller | b5585d4 | 2015-11-17 07:18:31 -0800 | [diff] [blame] | 847 | if (cpa->connected_subchannel == connected_subchannel) { |
| 848 | cpa->connected_subchannel = NULL; |
Craig Tiller | 91031da | 2016-12-28 15:44:25 -0800 | [diff] [blame] | 849 | grpc_closure_sched( |
Mark D. Roth | 932b10c | 2016-09-09 08:44:30 -0700 | [diff] [blame] | 850 | exec_ctx, cpa->on_ready, |
Craig Tiller | 91031da | 2016-12-28 15:44:25 -0800 | [diff] [blame] | 851 | GRPC_ERROR_CREATE_REFERENCING("Pick cancelled", &error, 1)); |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 852 | } |
| 853 | } |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 854 | GPR_TIMER_END("pick_subchannel", 0); |
Mark D. Roth | 697a1f6 | 2016-09-07 13:35:07 -0700 | [diff] [blame] | 855 | GRPC_ERROR_UNREF(error); |
Mark D. Roth | 4c0fe49 | 2016-08-31 13:51:55 -0700 | [diff] [blame] | 856 | return true; |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 857 | } |
Mark D. Roth | 697a1f6 | 2016-09-07 13:35:07 -0700 | [diff] [blame] | 858 | GPR_ASSERT(error == GRPC_ERROR_NONE); |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 859 | if (chand->lb_policy != NULL) { |
Craig Tiller | 86c0f8a | 2015-12-01 20:05:40 -0800 | [diff] [blame] | 860 | grpc_lb_policy *lb_policy = chand->lb_policy; |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 861 | GRPC_LB_POLICY_REF(lb_policy, "pick_subchannel"); |
Mark D. Roth | e40dd29 | 2016-10-05 14:58:37 -0700 | [diff] [blame] | 862 | // If the application explicitly set wait_for_ready, use that. |
| 863 | // Otherwise, if the service config specified a value for this |
| 864 | // method, use that. |
Mark D. Roth | c1c3858 | 2016-10-11 11:03:27 -0700 | [diff] [blame] | 865 | const bool wait_for_ready_set_from_api = |
| 866 | initial_metadata_flags & |
| 867 | GRPC_INITIAL_METADATA_WAIT_FOR_READY_EXPLICITLY_SET; |
| 868 | const bool wait_for_ready_set_from_service_config = |
Mark D. Roth | 95b627b | 2017-02-24 11:02:58 -0800 | [diff] [blame] | 869 | calld->method_params != NULL && |
| 870 | calld->method_params->wait_for_ready != WAIT_FOR_READY_UNSET; |
Mark D. Roth | c1c3858 | 2016-10-11 11:03:27 -0700 | [diff] [blame] | 871 | if (!wait_for_ready_set_from_api && |
| 872 | wait_for_ready_set_from_service_config) { |
Mark D. Roth | 95b627b | 2017-02-24 11:02:58 -0800 | [diff] [blame] | 873 | if (calld->method_params->wait_for_ready == WAIT_FOR_READY_TRUE) { |
Mark D. Roth | e40dd29 | 2016-10-05 14:58:37 -0700 | [diff] [blame] | 874 | initial_metadata_flags |= GRPC_INITIAL_METADATA_WAIT_FOR_READY; |
| 875 | } else { |
| 876 | initial_metadata_flags &= ~GRPC_INITIAL_METADATA_WAIT_FOR_READY; |
| 877 | } |
| 878 | } |
David Garcia Quintas | 92eb6b9 | 2016-09-30 14:07:39 -0700 | [diff] [blame] | 879 | const grpc_lb_policy_pick_args inputs = { |
Yuchen Zeng | ac8bc42 | 2016-10-05 14:00:02 -0700 | [diff] [blame] | 880 | initial_metadata, initial_metadata_flags, &calld->lb_token_mdelem, |
| 881 | gpr_inf_future(GPR_CLOCK_MONOTONIC)}; |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 882 | const bool result = grpc_lb_policy_pick_locked( |
Mark D. Roth | 55f25b6 | 2016-10-12 14:55:20 -0700 | [diff] [blame] | 883 | exec_ctx, lb_policy, &inputs, connected_subchannel, NULL, on_ready); |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 884 | GRPC_LB_POLICY_UNREF(exec_ctx, lb_policy, "pick_subchannel"); |
| 885 | GPR_TIMER_END("pick_subchannel", 0); |
Mark D. Roth | 9dab7d5 | 2016-10-07 07:48:03 -0700 | [diff] [blame] | 886 | return result; |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 887 | } |
| 888 | if (chand->resolver != NULL && !chand->started_resolving) { |
Mark D. Roth | 4c0fe49 | 2016-08-31 13:51:55 -0700 | [diff] [blame] | 889 | chand->started_resolving = true; |
Craig Tiller | 906e3bc | 2015-11-24 07:31:31 -0800 | [diff] [blame] | 890 | GRPC_CHANNEL_STACK_REF(chand->owning_stack, "resolver"); |
Craig Tiller | 972470b | 2017-02-09 15:05:36 -0800 | [diff] [blame] | 891 | grpc_resolver_next_locked(exec_ctx, chand->resolver, |
| 892 | &chand->resolver_result, |
| 893 | &chand->on_resolver_result_changed); |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 894 | } |
Craig Tiller | 0eab697 | 2016-04-23 12:59:57 -0700 | [diff] [blame] | 895 | if (chand->resolver != NULL) { |
| 896 | cpa = gpr_malloc(sizeof(*cpa)); |
| 897 | cpa->initial_metadata = initial_metadata; |
| 898 | cpa->initial_metadata_flags = initial_metadata_flags; |
| 899 | cpa->connected_subchannel = connected_subchannel; |
| 900 | cpa->on_ready = on_ready; |
| 901 | cpa->elem = elem; |
Craig Tiller | befafe6 | 2017-02-09 11:30:54 -0800 | [diff] [blame] | 902 | grpc_closure_init(&cpa->closure, continue_picking_locked, cpa, |
| 903 | grpc_combiner_scheduler(chand->combiner, true)); |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 904 | grpc_closure_list_append(&chand->waiting_for_config_closures, &cpa->closure, |
| 905 | GRPC_ERROR_NONE); |
Craig Tiller | 0eab697 | 2016-04-23 12:59:57 -0700 | [diff] [blame] | 906 | } else { |
Craig Tiller | 91031da | 2016-12-28 15:44:25 -0800 | [diff] [blame] | 907 | grpc_closure_sched(exec_ctx, on_ready, GRPC_ERROR_CREATE("Disconnected")); |
Craig Tiller | 0eab697 | 2016-04-23 12:59:57 -0700 | [diff] [blame] | 908 | } |
Craig Tiller | bfc9adc | 2016-06-27 13:16:22 -0700 | [diff] [blame] | 909 | |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 910 | GPR_TIMER_END("pick_subchannel", 0); |
Mark D. Roth | 4c0fe49 | 2016-08-31 13:51:55 -0700 | [diff] [blame] | 911 | return false; |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 912 | } |
| 913 | |
Craig Tiller | a8610c0 | 2017-02-14 10:05:11 -0800 | [diff] [blame] | 914 | static void start_transport_stream_op_locked_inner(grpc_exec_ctx *exec_ctx, |
| 915 | grpc_transport_stream_op *op, |
| 916 | grpc_call_element *elem) { |
Yuchen Zeng | 19656b1 | 2016-09-01 18:00:45 -0700 | [diff] [blame] | 917 | channel_data *chand = elem->channel_data; |
Craig Tiller | a11bfc8 | 2017-02-14 09:56:33 -0800 | [diff] [blame] | 918 | call_data *calld = elem->call_data; |
Craig Tiller | befafe6 | 2017-02-09 11:30:54 -0800 | [diff] [blame] | 919 | grpc_subchannel_call *call; |
| 920 | |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 921 | /* need to recheck that another thread hasn't set the call */ |
| 922 | call = GET_CALL(calld); |
| 923 | if (call == CANCELLED_CALL) { |
Mark D. Roth | f28763c | 2016-09-14 15:18:40 -0700 | [diff] [blame] | 924 | grpc_transport_stream_op_finish_with_failure( |
| 925 | exec_ctx, op, GRPC_ERROR_REF(calld->cancel_error)); |
Craig Tiller | a11bfc8 | 2017-02-14 09:56:33 -0800 | [diff] [blame] | 926 | /* early out */ |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 927 | return; |
| 928 | } |
| 929 | if (call != NULL) { |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 930 | grpc_subchannel_call_process_op(exec_ctx, call, op); |
Craig Tiller | a11bfc8 | 2017-02-14 09:56:33 -0800 | [diff] [blame] | 931 | /* early out */ |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 932 | return; |
| 933 | } |
| 934 | /* if this is a cancellation, then we can raise our cancelled flag */ |
| 935 | if (op->cancel_error != GRPC_ERROR_NONE) { |
| 936 | if (!gpr_atm_rel_cas(&calld->subchannel_call, 0, |
| 937 | (gpr_atm)(uintptr_t)CANCELLED_CALL)) { |
Craig Tiller | a11bfc8 | 2017-02-14 09:56:33 -0800 | [diff] [blame] | 938 | /* recurse to retry */ |
Craig Tiller | a8610c0 | 2017-02-14 10:05:11 -0800 | [diff] [blame] | 939 | start_transport_stream_op_locked_inner(exec_ctx, op, elem); |
Craig Tiller | a11bfc8 | 2017-02-14 09:56:33 -0800 | [diff] [blame] | 940 | /* early out */ |
| 941 | return; |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 942 | } else { |
Craig Tiller | be9691a | 2017-02-14 10:00:42 -0800 | [diff] [blame] | 943 | /* Stash a copy of cancel_error in our call data, so that we can use |
| 944 | it for subsequent operations. This ensures that if the call is |
| 945 | cancelled before any ops are passed down (e.g., if the deadline |
| 946 | is in the past when the call starts), we can return the right |
| 947 | error to the caller when the first op does get passed down. */ |
Mark D. Roth | f28763c | 2016-09-14 15:18:40 -0700 | [diff] [blame] | 948 | calld->cancel_error = GRPC_ERROR_REF(op->cancel_error); |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 949 | switch (calld->creation_phase) { |
| 950 | case GRPC_SUBCHANNEL_CALL_HOLDER_NOT_CREATING: |
| 951 | fail_locked(exec_ctx, calld, GRPC_ERROR_REF(op->cancel_error)); |
| 952 | break; |
| 953 | case GRPC_SUBCHANNEL_CALL_HOLDER_PICKING_SUBCHANNEL: |
Craig Tiller | befafe6 | 2017-02-09 11:30:54 -0800 | [diff] [blame] | 954 | pick_subchannel_locked(exec_ctx, elem, NULL, 0, |
| 955 | &calld->connected_subchannel, NULL, |
| 956 | GRPC_ERROR_REF(op->cancel_error)); |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 957 | break; |
| 958 | } |
Mark D. Roth | 72f6da8 | 2016-09-02 13:42:38 -0700 | [diff] [blame] | 959 | grpc_transport_stream_op_finish_with_failure( |
| 960 | exec_ctx, op, GRPC_ERROR_REF(op->cancel_error)); |
Craig Tiller | a11bfc8 | 2017-02-14 09:56:33 -0800 | [diff] [blame] | 961 | /* early out */ |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 962 | return; |
| 963 | } |
| 964 | } |
| 965 | /* if we don't have a subchannel, try to get one */ |
| 966 | if (calld->creation_phase == GRPC_SUBCHANNEL_CALL_HOLDER_NOT_CREATING && |
| 967 | calld->connected_subchannel == NULL && |
| 968 | op->send_initial_metadata != NULL) { |
| 969 | calld->creation_phase = GRPC_SUBCHANNEL_CALL_HOLDER_PICKING_SUBCHANNEL; |
Craig Tiller | befafe6 | 2017-02-09 11:30:54 -0800 | [diff] [blame] | 970 | grpc_closure_init(&calld->next_step, subchannel_ready_locked, elem, |
| 971 | grpc_combiner_scheduler(chand->combiner, true)); |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 972 | GRPC_CALL_STACK_REF(calld->owning_call, "pick_subchannel"); |
Yuchen Zeng | 144ce65 | 2016-09-01 18:19:34 -0700 | [diff] [blame] | 973 | /* If a subchannel is not available immediately, the polling entity from |
| 974 | call_data should be provided to channel_data's interested_parties, so |
| 975 | that IO of the lb_policy and resolver could be done under it. */ |
Craig Tiller | befafe6 | 2017-02-09 11:30:54 -0800 | [diff] [blame] | 976 | if (pick_subchannel_locked(exec_ctx, elem, op->send_initial_metadata, |
| 977 | op->send_initial_metadata_flags, |
| 978 | &calld->connected_subchannel, &calld->next_step, |
| 979 | GRPC_ERROR_NONE)) { |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 980 | calld->creation_phase = GRPC_SUBCHANNEL_CALL_HOLDER_NOT_CREATING; |
| 981 | GRPC_CALL_STACK_UNREF(exec_ctx, calld->owning_call, "pick_subchannel"); |
Yuchen Zeng | 19656b1 | 2016-09-01 18:00:45 -0700 | [diff] [blame] | 982 | } else { |
Yuchen Zeng | 19656b1 | 2016-09-01 18:00:45 -0700 | [diff] [blame] | 983 | grpc_polling_entity_add_to_pollset_set(exec_ctx, calld->pollent, |
| 984 | chand->interested_parties); |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 985 | } |
| 986 | } |
| 987 | /* if we've got a subchannel, then let's ask it to create a call */ |
| 988 | if (calld->creation_phase == GRPC_SUBCHANNEL_CALL_HOLDER_NOT_CREATING && |
| 989 | calld->connected_subchannel != NULL) { |
| 990 | grpc_subchannel_call *subchannel_call = NULL; |
Craig Tiller | d426cac | 2017-03-13 12:30:45 -0700 | [diff] [blame^] | 991 | grpc_connected_subchannel_call_args call_args = { |
| 992 | .pollent = calld->pollent, |
| 993 | .path = calld->path, |
| 994 | .start_time = calld->call_start_time, |
| 995 | .deadline = calld->deadline, |
| 996 | .arena = calld->arena}; |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 997 | grpc_error *error = grpc_connected_subchannel_create_call( |
Craig Tiller | d426cac | 2017-03-13 12:30:45 -0700 | [diff] [blame^] | 998 | exec_ctx, calld->connected_subchannel, &call_args, &subchannel_call); |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 999 | if (error != GRPC_ERROR_NONE) { |
| 1000 | subchannel_call = CANCELLED_CALL; |
| 1001 | fail_locked(exec_ctx, calld, GRPC_ERROR_REF(error)); |
| 1002 | grpc_transport_stream_op_finish_with_failure(exec_ctx, op, error); |
| 1003 | } |
| 1004 | gpr_atm_rel_store(&calld->subchannel_call, |
| 1005 | (gpr_atm)(uintptr_t)subchannel_call); |
| 1006 | retry_waiting_locked(exec_ctx, calld); |
Craig Tiller | a11bfc8 | 2017-02-14 09:56:33 -0800 | [diff] [blame] | 1007 | /* recurse to retry */ |
Craig Tiller | a8610c0 | 2017-02-14 10:05:11 -0800 | [diff] [blame] | 1008 | start_transport_stream_op_locked_inner(exec_ctx, op, elem); |
Craig Tiller | a11bfc8 | 2017-02-14 09:56:33 -0800 | [diff] [blame] | 1009 | /* early out */ |
| 1010 | return; |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 1011 | } |
| 1012 | /* nothing to be done but wait */ |
| 1013 | add_waiting_locked(calld, op); |
Craig Tiller | a11bfc8 | 2017-02-14 09:56:33 -0800 | [diff] [blame] | 1014 | } |
| 1015 | |
Mark D. Roth | 95b627b | 2017-02-24 11:02:58 -0800 | [diff] [blame] | 1016 | static void start_transport_stream_op_locked(grpc_exec_ctx *exec_ctx, void *arg, |
Mark D. Roth | 722de8d | 2017-02-27 10:50:44 -0800 | [diff] [blame] | 1017 | grpc_error *error_ignored) { |
Mark D. Roth | 95b627b | 2017-02-24 11:02:58 -0800 | [diff] [blame] | 1018 | GPR_TIMER_BEGIN("start_transport_stream_op_locked", 0); |
Craig Tiller | a11bfc8 | 2017-02-14 09:56:33 -0800 | [diff] [blame] | 1019 | |
| 1020 | grpc_transport_stream_op *op = arg; |
| 1021 | grpc_call_element *elem = op->handler_private.args[0]; |
| 1022 | call_data *calld = elem->call_data; |
| 1023 | |
Craig Tiller | a8610c0 | 2017-02-14 10:05:11 -0800 | [diff] [blame] | 1024 | start_transport_stream_op_locked_inner(exec_ctx, op, elem); |
Craig Tiller | a11bfc8 | 2017-02-14 09:56:33 -0800 | [diff] [blame] | 1025 | |
Craig Tiller | d2e5cfc | 2017-02-09 13:02:20 -0800 | [diff] [blame] | 1026 | GRPC_CALL_STACK_UNREF(exec_ctx, calld->owning_call, |
| 1027 | "start_transport_stream_op"); |
Mark D. Roth | 95b627b | 2017-02-24 11:02:58 -0800 | [diff] [blame] | 1028 | GPR_TIMER_END("start_transport_stream_op_locked", 0); |
Craig Tiller | befafe6 | 2017-02-09 11:30:54 -0800 | [diff] [blame] | 1029 | } |
| 1030 | |
Craig Tiller | be9691a | 2017-02-14 10:00:42 -0800 | [diff] [blame] | 1031 | /* The logic here is fairly complicated, due to (a) the fact that we |
| 1032 | need to handle the case where we receive the send op before the |
| 1033 | initial metadata op, and (b) the need for efficiency, especially in |
| 1034 | the streaming case. |
| 1035 | |
| 1036 | We use double-checked locking to initially see if initialization has been |
| 1037 | performed. If it has not, we acquire the combiner and perform initialization. |
| 1038 | If it has, we proceed on the fast path. */ |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 1039 | static void cc_start_transport_stream_op(grpc_exec_ctx *exec_ctx, |
| 1040 | grpc_call_element *elem, |
| 1041 | grpc_transport_stream_op *op) { |
| 1042 | call_data *calld = elem->call_data; |
| 1043 | channel_data *chand = elem->channel_data; |
| 1044 | GRPC_CALL_LOG_OP(GPR_INFO, elem, op); |
| 1045 | grpc_deadline_state_client_start_transport_stream_op(exec_ctx, elem, op); |
| 1046 | /* try to (atomically) get the call */ |
| 1047 | grpc_subchannel_call *call = GET_CALL(calld); |
| 1048 | GPR_TIMER_BEGIN("cc_start_transport_stream_op", 0); |
| 1049 | if (call == CANCELLED_CALL) { |
| 1050 | grpc_transport_stream_op_finish_with_failure( |
| 1051 | exec_ctx, op, GRPC_ERROR_REF(calld->cancel_error)); |
| 1052 | GPR_TIMER_END("cc_start_transport_stream_op", 0); |
Craig Tiller | a11bfc8 | 2017-02-14 09:56:33 -0800 | [diff] [blame] | 1053 | /* early out */ |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 1054 | return; |
| 1055 | } |
| 1056 | if (call != NULL) { |
| 1057 | grpc_subchannel_call_process_op(exec_ctx, call, op); |
| 1058 | GPR_TIMER_END("cc_start_transport_stream_op", 0); |
Craig Tiller | a11bfc8 | 2017-02-14 09:56:33 -0800 | [diff] [blame] | 1059 | /* early out */ |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 1060 | return; |
| 1061 | } |
| 1062 | /* we failed; lock and figure out what to do */ |
Craig Tiller | d2e5cfc | 2017-02-09 13:02:20 -0800 | [diff] [blame] | 1063 | GRPC_CALL_STACK_REF(calld->owning_call, "start_transport_stream_op"); |
Craig Tiller | 4a84bdd | 2017-02-14 09:48:41 -0800 | [diff] [blame] | 1064 | op->handler_private.args[0] = elem; |
Craig Tiller | befafe6 | 2017-02-09 11:30:54 -0800 | [diff] [blame] | 1065 | grpc_closure_sched( |
| 1066 | exec_ctx, |
Craig Tiller | 4a84bdd | 2017-02-14 09:48:41 -0800 | [diff] [blame] | 1067 | grpc_closure_init(&op->handler_private.closure, |
Mark D. Roth | 95b627b | 2017-02-24 11:02:58 -0800 | [diff] [blame] | 1068 | start_transport_stream_op_locked, op, |
Craig Tiller | befafe6 | 2017-02-09 11:30:54 -0800 | [diff] [blame] | 1069 | grpc_combiner_scheduler(chand->combiner, false)), |
| 1070 | GRPC_ERROR_NONE); |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 1071 | GPR_TIMER_END("cc_start_transport_stream_op", 0); |
| 1072 | } |
| 1073 | |
Mark D. Roth | 95b627b | 2017-02-24 11:02:58 -0800 | [diff] [blame] | 1074 | // Sets calld->method_params. |
| 1075 | // If the method params specify a timeout, populates |
| 1076 | // *per_method_deadline and returns true. |
| 1077 | static bool set_call_method_params_from_service_config_locked( |
| 1078 | grpc_exec_ctx *exec_ctx, grpc_call_element *elem, |
| 1079 | gpr_timespec *per_method_deadline) { |
| 1080 | channel_data *chand = elem->channel_data; |
| 1081 | call_data *calld = elem->call_data; |
| 1082 | if (chand->method_params_table != NULL) { |
| 1083 | calld->method_params = grpc_method_config_table_get( |
| 1084 | exec_ctx, chand->method_params_table, calld->path); |
| 1085 | if (calld->method_params != NULL) { |
| 1086 | method_parameters_ref(calld->method_params); |
| 1087 | if (gpr_time_cmp(calld->method_params->timeout, |
| 1088 | gpr_time_0(GPR_TIMESPAN)) != 0) { |
Mark D. Roth | 722de8d | 2017-02-27 10:50:44 -0800 | [diff] [blame] | 1089 | *per_method_deadline = |
| 1090 | gpr_time_add(calld->call_start_time, calld->method_params->timeout); |
Mark D. Roth | 95b627b | 2017-02-24 11:02:58 -0800 | [diff] [blame] | 1091 | return true; |
| 1092 | } |
| 1093 | } |
| 1094 | } |
| 1095 | return false; |
| 1096 | } |
| 1097 | |
Mark D. Roth | e40dd29 | 2016-10-05 14:58:37 -0700 | [diff] [blame] | 1098 | // Gets data from the service config. Invoked when the resolver returns |
| 1099 | // its initial result. |
Craig Tiller | befafe6 | 2017-02-09 11:30:54 -0800 | [diff] [blame] | 1100 | static void read_service_config_locked(grpc_exec_ctx *exec_ctx, void *arg, |
| 1101 | grpc_error *error) { |
Mark D. Roth | e40dd29 | 2016-10-05 14:58:37 -0700 | [diff] [blame] | 1102 | grpc_call_element *elem = arg; |
Mark D. Roth | e40dd29 | 2016-10-05 14:58:37 -0700 | [diff] [blame] | 1103 | call_data *calld = elem->call_data; |
| 1104 | // If this is an error, there's no point in looking at the service config. |
Mark D. Roth | 196387a | 2016-10-12 14:53:36 -0700 | [diff] [blame] | 1105 | if (error == GRPC_ERROR_NONE) { |
Mark D. Roth | 95b627b | 2017-02-24 11:02:58 -0800 | [diff] [blame] | 1106 | gpr_timespec per_method_deadline; |
| 1107 | if (set_call_method_params_from_service_config_locked( |
| 1108 | exec_ctx, elem, &per_method_deadline)) { |
| 1109 | // If the deadline from the service config is shorter than the one |
| 1110 | // from the client API, reset the deadline timer. |
| 1111 | if (gpr_time_cmp(per_method_deadline, calld->deadline) < 0) { |
| 1112 | calld->deadline = per_method_deadline; |
| 1113 | grpc_deadline_state_reset(exec_ctx, elem, calld->deadline); |
Mark D. Roth | 196387a | 2016-10-12 14:53:36 -0700 | [diff] [blame] | 1114 | } |
Mark D. Roth | 196387a | 2016-10-12 14:53:36 -0700 | [diff] [blame] | 1115 | } |
Mark D. Roth | e40dd29 | 2016-10-05 14:58:37 -0700 | [diff] [blame] | 1116 | } |
Mark D. Roth | 31292f2 | 2016-10-12 13:14:07 -0700 | [diff] [blame] | 1117 | GRPC_CALL_STACK_UNREF(exec_ctx, calld->owning_call, "read_service_config"); |
Mark D. Roth | e40dd29 | 2016-10-05 14:58:37 -0700 | [diff] [blame] | 1118 | } |
| 1119 | |
Craig Tiller | befafe6 | 2017-02-09 11:30:54 -0800 | [diff] [blame] | 1120 | static void initial_read_service_config_locked(grpc_exec_ctx *exec_ctx, |
| 1121 | void *arg, |
| 1122 | grpc_error *error_ignored) { |
| 1123 | grpc_call_element *elem = arg; |
| 1124 | channel_data *chand = elem->channel_data; |
| 1125 | call_data *calld = elem->call_data; |
| 1126 | // If the resolver has already returned results, then we can access |
| 1127 | // the service config parameters immediately. Otherwise, we need to |
| 1128 | // defer that work until the resolver returns an initial result. |
Craig Tiller | befafe6 | 2017-02-09 11:30:54 -0800 | [diff] [blame] | 1129 | if (chand->lb_policy != NULL) { |
| 1130 | // We already have a resolver result, so check for service config. |
Mark D. Roth | 95b627b | 2017-02-24 11:02:58 -0800 | [diff] [blame] | 1131 | gpr_timespec per_method_deadline; |
| 1132 | if (set_call_method_params_from_service_config_locked( |
| 1133 | exec_ctx, elem, &per_method_deadline)) { |
| 1134 | calld->deadline = gpr_time_min(calld->deadline, per_method_deadline); |
Craig Tiller | befafe6 | 2017-02-09 11:30:54 -0800 | [diff] [blame] | 1135 | } |
| 1136 | } else { |
| 1137 | // We don't yet have a resolver result, so register a callback to |
| 1138 | // get the service config data once the resolver returns. |
| 1139 | // Take a reference to the call stack to be owned by the callback. |
| 1140 | GRPC_CALL_STACK_REF(calld->owning_call, "read_service_config"); |
| 1141 | grpc_closure_init(&calld->read_service_config, read_service_config_locked, |
| 1142 | elem, grpc_combiner_scheduler(chand->combiner, false)); |
| 1143 | grpc_closure_list_append(&chand->waiting_for_config_closures, |
| 1144 | &calld->read_service_config, GRPC_ERROR_NONE); |
| 1145 | } |
| 1146 | // Start the deadline timer with the current deadline value. If we |
| 1147 | // do not yet have service config data, then the timer may be reset |
| 1148 | // later. |
| 1149 | grpc_deadline_state_start(exec_ctx, elem, calld->deadline); |
Craig Tiller | d2e5cfc | 2017-02-09 13:02:20 -0800 | [diff] [blame] | 1150 | GRPC_CALL_STACK_UNREF(exec_ctx, calld->owning_call, |
| 1151 | "initial_read_service_config"); |
Craig Tiller | befafe6 | 2017-02-09 11:30:54 -0800 | [diff] [blame] | 1152 | } |
| 1153 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1154 | /* Constructor for call_data */ |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 1155 | static grpc_error *cc_init_call_elem(grpc_exec_ctx *exec_ctx, |
| 1156 | grpc_call_element *elem, |
Craig Tiller | c52ba3a | 2017-02-15 22:57:43 -0800 | [diff] [blame] | 1157 | const grpc_call_element_args *args) { |
Mark D. Roth | aa850a7 | 2016-09-26 13:38:02 -0700 | [diff] [blame] | 1158 | channel_data *chand = elem->channel_data; |
Mark D. Roth | 4c0fe49 | 2016-08-31 13:51:55 -0700 | [diff] [blame] | 1159 | call_data *calld = elem->call_data; |
Mark D. Roth | e40dd29 | 2016-10-05 14:58:37 -0700 | [diff] [blame] | 1160 | // Initialize data members. |
| 1161 | grpc_deadline_state_init(exec_ctx, elem, args->call_stack); |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 1162 | calld->path = grpc_slice_ref_internal(args->path); |
Mark D. Roth | ff08f33 | 2016-10-14 13:01:01 -0700 | [diff] [blame] | 1163 | calld->call_start_time = args->start_time; |
Mark D. Roth | e40dd29 | 2016-10-05 14:58:37 -0700 | [diff] [blame] | 1164 | calld->deadline = gpr_convert_clock_type(args->deadline, GPR_CLOCK_MONOTONIC); |
Mark D. Roth | 95b627b | 2017-02-24 11:02:58 -0800 | [diff] [blame] | 1165 | calld->method_params = NULL; |
Mark D. Roth | f28763c | 2016-09-14 15:18:40 -0700 | [diff] [blame] | 1166 | calld->cancel_error = GRPC_ERROR_NONE; |
Mark D. Roth | 4c0fe49 | 2016-08-31 13:51:55 -0700 | [diff] [blame] | 1167 | gpr_atm_rel_store(&calld->subchannel_call, 0); |
Mark D. Roth | 4c0fe49 | 2016-08-31 13:51:55 -0700 | [diff] [blame] | 1168 | calld->connected_subchannel = NULL; |
| 1169 | calld->waiting_ops = NULL; |
| 1170 | calld->waiting_ops_count = 0; |
| 1171 | calld->waiting_ops_capacity = 0; |
| 1172 | calld->creation_phase = GRPC_SUBCHANNEL_CALL_HOLDER_NOT_CREATING; |
| 1173 | calld->owning_call = args->call_stack; |
| 1174 | calld->pollent = NULL; |
Craig Tiller | d426cac | 2017-03-13 12:30:45 -0700 | [diff] [blame^] | 1175 | calld->arena = args->arena; |
Craig Tiller | d2e5cfc | 2017-02-09 13:02:20 -0800 | [diff] [blame] | 1176 | GRPC_CALL_STACK_REF(calld->owning_call, "initial_read_service_config"); |
Craig Tiller | befafe6 | 2017-02-09 11:30:54 -0800 | [diff] [blame] | 1177 | grpc_closure_sched( |
| 1178 | exec_ctx, |
| 1179 | grpc_closure_init(&calld->read_service_config, |
| 1180 | initial_read_service_config_locked, elem, |
| 1181 | grpc_combiner_scheduler(chand->combiner, false)), |
| 1182 | GRPC_ERROR_NONE); |
Mark D. Roth | 0badbe8 | 2016-06-23 10:15:12 -0700 | [diff] [blame] | 1183 | return GRPC_ERROR_NONE; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1184 | } |
| 1185 | |
| 1186 | /* Destructor for call_data */ |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 1187 | static void cc_destroy_call_elem(grpc_exec_ctx *exec_ctx, |
| 1188 | grpc_call_element *elem, |
| 1189 | const grpc_call_final_info *final_info, |
Craig Tiller | d426cac | 2017-03-13 12:30:45 -0700 | [diff] [blame^] | 1190 | grpc_closure *then_schedule_closure) { |
Mark D. Roth | 4c0fe49 | 2016-08-31 13:51:55 -0700 | [diff] [blame] | 1191 | call_data *calld = elem->call_data; |
Mark D. Roth | f28763c | 2016-09-14 15:18:40 -0700 | [diff] [blame] | 1192 | grpc_deadline_state_destroy(exec_ctx, elem); |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 1193 | grpc_slice_unref_internal(exec_ctx, calld->path); |
Mark D. Roth | 95b627b | 2017-02-24 11:02:58 -0800 | [diff] [blame] | 1194 | if (calld->method_params != NULL) { |
| 1195 | method_parameters_unref(calld->method_params); |
| 1196 | } |
Mark D. Roth | f28763c | 2016-09-14 15:18:40 -0700 | [diff] [blame] | 1197 | GRPC_ERROR_UNREF(calld->cancel_error); |
Mark D. Roth | 4c0fe49 | 2016-08-31 13:51:55 -0700 | [diff] [blame] | 1198 | grpc_subchannel_call *call = GET_CALL(calld); |
| 1199 | if (call != NULL && call != CANCELLED_CALL) { |
Craig Tiller | d426cac | 2017-03-13 12:30:45 -0700 | [diff] [blame^] | 1200 | grpc_subchannel_call_set_cleanup_closure(call, then_schedule_closure); |
| 1201 | then_schedule_closure = NULL; |
Mark D. Roth | 4c0fe49 | 2016-08-31 13:51:55 -0700 | [diff] [blame] | 1202 | GRPC_SUBCHANNEL_CALL_UNREF(exec_ctx, call, "client_channel_destroy_call"); |
| 1203 | } |
| 1204 | GPR_ASSERT(calld->creation_phase == GRPC_SUBCHANNEL_CALL_HOLDER_NOT_CREATING); |
Mark D. Roth | 4c0fe49 | 2016-08-31 13:51:55 -0700 | [diff] [blame] | 1205 | GPR_ASSERT(calld->waiting_ops_count == 0); |
Craig Tiller | 693d394 | 2016-10-27 16:51:25 -0700 | [diff] [blame] | 1206 | if (calld->connected_subchannel != NULL) { |
| 1207 | GRPC_CONNECTED_SUBCHANNEL_UNREF(exec_ctx, calld->connected_subchannel, |
| 1208 | "picked"); |
| 1209 | } |
Mark D. Roth | 4c0fe49 | 2016-08-31 13:51:55 -0700 | [diff] [blame] | 1210 | gpr_free(calld->waiting_ops); |
Craig Tiller | d426cac | 2017-03-13 12:30:45 -0700 | [diff] [blame^] | 1211 | grpc_closure_sched(exec_ctx, then_schedule_closure, GRPC_ERROR_NONE); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1212 | } |
| 1213 | |
David Garcia Quintas | f72eb97 | 2016-05-03 18:28:09 -0700 | [diff] [blame] | 1214 | static void cc_set_pollset_or_pollset_set(grpc_exec_ctx *exec_ctx, |
| 1215 | grpc_call_element *elem, |
David Garcia Quintas | 2a50dfe | 2016-05-31 15:09:12 -0700 | [diff] [blame] | 1216 | grpc_polling_entity *pollent) { |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 1217 | call_data *calld = elem->call_data; |
David Garcia Quintas | 2a50dfe | 2016-05-31 15:09:12 -0700 | [diff] [blame] | 1218 | calld->pollent = pollent; |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 1219 | } |
| 1220 | |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 1221 | /************************************************************************* |
| 1222 | * EXPORTED SYMBOLS |
| 1223 | */ |
| 1224 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1225 | const grpc_channel_filter grpc_client_channel_filter = { |
Craig Tiller | f40df23 | 2016-03-25 13:38:14 -0700 | [diff] [blame] | 1226 | cc_start_transport_stream_op, |
| 1227 | cc_start_transport_op, |
| 1228 | sizeof(call_data), |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 1229 | cc_init_call_elem, |
David Garcia Quintas | 4afce7e | 2016-04-18 16:25:17 -0700 | [diff] [blame] | 1230 | cc_set_pollset_or_pollset_set, |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 1231 | cc_destroy_call_elem, |
Craig Tiller | f40df23 | 2016-03-25 13:38:14 -0700 | [diff] [blame] | 1232 | sizeof(channel_data), |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 1233 | cc_init_channel_elem, |
| 1234 | cc_destroy_channel_elem, |
Craig Tiller | f40df23 | 2016-03-25 13:38:14 -0700 | [diff] [blame] | 1235 | cc_get_peer, |
Mark D. Roth | b2d2488 | 2016-10-27 15:44:07 -0700 | [diff] [blame] | 1236 | cc_get_channel_info, |
Craig Tiller | f40df23 | 2016-03-25 13:38:14 -0700 | [diff] [blame] | 1237 | "client-channel", |
Craig Tiller | 87d5b19 | 2015-04-16 14:37:57 -0700 | [diff] [blame] | 1238 | }; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1239 | |
Craig Tiller | 613dafa | 2017-02-09 12:00:43 -0800 | [diff] [blame] | 1240 | static void try_to_connect_locked(grpc_exec_ctx *exec_ctx, void *arg, |
| 1241 | grpc_error *error_ignored) { |
| 1242 | channel_data *chand = arg; |
| 1243 | if (chand->lb_policy != NULL) { |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 1244 | grpc_lb_policy_exit_idle_locked(exec_ctx, chand->lb_policy); |
Craig Tiller | 613dafa | 2017-02-09 12:00:43 -0800 | [diff] [blame] | 1245 | } else { |
| 1246 | chand->exit_idle_when_lb_policy_arrives = true; |
| 1247 | if (!chand->started_resolving && chand->resolver != NULL) { |
| 1248 | GRPC_CHANNEL_STACK_REF(chand->owning_stack, "resolver"); |
| 1249 | chand->started_resolving = true; |
Craig Tiller | 972470b | 2017-02-09 15:05:36 -0800 | [diff] [blame] | 1250 | grpc_resolver_next_locked(exec_ctx, chand->resolver, |
| 1251 | &chand->resolver_result, |
| 1252 | &chand->on_resolver_result_changed); |
Craig Tiller | 613dafa | 2017-02-09 12:00:43 -0800 | [diff] [blame] | 1253 | } |
| 1254 | } |
Craig Tiller | d2e5cfc | 2017-02-09 13:02:20 -0800 | [diff] [blame] | 1255 | GRPC_CHANNEL_STACK_UNREF(exec_ctx, chand->owning_stack, "try_to_connect"); |
Craig Tiller | 613dafa | 2017-02-09 12:00:43 -0800 | [diff] [blame] | 1256 | } |
| 1257 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1258 | grpc_connectivity_state grpc_client_channel_check_connectivity_state( |
| 1259 | grpc_exec_ctx *exec_ctx, grpc_channel_element *elem, int try_to_connect) { |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 1260 | channel_data *chand = elem->channel_data; |
Craig Tiller | a8610c0 | 2017-02-14 10:05:11 -0800 | [diff] [blame] | 1261 | grpc_connectivity_state out = |
| 1262 | grpc_connectivity_state_check(&chand->state_tracker); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1263 | if (out == GRPC_CHANNEL_IDLE && try_to_connect) { |
Craig Tiller | d2e5cfc | 2017-02-09 13:02:20 -0800 | [diff] [blame] | 1264 | GRPC_CHANNEL_STACK_REF(chand->owning_stack, "try_to_connect"); |
Craig Tiller | 613dafa | 2017-02-09 12:00:43 -0800 | [diff] [blame] | 1265 | grpc_closure_sched( |
| 1266 | exec_ctx, |
| 1267 | grpc_closure_create(try_to_connect_locked, chand, |
| 1268 | grpc_combiner_scheduler(chand->combiner, false)), |
| 1269 | GRPC_ERROR_NONE); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1270 | } |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 1271 | return out; |
| 1272 | } |
| 1273 | |
Craig Tiller | 86c9958 | 2015-11-25 15:22:26 -0800 | [diff] [blame] | 1274 | typedef struct { |
| 1275 | channel_data *chand; |
| 1276 | grpc_pollset *pollset; |
| 1277 | grpc_closure *on_complete; |
Craig Tiller | 613dafa | 2017-02-09 12:00:43 -0800 | [diff] [blame] | 1278 | grpc_connectivity_state *state; |
Craig Tiller | 86c9958 | 2015-11-25 15:22:26 -0800 | [diff] [blame] | 1279 | grpc_closure my_closure; |
| 1280 | } external_connectivity_watcher; |
| 1281 | |
Craig Tiller | 1d881fb | 2015-12-01 07:39:04 -0800 | [diff] [blame] | 1282 | static void on_external_watch_complete(grpc_exec_ctx *exec_ctx, void *arg, |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 1283 | grpc_error *error) { |
Craig Tiller | 86c9958 | 2015-11-25 15:22:26 -0800 | [diff] [blame] | 1284 | external_connectivity_watcher *w = arg; |
| 1285 | grpc_closure *follow_up = w->on_complete; |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 1286 | grpc_pollset_set_del_pollset(exec_ctx, w->chand->interested_parties, |
Craig Tiller | 1d881fb | 2015-12-01 07:39:04 -0800 | [diff] [blame] | 1287 | w->pollset); |
| 1288 | GRPC_CHANNEL_STACK_UNREF(exec_ctx, w->chand->owning_stack, |
| 1289 | "external_connectivity_watcher"); |
Craig Tiller | 86c9958 | 2015-11-25 15:22:26 -0800 | [diff] [blame] | 1290 | gpr_free(w); |
Craig Tiller | 613dafa | 2017-02-09 12:00:43 -0800 | [diff] [blame] | 1291 | grpc_closure_run(exec_ctx, follow_up, GRPC_ERROR_REF(error)); |
| 1292 | } |
| 1293 | |
Craig Tiller | a8610c0 | 2017-02-14 10:05:11 -0800 | [diff] [blame] | 1294 | static void watch_connectivity_state_locked(grpc_exec_ctx *exec_ctx, void *arg, |
| 1295 | grpc_error *error_ignored) { |
Craig Tiller | 613dafa | 2017-02-09 12:00:43 -0800 | [diff] [blame] | 1296 | external_connectivity_watcher *w = arg; |
| 1297 | grpc_closure_init(&w->my_closure, on_external_watch_complete, w, |
| 1298 | grpc_schedule_on_exec_ctx); |
| 1299 | grpc_connectivity_state_notify_on_state_change( |
| 1300 | exec_ctx, &w->chand->state_tracker, w->state, &w->my_closure); |
Craig Tiller | 86c9958 | 2015-11-25 15:22:26 -0800 | [diff] [blame] | 1301 | } |
| 1302 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1303 | void grpc_client_channel_watch_connectivity_state( |
Craig Tiller | 906e3bc | 2015-11-24 07:31:31 -0800 | [diff] [blame] | 1304 | grpc_exec_ctx *exec_ctx, grpc_channel_element *elem, grpc_pollset *pollset, |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1305 | grpc_connectivity_state *state, grpc_closure *on_complete) { |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 1306 | channel_data *chand = elem->channel_data; |
Craig Tiller | 86c9958 | 2015-11-25 15:22:26 -0800 | [diff] [blame] | 1307 | external_connectivity_watcher *w = gpr_malloc(sizeof(*w)); |
| 1308 | w->chand = chand; |
| 1309 | w->pollset = pollset; |
| 1310 | w->on_complete = on_complete; |
Craig Tiller | 613dafa | 2017-02-09 12:00:43 -0800 | [diff] [blame] | 1311 | w->state = state; |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 1312 | grpc_pollset_set_add_pollset(exec_ctx, chand->interested_parties, pollset); |
Craig Tiller | 1d881fb | 2015-12-01 07:39:04 -0800 | [diff] [blame] | 1313 | GRPC_CHANNEL_STACK_REF(w->chand->owning_stack, |
| 1314 | "external_connectivity_watcher"); |
Craig Tiller | 613dafa | 2017-02-09 12:00:43 -0800 | [diff] [blame] | 1315 | grpc_closure_sched( |
| 1316 | exec_ctx, |
Craig Tiller | a8610c0 | 2017-02-14 10:05:11 -0800 | [diff] [blame] | 1317 | grpc_closure_init(&w->my_closure, watch_connectivity_state_locked, w, |
Craig Tiller | 613dafa | 2017-02-09 12:00:43 -0800 | [diff] [blame] | 1318 | grpc_combiner_scheduler(chand->combiner, true)), |
| 1319 | GRPC_ERROR_NONE); |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 1320 | } |