Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 3 | * Copyright 2015 gRPC authors. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 4 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 8 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 10 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 16 | * |
| 17 | */ |
| 18 | |
Craig Tiller | 9eb0fde | 2017-03-31 16:59:30 -0700 | [diff] [blame] | 19 | #include "src/core/ext/filters/client_channel/client_channel.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 20 | |
Mark D. Roth | 4c0fe49 | 2016-08-31 13:51:55 -0700 | [diff] [blame] | 21 | #include <stdbool.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 22 | #include <stdio.h> |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 23 | #include <string.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 24 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 25 | #include <grpc/support/alloc.h> |
| 26 | #include <grpc/support/log.h> |
Mark D. Roth | b2d2488 | 2016-10-27 15:44:07 -0700 | [diff] [blame] | 27 | #include <grpc/support/string_util.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 28 | #include <grpc/support/sync.h> |
| 29 | #include <grpc/support/useful.h> |
| 30 | |
Craig Tiller | 9eb0fde | 2017-03-31 16:59:30 -0700 | [diff] [blame] | 31 | #include "src/core/ext/filters/client_channel/http_connect_handshaker.h" |
| 32 | #include "src/core/ext/filters/client_channel/lb_policy_registry.h" |
| 33 | #include "src/core/ext/filters/client_channel/proxy_mapper_registry.h" |
| 34 | #include "src/core/ext/filters/client_channel/resolver_registry.h" |
| 35 | #include "src/core/ext/filters/client_channel/retry_throttle.h" |
| 36 | #include "src/core/ext/filters/client_channel/subchannel.h" |
Craig Tiller | 3be7dd0 | 2017-04-03 14:30:03 -0700 | [diff] [blame] | 37 | #include "src/core/ext/filters/deadline/deadline_filter.h" |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 38 | #include "src/core/lib/channel/channel_args.h" |
| 39 | #include "src/core/lib/channel/connected_channel.h" |
Craig Tiller | befafe6 | 2017-02-09 11:30:54 -0800 | [diff] [blame] | 40 | #include "src/core/lib/iomgr/combiner.h" |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 41 | #include "src/core/lib/iomgr/iomgr.h" |
Mark D. Roth | 4c0fe49 | 2016-08-31 13:51:55 -0700 | [diff] [blame] | 42 | #include "src/core/lib/iomgr/polling_entity.h" |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 43 | #include "src/core/lib/profiling/timers.h" |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 44 | #include "src/core/lib/slice/slice_internal.h" |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 45 | #include "src/core/lib/support/string.h" |
| 46 | #include "src/core/lib/surface/channel.h" |
| 47 | #include "src/core/lib/transport/connectivity_state.h" |
Mark D. Roth | 9fe284e | 2016-09-12 11:22:27 -0700 | [diff] [blame] | 48 | #include "src/core/lib/transport/metadata.h" |
| 49 | #include "src/core/lib/transport/metadata_batch.h" |
Mark D. Roth | ea846a0 | 2016-11-03 11:32:54 -0700 | [diff] [blame] | 50 | #include "src/core/lib/transport/service_config.h" |
Mark D. Roth | 9fe284e | 2016-09-12 11:22:27 -0700 | [diff] [blame] | 51 | #include "src/core/lib/transport/static_metadata.h" |
Craig Tiller | 8910ac6 | 2015-10-08 16:49:15 -0700 | [diff] [blame] | 52 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 53 | /* Client channel implementation */ |
| 54 | |
ncteisen | 7712c7c | 2017-07-12 23:11:27 -0700 | [diff] [blame] | 55 | grpc_tracer_flag grpc_client_channel_trace = |
| 56 | GRPC_TRACER_INITIALIZER(false, "client_channel"); |
Mark D. Roth | 60751fe | 2017-07-07 12:50:33 -0700 | [diff] [blame] | 57 | |
Mark D. Roth | 26b7be4 | 2016-10-24 10:08:07 -0700 | [diff] [blame] | 58 | /************************************************************************* |
| 59 | * METHOD-CONFIG TABLE |
| 60 | */ |
| 61 | |
Mark D. Roth | 9d48094 | 2016-10-19 14:18:05 -0700 | [diff] [blame] | 62 | typedef enum { |
Craig Tiller | 7acc37e | 2017-02-28 10:01:37 -0800 | [diff] [blame] | 63 | /* zero so it can be default initialized */ |
| 64 | WAIT_FOR_READY_UNSET = 0, |
Mark D. Roth | 9d48094 | 2016-10-19 14:18:05 -0700 | [diff] [blame] | 65 | WAIT_FOR_READY_FALSE, |
| 66 | WAIT_FOR_READY_TRUE |
| 67 | } wait_for_ready_value; |
| 68 | |
Mark D. Roth | 95b627b | 2017-02-24 11:02:58 -0800 | [diff] [blame] | 69 | typedef struct { |
| 70 | gpr_refcount refs; |
Mark D. Roth | 9d48094 | 2016-10-19 14:18:05 -0700 | [diff] [blame] | 71 | gpr_timespec timeout; |
| 72 | wait_for_ready_value wait_for_ready; |
| 73 | } method_parameters; |
| 74 | |
Mark D. Roth | 722de8d | 2017-02-27 10:50:44 -0800 | [diff] [blame] | 75 | static method_parameters *method_parameters_ref( |
Mark D. Roth | 95b627b | 2017-02-24 11:02:58 -0800 | [diff] [blame] | 76 | method_parameters *method_params) { |
| 77 | gpr_ref(&method_params->refs); |
| 78 | return method_params; |
Mark D. Roth | 9d48094 | 2016-10-19 14:18:05 -0700 | [diff] [blame] | 79 | } |
| 80 | |
Mark D. Roth | 95b627b | 2017-02-24 11:02:58 -0800 | [diff] [blame] | 81 | static void method_parameters_unref(method_parameters *method_params) { |
| 82 | if (gpr_unref(&method_params->refs)) { |
| 83 | gpr_free(method_params); |
| 84 | } |
| 85 | } |
| 86 | |
Mark D. Roth | 95b627b | 2017-02-24 11:02:58 -0800 | [diff] [blame] | 87 | static void method_parameters_free(grpc_exec_ctx *exec_ctx, void *value) { |
Yash Tibrewal | bc130da | 2017-09-12 22:44:08 -0700 | [diff] [blame] | 88 | method_parameters_unref((method_parameters *)value); |
Craig Tiller | 87a7e1f | 2016-11-09 09:42:19 -0800 | [diff] [blame] | 89 | } |
| 90 | |
Mark D. Roth | 95b627b | 2017-02-24 11:02:58 -0800 | [diff] [blame] | 91 | static bool parse_wait_for_ready(grpc_json *field, |
| 92 | wait_for_ready_value *wait_for_ready) { |
| 93 | if (field->type != GRPC_JSON_TRUE && field->type != GRPC_JSON_FALSE) { |
| 94 | return false; |
| 95 | } |
| 96 | *wait_for_ready = field->type == GRPC_JSON_TRUE ? WAIT_FOR_READY_TRUE |
| 97 | : WAIT_FOR_READY_FALSE; |
| 98 | return true; |
| 99 | } |
| 100 | |
Mark D. Roth | 722de8d | 2017-02-27 10:50:44 -0800 | [diff] [blame] | 101 | static bool parse_timeout(grpc_json *field, gpr_timespec *timeout) { |
Mark D. Roth | 95b627b | 2017-02-24 11:02:58 -0800 | [diff] [blame] | 102 | if (field->type != GRPC_JSON_STRING) return false; |
| 103 | size_t len = strlen(field->value); |
| 104 | if (field->value[len - 1] != 's') return false; |
| 105 | char *buf = gpr_strdup(field->value); |
| 106 | buf[len - 1] = '\0'; // Remove trailing 's'. |
| 107 | char *decimal_point = strchr(buf, '.'); |
| 108 | if (decimal_point != NULL) { |
| 109 | *decimal_point = '\0'; |
| 110 | timeout->tv_nsec = gpr_parse_nonnegative_int(decimal_point + 1); |
| 111 | if (timeout->tv_nsec == -1) { |
| 112 | gpr_free(buf); |
| 113 | return false; |
| 114 | } |
| 115 | // There should always be exactly 3, 6, or 9 fractional digits. |
| 116 | int multiplier = 1; |
| 117 | switch (strlen(decimal_point + 1)) { |
| 118 | case 9: |
| 119 | break; |
| 120 | case 6: |
| 121 | multiplier *= 1000; |
| 122 | break; |
| 123 | case 3: |
| 124 | multiplier *= 1000000; |
| 125 | break; |
| 126 | default: // Unsupported number of digits. |
| 127 | gpr_free(buf); |
| 128 | return false; |
| 129 | } |
| 130 | timeout->tv_nsec *= multiplier; |
| 131 | } |
| 132 | timeout->tv_sec = gpr_parse_nonnegative_int(buf); |
| 133 | gpr_free(buf); |
| 134 | if (timeout->tv_sec == -1) return false; |
| 135 | return true; |
| 136 | } |
| 137 | |
Mark D. Roth | e30baeb | 2016-11-03 08:16:19 -0700 | [diff] [blame] | 138 | static void *method_parameters_create_from_json(const grpc_json *json) { |
Mark D. Roth | c968e60 | 2016-11-02 14:07:36 -0700 | [diff] [blame] | 139 | wait_for_ready_value wait_for_ready = WAIT_FOR_READY_UNSET; |
Mark D. Roth | 47f1084 | 2016-11-03 08:45:27 -0700 | [diff] [blame] | 140 | gpr_timespec timeout = {0, 0, GPR_TIMESPAN}; |
| 141 | for (grpc_json *field = json->child; field != NULL; field = field->next) { |
Mark D. Roth | c968e60 | 2016-11-02 14:07:36 -0700 | [diff] [blame] | 142 | if (field->key == NULL) continue; |
Mark D. Roth | 84c8a02 | 2016-11-10 09:39:34 -0800 | [diff] [blame] | 143 | if (strcmp(field->key, "waitForReady") == 0) { |
Mark D. Roth | c968e60 | 2016-11-02 14:07:36 -0700 | [diff] [blame] | 144 | if (wait_for_ready != WAIT_FOR_READY_UNSET) return NULL; // Duplicate. |
Mark D. Roth | 95b627b | 2017-02-24 11:02:58 -0800 | [diff] [blame] | 145 | if (!parse_wait_for_ready(field, &wait_for_ready)) return NULL; |
Mark D. Roth | c968e60 | 2016-11-02 14:07:36 -0700 | [diff] [blame] | 146 | } else if (strcmp(field->key, "timeout") == 0) { |
| 147 | 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] | 148 | if (!parse_timeout(field, &timeout)) return NULL; |
Mark D. Roth | c968e60 | 2016-11-02 14:07:36 -0700 | [diff] [blame] | 149 | } |
| 150 | } |
Yash Tibrewal | ca3c1c0 | 2017-09-07 22:47:16 -0700 | [diff] [blame] | 151 | method_parameters *value = |
| 152 | (method_parameters *)gpr_malloc(sizeof(method_parameters)); |
Mark D. Roth | 95b627b | 2017-02-24 11:02:58 -0800 | [diff] [blame] | 153 | gpr_ref_init(&value->refs, 1); |
Mark D. Roth | c968e60 | 2016-11-02 14:07:36 -0700 | [diff] [blame] | 154 | value->timeout = timeout; |
| 155 | value->wait_for_ready = wait_for_ready; |
Mark D. Roth | 9d48094 | 2016-10-19 14:18:05 -0700 | [diff] [blame] | 156 | return value; |
| 157 | } |
| 158 | |
Alexander Polcyn | c3b1f18 | 2017-04-18 13:51:36 -0700 | [diff] [blame] | 159 | struct external_connectivity_watcher; |
| 160 | |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 161 | /************************************************************************* |
| 162 | * CHANNEL-WIDE FUNCTIONS |
| 163 | */ |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 164 | |
Craig Tiller | 800dacb | 2015-10-06 09:10:26 -0700 | [diff] [blame] | 165 | typedef struct client_channel_channel_data { |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 166 | /** resolver for this channel */ |
| 167 | grpc_resolver *resolver; |
Craig Tiller | 20a3c35 | 2015-08-05 08:39:50 -0700 | [diff] [blame] | 168 | /** have we started resolving this channel */ |
Mark D. Roth | 4c0fe49 | 2016-08-31 13:51:55 -0700 | [diff] [blame] | 169 | bool started_resolving; |
Craig Tiller | 3be7dd0 | 2017-04-03 14:30:03 -0700 | [diff] [blame] | 170 | /** is deadline checking enabled? */ |
| 171 | bool deadline_checking_enabled; |
Mark D. Roth | 0e48a9a | 2016-09-08 14:14:39 -0700 | [diff] [blame] | 172 | /** client channel factory */ |
| 173 | grpc_client_channel_factory *client_channel_factory; |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 174 | |
Craig Tiller | befafe6 | 2017-02-09 11:30:54 -0800 | [diff] [blame] | 175 | /** combiner protecting all variables below in this data structure */ |
| 176 | grpc_combiner *combiner; |
Mark D. Roth | 046cf76 | 2016-09-26 11:13:51 -0700 | [diff] [blame] | 177 | /** currently active load balancer */ |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 178 | grpc_lb_policy *lb_policy; |
Mark D. Roth | d6d192d | 2017-02-23 08:58:42 -0800 | [diff] [blame] | 179 | /** retry throttle data */ |
| 180 | grpc_server_retry_throttle_data *retry_throttle_data; |
Mark D. Roth | 9d48094 | 2016-10-19 14:18:05 -0700 | [diff] [blame] | 181 | /** maps method names to method_parameters structs */ |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 182 | grpc_slice_hash_table *method_params_table; |
Mark D. Roth | 046cf76 | 2016-09-26 11:13:51 -0700 | [diff] [blame] | 183 | /** incoming resolver result - set by resolver.next() */ |
Mark D. Roth | af84245 | 2016-10-21 15:05:15 -0700 | [diff] [blame] | 184 | grpc_channel_args *resolver_result; |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 185 | /** a list of closures that are all waiting for resolver result to come in */ |
| 186 | grpc_closure_list waiting_for_resolver_result_closures; |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 187 | /** resolver callback */ |
Mark D. Roth | ff4df06 | 2016-08-22 15:02:49 -0700 | [diff] [blame] | 188 | grpc_closure on_resolver_result_changed; |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 189 | /** connectivity state being tracked */ |
Craig Tiller | ca3e9d3 | 2015-06-27 18:37:27 -0700 | [diff] [blame] | 190 | grpc_connectivity_state_tracker state_tracker; |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 191 | /** when an lb_policy arrives, should we try to exit idle */ |
Mark D. Roth | 4c0fe49 | 2016-08-31 13:51:55 -0700 | [diff] [blame] | 192 | bool exit_idle_when_lb_policy_arrives; |
Craig Tiller | 906e3bc | 2015-11-24 07:31:31 -0800 | [diff] [blame] | 193 | /** owning stack */ |
| 194 | grpc_channel_stack *owning_stack; |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 195 | /** interested parties (owned) */ |
| 196 | grpc_pollset_set *interested_parties; |
Craig Tiller | 613dafa | 2017-02-09 12:00:43 -0800 | [diff] [blame] | 197 | |
Alexander Polcyn | c3b1f18 | 2017-04-18 13:51:36 -0700 | [diff] [blame] | 198 | /* external_connectivity_watcher_list head is guarded by its own mutex, since |
| 199 | * counts need to be grabbed immediately without polling on a cq */ |
| 200 | gpr_mu external_connectivity_watcher_list_mu; |
| 201 | struct external_connectivity_watcher *external_connectivity_watcher_list_head; |
| 202 | |
Craig Tiller | 613dafa | 2017-02-09 12:00:43 -0800 | [diff] [blame] | 203 | /* 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] | 204 | to be instantaneously available */ |
Craig Tiller | 613dafa | 2017-02-09 12:00:43 -0800 | [diff] [blame] | 205 | gpr_mu info_mu; |
| 206 | char *info_lb_policy_name; |
| 207 | /** service config in JSON form */ |
| 208 | char *info_service_config_json; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 209 | } channel_data; |
| 210 | |
Craig Tiller | d6c98df | 2015-08-18 09:33:44 -0700 | [diff] [blame] | 211 | /** 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] | 212 | resolver, to watch for state changes from the lb_policy. When a state |
| 213 | change is seen, we update the channel, and create a new watcher. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 214 | typedef struct { |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 215 | channel_data *chand; |
Craig Tiller | 3382511 | 2015-09-18 07:44:19 -0700 | [diff] [blame] | 216 | grpc_closure on_changed; |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 217 | grpc_connectivity_state state; |
| 218 | grpc_lb_policy *lb_policy; |
| 219 | } lb_policy_connectivity_watcher; |
| 220 | |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 221 | static void watch_lb_policy_locked(grpc_exec_ctx *exec_ctx, channel_data *chand, |
| 222 | grpc_lb_policy *lb_policy, |
| 223 | grpc_connectivity_state current_state); |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 224 | |
Craig Tiller | 8c0d96f | 2016-03-11 14:27:52 -0800 | [diff] [blame] | 225 | static void set_channel_connectivity_state_locked(grpc_exec_ctx *exec_ctx, |
| 226 | channel_data *chand, |
| 227 | grpc_connectivity_state state, |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 228 | grpc_error *error, |
Craig Tiller | 8c0d96f | 2016-03-11 14:27:52 -0800 | [diff] [blame] | 229 | const char *reason) { |
David Garcia Quintas | 3725128 | 2017-04-14 13:46:03 -0700 | [diff] [blame] | 230 | /* TODO: Improve failure handling: |
| 231 | * - Make it possible for policies to return GRPC_CHANNEL_TRANSIENT_FAILURE. |
| 232 | * - Hand over pending picks from old policies during the switch that happens |
| 233 | * when resolver provides an update. */ |
David Garcia Quintas | 956f700 | 2017-04-13 15:40:06 -0700 | [diff] [blame] | 234 | if (chand->lb_policy != NULL) { |
| 235 | if (state == GRPC_CHANNEL_TRANSIENT_FAILURE) { |
| 236 | /* cancel picks with wait_for_ready=false */ |
| 237 | grpc_lb_policy_cancel_picks_locked( |
| 238 | exec_ctx, chand->lb_policy, |
| 239 | /* mask= */ GRPC_INITIAL_METADATA_WAIT_FOR_READY, |
| 240 | /* check= */ 0, GRPC_ERROR_REF(error)); |
| 241 | } else if (state == GRPC_CHANNEL_SHUTDOWN) { |
| 242 | /* cancel all picks */ |
| 243 | grpc_lb_policy_cancel_picks_locked(exec_ctx, chand->lb_policy, |
| 244 | /* mask= */ 0, /* check= */ 0, |
| 245 | GRPC_ERROR_REF(error)); |
| 246 | } |
Craig Tiller | 8c0d96f | 2016-03-11 14:27:52 -0800 | [diff] [blame] | 247 | } |
Mark D. Roth | 60751fe | 2017-07-07 12:50:33 -0700 | [diff] [blame] | 248 | if (GRPC_TRACER_ON(grpc_client_channel_trace)) { |
| 249 | gpr_log(GPR_DEBUG, "chand=%p: setting connectivity state to %s", chand, |
| 250 | grpc_connectivity_state_name(state)); |
| 251 | } |
Craig Tiller | 9ccf5f1 | 2016-05-07 21:41:01 -0700 | [diff] [blame] | 252 | grpc_connectivity_state_set(exec_ctx, &chand->state_tracker, state, error, |
| 253 | reason); |
Craig Tiller | 8c0d96f | 2016-03-11 14:27:52 -0800 | [diff] [blame] | 254 | } |
| 255 | |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 256 | 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] | 257 | void *arg, grpc_error *error) { |
Yash Tibrewal | ca3c1c0 | 2017-09-07 22:47:16 -0700 | [diff] [blame] | 258 | lb_policy_connectivity_watcher *w = (lb_policy_connectivity_watcher *)arg; |
Craig Tiller | cb2609f | 2015-11-24 17:19:19 -0800 | [diff] [blame] | 259 | grpc_connectivity_state publish_state = w->state; |
Craig Tiller | c5de835 | 2017-02-09 14:08:05 -0800 | [diff] [blame] | 260 | /* check if the notification is for the latest policy */ |
| 261 | if (w->lb_policy == w->chand->lb_policy) { |
Mark D. Roth | 60751fe | 2017-07-07 12:50:33 -0700 | [diff] [blame] | 262 | if (GRPC_TRACER_ON(grpc_client_channel_trace)) { |
| 263 | gpr_log(GPR_DEBUG, "chand=%p: lb_policy=%p state changed to %s", w->chand, |
| 264 | w->lb_policy, grpc_connectivity_state_name(w->state)); |
| 265 | } |
Craig Tiller | c5de835 | 2017-02-09 14:08:05 -0800 | [diff] [blame] | 266 | if (publish_state == GRPC_CHANNEL_SHUTDOWN && w->chand->resolver != NULL) { |
| 267 | publish_state = GRPC_CHANNEL_TRANSIENT_FAILURE; |
Craig Tiller | 972470b | 2017-02-09 15:05:36 -0800 | [diff] [blame] | 268 | grpc_resolver_channel_saw_error_locked(exec_ctx, w->chand->resolver); |
Craig Tiller | c5de835 | 2017-02-09 14:08:05 -0800 | [diff] [blame] | 269 | GRPC_LB_POLICY_UNREF(exec_ctx, w->chand->lb_policy, "channel"); |
| 270 | w->chand->lb_policy = NULL; |
| 271 | } |
| 272 | set_channel_connectivity_state_locked(exec_ctx, w->chand, publish_state, |
| 273 | GRPC_ERROR_REF(error), "lb_changed"); |
| 274 | if (w->state != GRPC_CHANNEL_SHUTDOWN) { |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 275 | 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] | 276 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 277 | } |
Craig Tiller | 906e3bc | 2015-11-24 07:31:31 -0800 | [diff] [blame] | 278 | 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] | 279 | gpr_free(w); |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 280 | } |
| 281 | |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 282 | static void watch_lb_policy_locked(grpc_exec_ctx *exec_ctx, channel_data *chand, |
| 283 | grpc_lb_policy *lb_policy, |
| 284 | grpc_connectivity_state current_state) { |
Yash Tibrewal | ca3c1c0 | 2017-09-07 22:47:16 -0700 | [diff] [blame] | 285 | lb_policy_connectivity_watcher *w = |
| 286 | (lb_policy_connectivity_watcher *)gpr_malloc(sizeof(*w)); |
Craig Tiller | 906e3bc | 2015-11-24 07:31:31 -0800 | [diff] [blame] | 287 | GRPC_CHANNEL_STACK_REF(chand->owning_stack, "watch_lb_policy"); |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 288 | w->chand = chand; |
ncteisen | 274bbbe | 2017-06-08 14:57:11 -0700 | [diff] [blame] | 289 | GRPC_CLOSURE_INIT(&w->on_changed, on_lb_policy_state_changed_locked, w, |
Craig Tiller | ee4b145 | 2017-05-12 10:56:03 -0700 | [diff] [blame] | 290 | grpc_combiner_scheduler(chand->combiner)); |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 291 | w->state = current_state; |
| 292 | w->lb_policy = lb_policy; |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 293 | grpc_lb_policy_notify_on_state_change_locked(exec_ctx, lb_policy, &w->state, |
| 294 | &w->on_changed); |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 295 | } |
| 296 | |
Mark D. Roth | 60751fe | 2017-07-07 12:50:33 -0700 | [diff] [blame] | 297 | static void start_resolving_locked(grpc_exec_ctx *exec_ctx, |
| 298 | channel_data *chand) { |
| 299 | if (GRPC_TRACER_ON(grpc_client_channel_trace)) { |
| 300 | gpr_log(GPR_DEBUG, "chand=%p: starting name resolution", chand); |
| 301 | } |
| 302 | GPR_ASSERT(!chand->started_resolving); |
| 303 | chand->started_resolving = true; |
| 304 | GRPC_CHANNEL_STACK_REF(chand->owning_stack, "resolver"); |
| 305 | grpc_resolver_next_locked(exec_ctx, chand->resolver, &chand->resolver_result, |
| 306 | &chand->on_resolver_result_changed); |
| 307 | } |
| 308 | |
Mark D. Roth | d6d192d | 2017-02-23 08:58:42 -0800 | [diff] [blame] | 309 | typedef struct { |
| 310 | char *server_name; |
| 311 | grpc_server_retry_throttle_data *retry_throttle_data; |
| 312 | } service_config_parsing_state; |
| 313 | |
| 314 | static void parse_retry_throttle_params(const grpc_json *field, void *arg) { |
Yash Tibrewal | ca3c1c0 | 2017-09-07 22:47:16 -0700 | [diff] [blame] | 315 | service_config_parsing_state *parsing_state = |
| 316 | (service_config_parsing_state *)arg; |
Mark D. Roth | d6d192d | 2017-02-23 08:58:42 -0800 | [diff] [blame] | 317 | if (strcmp(field->key, "retryThrottling") == 0) { |
| 318 | if (parsing_state->retry_throttle_data != NULL) return; // Duplicate. |
| 319 | if (field->type != GRPC_JSON_OBJECT) return; |
| 320 | int max_milli_tokens = 0; |
| 321 | int milli_token_ratio = 0; |
| 322 | for (grpc_json *sub_field = field->child; sub_field != NULL; |
| 323 | sub_field = sub_field->next) { |
Mark D. Roth | b332256 | 2017-02-23 14:38:02 -0800 | [diff] [blame] | 324 | if (sub_field->key == NULL) return; |
Mark D. Roth | d6d192d | 2017-02-23 08:58:42 -0800 | [diff] [blame] | 325 | if (strcmp(sub_field->key, "maxTokens") == 0) { |
| 326 | if (max_milli_tokens != 0) return; // Duplicate. |
| 327 | if (sub_field->type != GRPC_JSON_NUMBER) return; |
| 328 | max_milli_tokens = gpr_parse_nonnegative_int(sub_field->value); |
| 329 | if (max_milli_tokens == -1) return; |
| 330 | max_milli_tokens *= 1000; |
| 331 | } else if (strcmp(sub_field->key, "tokenRatio") == 0) { |
| 332 | if (milli_token_ratio != 0) return; // Duplicate. |
| 333 | if (sub_field->type != GRPC_JSON_NUMBER) return; |
| 334 | // We support up to 3 decimal digits. |
| 335 | size_t whole_len = strlen(sub_field->value); |
| 336 | uint32_t multiplier = 1; |
| 337 | uint32_t decimal_value = 0; |
| 338 | const char *decimal_point = strchr(sub_field->value, '.'); |
| 339 | if (decimal_point != NULL) { |
| 340 | whole_len = (size_t)(decimal_point - sub_field->value); |
| 341 | multiplier = 1000; |
| 342 | size_t decimal_len = strlen(decimal_point + 1); |
| 343 | if (decimal_len > 3) decimal_len = 3; |
| 344 | if (!gpr_parse_bytes_to_uint32(decimal_point + 1, decimal_len, |
| 345 | &decimal_value)) { |
| 346 | return; |
| 347 | } |
| 348 | uint32_t decimal_multiplier = 1; |
| 349 | for (size_t i = 0; i < (3 - decimal_len); ++i) { |
| 350 | decimal_multiplier *= 10; |
| 351 | } |
| 352 | decimal_value *= decimal_multiplier; |
| 353 | } |
| 354 | uint32_t whole_value; |
| 355 | if (!gpr_parse_bytes_to_uint32(sub_field->value, whole_len, |
| 356 | &whole_value)) { |
| 357 | return; |
| 358 | } |
| 359 | milli_token_ratio = (int)((whole_value * multiplier) + decimal_value); |
Mark D. Roth | b332256 | 2017-02-23 14:38:02 -0800 | [diff] [blame] | 360 | if (milli_token_ratio <= 0) return; |
Mark D. Roth | d6d192d | 2017-02-23 08:58:42 -0800 | [diff] [blame] | 361 | } |
| 362 | } |
| 363 | parsing_state->retry_throttle_data = |
| 364 | grpc_retry_throttle_map_get_data_for_server( |
| 365 | parsing_state->server_name, max_milli_tokens, milli_token_ratio); |
| 366 | } |
| 367 | } |
| 368 | |
Craig Tiller | befafe6 | 2017-02-09 11:30:54 -0800 | [diff] [blame] | 369 | static void on_resolver_result_changed_locked(grpc_exec_ctx *exec_ctx, |
| 370 | void *arg, grpc_error *error) { |
Yash Tibrewal | ca3c1c0 | 2017-09-07 22:47:16 -0700 | [diff] [blame] | 371 | channel_data *chand = (channel_data *)arg; |
Mark D. Roth | 60751fe | 2017-07-07 12:50:33 -0700 | [diff] [blame] | 372 | if (GRPC_TRACER_ON(grpc_client_channel_trace)) { |
| 373 | gpr_log(GPR_DEBUG, "chand=%p: got resolver result: error=%s", chand, |
| 374 | grpc_error_string(error)); |
| 375 | } |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 376 | // Extract the following fields from the resolver result, if non-NULL. |
Mark D. Roth | 15494b5 | 2017-07-12 15:26:55 -0700 | [diff] [blame] | 377 | bool lb_policy_updated = false; |
Yash Tibrewal | 9eb8672 | 2017-09-17 23:43:30 -0700 | [diff] [blame] | 378 | char *lb_policy_name_dup = NULL; |
Mark D. Roth | 60751fe | 2017-07-07 12:50:33 -0700 | [diff] [blame] | 379 | bool lb_policy_name_changed = false; |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 380 | grpc_lb_policy *new_lb_policy = NULL; |
Mark D. Roth | c625c7a | 2016-11-09 14:12:37 -0800 | [diff] [blame] | 381 | char *service_config_json = NULL; |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 382 | grpc_server_retry_throttle_data *retry_throttle_data = NULL; |
| 383 | grpc_slice_hash_table *method_params_table = NULL; |
Mark D. Roth | 046cf76 | 2016-09-26 11:13:51 -0700 | [diff] [blame] | 384 | if (chand->resolver_result != NULL) { |
Mark D. Roth | 5bd7be0 | 2016-10-21 14:19:50 -0700 | [diff] [blame] | 385 | // Find LB policy name. |
Yash Tibrewal | 9eb8672 | 2017-09-17 23:43:30 -0700 | [diff] [blame] | 386 | const char *lb_policy_name = NULL; |
Mark D. Roth | af84245 | 2016-10-21 15:05:15 -0700 | [diff] [blame] | 387 | const grpc_arg *channel_arg = |
Mark D. Roth | 4112499 | 2016-11-03 11:22:20 -0700 | [diff] [blame] | 388 | 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] | 389 | if (channel_arg != NULL) { |
| 390 | GPR_ASSERT(channel_arg->type == GRPC_ARG_STRING); |
| 391 | lb_policy_name = channel_arg->value.string; |
Mark D. Roth | 5bd7be0 | 2016-10-21 14:19:50 -0700 | [diff] [blame] | 392 | } |
Mark D. Roth | 1eb96dc | 2017-03-22 12:19:03 -0700 | [diff] [blame] | 393 | // Special case: If at least one balancer address is present, we use |
| 394 | // the grpclb policy, regardless of what the resolver actually specified. |
Mark D. Roth | af84245 | 2016-10-21 15:05:15 -0700 | [diff] [blame] | 395 | channel_arg = |
Mark D. Roth | 4112499 | 2016-11-03 11:22:20 -0700 | [diff] [blame] | 396 | grpc_channel_args_find(chand->resolver_result, GRPC_ARG_LB_ADDRESSES); |
David Garcia Quintas | 143cb77 | 2017-03-31 13:39:27 -0700 | [diff] [blame] | 397 | if (channel_arg != NULL && channel_arg->type == GRPC_ARG_POINTER) { |
Yash Tibrewal | ca3c1c0 | 2017-09-07 22:47:16 -0700 | [diff] [blame] | 398 | grpc_lb_addresses *addresses = |
| 399 | (grpc_lb_addresses *)channel_arg->value.pointer.p; |
Mark D. Roth | 1eb96dc | 2017-03-22 12:19:03 -0700 | [diff] [blame] | 400 | bool found_balancer_address = false; |
Mark D. Roth | af84245 | 2016-10-21 15:05:15 -0700 | [diff] [blame] | 401 | for (size_t i = 0; i < addresses->num_addresses; ++i) { |
Mark D. Roth | 1eb96dc | 2017-03-22 12:19:03 -0700 | [diff] [blame] | 402 | if (addresses->addresses[i].is_balancer) { |
| 403 | found_balancer_address = true; |
Mark D. Roth | af84245 | 2016-10-21 15:05:15 -0700 | [diff] [blame] | 404 | break; |
| 405 | } |
Mark D. Roth | 88405f7 | 2016-10-03 08:24:52 -0700 | [diff] [blame] | 406 | } |
Mark D. Roth | 1eb96dc | 2017-03-22 12:19:03 -0700 | [diff] [blame] | 407 | if (found_balancer_address) { |
Mark D. Roth | af84245 | 2016-10-21 15:05:15 -0700 | [diff] [blame] | 408 | if (lb_policy_name != NULL && strcmp(lb_policy_name, "grpclb") != 0) { |
| 409 | gpr_log(GPR_INFO, |
Mark D. Roth | 1eb96dc | 2017-03-22 12:19:03 -0700 | [diff] [blame] | 410 | "resolver requested LB policy %s but provided at least one " |
| 411 | "balancer address -- forcing use of grpclb LB policy", |
Mark D. Roth | 5f40e5d | 2016-10-24 13:09:05 -0700 | [diff] [blame] | 412 | lb_policy_name); |
Mark D. Roth | af84245 | 2016-10-21 15:05:15 -0700 | [diff] [blame] | 413 | } |
| 414 | lb_policy_name = "grpclb"; |
Mark D. Roth | 88405f7 | 2016-10-03 08:24:52 -0700 | [diff] [blame] | 415 | } |
Mark D. Roth | 88405f7 | 2016-10-03 08:24:52 -0700 | [diff] [blame] | 416 | } |
| 417 | // Use pick_first if nothing was specified and we didn't select grpclb |
| 418 | // above. |
| 419 | if (lb_policy_name == NULL) lb_policy_name = "pick_first"; |
Mark D. Roth | 4112499 | 2016-11-03 11:22:20 -0700 | [diff] [blame] | 420 | grpc_lb_policy_args lb_policy_args; |
| 421 | lb_policy_args.args = chand->resolver_result; |
| 422 | lb_policy_args.client_channel_factory = chand->client_channel_factory; |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 423 | lb_policy_args.combiner = chand->combiner; |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 424 | // Check to see if we're already using the right LB policy. |
| 425 | // Note: It's safe to use chand->info_lb_policy_name here without |
| 426 | // taking a lock on chand->info_mu, because this function is the |
| 427 | // only thing that modifies its value, and it can only be invoked |
| 428 | // once at any given time. |
Mark D. Roth | 60751fe | 2017-07-07 12:50:33 -0700 | [diff] [blame] | 429 | lb_policy_name_changed = |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 430 | chand->info_lb_policy_name == NULL || |
| 431 | strcmp(chand->info_lb_policy_name, lb_policy_name) != 0; |
Mark D. Roth | 60751fe | 2017-07-07 12:50:33 -0700 | [diff] [blame] | 432 | if (chand->lb_policy != NULL && !lb_policy_name_changed) { |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 433 | // Continue using the same LB policy. Update with new addresses. |
Mark D. Roth | 15494b5 | 2017-07-12 15:26:55 -0700 | [diff] [blame] | 434 | lb_policy_updated = true; |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 435 | grpc_lb_policy_update_locked(exec_ctx, chand->lb_policy, &lb_policy_args); |
| 436 | } else { |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 437 | // Instantiate new LB policy. |
| 438 | new_lb_policy = |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 439 | grpc_lb_policy_create(exec_ctx, lb_policy_name, &lb_policy_args); |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 440 | if (new_lb_policy == NULL) { |
| 441 | gpr_log(GPR_ERROR, "could not create LB policy \"%s\"", lb_policy_name); |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 442 | } |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 443 | } |
Mark D. Roth | 4112499 | 2016-11-03 11:22:20 -0700 | [diff] [blame] | 444 | // Find service config. |
Mark D. Roth | af84245 | 2016-10-21 15:05:15 -0700 | [diff] [blame] | 445 | channel_arg = |
Mark D. Roth | 4112499 | 2016-11-03 11:22:20 -0700 | [diff] [blame] | 446 | grpc_channel_args_find(chand->resolver_result, GRPC_ARG_SERVICE_CONFIG); |
Mark D. Roth | 046cf76 | 2016-09-26 11:13:51 -0700 | [diff] [blame] | 447 | if (channel_arg != NULL) { |
Mark D. Roth | 9ec28af | 2016-11-03 12:32:39 -0700 | [diff] [blame] | 448 | GPR_ASSERT(channel_arg->type == GRPC_ARG_STRING); |
Mark D. Roth | c625c7a | 2016-11-09 14:12:37 -0800 | [diff] [blame] | 449 | service_config_json = gpr_strdup(channel_arg->value.string); |
Mark D. Roth | 70a1abd | 2016-11-04 09:26:37 -0700 | [diff] [blame] | 450 | grpc_service_config *service_config = |
Mark D. Roth | c625c7a | 2016-11-09 14:12:37 -0800 | [diff] [blame] | 451 | grpc_service_config_create(service_config_json); |
Mark D. Roth | bdc58b2 | 2016-11-04 09:25:57 -0700 | [diff] [blame] | 452 | if (service_config != NULL) { |
Mark D. Roth | d6d192d | 2017-02-23 08:58:42 -0800 | [diff] [blame] | 453 | channel_arg = |
| 454 | grpc_channel_args_find(chand->resolver_result, GRPC_ARG_SERVER_URI); |
| 455 | GPR_ASSERT(channel_arg != NULL); |
| 456 | GPR_ASSERT(channel_arg->type == GRPC_ARG_STRING); |
Mark D. Roth | 9ccbc4d | 2017-03-15 08:30:04 -0700 | [diff] [blame] | 457 | grpc_uri *uri = |
| 458 | grpc_uri_parse(exec_ctx, channel_arg->value.string, true); |
Mark D. Roth | d6d192d | 2017-02-23 08:58:42 -0800 | [diff] [blame] | 459 | GPR_ASSERT(uri->path[0] != '\0'); |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 460 | service_config_parsing_state parsing_state; |
| 461 | memset(&parsing_state, 0, sizeof(parsing_state)); |
Mark D. Roth | d6d192d | 2017-02-23 08:58:42 -0800 | [diff] [blame] | 462 | parsing_state.server_name = |
| 463 | uri->path[0] == '/' ? uri->path + 1 : uri->path; |
| 464 | grpc_service_config_parse_global_params( |
| 465 | service_config, parse_retry_throttle_params, &parsing_state); |
Mark D. Roth | d6d192d | 2017-02-23 08:58:42 -0800 | [diff] [blame] | 466 | grpc_uri_destroy(uri); |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 467 | retry_throttle_data = parsing_state.retry_throttle_data; |
Mark D. Roth | bdc58b2 | 2016-11-04 09:25:57 -0700 | [diff] [blame] | 468 | method_params_table = grpc_service_config_create_method_config_table( |
Craig Tiller | b28c7e8 | 2016-11-18 10:29:04 -0800 | [diff] [blame] | 469 | exec_ctx, service_config, method_parameters_create_from_json, |
Mark D. Roth | e300670 | 2017-04-19 07:43:56 -0700 | [diff] [blame] | 470 | method_parameters_free); |
Mark D. Roth | bdc58b2 | 2016-11-04 09:25:57 -0700 | [diff] [blame] | 471 | grpc_service_config_destroy(service_config); |
| 472 | } |
Mark D. Roth | 9fe284e | 2016-09-12 11:22:27 -0700 | [diff] [blame] | 473 | } |
Mark D. Roth | f79ce7d | 2016-11-04 08:43:36 -0700 | [diff] [blame] | 474 | // Before we clean up, save a copy of lb_policy_name, since it might |
| 475 | // be pointing to data inside chand->resolver_result. |
| 476 | // The copy will be saved in chand->lb_policy_name below. |
Yash Tibrewal | 9eb8672 | 2017-09-17 23:43:30 -0700 | [diff] [blame] | 477 | lb_policy_name_dup = gpr_strdup(lb_policy_name); |
Craig Tiller | 87a7e1f | 2016-11-09 09:42:19 -0800 | [diff] [blame] | 478 | grpc_channel_args_destroy(exec_ctx, chand->resolver_result); |
Mark D. Roth | 046cf76 | 2016-09-26 11:13:51 -0700 | [diff] [blame] | 479 | chand->resolver_result = NULL; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 480 | } |
Mark D. Roth | 60751fe | 2017-07-07 12:50:33 -0700 | [diff] [blame] | 481 | if (GRPC_TRACER_ON(grpc_client_channel_trace)) { |
| 482 | gpr_log(GPR_DEBUG, |
| 483 | "chand=%p: resolver result: lb_policy_name=\"%s\"%s, " |
| 484 | "service_config=\"%s\"", |
Yash Tibrewal | 9eb8672 | 2017-09-17 23:43:30 -0700 | [diff] [blame] | 485 | chand, lb_policy_name_dup, |
| 486 | lb_policy_name_changed ? " (changed)" : "", service_config_json); |
Mark D. Roth | 60751fe | 2017-07-07 12:50:33 -0700 | [diff] [blame] | 487 | } |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 488 | // Now swap out fields in chand. Note that the new values may still |
| 489 | // be NULL if (e.g.) the resolver failed to return results or the |
| 490 | // results did not contain the necessary data. |
| 491 | // |
| 492 | // First, swap out the data used by cc_get_channel_info(). |
Craig Tiller | 613dafa | 2017-02-09 12:00:43 -0800 | [diff] [blame] | 493 | gpr_mu_lock(&chand->info_mu); |
Yash Tibrewal | 9eb8672 | 2017-09-17 23:43:30 -0700 | [diff] [blame] | 494 | if (lb_policy_name_dup != NULL) { |
Craig Tiller | 613dafa | 2017-02-09 12:00:43 -0800 | [diff] [blame] | 495 | gpr_free(chand->info_lb_policy_name); |
Yash Tibrewal | 9eb8672 | 2017-09-17 23:43:30 -0700 | [diff] [blame] | 496 | chand->info_lb_policy_name = lb_policy_name_dup; |
Mark D. Roth | b2d2488 | 2016-10-27 15:44:07 -0700 | [diff] [blame] | 497 | } |
Mark D. Roth | c625c7a | 2016-11-09 14:12:37 -0800 | [diff] [blame] | 498 | if (service_config_json != NULL) { |
Craig Tiller | 613dafa | 2017-02-09 12:00:43 -0800 | [diff] [blame] | 499 | gpr_free(chand->info_service_config_json); |
| 500 | chand->info_service_config_json = service_config_json; |
Mark D. Roth | c625c7a | 2016-11-09 14:12:37 -0800 | [diff] [blame] | 501 | } |
Craig Tiller | 613dafa | 2017-02-09 12:00:43 -0800 | [diff] [blame] | 502 | gpr_mu_unlock(&chand->info_mu); |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 503 | // Swap out the retry throttle data. |
Mark D. Roth | d6d192d | 2017-02-23 08:58:42 -0800 | [diff] [blame] | 504 | if (chand->retry_throttle_data != NULL) { |
| 505 | grpc_server_retry_throttle_data_unref(chand->retry_throttle_data); |
| 506 | } |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 507 | chand->retry_throttle_data = retry_throttle_data; |
| 508 | // Swap out the method params table. |
Mark D. Roth | 9d48094 | 2016-10-19 14:18:05 -0700 | [diff] [blame] | 509 | if (chand->method_params_table != NULL) { |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 510 | grpc_slice_hash_table_unref(exec_ctx, chand->method_params_table); |
Mark D. Roth | 046cf76 | 2016-09-26 11:13:51 -0700 | [diff] [blame] | 511 | } |
Mark D. Roth | 9d48094 | 2016-10-19 14:18:05 -0700 | [diff] [blame] | 512 | chand->method_params_table = method_params_table; |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 513 | // If we have a new LB policy or are shutting down (in which case |
| 514 | // new_lb_policy will be NULL), swap out the LB policy, unreffing the |
| 515 | // old one and removing its fds from chand->interested_parties. |
| 516 | // Note that we do NOT do this if either (a) we updated the existing |
| 517 | // LB policy above or (b) we failed to create the new LB policy (in |
| 518 | // which case we want to continue using the most recent one we had). |
| 519 | if (new_lb_policy != NULL || error != GRPC_ERROR_NONE || |
| 520 | chand->resolver == NULL) { |
| 521 | if (chand->lb_policy != NULL) { |
Mark D. Roth | 60751fe | 2017-07-07 12:50:33 -0700 | [diff] [blame] | 522 | if (GRPC_TRACER_ON(grpc_client_channel_trace)) { |
| 523 | gpr_log(GPR_DEBUG, "chand=%p: unreffing lb_policy=%p", chand, |
| 524 | chand->lb_policy); |
| 525 | } |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 526 | grpc_pollset_set_del_pollset_set(exec_ctx, |
| 527 | chand->lb_policy->interested_parties, |
| 528 | chand->interested_parties); |
| 529 | GRPC_LB_POLICY_UNREF(exec_ctx, chand->lb_policy, "channel"); |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 530 | } |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 531 | chand->lb_policy = new_lb_policy; |
| 532 | } |
| 533 | // Now that we've swapped out the relevant fields of chand, check for |
| 534 | // error or shutdown. |
| 535 | if (error != GRPC_ERROR_NONE || chand->resolver == NULL) { |
Mark D. Roth | 60751fe | 2017-07-07 12:50:33 -0700 | [diff] [blame] | 536 | if (GRPC_TRACER_ON(grpc_client_channel_trace)) { |
| 537 | gpr_log(GPR_DEBUG, "chand=%p: shutting down", chand); |
| 538 | } |
Craig Tiller | 76a5c0e | 2016-03-09 09:05:30 -0800 | [diff] [blame] | 539 | if (chand->resolver != NULL) { |
Mark D. Roth | 60751fe | 2017-07-07 12:50:33 -0700 | [diff] [blame] | 540 | if (GRPC_TRACER_ON(grpc_client_channel_trace)) { |
| 541 | gpr_log(GPR_DEBUG, "chand=%p: shutting down resolver", chand); |
| 542 | } |
Craig Tiller | 972470b | 2017-02-09 15:05:36 -0800 | [diff] [blame] | 543 | grpc_resolver_shutdown_locked(exec_ctx, chand->resolver); |
Craig Tiller | 76a5c0e | 2016-03-09 09:05:30 -0800 | [diff] [blame] | 544 | GRPC_RESOLVER_UNREF(exec_ctx, chand->resolver, "channel"); |
| 545 | chand->resolver = NULL; |
| 546 | } |
Craig Tiller | 8c0d96f | 2016-03-11 14:27:52 -0800 | [diff] [blame] | 547 | set_channel_connectivity_state_locked( |
Craig Tiller | d925c93 | 2016-06-06 08:38:50 -0700 | [diff] [blame] | 548 | exec_ctx, chand, GRPC_CHANNEL_SHUTDOWN, |
ncteisen | 4b36a3d | 2017-03-13 19:08:06 -0700 | [diff] [blame] | 549 | GRPC_ERROR_CREATE_REFERENCING_FROM_STATIC_STRING( |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 550 | "Got resolver result after disconnection", &error, 1), |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 551 | "resolver_gone"); |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 552 | GRPC_CHANNEL_STACK_UNREF(exec_ctx, chand->owning_stack, "resolver"); |
| 553 | grpc_closure_list_fail_all(&chand->waiting_for_resolver_result_closures, |
| 554 | GRPC_ERROR_CREATE_REFERENCING_FROM_STATIC_STRING( |
| 555 | "Channel disconnected", &error, 1)); |
| 556 | GRPC_CLOSURE_LIST_SCHED(exec_ctx, |
| 557 | &chand->waiting_for_resolver_result_closures); |
| 558 | } else { // Not shutting down. |
| 559 | grpc_connectivity_state state = GRPC_CHANNEL_TRANSIENT_FAILURE; |
| 560 | grpc_error *state_error = |
| 561 | GRPC_ERROR_CREATE_FROM_STATIC_STRING("No load balancing policy"); |
| 562 | if (new_lb_policy != NULL) { |
Mark D. Roth | 60751fe | 2017-07-07 12:50:33 -0700 | [diff] [blame] | 563 | if (GRPC_TRACER_ON(grpc_client_channel_trace)) { |
| 564 | gpr_log(GPR_DEBUG, "chand=%p: initializing new LB policy", chand); |
| 565 | } |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 566 | GRPC_ERROR_UNREF(state_error); |
| 567 | state = grpc_lb_policy_check_connectivity_locked(exec_ctx, new_lb_policy, |
| 568 | &state_error); |
| 569 | grpc_pollset_set_add_pollset_set(exec_ctx, |
| 570 | new_lb_policy->interested_parties, |
| 571 | chand->interested_parties); |
| 572 | GRPC_CLOSURE_LIST_SCHED(exec_ctx, |
| 573 | &chand->waiting_for_resolver_result_closures); |
| 574 | if (chand->exit_idle_when_lb_policy_arrives) { |
| 575 | grpc_lb_policy_exit_idle_locked(exec_ctx, new_lb_policy); |
| 576 | chand->exit_idle_when_lb_policy_arrives = false; |
| 577 | } |
| 578 | watch_lb_policy_locked(exec_ctx, chand, new_lb_policy, state); |
| 579 | } |
Mark D. Roth | 15494b5 | 2017-07-12 15:26:55 -0700 | [diff] [blame] | 580 | if (!lb_policy_updated) { |
| 581 | set_channel_connectivity_state_locked(exec_ctx, chand, state, |
| 582 | GRPC_ERROR_REF(state_error), |
| 583 | "new_lb+resolver"); |
| 584 | } |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 585 | grpc_resolver_next_locked(exec_ctx, chand->resolver, |
| 586 | &chand->resolver_result, |
| 587 | &chand->on_resolver_result_changed); |
| 588 | GRPC_ERROR_UNREF(state_error); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 589 | } |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 590 | } |
| 591 | |
Craig Tiller | a8610c0 | 2017-02-14 10:05:11 -0800 | [diff] [blame] | 592 | static void start_transport_op_locked(grpc_exec_ctx *exec_ctx, void *arg, |
| 593 | grpc_error *error_ignored) { |
Yash Tibrewal | ca3c1c0 | 2017-09-07 22:47:16 -0700 | [diff] [blame] | 594 | grpc_transport_op *op = (grpc_transport_op *)arg; |
| 595 | grpc_channel_element *elem = |
| 596 | (grpc_channel_element *)op->handler_private.extra_arg; |
| 597 | channel_data *chand = (channel_data *)elem->channel_data; |
Craig Tiller | 000cd8f | 2015-09-18 07:20:29 -0700 | [diff] [blame] | 598 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 599 | if (op->on_connectivity_state_change != NULL) { |
| 600 | grpc_connectivity_state_notify_on_state_change( |
| 601 | exec_ctx, &chand->state_tracker, op->connectivity_state, |
| 602 | op->on_connectivity_state_change); |
| 603 | op->on_connectivity_state_change = NULL; |
| 604 | op->connectivity_state = NULL; |
| 605 | } |
| 606 | |
Craig Tiller | 26dab31 | 2015-12-07 14:43:47 -0800 | [diff] [blame] | 607 | if (op->send_ping != NULL) { |
Craig Tiller | 87b71e2 | 2015-12-07 15:14:14 -0800 | [diff] [blame] | 608 | if (chand->lb_policy == NULL) { |
ncteisen | 274bbbe | 2017-06-08 14:57:11 -0700 | [diff] [blame] | 609 | GRPC_CLOSURE_SCHED( |
ncteisen | 4b36a3d | 2017-03-13 19:08:06 -0700 | [diff] [blame] | 610 | exec_ctx, op->send_ping, |
| 611 | GRPC_ERROR_CREATE_FROM_STATIC_STRING("Ping with no load balancing")); |
Craig Tiller | 26dab31 | 2015-12-07 14:43:47 -0800 | [diff] [blame] | 612 | } else { |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 613 | 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] | 614 | op->bind_pollset = NULL; |
| 615 | } |
| 616 | op->send_ping = NULL; |
| 617 | } |
| 618 | |
Craig Tiller | 1c51edc | 2016-05-07 16:18:43 -0700 | [diff] [blame] | 619 | if (op->disconnect_with_error != GRPC_ERROR_NONE) { |
| 620 | if (chand->resolver != NULL) { |
| 621 | set_channel_connectivity_state_locked( |
Craig Tiller | d925c93 | 2016-06-06 08:38:50 -0700 | [diff] [blame] | 622 | exec_ctx, chand, GRPC_CHANNEL_SHUTDOWN, |
Craig Tiller | 1c51edc | 2016-05-07 16:18:43 -0700 | [diff] [blame] | 623 | GRPC_ERROR_REF(op->disconnect_with_error), "disconnect"); |
Craig Tiller | 972470b | 2017-02-09 15:05:36 -0800 | [diff] [blame] | 624 | grpc_resolver_shutdown_locked(exec_ctx, chand->resolver); |
Craig Tiller | 1c51edc | 2016-05-07 16:18:43 -0700 | [diff] [blame] | 625 | GRPC_RESOLVER_UNREF(exec_ctx, chand->resolver, "channel"); |
| 626 | chand->resolver = NULL; |
| 627 | if (!chand->started_resolving) { |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 628 | grpc_closure_list_fail_all(&chand->waiting_for_resolver_result_closures, |
Craig Tiller | 1c51edc | 2016-05-07 16:18:43 -0700 | [diff] [blame] | 629 | GRPC_ERROR_REF(op->disconnect_with_error)); |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 630 | GRPC_CLOSURE_LIST_SCHED(exec_ctx, |
| 631 | &chand->waiting_for_resolver_result_closures); |
Craig Tiller | 1c51edc | 2016-05-07 16:18:43 -0700 | [diff] [blame] | 632 | } |
| 633 | if (chand->lb_policy != NULL) { |
| 634 | grpc_pollset_set_del_pollset_set(exec_ctx, |
| 635 | chand->lb_policy->interested_parties, |
| 636 | chand->interested_parties); |
| 637 | GRPC_LB_POLICY_UNREF(exec_ctx, chand->lb_policy, "channel"); |
| 638 | chand->lb_policy = NULL; |
| 639 | } |
Craig Tiller | b12d22a | 2016-04-23 12:50:21 -0700 | [diff] [blame] | 640 | } |
Craig Tiller | 1c51edc | 2016-05-07 16:18:43 -0700 | [diff] [blame] | 641 | GRPC_ERROR_UNREF(op->disconnect_with_error); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 642 | } |
Craig Tiller | d2e5cfc | 2017-02-09 13:02:20 -0800 | [diff] [blame] | 643 | GRPC_CHANNEL_STACK_UNREF(exec_ctx, chand->owning_stack, "start_transport_op"); |
| 644 | |
ncteisen | 274bbbe | 2017-06-08 14:57:11 -0700 | [diff] [blame] | 645 | GRPC_CLOSURE_SCHED(exec_ctx, op->on_consumed, GRPC_ERROR_NONE); |
Craig Tiller | befafe6 | 2017-02-09 11:30:54 -0800 | [diff] [blame] | 646 | } |
| 647 | |
| 648 | static void cc_start_transport_op(grpc_exec_ctx *exec_ctx, |
| 649 | grpc_channel_element *elem, |
| 650 | grpc_transport_op *op) { |
Yash Tibrewal | ca3c1c0 | 2017-09-07 22:47:16 -0700 | [diff] [blame] | 651 | channel_data *chand = (channel_data *)elem->channel_data; |
Craig Tiller | befafe6 | 2017-02-09 11:30:54 -0800 | [diff] [blame] | 652 | |
Craig Tiller | befafe6 | 2017-02-09 11:30:54 -0800 | [diff] [blame] | 653 | GPR_ASSERT(op->set_accept_stream == false); |
| 654 | if (op->bind_pollset != NULL) { |
| 655 | grpc_pollset_set_add_pollset(exec_ctx, chand->interested_parties, |
| 656 | op->bind_pollset); |
| 657 | } |
| 658 | |
Craig Tiller | c55c102 | 2017-03-10 10:26:42 -0800 | [diff] [blame] | 659 | op->handler_private.extra_arg = elem; |
Craig Tiller | d2e5cfc | 2017-02-09 13:02:20 -0800 | [diff] [blame] | 660 | GRPC_CHANNEL_STACK_REF(chand->owning_stack, "start_transport_op"); |
ncteisen | 274bbbe | 2017-06-08 14:57:11 -0700 | [diff] [blame] | 661 | GRPC_CLOSURE_SCHED( |
Craig Tiller | c55c102 | 2017-03-10 10:26:42 -0800 | [diff] [blame] | 662 | exec_ctx, |
ncteisen | 274bbbe | 2017-06-08 14:57:11 -0700 | [diff] [blame] | 663 | GRPC_CLOSURE_INIT(&op->handler_private.closure, start_transport_op_locked, |
Craig Tiller | ee4b145 | 2017-05-12 10:56:03 -0700 | [diff] [blame] | 664 | op, grpc_combiner_scheduler(chand->combiner)), |
Craig Tiller | befafe6 | 2017-02-09 11:30:54 -0800 | [diff] [blame] | 665 | GRPC_ERROR_NONE); |
Craig Tiller | ca3e9d3 | 2015-06-27 18:37:27 -0700 | [diff] [blame] | 666 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 667 | |
Mark D. Roth | b2d2488 | 2016-10-27 15:44:07 -0700 | [diff] [blame] | 668 | static void cc_get_channel_info(grpc_exec_ctx *exec_ctx, |
| 669 | grpc_channel_element *elem, |
Mark D. Roth | f79ce7d | 2016-11-04 08:43:36 -0700 | [diff] [blame] | 670 | const grpc_channel_info *info) { |
Yash Tibrewal | ca3c1c0 | 2017-09-07 22:47:16 -0700 | [diff] [blame] | 671 | channel_data *chand = (channel_data *)elem->channel_data; |
Craig Tiller | 613dafa | 2017-02-09 12:00:43 -0800 | [diff] [blame] | 672 | gpr_mu_lock(&chand->info_mu); |
Mark D. Roth | b2d2488 | 2016-10-27 15:44:07 -0700 | [diff] [blame] | 673 | if (info->lb_policy_name != NULL) { |
Craig Tiller | 613dafa | 2017-02-09 12:00:43 -0800 | [diff] [blame] | 674 | *info->lb_policy_name = chand->info_lb_policy_name == NULL |
Mark D. Roth | 78afd77 | 2016-11-04 12:49:49 -0700 | [diff] [blame] | 675 | ? NULL |
Craig Tiller | 613dafa | 2017-02-09 12:00:43 -0800 | [diff] [blame] | 676 | : gpr_strdup(chand->info_lb_policy_name); |
Mark D. Roth | b2d2488 | 2016-10-27 15:44:07 -0700 | [diff] [blame] | 677 | } |
Mark D. Roth | c625c7a | 2016-11-09 14:12:37 -0800 | [diff] [blame] | 678 | if (info->service_config_json != NULL) { |
Craig Tiller | 613dafa | 2017-02-09 12:00:43 -0800 | [diff] [blame] | 679 | *info->service_config_json = |
| 680 | chand->info_service_config_json == NULL |
| 681 | ? NULL |
| 682 | : gpr_strdup(chand->info_service_config_json); |
Mark D. Roth | c625c7a | 2016-11-09 14:12:37 -0800 | [diff] [blame] | 683 | } |
Craig Tiller | 613dafa | 2017-02-09 12:00:43 -0800 | [diff] [blame] | 684 | gpr_mu_unlock(&chand->info_mu); |
Mark D. Roth | b2d2488 | 2016-10-27 15:44:07 -0700 | [diff] [blame] | 685 | } |
| 686 | |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 687 | /* Constructor for channel_data */ |
Mark D. Roth | c108788 | 2016-11-18 10:54:45 -0800 | [diff] [blame] | 688 | 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] | 689 | grpc_channel_element *elem, |
| 690 | grpc_channel_element_args *args) { |
Yash Tibrewal | ca3c1c0 | 2017-09-07 22:47:16 -0700 | [diff] [blame] | 691 | channel_data *chand = (channel_data *)elem->channel_data; |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 692 | GPR_ASSERT(args->is_last); |
| 693 | GPR_ASSERT(elem->filter == &grpc_client_channel_filter); |
Mark D. Roth | 21d4b2d | 2016-11-18 09:53:41 -0800 | [diff] [blame] | 694 | // Initialize data members. |
Craig Tiller | ee4b145 | 2017-05-12 10:56:03 -0700 | [diff] [blame] | 695 | chand->combiner = grpc_combiner_create(); |
Craig Tiller | d8547751 | 2017-02-09 12:02:39 -0800 | [diff] [blame] | 696 | gpr_mu_init(&chand->info_mu); |
Alexander Polcyn | c3b1f18 | 2017-04-18 13:51:36 -0700 | [diff] [blame] | 697 | gpr_mu_init(&chand->external_connectivity_watcher_list_mu); |
| 698 | |
| 699 | gpr_mu_lock(&chand->external_connectivity_watcher_list_mu); |
| 700 | chand->external_connectivity_watcher_list_head = NULL; |
| 701 | gpr_mu_unlock(&chand->external_connectivity_watcher_list_mu); |
| 702 | |
Mark D. Roth | 21d4b2d | 2016-11-18 09:53:41 -0800 | [diff] [blame] | 703 | chand->owning_stack = args->channel_stack; |
ncteisen | 274bbbe | 2017-06-08 14:57:11 -0700 | [diff] [blame] | 704 | GRPC_CLOSURE_INIT(&chand->on_resolver_result_changed, |
Craig Tiller | befafe6 | 2017-02-09 11:30:54 -0800 | [diff] [blame] | 705 | on_resolver_result_changed_locked, chand, |
Craig Tiller | ee4b145 | 2017-05-12 10:56:03 -0700 | [diff] [blame] | 706 | grpc_combiner_scheduler(chand->combiner)); |
Mark D. Roth | 21d4b2d | 2016-11-18 09:53:41 -0800 | [diff] [blame] | 707 | chand->interested_parties = grpc_pollset_set_create(); |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 708 | grpc_connectivity_state_init(&chand->state_tracker, GRPC_CHANNEL_IDLE, |
| 709 | "client_channel"); |
Mark D. Roth | 21d4b2d | 2016-11-18 09:53:41 -0800 | [diff] [blame] | 710 | // Record client channel factory. |
| 711 | const grpc_arg *arg = grpc_channel_args_find(args->channel_args, |
| 712 | GRPC_ARG_CLIENT_CHANNEL_FACTORY); |
David Garcia Quintas | 228a514 | 2017-03-30 19:43:00 -0700 | [diff] [blame] | 713 | if (arg == NULL) { |
| 714 | return GRPC_ERROR_CREATE_FROM_STATIC_STRING( |
| 715 | "Missing client channel factory in args for client channel filter"); |
| 716 | } |
| 717 | if (arg->type != GRPC_ARG_POINTER) { |
| 718 | return GRPC_ERROR_CREATE_FROM_STATIC_STRING( |
| 719 | "client channel factory arg must be a pointer"); |
| 720 | } |
Yash Tibrewal | bc130da | 2017-09-12 22:44:08 -0700 | [diff] [blame] | 721 | grpc_client_channel_factory_ref( |
| 722 | (grpc_client_channel_factory *)arg->value.pointer.p); |
Yash Tibrewal | ca3c1c0 | 2017-09-07 22:47:16 -0700 | [diff] [blame] | 723 | chand->client_channel_factory = |
| 724 | (grpc_client_channel_factory *)arg->value.pointer.p; |
Mark D. Roth | dc9bee7 | 2017-02-07 12:29:14 -0800 | [diff] [blame] | 725 | // Get server name to resolve, using proxy mapper if needed. |
Mark D. Roth | 86e9059 | 2016-11-18 09:56:40 -0800 | [diff] [blame] | 726 | arg = grpc_channel_args_find(args->channel_args, GRPC_ARG_SERVER_URI); |
David Garcia Quintas | 228a514 | 2017-03-30 19:43:00 -0700 | [diff] [blame] | 727 | if (arg == NULL) { |
| 728 | return GRPC_ERROR_CREATE_FROM_STATIC_STRING( |
| 729 | "Missing server uri in args for client channel filter"); |
| 730 | } |
| 731 | if (arg->type != GRPC_ARG_STRING) { |
| 732 | return GRPC_ERROR_CREATE_FROM_STATIC_STRING( |
| 733 | "server uri arg must be a string"); |
| 734 | } |
Mark D. Roth | dc9bee7 | 2017-02-07 12:29:14 -0800 | [diff] [blame] | 735 | char *proxy_name = NULL; |
| 736 | grpc_channel_args *new_args = NULL; |
| 737 | grpc_proxy_mappers_map_name(exec_ctx, arg->value.string, args->channel_args, |
| 738 | &proxy_name, &new_args); |
| 739 | // Instantiate resolver. |
Mark D. Roth | 45ccec5 | 2017-01-18 14:04:01 -0800 | [diff] [blame] | 740 | chand->resolver = grpc_resolver_create( |
Mark D. Roth | dc9bee7 | 2017-02-07 12:29:14 -0800 | [diff] [blame] | 741 | exec_ctx, proxy_name != NULL ? proxy_name : arg->value.string, |
| 742 | new_args != NULL ? new_args : args->channel_args, |
Craig Tiller | 972470b | 2017-02-09 15:05:36 -0800 | [diff] [blame] | 743 | chand->interested_parties, chand->combiner); |
Mark D. Roth | dc9bee7 | 2017-02-07 12:29:14 -0800 | [diff] [blame] | 744 | if (proxy_name != NULL) gpr_free(proxy_name); |
| 745 | 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] | 746 | if (chand->resolver == NULL) { |
ncteisen | 4b36a3d | 2017-03-13 19:08:06 -0700 | [diff] [blame] | 747 | return GRPC_ERROR_CREATE_FROM_STATIC_STRING("resolver creation failed"); |
Mark D. Roth | 5e2566e | 2016-11-18 10:53:13 -0800 | [diff] [blame] | 748 | } |
Craig Tiller | 3be7dd0 | 2017-04-03 14:30:03 -0700 | [diff] [blame] | 749 | chand->deadline_checking_enabled = |
| 750 | grpc_deadline_checking_enabled(args->channel_args); |
Mark D. Roth | 5e2566e | 2016-11-18 10:53:13 -0800 | [diff] [blame] | 751 | return GRPC_ERROR_NONE; |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 752 | } |
| 753 | |
Craig Tiller | 972470b | 2017-02-09 15:05:36 -0800 | [diff] [blame] | 754 | static void shutdown_resolver_locked(grpc_exec_ctx *exec_ctx, void *arg, |
| 755 | grpc_error *error) { |
Yash Tibrewal | ca3c1c0 | 2017-09-07 22:47:16 -0700 | [diff] [blame] | 756 | grpc_resolver *resolver = (grpc_resolver *)arg; |
Craig Tiller | 972470b | 2017-02-09 15:05:36 -0800 | [diff] [blame] | 757 | grpc_resolver_shutdown_locked(exec_ctx, resolver); |
| 758 | GRPC_RESOLVER_UNREF(exec_ctx, resolver, "channel"); |
| 759 | } |
| 760 | |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 761 | /* Destructor for channel_data */ |
| 762 | static void cc_destroy_channel_elem(grpc_exec_ctx *exec_ctx, |
| 763 | grpc_channel_element *elem) { |
Yash Tibrewal | ca3c1c0 | 2017-09-07 22:47:16 -0700 | [diff] [blame] | 764 | channel_data *chand = (channel_data *)elem->channel_data; |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 765 | if (chand->resolver != NULL) { |
ncteisen | 274bbbe | 2017-06-08 14:57:11 -0700 | [diff] [blame] | 766 | GRPC_CLOSURE_SCHED( |
| 767 | exec_ctx, GRPC_CLOSURE_CREATE(shutdown_resolver_locked, chand->resolver, |
Craig Tiller | ee4b145 | 2017-05-12 10:56:03 -0700 | [diff] [blame] | 768 | grpc_combiner_scheduler(chand->combiner)), |
Craig Tiller | 972470b | 2017-02-09 15:05:36 -0800 | [diff] [blame] | 769 | GRPC_ERROR_NONE); |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 770 | } |
Mark D. Roth | 0e48a9a | 2016-09-08 14:14:39 -0700 | [diff] [blame] | 771 | if (chand->client_channel_factory != NULL) { |
| 772 | grpc_client_channel_factory_unref(exec_ctx, chand->client_channel_factory); |
| 773 | } |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 774 | if (chand->lb_policy != NULL) { |
| 775 | grpc_pollset_set_del_pollset_set(exec_ctx, |
| 776 | chand->lb_policy->interested_parties, |
| 777 | chand->interested_parties); |
| 778 | GRPC_LB_POLICY_UNREF(exec_ctx, chand->lb_policy, "channel"); |
| 779 | } |
Craig Tiller | 613dafa | 2017-02-09 12:00:43 -0800 | [diff] [blame] | 780 | gpr_free(chand->info_lb_policy_name); |
| 781 | gpr_free(chand->info_service_config_json); |
Mark D. Roth | d6d192d | 2017-02-23 08:58:42 -0800 | [diff] [blame] | 782 | if (chand->retry_throttle_data != NULL) { |
| 783 | grpc_server_retry_throttle_data_unref(chand->retry_throttle_data); |
| 784 | } |
Mark D. Roth | 9d48094 | 2016-10-19 14:18:05 -0700 | [diff] [blame] | 785 | if (chand->method_params_table != NULL) { |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 786 | grpc_slice_hash_table_unref(exec_ctx, chand->method_params_table); |
Mark D. Roth | 9fe284e | 2016-09-12 11:22:27 -0700 | [diff] [blame] | 787 | } |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 788 | grpc_connectivity_state_destroy(exec_ctx, &chand->state_tracker); |
Craig Tiller | 9e5ac1b | 2017-02-14 22:25:50 -0800 | [diff] [blame] | 789 | grpc_pollset_set_destroy(exec_ctx, chand->interested_parties); |
Craig Tiller | f102167 | 2017-02-09 21:29:50 -0800 | [diff] [blame] | 790 | GRPC_COMBINER_UNREF(exec_ctx, chand->combiner, "client_channel"); |
Craig Tiller | d8547751 | 2017-02-09 12:02:39 -0800 | [diff] [blame] | 791 | gpr_mu_destroy(&chand->info_mu); |
Alexander Polcyn | c3b1f18 | 2017-04-18 13:51:36 -0700 | [diff] [blame] | 792 | gpr_mu_destroy(&chand->external_connectivity_watcher_list_mu); |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 793 | } |
| 794 | |
| 795 | /************************************************************************* |
| 796 | * PER-CALL FUNCTIONS |
| 797 | */ |
| 798 | |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 799 | // Max number of batches that can be pending on a call at any given |
| 800 | // time. This includes: |
| 801 | // recv_initial_metadata |
| 802 | // send_initial_metadata |
| 803 | // recv_message |
| 804 | // send_message |
| 805 | // recv_trailing_metadata |
| 806 | // send_trailing_metadata |
Mark D. Roth | 764cf04 | 2017-09-01 09:00:06 -0700 | [diff] [blame] | 807 | // We also add room for a single cancel_stream batch. |
| 808 | #define MAX_WAITING_BATCHES 7 |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 809 | |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 810 | /** Call data. Holds a pointer to grpc_subchannel_call and the |
| 811 | associated machinery to create such a pointer. |
| 812 | Handles queueing of stream ops until a call object is ready, waiting |
| 813 | for initial metadata before trying to create a call object, |
| 814 | and handling cancellation gracefully. */ |
| 815 | typedef struct client_channel_call_data { |
Mark D. Roth | 72f6da8 | 2016-09-02 13:42:38 -0700 | [diff] [blame] | 816 | // State for handling deadlines. |
| 817 | // 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] | 818 | // TODO(roth): This is slightly sub-optimal in that grpc_deadline_state |
Mark D. Roth | 66f3d2b | 2017-09-01 09:02:17 -0700 | [diff] [blame] | 819 | // and this struct both independently store pointers to the call stack |
| 820 | // and call combiner. If/when we have time, find a way to avoid this |
| 821 | // without breaking the grpc_deadline_state abstraction. |
Mark D. Roth | 72f6da8 | 2016-09-02 13:42:38 -0700 | [diff] [blame] | 822 | grpc_deadline_state deadline_state; |
Mark D. Roth | f28763c | 2016-09-14 15:18:40 -0700 | [diff] [blame] | 823 | |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 824 | grpc_slice path; // Request path. |
Mark D. Roth | e40dd29 | 2016-10-05 14:58:37 -0700 | [diff] [blame] | 825 | gpr_timespec call_start_time; |
| 826 | gpr_timespec deadline; |
Mark D. Roth | 764cf04 | 2017-09-01 09:00:06 -0700 | [diff] [blame] | 827 | gpr_arena *arena; |
Mark D. Roth | 66f3d2b | 2017-09-01 09:02:17 -0700 | [diff] [blame] | 828 | grpc_call_stack *owning_call; |
Mark D. Roth | 764cf04 | 2017-09-01 09:00:06 -0700 | [diff] [blame] | 829 | grpc_call_combiner *call_combiner; |
| 830 | |
Mark D. Roth | 9ccbc4d | 2017-03-15 08:30:04 -0700 | [diff] [blame] | 831 | grpc_server_retry_throttle_data *retry_throttle_data; |
Mark D. Roth | 95b627b | 2017-02-24 11:02:58 -0800 | [diff] [blame] | 832 | method_parameters *method_params; |
Mark D. Roth | aa850a7 | 2016-09-26 13:38:02 -0700 | [diff] [blame] | 833 | |
Mark D. Roth | 764cf04 | 2017-09-01 09:00:06 -0700 | [diff] [blame] | 834 | grpc_subchannel_call *subchannel_call; |
| 835 | grpc_error *error; |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 836 | |
Mark D. Roth | 60751fe | 2017-07-07 12:50:33 -0700 | [diff] [blame] | 837 | grpc_lb_policy *lb_policy; // Holds ref while LB pick is pending. |
| 838 | grpc_closure lb_pick_closure; |
Mark D. Roth | 66f3d2b | 2017-09-01 09:02:17 -0700 | [diff] [blame] | 839 | grpc_closure lb_pick_cancel_closure; |
Mark D. Roth | 60751fe | 2017-07-07 12:50:33 -0700 | [diff] [blame] | 840 | |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 841 | grpc_connected_subchannel *connected_subchannel; |
Mark D. Roth | 09e458c | 2017-05-02 08:13:26 -0700 | [diff] [blame] | 842 | grpc_call_context_element subchannel_call_context[GRPC_CONTEXT_COUNT]; |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 843 | grpc_polling_entity *pollent; |
| 844 | |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 845 | grpc_transport_stream_op_batch *waiting_for_pick_batches[MAX_WAITING_BATCHES]; |
| 846 | size_t waiting_for_pick_batches_count; |
Mark D. Roth | 764cf04 | 2017-09-01 09:00:06 -0700 | [diff] [blame] | 847 | grpc_closure handle_pending_batch_in_call_combiner[MAX_WAITING_BATCHES]; |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 848 | |
Mark D. Roth | 764cf04 | 2017-09-01 09:00:06 -0700 | [diff] [blame] | 849 | grpc_transport_stream_op_batch *initial_metadata_batch; |
David Garcia Quintas | d1a47f1 | 2016-09-02 12:46:44 +0200 | [diff] [blame] | 850 | |
| 851 | grpc_linked_mdelem lb_token_mdelem; |
Mark D. Roth | d6d192d | 2017-02-23 08:58:42 -0800 | [diff] [blame] | 852 | |
| 853 | grpc_closure on_complete; |
| 854 | grpc_closure *original_on_complete; |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 855 | } call_data; |
| 856 | |
Mark D. Roth | bf19961 | 2017-08-29 16:59:07 -0700 | [diff] [blame] | 857 | grpc_subchannel_call *grpc_client_channel_get_subchannel_call( |
Mark D. Roth | 764cf04 | 2017-09-01 09:00:06 -0700 | [diff] [blame] | 858 | grpc_call_element *elem) { |
Yash Tibrewal | ca3c1c0 | 2017-09-07 22:47:16 -0700 | [diff] [blame] | 859 | call_data *calld = (call_data *)elem->call_data; |
Mark D. Roth | 764cf04 | 2017-09-01 09:00:06 -0700 | [diff] [blame] | 860 | return calld->subchannel_call; |
Mark D. Roth | 76e264b | 2017-08-25 09:03:33 -0700 | [diff] [blame] | 861 | } |
| 862 | |
Mark D. Roth | 764cf04 | 2017-09-01 09:00:06 -0700 | [diff] [blame] | 863 | // This is called via the call combiner, so access to calld is synchronized. |
| 864 | static void waiting_for_pick_batches_add( |
Mark D. Roth | bf19961 | 2017-08-29 16:59:07 -0700 | [diff] [blame] | 865 | call_data *calld, grpc_transport_stream_op_batch *batch) { |
Mark D. Roth | 764cf04 | 2017-09-01 09:00:06 -0700 | [diff] [blame] | 866 | if (batch->send_initial_metadata) { |
| 867 | GPR_ASSERT(calld->initial_metadata_batch == NULL); |
| 868 | calld->initial_metadata_batch = batch; |
| 869 | } else { |
| 870 | GPR_ASSERT(calld->waiting_for_pick_batches_count < MAX_WAITING_BATCHES); |
| 871 | calld->waiting_for_pick_batches[calld->waiting_for_pick_batches_count++] = |
| 872 | batch; |
| 873 | } |
Mark D. Roth | bf19961 | 2017-08-29 16:59:07 -0700 | [diff] [blame] | 874 | } |
| 875 | |
Mark D. Roth | 764cf04 | 2017-09-01 09:00:06 -0700 | [diff] [blame] | 876 | // This is called via the call combiner, so access to calld is synchronized. |
| 877 | static void fail_pending_batch_in_call_combiner(grpc_exec_ctx *exec_ctx, |
| 878 | void *arg, grpc_error *error) { |
Yash Tibrewal | ca3c1c0 | 2017-09-07 22:47:16 -0700 | [diff] [blame] | 879 | call_data *calld = (call_data *)arg; |
Mark D. Roth | 764cf04 | 2017-09-01 09:00:06 -0700 | [diff] [blame] | 880 | if (calld->waiting_for_pick_batches_count > 0) { |
| 881 | --calld->waiting_for_pick_batches_count; |
| 882 | grpc_transport_stream_op_batch_finish_with_failure( |
| 883 | exec_ctx, |
| 884 | calld->waiting_for_pick_batches[calld->waiting_for_pick_batches_count], |
| 885 | GRPC_ERROR_REF(error), calld->call_combiner); |
| 886 | } |
| 887 | } |
| 888 | |
| 889 | // This is called via the call combiner, so access to calld is synchronized. |
| 890 | static void waiting_for_pick_batches_fail(grpc_exec_ctx *exec_ctx, |
| 891 | grpc_call_element *elem, |
| 892 | grpc_error *error) { |
Yash Tibrewal | ca3c1c0 | 2017-09-07 22:47:16 -0700 | [diff] [blame] | 893 | call_data *calld = (call_data *)elem->call_data; |
Mark D. Roth | 60751fe | 2017-07-07 12:50:33 -0700 | [diff] [blame] | 894 | if (GRPC_TRACER_ON(grpc_client_channel_trace)) { |
| 895 | gpr_log(GPR_DEBUG, |
| 896 | "chand=%p calld=%p: failing %" PRIdPTR " pending batches: %s", |
| 897 | elem->channel_data, calld, calld->waiting_for_pick_batches_count, |
| 898 | grpc_error_string(error)); |
| 899 | } |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 900 | for (size_t i = 0; i < calld->waiting_for_pick_batches_count; ++i) { |
Mark D. Roth | 764cf04 | 2017-09-01 09:00:06 -0700 | [diff] [blame] | 901 | GRPC_CLOSURE_INIT(&calld->handle_pending_batch_in_call_combiner[i], |
| 902 | fail_pending_batch_in_call_combiner, calld, |
| 903 | grpc_schedule_on_exec_ctx); |
| 904 | GRPC_CALL_COMBINER_START(exec_ctx, calld->call_combiner, |
| 905 | &calld->handle_pending_batch_in_call_combiner[i], |
| 906 | GRPC_ERROR_REF(error), |
| 907 | "waiting_for_pick_batches_fail"); |
Mark D. Roth | 76e264b | 2017-08-25 09:03:33 -0700 | [diff] [blame] | 908 | } |
Mark D. Roth | 764cf04 | 2017-09-01 09:00:06 -0700 | [diff] [blame] | 909 | if (calld->initial_metadata_batch != NULL) { |
| 910 | grpc_transport_stream_op_batch_finish_with_failure( |
| 911 | exec_ctx, calld->initial_metadata_batch, GRPC_ERROR_REF(error), |
| 912 | calld->call_combiner); |
| 913 | } else { |
| 914 | GRPC_CALL_COMBINER_STOP(exec_ctx, calld->call_combiner, |
| 915 | "waiting_for_pick_batches_fail"); |
| 916 | } |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 917 | GRPC_ERROR_UNREF(error); |
| 918 | } |
| 919 | |
Mark D. Roth | 764cf04 | 2017-09-01 09:00:06 -0700 | [diff] [blame] | 920 | // This is called via the call combiner, so access to calld is synchronized. |
| 921 | static void run_pending_batch_in_call_combiner(grpc_exec_ctx *exec_ctx, |
| 922 | void *arg, grpc_error *ignored) { |
Yash Tibrewal | ca3c1c0 | 2017-09-07 22:47:16 -0700 | [diff] [blame] | 923 | call_data *calld = (call_data *)arg; |
Mark D. Roth | 764cf04 | 2017-09-01 09:00:06 -0700 | [diff] [blame] | 924 | if (calld->waiting_for_pick_batches_count > 0) { |
| 925 | --calld->waiting_for_pick_batches_count; |
| 926 | grpc_subchannel_call_process_op( |
| 927 | exec_ctx, calld->subchannel_call, |
| 928 | calld->waiting_for_pick_batches[calld->waiting_for_pick_batches_count]); |
Mark D. Roth | bf19961 | 2017-08-29 16:59:07 -0700 | [diff] [blame] | 929 | } |
Mark D. Roth | 764cf04 | 2017-09-01 09:00:06 -0700 | [diff] [blame] | 930 | } |
| 931 | |
| 932 | // This is called via the call combiner, so access to calld is synchronized. |
| 933 | static void waiting_for_pick_batches_resume(grpc_exec_ctx *exec_ctx, |
| 934 | grpc_call_element *elem) { |
Yash Tibrewal | ca3c1c0 | 2017-09-07 22:47:16 -0700 | [diff] [blame] | 935 | channel_data *chand = (channel_data *)elem->channel_data; |
| 936 | call_data *calld = (call_data *)elem->call_data; |
Mark D. Roth | 60751fe | 2017-07-07 12:50:33 -0700 | [diff] [blame] | 937 | if (GRPC_TRACER_ON(grpc_client_channel_trace)) { |
| 938 | gpr_log(GPR_DEBUG, "chand=%p calld=%p: sending %" PRIdPTR |
| 939 | " pending batches to subchannel_call=%p", |
Mark D. Roth | 764cf04 | 2017-09-01 09:00:06 -0700 | [diff] [blame] | 940 | chand, calld, calld->waiting_for_pick_batches_count, |
| 941 | calld->subchannel_call); |
Mark D. Roth | 60751fe | 2017-07-07 12:50:33 -0700 | [diff] [blame] | 942 | } |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 943 | for (size_t i = 0; i < calld->waiting_for_pick_batches_count; ++i) { |
Mark D. Roth | 764cf04 | 2017-09-01 09:00:06 -0700 | [diff] [blame] | 944 | GRPC_CLOSURE_INIT(&calld->handle_pending_batch_in_call_combiner[i], |
| 945 | run_pending_batch_in_call_combiner, calld, |
| 946 | grpc_schedule_on_exec_ctx); |
| 947 | GRPC_CALL_COMBINER_START(exec_ctx, calld->call_combiner, |
| 948 | &calld->handle_pending_batch_in_call_combiner[i], |
| 949 | GRPC_ERROR_NONE, |
| 950 | "waiting_for_pick_batches_resume"); |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 951 | } |
Mark D. Roth | 764cf04 | 2017-09-01 09:00:06 -0700 | [diff] [blame] | 952 | GPR_ASSERT(calld->initial_metadata_batch != NULL); |
| 953 | grpc_subchannel_call_process_op(exec_ctx, calld->subchannel_call, |
| 954 | calld->initial_metadata_batch); |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 955 | } |
| 956 | |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 957 | // Applies service config to the call. Must be invoked once we know |
| 958 | // that the resolver has returned results to the channel. |
| 959 | static void apply_service_config_to_call_locked(grpc_exec_ctx *exec_ctx, |
| 960 | grpc_call_element *elem) { |
Yash Tibrewal | ca3c1c0 | 2017-09-07 22:47:16 -0700 | [diff] [blame] | 961 | channel_data *chand = (channel_data *)elem->channel_data; |
| 962 | call_data *calld = (call_data *)elem->call_data; |
Mark D. Roth | 60751fe | 2017-07-07 12:50:33 -0700 | [diff] [blame] | 963 | if (GRPC_TRACER_ON(grpc_client_channel_trace)) { |
| 964 | gpr_log(GPR_DEBUG, "chand=%p calld=%p: applying service config to call", |
| 965 | chand, calld); |
| 966 | } |
Mark D. Roth | 9ccbc4d | 2017-03-15 08:30:04 -0700 | [diff] [blame] | 967 | if (chand->retry_throttle_data != NULL) { |
| 968 | calld->retry_throttle_data = |
| 969 | grpc_server_retry_throttle_data_ref(chand->retry_throttle_data); |
| 970 | } |
Craig Tiller | 11c17d4 | 2017-03-13 13:36:34 -0700 | [diff] [blame] | 971 | if (chand->method_params_table != NULL) { |
Yash Tibrewal | ca3c1c0 | 2017-09-07 22:47:16 -0700 | [diff] [blame] | 972 | calld->method_params = (method_parameters *)grpc_method_config_table_get( |
Craig Tiller | 11c17d4 | 2017-03-13 13:36:34 -0700 | [diff] [blame] | 973 | exec_ctx, chand->method_params_table, calld->path); |
| 974 | if (calld->method_params != NULL) { |
| 975 | method_parameters_ref(calld->method_params); |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 976 | // If the deadline from the service config is shorter than the one |
| 977 | // from the client API, reset the deadline timer. |
| 978 | if (chand->deadline_checking_enabled && |
| 979 | gpr_time_cmp(calld->method_params->timeout, |
Craig Tiller | 11c17d4 | 2017-03-13 13:36:34 -0700 | [diff] [blame] | 980 | gpr_time_0(GPR_TIMESPAN)) != 0) { |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 981 | const gpr_timespec per_method_deadline = |
Craig Tiller | 11c17d4 | 2017-03-13 13:36:34 -0700 | [diff] [blame] | 982 | gpr_time_add(calld->call_start_time, calld->method_params->timeout); |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 983 | if (gpr_time_cmp(per_method_deadline, calld->deadline) < 0) { |
| 984 | calld->deadline = per_method_deadline; |
| 985 | grpc_deadline_state_reset(exec_ctx, elem, calld->deadline); |
| 986 | } |
Craig Tiller | 11c17d4 | 2017-03-13 13:36:34 -0700 | [diff] [blame] | 987 | } |
| 988 | } |
| 989 | } |
Craig Tiller | 11c17d4 | 2017-03-13 13:36:34 -0700 | [diff] [blame] | 990 | } |
Craig Tiller | ea4a4f1 | 2017-03-13 13:36:52 -0700 | [diff] [blame] | 991 | |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 992 | static void create_subchannel_call_locked(grpc_exec_ctx *exec_ctx, |
Mark D. Roth | 60751fe | 2017-07-07 12:50:33 -0700 | [diff] [blame] | 993 | grpc_call_element *elem, |
| 994 | grpc_error *error) { |
Yash Tibrewal | ca3c1c0 | 2017-09-07 22:47:16 -0700 | [diff] [blame] | 995 | channel_data *chand = (channel_data *)elem->channel_data; |
| 996 | call_data *calld = (call_data *)elem->call_data; |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 997 | const grpc_connected_subchannel_call_args call_args = { |
Yash Tibrewal | d8b84a2 | 2017-09-25 13:38:03 -0700 | [diff] [blame^] | 998 | calld->pollent, // pollent |
| 999 | calld->path, // path |
| 1000 | calld->call_start_time, // start_time |
| 1001 | calld->deadline, // deadline |
| 1002 | calld->arena, // arena |
| 1003 | calld->subchannel_call_context, // context |
| 1004 | calld->call_combiner // call_combiner |
| 1005 | }; |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 1006 | grpc_error *new_error = grpc_connected_subchannel_create_call( |
Mark D. Roth | 764cf04 | 2017-09-01 09:00:06 -0700 | [diff] [blame] | 1007 | exec_ctx, calld->connected_subchannel, &call_args, |
| 1008 | &calld->subchannel_call); |
Mark D. Roth | 60751fe | 2017-07-07 12:50:33 -0700 | [diff] [blame] | 1009 | if (GRPC_TRACER_ON(grpc_client_channel_trace)) { |
| 1010 | gpr_log(GPR_DEBUG, "chand=%p calld=%p: create subchannel_call=%p: error=%s", |
Mark D. Roth | 764cf04 | 2017-09-01 09:00:06 -0700 | [diff] [blame] | 1011 | chand, calld, calld->subchannel_call, grpc_error_string(new_error)); |
Mark D. Roth | 60751fe | 2017-07-07 12:50:33 -0700 | [diff] [blame] | 1012 | } |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 1013 | if (new_error != GRPC_ERROR_NONE) { |
| 1014 | new_error = grpc_error_add_child(new_error, error); |
Mark D. Roth | 764cf04 | 2017-09-01 09:00:06 -0700 | [diff] [blame] | 1015 | waiting_for_pick_batches_fail(exec_ctx, elem, new_error); |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 1016 | } else { |
Mark D. Roth | 764cf04 | 2017-09-01 09:00:06 -0700 | [diff] [blame] | 1017 | waiting_for_pick_batches_resume(exec_ctx, elem); |
Craig Tiller | 11c17d4 | 2017-03-13 13:36:34 -0700 | [diff] [blame] | 1018 | } |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 1019 | GRPC_ERROR_UNREF(error); |
Craig Tiller | 11c17d4 | 2017-03-13 13:36:34 -0700 | [diff] [blame] | 1020 | } |
| 1021 | |
Mark D. Roth | b292960 | 2017-09-11 09:31:11 -0700 | [diff] [blame] | 1022 | // Invoked when a pick is completed, on both success or failure. |
| 1023 | static void pick_done_locked(grpc_exec_ctx *exec_ctx, grpc_call_element *elem, |
| 1024 | grpc_error *error) { |
Yash Tibrewal | ca3c1c0 | 2017-09-07 22:47:16 -0700 | [diff] [blame] | 1025 | call_data *calld = (call_data *)elem->call_data; |
| 1026 | channel_data *chand = (channel_data *)elem->channel_data; |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 1027 | if (calld->connected_subchannel == NULL) { |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 1028 | // Failed to create subchannel. |
Mark D. Roth | 764cf04 | 2017-09-01 09:00:06 -0700 | [diff] [blame] | 1029 | GRPC_ERROR_UNREF(calld->error); |
| 1030 | calld->error = error == GRPC_ERROR_NONE |
| 1031 | ? GRPC_ERROR_CREATE_FROM_STATIC_STRING( |
| 1032 | "Call dropped by load balancing policy") |
| 1033 | : GRPC_ERROR_CREATE_REFERENCING_FROM_STATIC_STRING( |
| 1034 | "Failed to create subchannel", &error, 1); |
Mark D. Roth | 60751fe | 2017-07-07 12:50:33 -0700 | [diff] [blame] | 1035 | if (GRPC_TRACER_ON(grpc_client_channel_trace)) { |
| 1036 | gpr_log(GPR_DEBUG, |
| 1037 | "chand=%p calld=%p: failed to create subchannel: error=%s", chand, |
Mark D. Roth | 764cf04 | 2017-09-01 09:00:06 -0700 | [diff] [blame] | 1038 | calld, grpc_error_string(calld->error)); |
Mark D. Roth | 60751fe | 2017-07-07 12:50:33 -0700 | [diff] [blame] | 1039 | } |
Mark D. Roth | 764cf04 | 2017-09-01 09:00:06 -0700 | [diff] [blame] | 1040 | waiting_for_pick_batches_fail(exec_ctx, elem, GRPC_ERROR_REF(calld->error)); |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 1041 | } else { |
Mark D. Roth | 9fe284e | 2016-09-12 11:22:27 -0700 | [diff] [blame] | 1042 | /* Create call on subchannel. */ |
Mark D. Roth | 60751fe | 2017-07-07 12:50:33 -0700 | [diff] [blame] | 1043 | create_subchannel_call_locked(exec_ctx, elem, GRPC_ERROR_REF(error)); |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 1044 | } |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 1045 | GRPC_ERROR_UNREF(error); |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 1046 | } |
| 1047 | |
Mark D. Roth | b292960 | 2017-09-11 09:31:11 -0700 | [diff] [blame] | 1048 | // A wrapper around pick_done_locked() that is used in cases where |
| 1049 | // either (a) the pick was deferred pending a resolver result or (b) the |
| 1050 | // pick was done asynchronously. Removes the call's polling entity from |
| 1051 | // chand->interested_parties before invoking pick_done_locked(). |
| 1052 | static void async_pick_done_locked(grpc_exec_ctx *exec_ctx, |
| 1053 | grpc_call_element *elem, grpc_error *error) { |
| 1054 | channel_data *chand = (channel_data *)elem->channel_data; |
| 1055 | call_data *calld = (call_data *)elem->call_data; |
| 1056 | grpc_polling_entity_del_from_pollset_set(exec_ctx, calld->pollent, |
| 1057 | chand->interested_parties); |
| 1058 | pick_done_locked(exec_ctx, elem, error); |
| 1059 | } |
| 1060 | |
| 1061 | // Note: This runs under the client_channel combiner, but will NOT be |
| 1062 | // holding the call combiner. |
| 1063 | static void pick_callback_cancel_locked(grpc_exec_ctx *exec_ctx, void *arg, |
| 1064 | grpc_error *error) { |
| 1065 | grpc_call_element *elem = (grpc_call_element *)arg; |
| 1066 | channel_data *chand = (channel_data *)elem->channel_data; |
| 1067 | call_data *calld = (call_data *)elem->call_data; |
| 1068 | if (calld->lb_policy != NULL) { |
| 1069 | if (GRPC_TRACER_ON(grpc_client_channel_trace)) { |
| 1070 | gpr_log(GPR_DEBUG, "chand=%p calld=%p: cancelling pick from LB policy %p", |
| 1071 | chand, calld, calld->lb_policy); |
| 1072 | } |
| 1073 | grpc_lb_policy_cancel_pick_locked(exec_ctx, calld->lb_policy, |
| 1074 | &calld->connected_subchannel, |
| 1075 | GRPC_ERROR_REF(error)); |
| 1076 | } |
| 1077 | GRPC_CALL_STACK_UNREF(exec_ctx, calld->owning_call, "pick_callback_cancel"); |
| 1078 | } |
| 1079 | |
| 1080 | // Callback invoked by grpc_lb_policy_pick_locked() for async picks. |
| 1081 | // Unrefs the LB policy and invokes async_pick_done_locked(). |
| 1082 | static void pick_callback_done_locked(grpc_exec_ctx *exec_ctx, void *arg, |
| 1083 | grpc_error *error) { |
| 1084 | grpc_call_element *elem = (grpc_call_element *)arg; |
| 1085 | channel_data *chand = (channel_data *)elem->channel_data; |
| 1086 | call_data *calld = (call_data *)elem->call_data; |
| 1087 | if (GRPC_TRACER_ON(grpc_client_channel_trace)) { |
| 1088 | gpr_log(GPR_DEBUG, "chand=%p calld=%p: pick completed asynchronously", |
| 1089 | chand, calld); |
| 1090 | } |
| 1091 | GPR_ASSERT(calld->lb_policy != NULL); |
| 1092 | GRPC_LB_POLICY_UNREF(exec_ctx, calld->lb_policy, "pick_subchannel"); |
| 1093 | calld->lb_policy = NULL; |
| 1094 | async_pick_done_locked(exec_ctx, elem, GRPC_ERROR_REF(error)); |
| 1095 | } |
| 1096 | |
| 1097 | // Takes a ref to chand->lb_policy and calls grpc_lb_policy_pick_locked(). |
| 1098 | // If the pick was completed synchronously, unrefs the LB policy and |
| 1099 | // returns true. |
| 1100 | static bool pick_callback_start_locked(grpc_exec_ctx *exec_ctx, |
| 1101 | grpc_call_element *elem) { |
| 1102 | channel_data *chand = (channel_data *)elem->channel_data; |
| 1103 | call_data *calld = (call_data *)elem->call_data; |
| 1104 | if (GRPC_TRACER_ON(grpc_client_channel_trace)) { |
| 1105 | gpr_log(GPR_DEBUG, "chand=%p calld=%p: starting pick on lb_policy=%p", |
| 1106 | chand, calld, chand->lb_policy); |
| 1107 | } |
| 1108 | apply_service_config_to_call_locked(exec_ctx, elem); |
| 1109 | // If the application explicitly set wait_for_ready, use that. |
| 1110 | // Otherwise, if the service config specified a value for this |
| 1111 | // method, use that. |
| 1112 | uint32_t initial_metadata_flags = |
| 1113 | calld->initial_metadata_batch->payload->send_initial_metadata |
| 1114 | .send_initial_metadata_flags; |
| 1115 | const bool wait_for_ready_set_from_api = |
| 1116 | initial_metadata_flags & |
| 1117 | GRPC_INITIAL_METADATA_WAIT_FOR_READY_EXPLICITLY_SET; |
| 1118 | const bool wait_for_ready_set_from_service_config = |
| 1119 | calld->method_params != NULL && |
| 1120 | calld->method_params->wait_for_ready != WAIT_FOR_READY_UNSET; |
| 1121 | if (!wait_for_ready_set_from_api && wait_for_ready_set_from_service_config) { |
| 1122 | if (calld->method_params->wait_for_ready == WAIT_FOR_READY_TRUE) { |
| 1123 | initial_metadata_flags |= GRPC_INITIAL_METADATA_WAIT_FOR_READY; |
| 1124 | } else { |
| 1125 | initial_metadata_flags &= ~GRPC_INITIAL_METADATA_WAIT_FOR_READY; |
| 1126 | } |
| 1127 | } |
| 1128 | const grpc_lb_policy_pick_args inputs = { |
| 1129 | calld->initial_metadata_batch->payload->send_initial_metadata |
| 1130 | .send_initial_metadata, |
| 1131 | initial_metadata_flags, &calld->lb_token_mdelem}; |
| 1132 | // Keep a ref to the LB policy in calld while the pick is pending. |
| 1133 | GRPC_LB_POLICY_REF(chand->lb_policy, "pick_subchannel"); |
| 1134 | calld->lb_policy = chand->lb_policy; |
| 1135 | GRPC_CLOSURE_INIT(&calld->lb_pick_closure, pick_callback_done_locked, elem, |
| 1136 | grpc_combiner_scheduler(chand->combiner)); |
| 1137 | const bool pick_done = grpc_lb_policy_pick_locked( |
| 1138 | exec_ctx, chand->lb_policy, &inputs, &calld->connected_subchannel, |
| 1139 | calld->subchannel_call_context, NULL, &calld->lb_pick_closure); |
| 1140 | if (pick_done) { |
| 1141 | /* synchronous grpc_lb_policy_pick call. Unref the LB policy. */ |
| 1142 | if (GRPC_TRACER_ON(grpc_client_channel_trace)) { |
| 1143 | gpr_log(GPR_DEBUG, "chand=%p calld=%p: pick completed synchronously", |
| 1144 | chand, calld); |
| 1145 | } |
| 1146 | GRPC_LB_POLICY_UNREF(exec_ctx, calld->lb_policy, "pick_subchannel"); |
| 1147 | calld->lb_policy = NULL; |
| 1148 | } else { |
| 1149 | GRPC_CALL_STACK_REF(calld->owning_call, "pick_callback_cancel"); |
| 1150 | grpc_call_combiner_set_notify_on_cancel( |
| 1151 | exec_ctx, calld->call_combiner, |
| 1152 | GRPC_CLOSURE_INIT(&calld->lb_pick_cancel_closure, |
| 1153 | pick_callback_cancel_locked, elem, |
| 1154 | grpc_combiner_scheduler(chand->combiner))); |
| 1155 | } |
| 1156 | return pick_done; |
| 1157 | } |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 1158 | |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 1159 | typedef struct { |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 1160 | grpc_call_element *elem; |
Mark D. Roth | 66f3d2b | 2017-09-01 09:02:17 -0700 | [diff] [blame] | 1161 | bool finished; |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 1162 | grpc_closure closure; |
Mark D. Roth | 66f3d2b | 2017-09-01 09:02:17 -0700 | [diff] [blame] | 1163 | grpc_closure cancel_closure; |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 1164 | } pick_after_resolver_result_args; |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 1165 | |
Mark D. Roth | 764cf04 | 2017-09-01 09:00:06 -0700 | [diff] [blame] | 1166 | // Note: This runs under the client_channel combiner, but will NOT be |
| 1167 | // holding the call combiner. |
| 1168 | static void pick_after_resolver_result_cancel_locked(grpc_exec_ctx *exec_ctx, |
| 1169 | void *arg, |
| 1170 | grpc_error *error) { |
Yash Tibrewal | ca3c1c0 | 2017-09-07 22:47:16 -0700 | [diff] [blame] | 1171 | pick_after_resolver_result_args *args = |
| 1172 | (pick_after_resolver_result_args *)arg; |
Mark D. Roth | 66f3d2b | 2017-09-01 09:02:17 -0700 | [diff] [blame] | 1173 | if (args->finished) { |
| 1174 | gpr_free(args); |
Mark D. Roth | b2b9a0f | 2017-09-01 09:06:47 -0700 | [diff] [blame] | 1175 | return; |
Mark D. Roth | 764cf04 | 2017-09-01 09:00:06 -0700 | [diff] [blame] | 1176 | } |
Mark D. Roth | b2b9a0f | 2017-09-01 09:06:47 -0700 | [diff] [blame] | 1177 | // If we don't yet have a resolver result, then a closure for |
| 1178 | // pick_after_resolver_result_done_locked() will have been added to |
| 1179 | // chand->waiting_for_resolver_result_closures, and it may not be invoked |
| 1180 | // until after this call has been destroyed. We mark the operation as |
| 1181 | // finished, so that when pick_after_resolver_result_done_locked() |
| 1182 | // is called, it will be a no-op. We also immediately invoke |
Mark D. Roth | b292960 | 2017-09-11 09:31:11 -0700 | [diff] [blame] | 1183 | // async_pick_done_locked() to propagate the error back to the caller. |
| 1184 | args->finished = true; |
| 1185 | grpc_call_element *elem = args->elem; |
| 1186 | channel_data *chand = (channel_data *)elem->channel_data; |
| 1187 | call_data *calld = (call_data *)elem->call_data; |
Mark D. Roth | b2b9a0f | 2017-09-01 09:06:47 -0700 | [diff] [blame] | 1188 | if (GRPC_TRACER_ON(grpc_client_channel_trace)) { |
| 1189 | gpr_log(GPR_DEBUG, |
| 1190 | "chand=%p calld=%p: cancelling pick waiting for resolver result", |
| 1191 | chand, calld); |
| 1192 | } |
| 1193 | // Note: Although we are not in the call combiner here, we are |
| 1194 | // basically stealing the call combiner from the pending pick, so |
Mark D. Roth | b292960 | 2017-09-11 09:31:11 -0700 | [diff] [blame] | 1195 | // it's safe to call async_pick_done_locked() here -- we are |
Mark D. Roth | b2b9a0f | 2017-09-01 09:06:47 -0700 | [diff] [blame] | 1196 | // essentially calling it here instead of calling it in |
| 1197 | // pick_after_resolver_result_done_locked(). |
Mark D. Roth | b292960 | 2017-09-11 09:31:11 -0700 | [diff] [blame] | 1198 | async_pick_done_locked(exec_ctx, elem, |
| 1199 | GRPC_ERROR_CREATE_REFERENCING_FROM_STATIC_STRING( |
| 1200 | "Pick cancelled", &error, 1)); |
Mark D. Roth | 764cf04 | 2017-09-01 09:00:06 -0700 | [diff] [blame] | 1201 | } |
| 1202 | |
Mark D. Roth | 60751fe | 2017-07-07 12:50:33 -0700 | [diff] [blame] | 1203 | static void pick_after_resolver_result_done_locked(grpc_exec_ctx *exec_ctx, |
| 1204 | void *arg, |
| 1205 | grpc_error *error) { |
Yash Tibrewal | ca3c1c0 | 2017-09-07 22:47:16 -0700 | [diff] [blame] | 1206 | pick_after_resolver_result_args *args = |
| 1207 | (pick_after_resolver_result_args *)arg; |
Mark D. Roth | 66f3d2b | 2017-09-01 09:02:17 -0700 | [diff] [blame] | 1208 | if (args->finished) { |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 1209 | /* cancelled, do nothing */ |
Mark D. Roth | 60751fe | 2017-07-07 12:50:33 -0700 | [diff] [blame] | 1210 | if (GRPC_TRACER_ON(grpc_client_channel_trace)) { |
| 1211 | gpr_log(GPR_DEBUG, "call cancelled before resolver result"); |
| 1212 | } |
Mark D. Roth | 66f3d2b | 2017-09-01 09:02:17 -0700 | [diff] [blame] | 1213 | gpr_free(args); |
Mark D. Roth | b2b9a0f | 2017-09-01 09:06:47 -0700 | [diff] [blame] | 1214 | return; |
| 1215 | } |
| 1216 | args->finished = true; |
| 1217 | grpc_call_element *elem = args->elem; |
Yash Tibrewal | ca3c1c0 | 2017-09-07 22:47:16 -0700 | [diff] [blame] | 1218 | channel_data *chand = (channel_data *)elem->channel_data; |
| 1219 | call_data *calld = (call_data *)elem->call_data; |
Mark D. Roth | b2b9a0f | 2017-09-01 09:06:47 -0700 | [diff] [blame] | 1220 | if (error != GRPC_ERROR_NONE) { |
| 1221 | if (GRPC_TRACER_ON(grpc_client_channel_trace)) { |
| 1222 | gpr_log(GPR_DEBUG, "chand=%p calld=%p: resolver failed to return data", |
| 1223 | chand, calld); |
| 1224 | } |
Mark D. Roth | b292960 | 2017-09-11 09:31:11 -0700 | [diff] [blame] | 1225 | async_pick_done_locked(exec_ctx, elem, GRPC_ERROR_REF(error)); |
Mark D. Roth | 9dab7d5 | 2016-10-07 07:48:03 -0700 | [diff] [blame] | 1226 | } else { |
Mark D. Roth | b2b9a0f | 2017-09-01 09:06:47 -0700 | [diff] [blame] | 1227 | if (GRPC_TRACER_ON(grpc_client_channel_trace)) { |
| 1228 | gpr_log(GPR_DEBUG, "chand=%p calld=%p: resolver returned, doing pick", |
| 1229 | chand, calld); |
| 1230 | } |
Mark D. Roth | b292960 | 2017-09-11 09:31:11 -0700 | [diff] [blame] | 1231 | if (pick_callback_start_locked(exec_ctx, elem)) { |
| 1232 | // Even if the LB policy returns a result synchronously, we have |
| 1233 | // already added our polling entity to chand->interested_parties |
| 1234 | // in order to wait for the resolver result, so we need to |
| 1235 | // remove it here. Therefore, we call async_pick_done_locked() |
| 1236 | // instead of pick_done_locked(). |
| 1237 | async_pick_done_locked(exec_ctx, elem, GRPC_ERROR_NONE); |
Mark D. Roth | 9dab7d5 | 2016-10-07 07:48:03 -0700 | [diff] [blame] | 1238 | } |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 1239 | } |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 1240 | } |
| 1241 | |
Mark D. Roth | 60751fe | 2017-07-07 12:50:33 -0700 | [diff] [blame] | 1242 | static void pick_after_resolver_result_start_locked(grpc_exec_ctx *exec_ctx, |
| 1243 | grpc_call_element *elem) { |
Yash Tibrewal | ca3c1c0 | 2017-09-07 22:47:16 -0700 | [diff] [blame] | 1244 | channel_data *chand = (channel_data *)elem->channel_data; |
| 1245 | call_data *calld = (call_data *)elem->call_data; |
Mark D. Roth | 60751fe | 2017-07-07 12:50:33 -0700 | [diff] [blame] | 1246 | if (GRPC_TRACER_ON(grpc_client_channel_trace)) { |
| 1247 | gpr_log(GPR_DEBUG, |
| 1248 | "chand=%p calld=%p: deferring pick pending resolver result", chand, |
| 1249 | calld); |
Mark D. Roth | 64a317c | 2017-05-02 08:27:08 -0700 | [diff] [blame] | 1250 | } |
Mark D. Roth | 60751fe | 2017-07-07 12:50:33 -0700 | [diff] [blame] | 1251 | pick_after_resolver_result_args *args = |
| 1252 | (pick_after_resolver_result_args *)gpr_zalloc(sizeof(*args)); |
| 1253 | args->elem = elem; |
| 1254 | GRPC_CLOSURE_INIT(&args->closure, pick_after_resolver_result_done_locked, |
| 1255 | args, grpc_combiner_scheduler(chand->combiner)); |
| 1256 | grpc_closure_list_append(&chand->waiting_for_resolver_result_closures, |
| 1257 | &args->closure, GRPC_ERROR_NONE); |
Mark D. Roth | 764cf04 | 2017-09-01 09:00:06 -0700 | [diff] [blame] | 1258 | grpc_call_combiner_set_notify_on_cancel( |
| 1259 | exec_ctx, calld->call_combiner, |
Mark D. Roth | 66f3d2b | 2017-09-01 09:02:17 -0700 | [diff] [blame] | 1260 | GRPC_CLOSURE_INIT(&args->cancel_closure, |
| 1261 | pick_after_resolver_result_cancel_locked, args, |
Mark D. Roth | 764cf04 | 2017-09-01 09:00:06 -0700 | [diff] [blame] | 1262 | grpc_combiner_scheduler(chand->combiner))); |
Mark D. Roth | 60751fe | 2017-07-07 12:50:33 -0700 | [diff] [blame] | 1263 | } |
| 1264 | |
Mark D. Roth | 764cf04 | 2017-09-01 09:00:06 -0700 | [diff] [blame] | 1265 | static void start_pick_locked(grpc_exec_ctx *exec_ctx, void *arg, |
Mark D. Roth | b292960 | 2017-09-11 09:31:11 -0700 | [diff] [blame] | 1266 | grpc_error *ignored) { |
Mark D. Roth | 764cf04 | 2017-09-01 09:00:06 -0700 | [diff] [blame] | 1267 | grpc_call_element *elem = (grpc_call_element *)arg; |
| 1268 | call_data *calld = (call_data *)elem->call_data; |
| 1269 | channel_data *chand = (channel_data *)elem->channel_data; |
| 1270 | GPR_ASSERT(calld->connected_subchannel == NULL); |
Mark D. Roth | b292960 | 2017-09-11 09:31:11 -0700 | [diff] [blame] | 1271 | if (chand->lb_policy != NULL) { |
| 1272 | // We already have an LB policy, so ask it for a pick. |
| 1273 | if (pick_callback_start_locked(exec_ctx, elem)) { |
| 1274 | // Pick completed synchronously. |
| 1275 | pick_done_locked(exec_ctx, elem, GRPC_ERROR_NONE); |
| 1276 | return; |
Mark D. Roth | bf19961 | 2017-08-29 16:59:07 -0700 | [diff] [blame] | 1277 | } |
Mark D. Roth | 764cf04 | 2017-09-01 09:00:06 -0700 | [diff] [blame] | 1278 | } else { |
Mark D. Roth | b292960 | 2017-09-11 09:31:11 -0700 | [diff] [blame] | 1279 | // We do not yet have an LB policy, so wait for a resolver result. |
| 1280 | if (chand->resolver == NULL) { |
| 1281 | pick_done_locked(exec_ctx, elem, |
| 1282 | GRPC_ERROR_CREATE_FROM_STATIC_STRING("Disconnected")); |
| 1283 | return; |
| 1284 | } |
| 1285 | if (!chand->started_resolving) { |
| 1286 | start_resolving_locked(exec_ctx, chand); |
| 1287 | } |
| 1288 | pick_after_resolver_result_start_locked(exec_ctx, elem); |
Mark D. Roth | bf19961 | 2017-08-29 16:59:07 -0700 | [diff] [blame] | 1289 | } |
Mark D. Roth | b292960 | 2017-09-11 09:31:11 -0700 | [diff] [blame] | 1290 | // We need to wait for either a resolver result or for an async result |
| 1291 | // from the LB policy. Add the polling entity from call_data to the |
| 1292 | // channel_data's interested_parties, so that the I/O of the LB policy |
| 1293 | // and resolver can be done under it. The polling entity will be |
| 1294 | // removed in async_pick_done_locked(). |
| 1295 | grpc_polling_entity_add_to_pollset_set(exec_ctx, calld->pollent, |
| 1296 | chand->interested_parties); |
Craig Tiller | a11bfc8 | 2017-02-14 09:56:33 -0800 | [diff] [blame] | 1297 | } |
| 1298 | |
Mark D. Roth | de14410 | 2017-03-15 10:11:03 -0700 | [diff] [blame] | 1299 | static void on_complete(grpc_exec_ctx *exec_ctx, void *arg, grpc_error *error) { |
Yash Tibrewal | ca3c1c0 | 2017-09-07 22:47:16 -0700 | [diff] [blame] | 1300 | grpc_call_element *elem = (grpc_call_element *)arg; |
| 1301 | call_data *calld = (call_data *)elem->call_data; |
Mark D. Roth | 9ccbc4d | 2017-03-15 08:30:04 -0700 | [diff] [blame] | 1302 | if (calld->retry_throttle_data != NULL) { |
Mark D. Roth | d6d192d | 2017-02-23 08:58:42 -0800 | [diff] [blame] | 1303 | if (error == GRPC_ERROR_NONE) { |
| 1304 | grpc_server_retry_throttle_data_record_success( |
Mark D. Roth | 9ccbc4d | 2017-03-15 08:30:04 -0700 | [diff] [blame] | 1305 | calld->retry_throttle_data); |
Mark D. Roth | d6d192d | 2017-02-23 08:58:42 -0800 | [diff] [blame] | 1306 | } else { |
| 1307 | // 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] | 1308 | // decide whether or not to retry. Note that we should only |
| 1309 | // record failures whose statuses match the configured retryable |
| 1310 | // or non-fatal status codes. |
Mark D. Roth | d6d192d | 2017-02-23 08:58:42 -0800 | [diff] [blame] | 1311 | grpc_server_retry_throttle_data_record_failure( |
Mark D. Roth | 9ccbc4d | 2017-03-15 08:30:04 -0700 | [diff] [blame] | 1312 | calld->retry_throttle_data); |
Mark D. Roth | d6d192d | 2017-02-23 08:58:42 -0800 | [diff] [blame] | 1313 | } |
| 1314 | } |
ncteisen | 274bbbe | 2017-06-08 14:57:11 -0700 | [diff] [blame] | 1315 | GRPC_CLOSURE_RUN(exec_ctx, calld->original_on_complete, |
Mark D. Roth | 95039b5 | 2017-02-24 07:59:45 -0800 | [diff] [blame] | 1316 | GRPC_ERROR_REF(error)); |
Mark D. Roth | d6d192d | 2017-02-23 08:58:42 -0800 | [diff] [blame] | 1317 | } |
| 1318 | |
Craig Tiller | e1b51da | 2017-03-31 15:44:33 -0700 | [diff] [blame] | 1319 | static void cc_start_transport_stream_op_batch( |
| 1320 | grpc_exec_ctx *exec_ctx, grpc_call_element *elem, |
Mark D. Roth | 60751fe | 2017-07-07 12:50:33 -0700 | [diff] [blame] | 1321 | grpc_transport_stream_op_batch *batch) { |
Yash Tibrewal | ca3c1c0 | 2017-09-07 22:47:16 -0700 | [diff] [blame] | 1322 | call_data *calld = (call_data *)elem->call_data; |
| 1323 | channel_data *chand = (channel_data *)elem->channel_data; |
Craig Tiller | 3be7dd0 | 2017-04-03 14:30:03 -0700 | [diff] [blame] | 1324 | if (chand->deadline_checking_enabled) { |
Craig Tiller | 29ebc57 | 2017-04-04 08:00:55 -0700 | [diff] [blame] | 1325 | grpc_deadline_state_client_start_transport_stream_op_batch(exec_ctx, elem, |
Mark D. Roth | 60751fe | 2017-07-07 12:50:33 -0700 | [diff] [blame] | 1326 | batch); |
Craig Tiller | 3be7dd0 | 2017-04-03 14:30:03 -0700 | [diff] [blame] | 1327 | } |
Mark D. Roth | 764cf04 | 2017-09-01 09:00:06 -0700 | [diff] [blame] | 1328 | GPR_TIMER_BEGIN("cc_start_transport_stream_op_batch", 0); |
| 1329 | // If we've previously been cancelled, immediately fail any new batches. |
| 1330 | if (calld->error != GRPC_ERROR_NONE) { |
| 1331 | if (GRPC_TRACER_ON(grpc_client_channel_trace)) { |
| 1332 | gpr_log(GPR_DEBUG, "chand=%p calld=%p: failing batch with error: %s", |
| 1333 | chand, calld, grpc_error_string(calld->error)); |
| 1334 | } |
| 1335 | grpc_transport_stream_op_batch_finish_with_failure( |
| 1336 | exec_ctx, batch, GRPC_ERROR_REF(calld->error), calld->call_combiner); |
| 1337 | goto done; |
| 1338 | } |
| 1339 | if (batch->cancel_stream) { |
| 1340 | // Stash a copy of cancel_error in our call data, so that we can use |
| 1341 | // it for subsequent operations. This ensures that if the call is |
| 1342 | // cancelled before any batches are passed down (e.g., if the deadline |
| 1343 | // is in the past when the call starts), we can return the right |
| 1344 | // error to the caller when the first batch does get passed down. |
| 1345 | GRPC_ERROR_UNREF(calld->error); |
| 1346 | calld->error = GRPC_ERROR_REF(batch->payload->cancel_stream.cancel_error); |
| 1347 | if (GRPC_TRACER_ON(grpc_client_channel_trace)) { |
| 1348 | gpr_log(GPR_DEBUG, "chand=%p calld=%p: recording cancel_error=%s", chand, |
| 1349 | calld, grpc_error_string(calld->error)); |
| 1350 | } |
| 1351 | // If we have a subchannel call, send the cancellation batch down. |
| 1352 | // Otherwise, fail all pending batches. |
| 1353 | if (calld->subchannel_call != NULL) { |
| 1354 | grpc_subchannel_call_process_op(exec_ctx, calld->subchannel_call, batch); |
| 1355 | } else { |
| 1356 | waiting_for_pick_batches_add(calld, batch); |
| 1357 | waiting_for_pick_batches_fail(exec_ctx, elem, |
| 1358 | GRPC_ERROR_REF(calld->error)); |
| 1359 | } |
| 1360 | goto done; |
| 1361 | } |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 1362 | // Intercept on_complete for recv_trailing_metadata so that we can |
| 1363 | // check retry throttle status. |
Mark D. Roth | 60751fe | 2017-07-07 12:50:33 -0700 | [diff] [blame] | 1364 | if (batch->recv_trailing_metadata) { |
| 1365 | GPR_ASSERT(batch->on_complete != NULL); |
| 1366 | calld->original_on_complete = batch->on_complete; |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 1367 | GRPC_CLOSURE_INIT(&calld->on_complete, on_complete, elem, |
| 1368 | grpc_schedule_on_exec_ctx); |
Mark D. Roth | 60751fe | 2017-07-07 12:50:33 -0700 | [diff] [blame] | 1369 | batch->on_complete = &calld->on_complete; |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 1370 | } |
Mark D. Roth | 764cf04 | 2017-09-01 09:00:06 -0700 | [diff] [blame] | 1371 | // Check if we've already gotten a subchannel call. |
| 1372 | // Note that once we have completed the pick, we do not need to enter |
| 1373 | // the channel combiner, which is more efficient (especially for |
| 1374 | // streaming calls). |
| 1375 | if (calld->subchannel_call != NULL) { |
Mark D. Roth | 60751fe | 2017-07-07 12:50:33 -0700 | [diff] [blame] | 1376 | if (GRPC_TRACER_ON(grpc_client_channel_trace)) { |
| 1377 | gpr_log(GPR_DEBUG, |
| 1378 | "chand=%p calld=%p: sending batch to subchannel_call=%p", chand, |
Mark D. Roth | 764cf04 | 2017-09-01 09:00:06 -0700 | [diff] [blame] | 1379 | calld, calld->subchannel_call); |
Mark D. Roth | 60751fe | 2017-07-07 12:50:33 -0700 | [diff] [blame] | 1380 | } |
Mark D. Roth | 764cf04 | 2017-09-01 09:00:06 -0700 | [diff] [blame] | 1381 | grpc_subchannel_call_process_op(exec_ctx, calld->subchannel_call, batch); |
Mark D. Roth | 60751fe | 2017-07-07 12:50:33 -0700 | [diff] [blame] | 1382 | goto done; |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 1383 | } |
Mark D. Roth | 764cf04 | 2017-09-01 09:00:06 -0700 | [diff] [blame] | 1384 | // We do not yet have a subchannel call. |
| 1385 | // Add the batch to the waiting-for-pick list. |
| 1386 | waiting_for_pick_batches_add(calld, batch); |
| 1387 | // For batches containing a send_initial_metadata op, enter the channel |
| 1388 | // combiner to start a pick. |
| 1389 | if (batch->send_initial_metadata) { |
| 1390 | if (GRPC_TRACER_ON(grpc_client_channel_trace)) { |
Mark D. Roth | b292960 | 2017-09-11 09:31:11 -0700 | [diff] [blame] | 1391 | gpr_log(GPR_DEBUG, "chand=%p calld=%p: entering client_channel combiner", |
| 1392 | chand, calld); |
Mark D. Roth | 764cf04 | 2017-09-01 09:00:06 -0700 | [diff] [blame] | 1393 | } |
| 1394 | GRPC_CLOSURE_SCHED( |
| 1395 | exec_ctx, |
| 1396 | GRPC_CLOSURE_INIT(&batch->handler_private.closure, start_pick_locked, |
| 1397 | elem, grpc_combiner_scheduler(chand->combiner)), |
| 1398 | GRPC_ERROR_NONE); |
| 1399 | } else { |
| 1400 | // For all other batches, release the call combiner. |
| 1401 | if (GRPC_TRACER_ON(grpc_client_channel_trace)) { |
| 1402 | gpr_log(GPR_DEBUG, |
| 1403 | "chand=%p calld=%p: saved batch, yeilding call combiner", chand, |
| 1404 | calld); |
| 1405 | } |
| 1406 | GRPC_CALL_COMBINER_STOP(exec_ctx, calld->call_combiner, |
| 1407 | "batch does not include send_initial_metadata"); |
Mark D. Roth | 60751fe | 2017-07-07 12:50:33 -0700 | [diff] [blame] | 1408 | } |
Mark D. Roth | 60751fe | 2017-07-07 12:50:33 -0700 | [diff] [blame] | 1409 | done: |
Craig Tiller | a0f3abd | 2017-03-31 15:42:16 -0700 | [diff] [blame] | 1410 | GPR_TIMER_END("cc_start_transport_stream_op_batch", 0); |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 1411 | } |
| 1412 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1413 | /* Constructor for call_data */ |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 1414 | static grpc_error *cc_init_call_elem(grpc_exec_ctx *exec_ctx, |
| 1415 | grpc_call_element *elem, |
Craig Tiller | c52ba3a | 2017-02-15 22:57:43 -0800 | [diff] [blame] | 1416 | const grpc_call_element_args *args) { |
Yash Tibrewal | ca3c1c0 | 2017-09-07 22:47:16 -0700 | [diff] [blame] | 1417 | call_data *calld = (call_data *)elem->call_data; |
| 1418 | channel_data *chand = (channel_data *)elem->channel_data; |
Mark D. Roth | e40dd29 | 2016-10-05 14:58:37 -0700 | [diff] [blame] | 1419 | // Initialize data members. |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 1420 | calld->path = grpc_slice_ref_internal(args->path); |
Mark D. Roth | ff08f33 | 2016-10-14 13:01:01 -0700 | [diff] [blame] | 1421 | calld->call_start_time = args->start_time; |
Mark D. Roth | e40dd29 | 2016-10-05 14:58:37 -0700 | [diff] [blame] | 1422 | calld->deadline = gpr_convert_clock_type(args->deadline, GPR_CLOCK_MONOTONIC); |
Craig Tiller | d426cac | 2017-03-13 12:30:45 -0700 | [diff] [blame] | 1423 | calld->arena = args->arena; |
Mark D. Roth | 66f3d2b | 2017-09-01 09:02:17 -0700 | [diff] [blame] | 1424 | calld->owning_call = args->call_stack; |
Mark D. Roth | 764cf04 | 2017-09-01 09:00:06 -0700 | [diff] [blame] | 1425 | calld->call_combiner = args->call_combiner; |
Craig Tiller | 3be7dd0 | 2017-04-03 14:30:03 -0700 | [diff] [blame] | 1426 | if (chand->deadline_checking_enabled) { |
Mark D. Roth | 764cf04 | 2017-09-01 09:00:06 -0700 | [diff] [blame] | 1427 | grpc_deadline_state_init(exec_ctx, elem, args->call_stack, |
| 1428 | args->call_combiner, calld->deadline); |
Craig Tiller | 3be7dd0 | 2017-04-03 14:30:03 -0700 | [diff] [blame] | 1429 | } |
Mark D. Roth | 0badbe8 | 2016-06-23 10:15:12 -0700 | [diff] [blame] | 1430 | return GRPC_ERROR_NONE; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1431 | } |
| 1432 | |
| 1433 | /* Destructor for call_data */ |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 1434 | static void cc_destroy_call_elem(grpc_exec_ctx *exec_ctx, |
| 1435 | grpc_call_element *elem, |
| 1436 | const grpc_call_final_info *final_info, |
Craig Tiller | d426cac | 2017-03-13 12:30:45 -0700 | [diff] [blame] | 1437 | grpc_closure *then_schedule_closure) { |
Yash Tibrewal | ca3c1c0 | 2017-09-07 22:47:16 -0700 | [diff] [blame] | 1438 | call_data *calld = (call_data *)elem->call_data; |
| 1439 | channel_data *chand = (channel_data *)elem->channel_data; |
Craig Tiller | 3be7dd0 | 2017-04-03 14:30:03 -0700 | [diff] [blame] | 1440 | if (chand->deadline_checking_enabled) { |
| 1441 | grpc_deadline_state_destroy(exec_ctx, elem); |
| 1442 | } |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 1443 | grpc_slice_unref_internal(exec_ctx, calld->path); |
Mark D. Roth | 95b627b | 2017-02-24 11:02:58 -0800 | [diff] [blame] | 1444 | if (calld->method_params != NULL) { |
| 1445 | method_parameters_unref(calld->method_params); |
| 1446 | } |
Mark D. Roth | 764cf04 | 2017-09-01 09:00:06 -0700 | [diff] [blame] | 1447 | GRPC_ERROR_UNREF(calld->error); |
| 1448 | if (calld->subchannel_call != NULL) { |
| 1449 | grpc_subchannel_call_set_cleanup_closure(calld->subchannel_call, |
Craig Tiller | f7c8c9f | 2017-05-17 15:22:05 -0700 | [diff] [blame] | 1450 | then_schedule_closure); |
Craig Tiller | d426cac | 2017-03-13 12:30:45 -0700 | [diff] [blame] | 1451 | then_schedule_closure = NULL; |
Mark D. Roth | 764cf04 | 2017-09-01 09:00:06 -0700 | [diff] [blame] | 1452 | GRPC_SUBCHANNEL_CALL_UNREF(exec_ctx, calld->subchannel_call, |
Craig Tiller | f7c8c9f | 2017-05-17 15:22:05 -0700 | [diff] [blame] | 1453 | "client_channel_destroy_call"); |
Mark D. Roth | 4c0fe49 | 2016-08-31 13:51:55 -0700 | [diff] [blame] | 1454 | } |
Mark D. Roth | 60751fe | 2017-07-07 12:50:33 -0700 | [diff] [blame] | 1455 | GPR_ASSERT(calld->lb_policy == NULL); |
Mark D. Roth | 0ca0be8 | 2017-06-20 07:49:33 -0700 | [diff] [blame] | 1456 | GPR_ASSERT(calld->waiting_for_pick_batches_count == 0); |
Craig Tiller | 693d394 | 2016-10-27 16:51:25 -0700 | [diff] [blame] | 1457 | if (calld->connected_subchannel != NULL) { |
| 1458 | GRPC_CONNECTED_SUBCHANNEL_UNREF(exec_ctx, calld->connected_subchannel, |
| 1459 | "picked"); |
| 1460 | } |
Mark D. Roth | 09e458c | 2017-05-02 08:13:26 -0700 | [diff] [blame] | 1461 | for (size_t i = 0; i < GRPC_CONTEXT_COUNT; ++i) { |
| 1462 | if (calld->subchannel_call_context[i].value != NULL) { |
| 1463 | calld->subchannel_call_context[i].destroy( |
| 1464 | calld->subchannel_call_context[i].value); |
| 1465 | } |
| 1466 | } |
ncteisen | 274bbbe | 2017-06-08 14:57:11 -0700 | [diff] [blame] | 1467 | GRPC_CLOSURE_SCHED(exec_ctx, then_schedule_closure, GRPC_ERROR_NONE); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1468 | } |
| 1469 | |
David Garcia Quintas | f72eb97 | 2016-05-03 18:28:09 -0700 | [diff] [blame] | 1470 | static void cc_set_pollset_or_pollset_set(grpc_exec_ctx *exec_ctx, |
| 1471 | grpc_call_element *elem, |
David Garcia Quintas | 2a50dfe | 2016-05-31 15:09:12 -0700 | [diff] [blame] | 1472 | grpc_polling_entity *pollent) { |
Yash Tibrewal | ca3c1c0 | 2017-09-07 22:47:16 -0700 | [diff] [blame] | 1473 | call_data *calld = (call_data *)elem->call_data; |
David Garcia Quintas | 2a50dfe | 2016-05-31 15:09:12 -0700 | [diff] [blame] | 1474 | calld->pollent = pollent; |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 1475 | } |
| 1476 | |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 1477 | /************************************************************************* |
| 1478 | * EXPORTED SYMBOLS |
| 1479 | */ |
| 1480 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1481 | const grpc_channel_filter grpc_client_channel_filter = { |
Craig Tiller | a0f3abd | 2017-03-31 15:42:16 -0700 | [diff] [blame] | 1482 | cc_start_transport_stream_op_batch, |
Craig Tiller | f40df23 | 2016-03-25 13:38:14 -0700 | [diff] [blame] | 1483 | cc_start_transport_op, |
| 1484 | sizeof(call_data), |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 1485 | cc_init_call_elem, |
David Garcia Quintas | 4afce7e | 2016-04-18 16:25:17 -0700 | [diff] [blame] | 1486 | cc_set_pollset_or_pollset_set, |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 1487 | cc_destroy_call_elem, |
Craig Tiller | f40df23 | 2016-03-25 13:38:14 -0700 | [diff] [blame] | 1488 | sizeof(channel_data), |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 1489 | cc_init_channel_elem, |
| 1490 | cc_destroy_channel_elem, |
Mark D. Roth | b2d2488 | 2016-10-27 15:44:07 -0700 | [diff] [blame] | 1491 | cc_get_channel_info, |
Craig Tiller | f40df23 | 2016-03-25 13:38:14 -0700 | [diff] [blame] | 1492 | "client-channel", |
Craig Tiller | 87d5b19 | 2015-04-16 14:37:57 -0700 | [diff] [blame] | 1493 | }; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1494 | |
Craig Tiller | 613dafa | 2017-02-09 12:00:43 -0800 | [diff] [blame] | 1495 | static void try_to_connect_locked(grpc_exec_ctx *exec_ctx, void *arg, |
| 1496 | grpc_error *error_ignored) { |
Yash Tibrewal | ca3c1c0 | 2017-09-07 22:47:16 -0700 | [diff] [blame] | 1497 | channel_data *chand = (channel_data *)arg; |
Craig Tiller | 613dafa | 2017-02-09 12:00:43 -0800 | [diff] [blame] | 1498 | if (chand->lb_policy != NULL) { |
Craig Tiller | 2400bf5 | 2017-02-09 16:25:19 -0800 | [diff] [blame] | 1499 | grpc_lb_policy_exit_idle_locked(exec_ctx, chand->lb_policy); |
Craig Tiller | 613dafa | 2017-02-09 12:00:43 -0800 | [diff] [blame] | 1500 | } else { |
| 1501 | chand->exit_idle_when_lb_policy_arrives = true; |
| 1502 | if (!chand->started_resolving && chand->resolver != NULL) { |
Mark D. Roth | 60751fe | 2017-07-07 12:50:33 -0700 | [diff] [blame] | 1503 | start_resolving_locked(exec_ctx, chand); |
Craig Tiller | 613dafa | 2017-02-09 12:00:43 -0800 | [diff] [blame] | 1504 | } |
| 1505 | } |
Craig Tiller | d2e5cfc | 2017-02-09 13:02:20 -0800 | [diff] [blame] | 1506 | GRPC_CHANNEL_STACK_UNREF(exec_ctx, chand->owning_stack, "try_to_connect"); |
Craig Tiller | 613dafa | 2017-02-09 12:00:43 -0800 | [diff] [blame] | 1507 | } |
| 1508 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1509 | grpc_connectivity_state grpc_client_channel_check_connectivity_state( |
| 1510 | grpc_exec_ctx *exec_ctx, grpc_channel_element *elem, int try_to_connect) { |
Yash Tibrewal | ca3c1c0 | 2017-09-07 22:47:16 -0700 | [diff] [blame] | 1511 | channel_data *chand = (channel_data *)elem->channel_data; |
Craig Tiller | a8610c0 | 2017-02-14 10:05:11 -0800 | [diff] [blame] | 1512 | grpc_connectivity_state out = |
| 1513 | grpc_connectivity_state_check(&chand->state_tracker); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1514 | if (out == GRPC_CHANNEL_IDLE && try_to_connect) { |
Craig Tiller | d2e5cfc | 2017-02-09 13:02:20 -0800 | [diff] [blame] | 1515 | GRPC_CHANNEL_STACK_REF(chand->owning_stack, "try_to_connect"); |
ncteisen | 274bbbe | 2017-06-08 14:57:11 -0700 | [diff] [blame] | 1516 | GRPC_CLOSURE_SCHED( |
| 1517 | exec_ctx, GRPC_CLOSURE_CREATE(try_to_connect_locked, chand, |
Craig Tiller | ee4b145 | 2017-05-12 10:56:03 -0700 | [diff] [blame] | 1518 | grpc_combiner_scheduler(chand->combiner)), |
Craig Tiller | 613dafa | 2017-02-09 12:00:43 -0800 | [diff] [blame] | 1519 | GRPC_ERROR_NONE); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1520 | } |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 1521 | return out; |
| 1522 | } |
| 1523 | |
Alexander Polcyn | c3b1f18 | 2017-04-18 13:51:36 -0700 | [diff] [blame] | 1524 | typedef struct external_connectivity_watcher { |
Craig Tiller | 86c9958 | 2015-11-25 15:22:26 -0800 | [diff] [blame] | 1525 | channel_data *chand; |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 1526 | grpc_polling_entity pollent; |
Craig Tiller | 86c9958 | 2015-11-25 15:22:26 -0800 | [diff] [blame] | 1527 | grpc_closure *on_complete; |
Alexander Polcyn | c3b1f18 | 2017-04-18 13:51:36 -0700 | [diff] [blame] | 1528 | grpc_closure *watcher_timer_init; |
Craig Tiller | 613dafa | 2017-02-09 12:00:43 -0800 | [diff] [blame] | 1529 | grpc_connectivity_state *state; |
Craig Tiller | 86c9958 | 2015-11-25 15:22:26 -0800 | [diff] [blame] | 1530 | grpc_closure my_closure; |
Alexander Polcyn | c3b1f18 | 2017-04-18 13:51:36 -0700 | [diff] [blame] | 1531 | struct external_connectivity_watcher *next; |
Craig Tiller | 86c9958 | 2015-11-25 15:22:26 -0800 | [diff] [blame] | 1532 | } external_connectivity_watcher; |
| 1533 | |
Alexander Polcyn | c3b1f18 | 2017-04-18 13:51:36 -0700 | [diff] [blame] | 1534 | static external_connectivity_watcher *lookup_external_connectivity_watcher( |
| 1535 | channel_data *chand, grpc_closure *on_complete) { |
| 1536 | gpr_mu_lock(&chand->external_connectivity_watcher_list_mu); |
| 1537 | external_connectivity_watcher *w = |
| 1538 | chand->external_connectivity_watcher_list_head; |
| 1539 | while (w != NULL && w->on_complete != on_complete) { |
| 1540 | w = w->next; |
| 1541 | } |
| 1542 | gpr_mu_unlock(&chand->external_connectivity_watcher_list_mu); |
| 1543 | return w; |
| 1544 | } |
| 1545 | |
| 1546 | static void external_connectivity_watcher_list_append( |
| 1547 | channel_data *chand, external_connectivity_watcher *w) { |
| 1548 | GPR_ASSERT(!lookup_external_connectivity_watcher(chand, w->on_complete)); |
| 1549 | |
| 1550 | gpr_mu_lock(&w->chand->external_connectivity_watcher_list_mu); |
| 1551 | GPR_ASSERT(!w->next); |
| 1552 | w->next = chand->external_connectivity_watcher_list_head; |
| 1553 | chand->external_connectivity_watcher_list_head = w; |
| 1554 | gpr_mu_unlock(&w->chand->external_connectivity_watcher_list_mu); |
| 1555 | } |
| 1556 | |
| 1557 | static void external_connectivity_watcher_list_remove( |
| 1558 | channel_data *chand, external_connectivity_watcher *too_remove) { |
| 1559 | GPR_ASSERT( |
| 1560 | lookup_external_connectivity_watcher(chand, too_remove->on_complete)); |
| 1561 | gpr_mu_lock(&chand->external_connectivity_watcher_list_mu); |
| 1562 | if (too_remove == chand->external_connectivity_watcher_list_head) { |
| 1563 | chand->external_connectivity_watcher_list_head = too_remove->next; |
| 1564 | gpr_mu_unlock(&chand->external_connectivity_watcher_list_mu); |
| 1565 | return; |
| 1566 | } |
| 1567 | external_connectivity_watcher *w = |
| 1568 | chand->external_connectivity_watcher_list_head; |
| 1569 | while (w != NULL) { |
| 1570 | if (w->next == too_remove) { |
| 1571 | w->next = w->next->next; |
| 1572 | gpr_mu_unlock(&chand->external_connectivity_watcher_list_mu); |
| 1573 | return; |
| 1574 | } |
| 1575 | w = w->next; |
| 1576 | } |
| 1577 | GPR_UNREACHABLE_CODE(return ); |
| 1578 | } |
| 1579 | |
| 1580 | int grpc_client_channel_num_external_connectivity_watchers( |
| 1581 | grpc_channel_element *elem) { |
Yash Tibrewal | ca3c1c0 | 2017-09-07 22:47:16 -0700 | [diff] [blame] | 1582 | channel_data *chand = (channel_data *)elem->channel_data; |
Alexander Polcyn | c3b1f18 | 2017-04-18 13:51:36 -0700 | [diff] [blame] | 1583 | int count = 0; |
| 1584 | |
| 1585 | gpr_mu_lock(&chand->external_connectivity_watcher_list_mu); |
| 1586 | external_connectivity_watcher *w = |
| 1587 | chand->external_connectivity_watcher_list_head; |
| 1588 | while (w != NULL) { |
| 1589 | count++; |
| 1590 | w = w->next; |
| 1591 | } |
| 1592 | gpr_mu_unlock(&chand->external_connectivity_watcher_list_mu); |
| 1593 | |
| 1594 | return count; |
| 1595 | } |
| 1596 | |
Craig Tiller | 1d881fb | 2015-12-01 07:39:04 -0800 | [diff] [blame] | 1597 | 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] | 1598 | grpc_error *error) { |
Yash Tibrewal | ca3c1c0 | 2017-09-07 22:47:16 -0700 | [diff] [blame] | 1599 | external_connectivity_watcher *w = (external_connectivity_watcher *)arg; |
Craig Tiller | 86c9958 | 2015-11-25 15:22:26 -0800 | [diff] [blame] | 1600 | grpc_closure *follow_up = w->on_complete; |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 1601 | grpc_polling_entity_del_from_pollset_set(exec_ctx, &w->pollent, |
| 1602 | w->chand->interested_parties); |
Craig Tiller | 1d881fb | 2015-12-01 07:39:04 -0800 | [diff] [blame] | 1603 | GRPC_CHANNEL_STACK_UNREF(exec_ctx, w->chand->owning_stack, |
| 1604 | "external_connectivity_watcher"); |
Alexander Polcyn | c3b1f18 | 2017-04-18 13:51:36 -0700 | [diff] [blame] | 1605 | external_connectivity_watcher_list_remove(w->chand, w); |
Craig Tiller | 86c9958 | 2015-11-25 15:22:26 -0800 | [diff] [blame] | 1606 | gpr_free(w); |
ncteisen | 274bbbe | 2017-06-08 14:57:11 -0700 | [diff] [blame] | 1607 | GRPC_CLOSURE_RUN(exec_ctx, follow_up, GRPC_ERROR_REF(error)); |
Craig Tiller | 613dafa | 2017-02-09 12:00:43 -0800 | [diff] [blame] | 1608 | } |
| 1609 | |
Craig Tiller | a8610c0 | 2017-02-14 10:05:11 -0800 | [diff] [blame] | 1610 | static void watch_connectivity_state_locked(grpc_exec_ctx *exec_ctx, void *arg, |
| 1611 | grpc_error *error_ignored) { |
Yash Tibrewal | ca3c1c0 | 2017-09-07 22:47:16 -0700 | [diff] [blame] | 1612 | external_connectivity_watcher *w = (external_connectivity_watcher *)arg; |
Alexander Polcyn | c3b1f18 | 2017-04-18 13:51:36 -0700 | [diff] [blame] | 1613 | external_connectivity_watcher *found = NULL; |
| 1614 | if (w->state != NULL) { |
| 1615 | external_connectivity_watcher_list_append(w->chand, w); |
ncteisen | 274bbbe | 2017-06-08 14:57:11 -0700 | [diff] [blame] | 1616 | GRPC_CLOSURE_RUN(exec_ctx, w->watcher_timer_init, GRPC_ERROR_NONE); |
| 1617 | GRPC_CLOSURE_INIT(&w->my_closure, on_external_watch_complete, w, |
Alexander Polcyn | c3b1f18 | 2017-04-18 13:51:36 -0700 | [diff] [blame] | 1618 | grpc_schedule_on_exec_ctx); |
| 1619 | grpc_connectivity_state_notify_on_state_change( |
| 1620 | exec_ctx, &w->chand->state_tracker, w->state, &w->my_closure); |
| 1621 | } else { |
| 1622 | GPR_ASSERT(w->watcher_timer_init == NULL); |
| 1623 | found = lookup_external_connectivity_watcher(w->chand, w->on_complete); |
| 1624 | if (found) { |
| 1625 | GPR_ASSERT(found->on_complete == w->on_complete); |
| 1626 | grpc_connectivity_state_notify_on_state_change( |
| 1627 | exec_ctx, &found->chand->state_tracker, NULL, &found->my_closure); |
| 1628 | } |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 1629 | grpc_polling_entity_del_from_pollset_set(exec_ctx, &w->pollent, |
| 1630 | w->chand->interested_parties); |
Alexander Polcyn | c3b1f18 | 2017-04-18 13:51:36 -0700 | [diff] [blame] | 1631 | GRPC_CHANNEL_STACK_UNREF(exec_ctx, w->chand->owning_stack, |
| 1632 | "external_connectivity_watcher"); |
| 1633 | gpr_free(w); |
| 1634 | } |
Craig Tiller | 86c9958 | 2015-11-25 15:22:26 -0800 | [diff] [blame] | 1635 | } |
| 1636 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 1637 | void grpc_client_channel_watch_connectivity_state( |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 1638 | grpc_exec_ctx *exec_ctx, grpc_channel_element *elem, |
| 1639 | grpc_polling_entity pollent, grpc_connectivity_state *state, |
| 1640 | grpc_closure *closure, grpc_closure *watcher_timer_init) { |
Yash Tibrewal | ca3c1c0 | 2017-09-07 22:47:16 -0700 | [diff] [blame] | 1641 | channel_data *chand = (channel_data *)elem->channel_data; |
| 1642 | external_connectivity_watcher *w = |
| 1643 | (external_connectivity_watcher *)gpr_zalloc(sizeof(*w)); |
Craig Tiller | 86c9958 | 2015-11-25 15:22:26 -0800 | [diff] [blame] | 1644 | w->chand = chand; |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 1645 | w->pollent = pollent; |
Mark D. Roth | 9221083 | 2017-05-02 15:04:39 -0700 | [diff] [blame] | 1646 | w->on_complete = closure; |
Craig Tiller | 613dafa | 2017-02-09 12:00:43 -0800 | [diff] [blame] | 1647 | w->state = state; |
Alexander Polcyn | c3b1f18 | 2017-04-18 13:51:36 -0700 | [diff] [blame] | 1648 | w->watcher_timer_init = watcher_timer_init; |
David Garcia Quintas | 87d5a31 | 2017-06-06 19:45:58 -0700 | [diff] [blame] | 1649 | grpc_polling_entity_add_to_pollset_set(exec_ctx, &w->pollent, |
| 1650 | chand->interested_parties); |
Craig Tiller | 1d881fb | 2015-12-01 07:39:04 -0800 | [diff] [blame] | 1651 | GRPC_CHANNEL_STACK_REF(w->chand->owning_stack, |
| 1652 | "external_connectivity_watcher"); |
ncteisen | 274bbbe | 2017-06-08 14:57:11 -0700 | [diff] [blame] | 1653 | GRPC_CLOSURE_SCHED( |
Craig Tiller | 613dafa | 2017-02-09 12:00:43 -0800 | [diff] [blame] | 1654 | exec_ctx, |
ncteisen | 274bbbe | 2017-06-08 14:57:11 -0700 | [diff] [blame] | 1655 | GRPC_CLOSURE_INIT(&w->my_closure, watch_connectivity_state_locked, w, |
Craig Tiller | ee4b145 | 2017-05-12 10:56:03 -0700 | [diff] [blame] | 1656 | grpc_combiner_scheduler(chand->combiner)), |
Craig Tiller | 613dafa | 2017-02-09 12:00:43 -0800 | [diff] [blame] | 1657 | GRPC_ERROR_NONE); |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 1658 | } |