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