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