Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 6169d5f | 2016-03-31 07:46:18 -0700 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions are |
| 8 | * met: |
| 9 | * |
| 10 | * * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * * Redistributions in binary form must reproduce the above |
| 13 | * copyright notice, this list of conditions and the following disclaimer |
| 14 | * in the documentation and/or other materials provided with the |
| 15 | * distribution. |
| 16 | * * Neither the name of Google Inc. nor the names of its |
| 17 | * contributors may be used to endorse or promote products derived from |
| 18 | * this software without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | * |
| 32 | */ |
| 33 | |
Craig Tiller | d4c9833 | 2016-03-31 13:45:47 -0700 | [diff] [blame] | 34 | #include "src/core/ext/client_config/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> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 42 | #include <grpc/support/sync.h> |
| 43 | #include <grpc/support/useful.h> |
| 44 | |
Mark D. Roth | 0e48a9a | 2016-09-08 14:14:39 -0700 | [diff] [blame] | 45 | #include "src/core/ext/client_config/lb_policy_registry.h" |
Mark D. Roth | 046cf76 | 2016-09-26 11:13:51 -0700 | [diff] [blame^] | 46 | #include "src/core/ext/client_config/method_config.h" |
Mark D. Roth | 4c0fe49 | 2016-08-31 13:51:55 -0700 | [diff] [blame] | 47 | #include "src/core/ext/client_config/subchannel.h" |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 48 | #include "src/core/lib/channel/channel_args.h" |
| 49 | #include "src/core/lib/channel/connected_channel.h" |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 50 | #include "src/core/lib/iomgr/iomgr.h" |
Mark D. Roth | 4c0fe49 | 2016-08-31 13:51:55 -0700 | [diff] [blame] | 51 | #include "src/core/lib/iomgr/polling_entity.h" |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 52 | #include "src/core/lib/profiling/timers.h" |
| 53 | #include "src/core/lib/support/string.h" |
| 54 | #include "src/core/lib/surface/channel.h" |
| 55 | #include "src/core/lib/transport/connectivity_state.h" |
Mark D. Roth | 9fe284e | 2016-09-12 11:22:27 -0700 | [diff] [blame] | 56 | #include "src/core/lib/transport/metadata.h" |
| 57 | #include "src/core/lib/transport/metadata_batch.h" |
| 58 | #include "src/core/lib/transport/static_metadata.h" |
Craig Tiller | 8910ac6 | 2015-10-08 16:49:15 -0700 | [diff] [blame] | 59 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 60 | /* Client channel implementation */ |
| 61 | |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 62 | /************************************************************************* |
| 63 | * CHANNEL-WIDE FUNCTIONS |
| 64 | */ |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 65 | |
Craig Tiller | 800dacb | 2015-10-06 09:10:26 -0700 | [diff] [blame] | 66 | typedef struct client_channel_channel_data { |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 67 | /** resolver for this channel */ |
| 68 | grpc_resolver *resolver; |
Craig Tiller | 20a3c35 | 2015-08-05 08:39:50 -0700 | [diff] [blame] | 69 | /** have we started resolving this channel */ |
Mark D. Roth | 4c0fe49 | 2016-08-31 13:51:55 -0700 | [diff] [blame] | 70 | bool started_resolving; |
Mark D. Roth | 0e48a9a | 2016-09-08 14:14:39 -0700 | [diff] [blame] | 71 | /** client channel factory */ |
| 72 | grpc_client_channel_factory *client_channel_factory; |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 73 | |
Mark D. Roth | 046cf76 | 2016-09-26 11:13:51 -0700 | [diff] [blame^] | 74 | /** mutex protecting all variables below in this data structure */ |
Mark D. Roth | ff4df06 | 2016-08-22 15:02:49 -0700 | [diff] [blame] | 75 | gpr_mu mu; |
Mark D. Roth | 046cf76 | 2016-09-26 11:13:51 -0700 | [diff] [blame^] | 76 | /** currently active load balancer */ |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 77 | grpc_lb_policy *lb_policy; |
Mark D. Roth | 046cf76 | 2016-09-26 11:13:51 -0700 | [diff] [blame^] | 78 | /** method config table */ |
| 79 | grpc_method_config_table *method_config_table; |
| 80 | /** incoming resolver result - set by resolver.next() */ |
| 81 | grpc_resolver_result *resolver_result; |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 82 | /** 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] | 83 | grpc_closure_list waiting_for_config_closures; |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 84 | /** resolver callback */ |
Mark D. Roth | ff4df06 | 2016-08-22 15:02:49 -0700 | [diff] [blame] | 85 | grpc_closure on_resolver_result_changed; |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 86 | /** connectivity state being tracked */ |
Craig Tiller | ca3e9d3 | 2015-06-27 18:37:27 -0700 | [diff] [blame] | 87 | grpc_connectivity_state_tracker state_tracker; |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 88 | /** when an lb_policy arrives, should we try to exit idle */ |
Mark D. Roth | 4c0fe49 | 2016-08-31 13:51:55 -0700 | [diff] [blame] | 89 | bool exit_idle_when_lb_policy_arrives; |
Craig Tiller | 906e3bc | 2015-11-24 07:31:31 -0800 | [diff] [blame] | 90 | /** owning stack */ |
| 91 | grpc_channel_stack *owning_stack; |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 92 | /** interested parties (owned) */ |
| 93 | grpc_pollset_set *interested_parties; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 94 | } channel_data; |
| 95 | |
Craig Tiller | d6c98df | 2015-08-18 09:33:44 -0700 | [diff] [blame] | 96 | /** 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] | 97 | resolver, to watch for state changes from the lb_policy. When a state |
| 98 | change is seen, we update the channel, and create a new watcher. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 99 | typedef struct { |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 100 | channel_data *chand; |
Craig Tiller | 3382511 | 2015-09-18 07:44:19 -0700 | [diff] [blame] | 101 | grpc_closure on_changed; |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 102 | grpc_connectivity_state state; |
| 103 | grpc_lb_policy *lb_policy; |
| 104 | } lb_policy_connectivity_watcher; |
| 105 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 106 | static void watch_lb_policy(grpc_exec_ctx *exec_ctx, channel_data *chand, |
| 107 | grpc_lb_policy *lb_policy, |
| 108 | grpc_connectivity_state current_state); |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 109 | |
Craig Tiller | 8c0d96f | 2016-03-11 14:27:52 -0800 | [diff] [blame] | 110 | static void set_channel_connectivity_state_locked(grpc_exec_ctx *exec_ctx, |
| 111 | channel_data *chand, |
| 112 | grpc_connectivity_state state, |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 113 | grpc_error *error, |
Craig Tiller | 8c0d96f | 2016-03-11 14:27:52 -0800 | [diff] [blame] | 114 | const char *reason) { |
| 115 | if ((state == GRPC_CHANNEL_TRANSIENT_FAILURE || |
Craig Tiller | 48ed92e | 2016-06-02 11:07:12 -0700 | [diff] [blame] | 116 | state == GRPC_CHANNEL_SHUTDOWN) && |
Craig Tiller | 8c0d96f | 2016-03-11 14:27:52 -0800 | [diff] [blame] | 117 | chand->lb_policy != NULL) { |
| 118 | /* cancel fail-fast picks */ |
| 119 | grpc_lb_policy_cancel_picks( |
| 120 | exec_ctx, chand->lb_policy, |
| 121 | /* mask= */ GRPC_INITIAL_METADATA_IGNORE_CONNECTIVITY, |
| 122 | /* check= */ 0); |
| 123 | } |
Craig Tiller | 9ccf5f1 | 2016-05-07 21:41:01 -0700 | [diff] [blame] | 124 | grpc_connectivity_state_set(exec_ctx, &chand->state_tracker, state, error, |
| 125 | reason); |
Craig Tiller | 8c0d96f | 2016-03-11 14:27:52 -0800 | [diff] [blame] | 126 | } |
| 127 | |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 128 | static void on_lb_policy_state_changed_locked(grpc_exec_ctx *exec_ctx, |
| 129 | lb_policy_connectivity_watcher *w, |
| 130 | grpc_error *error) { |
Craig Tiller | cb2609f | 2015-11-24 17:19:19 -0800 | [diff] [blame] | 131 | grpc_connectivity_state publish_state = w->state; |
Craig Tiller | 5795da7 | 2015-09-17 15:27:13 -0700 | [diff] [blame] | 132 | /* check if the notification is for a stale policy */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 133 | if (w->lb_policy != w->chand->lb_policy) return; |
Craig Tiller | 5795da7 | 2015-09-17 15:27:13 -0700 | [diff] [blame] | 134 | |
Craig Tiller | 48ed92e | 2016-06-02 11:07:12 -0700 | [diff] [blame] | 135 | if (publish_state == GRPC_CHANNEL_SHUTDOWN && w->chand->resolver != NULL) { |
Craig Tiller | cb2609f | 2015-11-24 17:19:19 -0800 | [diff] [blame] | 136 | publish_state = GRPC_CHANNEL_TRANSIENT_FAILURE; |
| 137 | grpc_resolver_channel_saw_error(exec_ctx, w->chand->resolver); |
Craig Tiller | f62c4d5 | 2015-12-04 07:43:07 -0800 | [diff] [blame] | 138 | GRPC_LB_POLICY_UNREF(exec_ctx, w->chand->lb_policy, "channel"); |
| 139 | w->chand->lb_policy = NULL; |
Craig Tiller | cb2609f | 2015-11-24 17:19:19 -0800 | [diff] [blame] | 140 | } |
Craig Tiller | 8c0d96f | 2016-03-11 14:27:52 -0800 | [diff] [blame] | 141 | set_channel_connectivity_state_locked(exec_ctx, w->chand, publish_state, |
Craig Tiller | fc353d6 | 2016-05-10 12:58:03 -0700 | [diff] [blame] | 142 | GRPC_ERROR_REF(error), "lb_changed"); |
Craig Tiller | 48ed92e | 2016-06-02 11:07:12 -0700 | [diff] [blame] | 143 | if (w->state != GRPC_CHANNEL_SHUTDOWN) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 144 | watch_lb_policy(exec_ctx, w->chand, w->lb_policy, w->state); |
| 145 | } |
Craig Tiller | 5795da7 | 2015-09-17 15:27:13 -0700 | [diff] [blame] | 146 | } |
| 147 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 148 | 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] | 149 | grpc_error *error) { |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 150 | lb_policy_connectivity_watcher *w = arg; |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 151 | |
Mark D. Roth | ff4df06 | 2016-08-22 15:02:49 -0700 | [diff] [blame] | 152 | gpr_mu_lock(&w->chand->mu); |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 153 | on_lb_policy_state_changed_locked(exec_ctx, w, error); |
Mark D. Roth | ff4df06 | 2016-08-22 15:02:49 -0700 | [diff] [blame] | 154 | gpr_mu_unlock(&w->chand->mu); |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 155 | |
Craig Tiller | 906e3bc | 2015-11-24 07:31:31 -0800 | [diff] [blame] | 156 | 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] | 157 | gpr_free(w); |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 158 | } |
| 159 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 160 | static void watch_lb_policy(grpc_exec_ctx *exec_ctx, channel_data *chand, |
| 161 | grpc_lb_policy *lb_policy, |
| 162 | grpc_connectivity_state current_state) { |
| 163 | lb_policy_connectivity_watcher *w = gpr_malloc(sizeof(*w)); |
Craig Tiller | 906e3bc | 2015-11-24 07:31:31 -0800 | [diff] [blame] | 164 | GRPC_CHANNEL_STACK_REF(chand->owning_stack, "watch_lb_policy"); |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 165 | |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 166 | w->chand = chand; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 167 | grpc_closure_init(&w->on_changed, on_lb_policy_state_changed, w); |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 168 | w->state = current_state; |
| 169 | w->lb_policy = lb_policy; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 170 | grpc_lb_policy_notify_on_state_change(exec_ctx, lb_policy, &w->state, |
| 171 | &w->on_changed); |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 172 | } |
| 173 | |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 174 | static void on_resolver_result_changed(grpc_exec_ctx *exec_ctx, void *arg, |
| 175 | grpc_error *error) { |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 176 | channel_data *chand = arg; |
| 177 | grpc_lb_policy *lb_policy = NULL; |
| 178 | grpc_lb_policy *old_lb_policy; |
Mark D. Roth | 046cf76 | 2016-09-26 11:13:51 -0700 | [diff] [blame^] | 179 | grpc_method_config_table *method_config_table = NULL; |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 180 | grpc_connectivity_state state = GRPC_CHANNEL_TRANSIENT_FAILURE; |
Mark D. Roth | 4c0fe49 | 2016-08-31 13:51:55 -0700 | [diff] [blame] | 181 | bool exit_idle = false; |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 182 | grpc_error *state_error = GRPC_ERROR_CREATE("No load balancing policy"); |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 183 | |
Mark D. Roth | 046cf76 | 2016-09-26 11:13:51 -0700 | [diff] [blame^] | 184 | if (chand->resolver_result != NULL) { |
Mark D. Roth | 0e48a9a | 2016-09-08 14:14:39 -0700 | [diff] [blame] | 185 | grpc_lb_policy_args lb_policy_args; |
Mark D. Roth | 933c955 | 2016-09-21 08:12:11 -0700 | [diff] [blame] | 186 | lb_policy_args.server_name = |
Mark D. Roth | 046cf76 | 2016-09-26 11:13:51 -0700 | [diff] [blame^] | 187 | grpc_resolver_result_get_server_name(chand->resolver_result); |
Mark D. Roth | 0e48a9a | 2016-09-08 14:14:39 -0700 | [diff] [blame] | 188 | lb_policy_args.addresses = |
Mark D. Roth | 046cf76 | 2016-09-26 11:13:51 -0700 | [diff] [blame^] | 189 | grpc_resolver_result_get_addresses(chand->resolver_result); |
| 190 | lb_policy_args.additional_args = |
| 191 | grpc_resolver_result_get_lb_policy_args(chand->resolver_result); |
Mark D. Roth | 0e48a9a | 2016-09-08 14:14:39 -0700 | [diff] [blame] | 192 | lb_policy_args.client_channel_factory = chand->client_channel_factory; |
| 193 | lb_policy = grpc_lb_policy_create( |
| 194 | exec_ctx, |
Mark D. Roth | 046cf76 | 2016-09-26 11:13:51 -0700 | [diff] [blame^] | 195 | grpc_resolver_result_get_lb_policy_name(chand->resolver_result), |
Mark D. Roth | 0e48a9a | 2016-09-08 14:14:39 -0700 | [diff] [blame] | 196 | &lb_policy_args); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 197 | if (lb_policy != NULL) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 198 | GRPC_LB_POLICY_REF(lb_policy, "config_change"); |
Craig Tiller | f707d62 | 2016-05-06 14:26:12 -0700 | [diff] [blame] | 199 | GRPC_ERROR_UNREF(state_error); |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 200 | state = |
| 201 | grpc_lb_policy_check_connectivity(exec_ctx, lb_policy, &state_error); |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 202 | } |
Mark D. Roth | 046cf76 | 2016-09-26 11:13:51 -0700 | [diff] [blame^] | 203 | const grpc_arg *channel_arg = grpc_channel_args_find( |
| 204 | lb_policy_args.additional_args, GRPC_ARG_SERVICE_CONFIG); |
| 205 | if (channel_arg != NULL) { |
| 206 | GPR_ASSERT(channel_arg->type == GRPC_ARG_POINTER); |
| 207 | method_config_table = grpc_method_config_table_ref( |
| 208 | (grpc_method_config_table *)channel_arg->value.pointer.p); |
Mark D. Roth | 9fe284e | 2016-09-12 11:22:27 -0700 | [diff] [blame] | 209 | } |
Mark D. Roth | 046cf76 | 2016-09-26 11:13:51 -0700 | [diff] [blame^] | 210 | grpc_resolver_result_unref(exec_ctx, chand->resolver_result); |
| 211 | chand->resolver_result = NULL; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 212 | } |
| 213 | |
Craig Tiller | 86c9958 | 2015-11-25 15:22:26 -0800 | [diff] [blame] | 214 | if (lb_policy != NULL) { |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 215 | grpc_pollset_set_add_pollset_set(exec_ctx, lb_policy->interested_parties, |
| 216 | chand->interested_parties); |
Craig Tiller | 86c9958 | 2015-11-25 15:22:26 -0800 | [diff] [blame] | 217 | } |
| 218 | |
Mark D. Roth | ff4df06 | 2016-08-22 15:02:49 -0700 | [diff] [blame] | 219 | gpr_mu_lock(&chand->mu); |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 220 | old_lb_policy = chand->lb_policy; |
| 221 | chand->lb_policy = lb_policy; |
Mark D. Roth | 046cf76 | 2016-09-26 11:13:51 -0700 | [diff] [blame^] | 222 | if (chand->method_config_table != NULL) { |
| 223 | grpc_method_config_table_unref(chand->method_config_table); |
| 224 | } |
| 225 | chand->method_config_table = method_config_table; |
Craig Tiller | 0ede545 | 2016-04-23 12:21:45 -0700 | [diff] [blame] | 226 | if (lb_policy != NULL) { |
| 227 | grpc_exec_ctx_enqueue_list(exec_ctx, &chand->waiting_for_config_closures, |
| 228 | NULL); |
| 229 | } else if (chand->resolver == NULL /* disconnected */) { |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 230 | grpc_closure_list_fail_all( |
| 231 | &chand->waiting_for_config_closures, |
| 232 | GRPC_ERROR_CREATE_REFERENCING("Channel disconnected", &error, 1)); |
Craig Tiller | 6c39686 | 2016-01-28 13:53:40 -0800 | [diff] [blame] | 233 | grpc_exec_ctx_enqueue_list(exec_ctx, &chand->waiting_for_config_closures, |
| 234 | NULL); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 235 | } |
| 236 | if (lb_policy != NULL && chand->exit_idle_when_lb_policy_arrives) { |
| 237 | GRPC_LB_POLICY_REF(lb_policy, "exit_idle"); |
Mark D. Roth | 4c0fe49 | 2016-08-31 13:51:55 -0700 | [diff] [blame] | 238 | exit_idle = true; |
| 239 | chand->exit_idle_when_lb_policy_arrives = false; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 240 | } |
Craig Tiller | 9846503 | 2015-06-29 14:36:42 -0700 | [diff] [blame] | 241 | |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 242 | if (error == GRPC_ERROR_NONE && chand->resolver) { |
Craig Tiller | 9ccf5f1 | 2016-05-07 21:41:01 -0700 | [diff] [blame] | 243 | set_channel_connectivity_state_locked( |
| 244 | exec_ctx, chand, state, GRPC_ERROR_REF(state_error), "new_lb+resolver"); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 245 | if (lb_policy != NULL) { |
| 246 | watch_lb_policy(exec_ctx, chand, lb_policy, state); |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 247 | } |
Craig Tiller | 906e3bc | 2015-11-24 07:31:31 -0800 | [diff] [blame] | 248 | GRPC_CHANNEL_STACK_REF(chand->owning_stack, "resolver"); |
Mark D. Roth | 046cf76 | 2016-09-26 11:13:51 -0700 | [diff] [blame^] | 249 | grpc_resolver_next(exec_ctx, chand->resolver, &chand->resolver_result, |
Mark D. Roth | ff4df06 | 2016-08-22 15:02:49 -0700 | [diff] [blame] | 250 | &chand->on_resolver_result_changed); |
| 251 | gpr_mu_unlock(&chand->mu); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 252 | } else { |
Craig Tiller | 76a5c0e | 2016-03-09 09:05:30 -0800 | [diff] [blame] | 253 | if (chand->resolver != NULL) { |
| 254 | grpc_resolver_shutdown(exec_ctx, chand->resolver); |
| 255 | GRPC_RESOLVER_UNREF(exec_ctx, chand->resolver, "channel"); |
| 256 | chand->resolver = NULL; |
| 257 | } |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 258 | grpc_error *refs[] = {error, state_error}; |
Craig Tiller | 8c0d96f | 2016-03-11 14:27:52 -0800 | [diff] [blame] | 259 | set_channel_connectivity_state_locked( |
Craig Tiller | d925c93 | 2016-06-06 08:38:50 -0700 | [diff] [blame] | 260 | exec_ctx, chand, GRPC_CHANNEL_SHUTDOWN, |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 261 | GRPC_ERROR_CREATE_REFERENCING("Got config after disconnection", refs, |
| 262 | GPR_ARRAY_SIZE(refs)), |
| 263 | "resolver_gone"); |
Mark D. Roth | ff4df06 | 2016-08-22 15:02:49 -0700 | [diff] [blame] | 264 | gpr_mu_unlock(&chand->mu); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 265 | } |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 266 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 267 | if (exit_idle) { |
| 268 | grpc_lb_policy_exit_idle(exec_ctx, lb_policy); |
| 269 | GRPC_LB_POLICY_UNREF(exec_ctx, lb_policy, "exit_idle"); |
| 270 | } |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 271 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 272 | if (old_lb_policy != NULL) { |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 273 | grpc_pollset_set_del_pollset_set( |
| 274 | exec_ctx, old_lb_policy->interested_parties, chand->interested_parties); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 275 | GRPC_LB_POLICY_UNREF(exec_ctx, old_lb_policy, "channel"); |
| 276 | } |
Craig Tiller | 000cd8f | 2015-09-18 07:20:29 -0700 | [diff] [blame] | 277 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 278 | if (lb_policy != NULL) { |
| 279 | GRPC_LB_POLICY_UNREF(exec_ctx, lb_policy, "config_change"); |
| 280 | } |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 281 | |
Craig Tiller | 906e3bc | 2015-11-24 07:31:31 -0800 | [diff] [blame] | 282 | GRPC_CHANNEL_STACK_UNREF(exec_ctx, chand->owning_stack, "resolver"); |
Craig Tiller | 9ccf5f1 | 2016-05-07 21:41:01 -0700 | [diff] [blame] | 283 | GRPC_ERROR_UNREF(state_error); |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 284 | } |
| 285 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 286 | static void cc_start_transport_op(grpc_exec_ctx *exec_ctx, |
| 287 | grpc_channel_element *elem, |
| 288 | grpc_transport_op *op) { |
Craig Tiller | ca3e9d3 | 2015-06-27 18:37:27 -0700 | [diff] [blame] | 289 | channel_data *chand = elem->channel_data; |
Craig Tiller | 000cd8f | 2015-09-18 07:20:29 -0700 | [diff] [blame] | 290 | |
Craig Tiller | 332f1b3 | 2016-05-24 13:21:21 -0700 | [diff] [blame] | 291 | grpc_exec_ctx_sched(exec_ctx, op->on_consumed, GRPC_ERROR_NONE, NULL); |
Craig Tiller | ca3e9d3 | 2015-06-27 18:37:27 -0700 | [diff] [blame] | 292 | |
Craig Tiller | d7f12e3 | 2016-03-03 10:08:31 -0800 | [diff] [blame] | 293 | GPR_ASSERT(op->set_accept_stream == false); |
Craig Tiller | 28bf891 | 2015-12-07 16:07:04 -0800 | [diff] [blame] | 294 | if (op->bind_pollset != NULL) { |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 295 | grpc_pollset_set_add_pollset(exec_ctx, chand->interested_parties, |
Craig Tiller | e2c6237 | 2015-12-07 16:11:03 -0800 | [diff] [blame] | 296 | op->bind_pollset); |
Craig Tiller | 28bf891 | 2015-12-07 16:07:04 -0800 | [diff] [blame] | 297 | } |
Craig Tiller | ca3e9d3 | 2015-06-27 18:37:27 -0700 | [diff] [blame] | 298 | |
Mark D. Roth | ff4df06 | 2016-08-22 15:02:49 -0700 | [diff] [blame] | 299 | gpr_mu_lock(&chand->mu); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 300 | if (op->on_connectivity_state_change != NULL) { |
| 301 | grpc_connectivity_state_notify_on_state_change( |
| 302 | exec_ctx, &chand->state_tracker, op->connectivity_state, |
| 303 | op->on_connectivity_state_change); |
| 304 | op->on_connectivity_state_change = NULL; |
| 305 | op->connectivity_state = NULL; |
| 306 | } |
| 307 | |
Craig Tiller | 26dab31 | 2015-12-07 14:43:47 -0800 | [diff] [blame] | 308 | if (op->send_ping != NULL) { |
Craig Tiller | 87b71e2 | 2015-12-07 15:14:14 -0800 | [diff] [blame] | 309 | if (chand->lb_policy == NULL) { |
Craig Tiller | 332f1b3 | 2016-05-24 13:21:21 -0700 | [diff] [blame] | 310 | grpc_exec_ctx_sched(exec_ctx, op->send_ping, |
| 311 | GRPC_ERROR_CREATE("Ping with no load balancing"), |
| 312 | NULL); |
Craig Tiller | 26dab31 | 2015-12-07 14:43:47 -0800 | [diff] [blame] | 313 | } else { |
Craig Tiller | 28bf891 | 2015-12-07 16:07:04 -0800 | [diff] [blame] | 314 | 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] | 315 | op->bind_pollset = NULL; |
| 316 | } |
| 317 | op->send_ping = NULL; |
| 318 | } |
| 319 | |
Craig Tiller | 1c51edc | 2016-05-07 16:18:43 -0700 | [diff] [blame] | 320 | if (op->disconnect_with_error != GRPC_ERROR_NONE) { |
| 321 | if (chand->resolver != NULL) { |
| 322 | set_channel_connectivity_state_locked( |
Craig Tiller | d925c93 | 2016-06-06 08:38:50 -0700 | [diff] [blame] | 323 | exec_ctx, chand, GRPC_CHANNEL_SHUTDOWN, |
Craig Tiller | 1c51edc | 2016-05-07 16:18:43 -0700 | [diff] [blame] | 324 | GRPC_ERROR_REF(op->disconnect_with_error), "disconnect"); |
| 325 | grpc_resolver_shutdown(exec_ctx, chand->resolver); |
| 326 | GRPC_RESOLVER_UNREF(exec_ctx, chand->resolver, "channel"); |
| 327 | chand->resolver = NULL; |
| 328 | if (!chand->started_resolving) { |
| 329 | grpc_closure_list_fail_all(&chand->waiting_for_config_closures, |
| 330 | GRPC_ERROR_REF(op->disconnect_with_error)); |
Craig Tiller | 9ccf5f1 | 2016-05-07 21:41:01 -0700 | [diff] [blame] | 331 | grpc_exec_ctx_enqueue_list(exec_ctx, |
| 332 | &chand->waiting_for_config_closures, NULL); |
Craig Tiller | 1c51edc | 2016-05-07 16:18:43 -0700 | [diff] [blame] | 333 | } |
| 334 | if (chand->lb_policy != NULL) { |
| 335 | grpc_pollset_set_del_pollset_set(exec_ctx, |
| 336 | chand->lb_policy->interested_parties, |
| 337 | chand->interested_parties); |
| 338 | GRPC_LB_POLICY_UNREF(exec_ctx, chand->lb_policy, "channel"); |
| 339 | chand->lb_policy = NULL; |
| 340 | } |
Craig Tiller | b12d22a | 2016-04-23 12:50:21 -0700 | [diff] [blame] | 341 | } |
Craig Tiller | 1c51edc | 2016-05-07 16:18:43 -0700 | [diff] [blame] | 342 | GRPC_ERROR_UNREF(op->disconnect_with_error); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 343 | } |
Mark D. Roth | ff4df06 | 2016-08-22 15:02:49 -0700 | [diff] [blame] | 344 | gpr_mu_unlock(&chand->mu); |
Craig Tiller | ca3e9d3 | 2015-06-27 18:37:27 -0700 | [diff] [blame] | 345 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 346 | |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 347 | /* Constructor for channel_data */ |
| 348 | static void cc_init_channel_elem(grpc_exec_ctx *exec_ctx, |
| 349 | grpc_channel_element *elem, |
| 350 | grpc_channel_element_args *args) { |
| 351 | channel_data *chand = elem->channel_data; |
| 352 | |
| 353 | memset(chand, 0, sizeof(*chand)); |
| 354 | |
| 355 | GPR_ASSERT(args->is_last); |
| 356 | GPR_ASSERT(elem->filter == &grpc_client_channel_filter); |
| 357 | |
| 358 | gpr_mu_init(&chand->mu); |
| 359 | grpc_closure_init(&chand->on_resolver_result_changed, |
| 360 | on_resolver_result_changed, chand); |
| 361 | chand->owning_stack = args->channel_stack; |
| 362 | |
| 363 | grpc_connectivity_state_init(&chand->state_tracker, GRPC_CHANNEL_IDLE, |
| 364 | "client_channel"); |
| 365 | chand->interested_parties = grpc_pollset_set_create(); |
| 366 | } |
| 367 | |
| 368 | /* Destructor for channel_data */ |
| 369 | static void cc_destroy_channel_elem(grpc_exec_ctx *exec_ctx, |
| 370 | grpc_channel_element *elem) { |
| 371 | channel_data *chand = elem->channel_data; |
| 372 | |
| 373 | if (chand->resolver != NULL) { |
| 374 | grpc_resolver_shutdown(exec_ctx, chand->resolver); |
| 375 | GRPC_RESOLVER_UNREF(exec_ctx, chand->resolver, "channel"); |
| 376 | } |
Mark D. Roth | 0e48a9a | 2016-09-08 14:14:39 -0700 | [diff] [blame] | 377 | if (chand->client_channel_factory != NULL) { |
| 378 | grpc_client_channel_factory_unref(exec_ctx, chand->client_channel_factory); |
| 379 | } |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 380 | if (chand->lb_policy != NULL) { |
| 381 | grpc_pollset_set_del_pollset_set(exec_ctx, |
| 382 | chand->lb_policy->interested_parties, |
| 383 | chand->interested_parties); |
| 384 | GRPC_LB_POLICY_UNREF(exec_ctx, chand->lb_policy, "channel"); |
| 385 | } |
Mark D. Roth | 046cf76 | 2016-09-26 11:13:51 -0700 | [diff] [blame^] | 386 | if (chand->method_config_table != NULL) { |
| 387 | grpc_method_config_table_unref(chand->method_config_table); |
Mark D. Roth | 9fe284e | 2016-09-12 11:22:27 -0700 | [diff] [blame] | 388 | } |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 389 | grpc_connectivity_state_destroy(exec_ctx, &chand->state_tracker); |
| 390 | grpc_pollset_set_destroy(chand->interested_parties); |
| 391 | gpr_mu_destroy(&chand->mu); |
| 392 | } |
| 393 | |
| 394 | /************************************************************************* |
| 395 | * PER-CALL FUNCTIONS |
| 396 | */ |
| 397 | |
| 398 | #define GET_CALL(call_data) \ |
| 399 | ((grpc_subchannel_call *)(gpr_atm_acq_load(&(call_data)->subchannel_call))) |
| 400 | |
| 401 | #define CANCELLED_CALL ((grpc_subchannel_call *)1) |
| 402 | |
| 403 | typedef enum { |
| 404 | GRPC_SUBCHANNEL_CALL_HOLDER_NOT_CREATING, |
| 405 | GRPC_SUBCHANNEL_CALL_HOLDER_PICKING_SUBCHANNEL |
| 406 | } subchannel_creation_phase; |
| 407 | |
| 408 | /** Call data. Holds a pointer to grpc_subchannel_call and the |
| 409 | associated machinery to create such a pointer. |
| 410 | Handles queueing of stream ops until a call object is ready, waiting |
| 411 | for initial metadata before trying to create a call object, |
| 412 | and handling cancellation gracefully. */ |
| 413 | typedef struct client_channel_call_data { |
| 414 | /** either 0 for no call, 1 for cancelled, or a pointer to a |
| 415 | grpc_subchannel_call */ |
| 416 | gpr_atm subchannel_call; |
| 417 | |
| 418 | gpr_mu mu; |
| 419 | |
| 420 | subchannel_creation_phase creation_phase; |
| 421 | grpc_connected_subchannel *connected_subchannel; |
| 422 | grpc_polling_entity *pollent; |
| 423 | |
Mark D. Roth | 9fe284e | 2016-09-12 11:22:27 -0700 | [diff] [blame] | 424 | grpc_mdstr *path; |
| 425 | grpc_method_config *method_config; |
| 426 | |
Craig Tiller | 57726ca | 2016-09-12 11:59:45 -0700 | [diff] [blame] | 427 | grpc_transport_stream_op **waiting_ops; |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 428 | size_t waiting_ops_count; |
| 429 | size_t waiting_ops_capacity; |
| 430 | |
| 431 | grpc_closure next_step; |
| 432 | |
| 433 | grpc_call_stack *owning_call; |
David Garcia Quintas | d1a47f1 | 2016-09-02 12:46:44 +0200 | [diff] [blame] | 434 | |
| 435 | grpc_linked_mdelem lb_token_mdelem; |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 436 | } call_data; |
| 437 | |
| 438 | static void add_waiting_locked(call_data *calld, grpc_transport_stream_op *op) { |
| 439 | GPR_TIMER_BEGIN("add_waiting_locked", 0); |
| 440 | if (calld->waiting_ops_count == calld->waiting_ops_capacity) { |
| 441 | calld->waiting_ops_capacity = GPR_MAX(3, 2 * calld->waiting_ops_capacity); |
| 442 | calld->waiting_ops = |
| 443 | gpr_realloc(calld->waiting_ops, |
| 444 | calld->waiting_ops_capacity * sizeof(*calld->waiting_ops)); |
| 445 | } |
Craig Tiller | 57726ca | 2016-09-12 11:59:45 -0700 | [diff] [blame] | 446 | calld->waiting_ops[calld->waiting_ops_count++] = op; |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 447 | GPR_TIMER_END("add_waiting_locked", 0); |
| 448 | } |
| 449 | |
| 450 | static void fail_locked(grpc_exec_ctx *exec_ctx, call_data *calld, |
| 451 | grpc_error *error) { |
| 452 | size_t i; |
| 453 | for (i = 0; i < calld->waiting_ops_count; i++) { |
| 454 | grpc_transport_stream_op_finish_with_failure( |
Craig Tiller | 57726ca | 2016-09-12 11:59:45 -0700 | [diff] [blame] | 455 | exec_ctx, calld->waiting_ops[i], GRPC_ERROR_REF(error)); |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 456 | } |
| 457 | calld->waiting_ops_count = 0; |
| 458 | GRPC_ERROR_UNREF(error); |
| 459 | } |
| 460 | |
| 461 | typedef struct { |
Craig Tiller | 57726ca | 2016-09-12 11:59:45 -0700 | [diff] [blame] | 462 | grpc_transport_stream_op **ops; |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 463 | size_t nops; |
| 464 | grpc_subchannel_call *call; |
| 465 | } retry_ops_args; |
| 466 | |
| 467 | static void retry_ops(grpc_exec_ctx *exec_ctx, void *args, grpc_error *error) { |
| 468 | retry_ops_args *a = args; |
| 469 | size_t i; |
| 470 | for (i = 0; i < a->nops; i++) { |
Craig Tiller | 57726ca | 2016-09-12 11:59:45 -0700 | [diff] [blame] | 471 | 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] | 472 | } |
| 473 | GRPC_SUBCHANNEL_CALL_UNREF(exec_ctx, a->call, "retry_ops"); |
| 474 | gpr_free(a->ops); |
| 475 | gpr_free(a); |
| 476 | } |
| 477 | |
| 478 | 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] | 479 | if (calld->waiting_ops_count == 0) { |
| 480 | return; |
| 481 | } |
| 482 | |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 483 | retry_ops_args *a = gpr_malloc(sizeof(*a)); |
| 484 | a->ops = calld->waiting_ops; |
| 485 | a->nops = calld->waiting_ops_count; |
| 486 | a->call = GET_CALL(calld); |
| 487 | if (a->call == CANCELLED_CALL) { |
| 488 | gpr_free(a); |
| 489 | fail_locked(exec_ctx, calld, GRPC_ERROR_CANCELLED); |
| 490 | return; |
| 491 | } |
| 492 | calld->waiting_ops = NULL; |
| 493 | calld->waiting_ops_count = 0; |
| 494 | calld->waiting_ops_capacity = 0; |
| 495 | GRPC_SUBCHANNEL_CALL_REF(a->call, "retry_ops"); |
| 496 | grpc_exec_ctx_sched(exec_ctx, grpc_closure_create(retry_ops, a), |
| 497 | GRPC_ERROR_NONE, NULL); |
| 498 | } |
| 499 | |
| 500 | static void subchannel_ready(grpc_exec_ctx *exec_ctx, void *arg, |
| 501 | grpc_error *error) { |
Mark D. Roth | 9fe284e | 2016-09-12 11:22:27 -0700 | [diff] [blame] | 502 | grpc_call_element *elem = arg; |
| 503 | call_data *calld = elem->call_data; |
| 504 | channel_data *chand = elem->channel_data; |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 505 | gpr_mu_lock(&calld->mu); |
| 506 | GPR_ASSERT(calld->creation_phase == |
| 507 | GRPC_SUBCHANNEL_CALL_HOLDER_PICKING_SUBCHANNEL); |
| 508 | calld->creation_phase = GRPC_SUBCHANNEL_CALL_HOLDER_NOT_CREATING; |
| 509 | if (calld->connected_subchannel == NULL) { |
| 510 | gpr_atm_no_barrier_store(&calld->subchannel_call, 1); |
| 511 | fail_locked(exec_ctx, calld, GRPC_ERROR_CREATE_REFERENCING( |
| 512 | "Failed to create subchannel", &error, 1)); |
| 513 | } else if (1 == gpr_atm_acq_load(&calld->subchannel_call)) { |
| 514 | /* already cancelled before subchannel became ready */ |
| 515 | fail_locked(exec_ctx, calld, |
| 516 | GRPC_ERROR_CREATE_REFERENCING( |
| 517 | "Cancelled before creating subchannel", &error, 1)); |
| 518 | } else { |
Mark D. Roth | 9fe284e | 2016-09-12 11:22:27 -0700 | [diff] [blame] | 519 | /* Get method config. */ |
| 520 | // FIXME: need to actually use the config data! |
Mark D. Roth | 4c3a468 | 2016-09-23 15:19:26 -0700 | [diff] [blame] | 521 | // FIXME: think about refcounting vs. atomicity here |
Mark D. Roth | 046cf76 | 2016-09-26 11:13:51 -0700 | [diff] [blame^] | 522 | if (chand->method_config_table != NULL) { |
Mark D. Roth | 4c3a468 | 2016-09-23 15:19:26 -0700 | [diff] [blame] | 523 | calld->method_config = grpc_method_config_table_get_method_config( |
Mark D. Roth | 046cf76 | 2016-09-26 11:13:51 -0700 | [diff] [blame^] | 524 | chand->method_config_table, calld->path); |
Mark D. Roth | 4c3a468 | 2016-09-23 15:19:26 -0700 | [diff] [blame] | 525 | } |
Mark D. Roth | 9fe284e | 2016-09-12 11:22:27 -0700 | [diff] [blame] | 526 | /* Create call on subchannel. */ |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 527 | grpc_subchannel_call *subchannel_call = NULL; |
| 528 | grpc_error *new_error = grpc_connected_subchannel_create_call( |
| 529 | exec_ctx, calld->connected_subchannel, calld->pollent, |
| 530 | &subchannel_call); |
| 531 | if (new_error != GRPC_ERROR_NONE) { |
| 532 | new_error = grpc_error_add_child(new_error, error); |
| 533 | subchannel_call = CANCELLED_CALL; |
| 534 | fail_locked(exec_ctx, calld, new_error); |
| 535 | } |
| 536 | gpr_atm_rel_store(&calld->subchannel_call, |
| 537 | (gpr_atm)(uintptr_t)subchannel_call); |
| 538 | retry_waiting_locked(exec_ctx, calld); |
| 539 | } |
| 540 | gpr_mu_unlock(&calld->mu); |
| 541 | GRPC_CALL_STACK_UNREF(exec_ctx, calld->owning_call, "pick_subchannel"); |
| 542 | } |
| 543 | |
| 544 | static char *cc_get_peer(grpc_exec_ctx *exec_ctx, grpc_call_element *elem) { |
| 545 | call_data *calld = elem->call_data; |
| 546 | grpc_subchannel_call *subchannel_call = GET_CALL(calld); |
| 547 | if (subchannel_call == NULL || subchannel_call == CANCELLED_CALL) { |
| 548 | return NULL; |
| 549 | } else { |
| 550 | return grpc_subchannel_call_get_peer(exec_ctx, subchannel_call); |
| 551 | } |
| 552 | } |
| 553 | |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 554 | typedef struct { |
| 555 | grpc_metadata_batch *initial_metadata; |
Craig Tiller | 8c0d96f | 2016-03-11 14:27:52 -0800 | [diff] [blame] | 556 | uint32_t initial_metadata_flags; |
Craig Tiller | b5585d4 | 2015-11-17 07:18:31 -0800 | [diff] [blame] | 557 | grpc_connected_subchannel **connected_subchannel; |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 558 | grpc_closure *on_ready; |
| 559 | grpc_call_element *elem; |
| 560 | grpc_closure closure; |
| 561 | } continue_picking_args; |
| 562 | |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 563 | static bool pick_subchannel(grpc_exec_ctx *exec_ctx, grpc_call_element *elem, |
| 564 | grpc_metadata_batch *initial_metadata, |
| 565 | uint32_t initial_metadata_flags, |
| 566 | grpc_connected_subchannel **connected_subchannel, |
| 567 | grpc_closure *on_ready); |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 568 | |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 569 | static void continue_picking(grpc_exec_ctx *exec_ctx, void *arg, |
| 570 | grpc_error *error) { |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 571 | continue_picking_args *cpa = arg; |
Craig Tiller | 0ede545 | 2016-04-23 12:21:45 -0700 | [diff] [blame] | 572 | if (cpa->connected_subchannel == NULL) { |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 573 | /* cancelled, do nothing */ |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 574 | } else if (error != GRPC_ERROR_NONE) { |
Craig Tiller | 332f1b3 | 2016-05-24 13:21:21 -0700 | [diff] [blame] | 575 | grpc_exec_ctx_sched(exec_ctx, cpa->on_ready, GRPC_ERROR_REF(error), NULL); |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 576 | } else if (pick_subchannel(exec_ctx, cpa->elem, cpa->initial_metadata, |
| 577 | cpa->initial_metadata_flags, |
| 578 | cpa->connected_subchannel, cpa->on_ready)) { |
Craig Tiller | 332f1b3 | 2016-05-24 13:21:21 -0700 | [diff] [blame] | 579 | grpc_exec_ctx_sched(exec_ctx, cpa->on_ready, GRPC_ERROR_NONE, NULL); |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 580 | } |
| 581 | gpr_free(cpa); |
| 582 | } |
| 583 | |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 584 | static bool pick_subchannel(grpc_exec_ctx *exec_ctx, grpc_call_element *elem, |
| 585 | grpc_metadata_batch *initial_metadata, |
| 586 | uint32_t initial_metadata_flags, |
| 587 | grpc_connected_subchannel **connected_subchannel, |
| 588 | grpc_closure *on_ready) { |
| 589 | GPR_TIMER_BEGIN("pick_subchannel", 0); |
Craig Tiller | bfc9adc | 2016-06-27 13:16:22 -0700 | [diff] [blame] | 590 | |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 591 | channel_data *chand = elem->channel_data; |
| 592 | call_data *calld = elem->call_data; |
| 593 | continue_picking_args *cpa; |
| 594 | grpc_closure *closure; |
| 595 | |
Craig Tiller | b5585d4 | 2015-11-17 07:18:31 -0800 | [diff] [blame] | 596 | GPR_ASSERT(connected_subchannel); |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 597 | |
Mark D. Roth | ff4df06 | 2016-08-22 15:02:49 -0700 | [diff] [blame] | 598 | gpr_mu_lock(&chand->mu); |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 599 | if (initial_metadata == NULL) { |
| 600 | if (chand->lb_policy != NULL) { |
Craig Tiller | ab33b48 | 2015-11-21 08:11:04 -0800 | [diff] [blame] | 601 | grpc_lb_policy_cancel_pick(exec_ctx, chand->lb_policy, |
| 602 | connected_subchannel); |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 603 | } |
| 604 | for (closure = chand->waiting_for_config_closures.head; closure != NULL; |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 605 | closure = closure->next_data.next) { |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 606 | cpa = closure->cb_arg; |
Craig Tiller | b5585d4 | 2015-11-17 07:18:31 -0800 | [diff] [blame] | 607 | if (cpa->connected_subchannel == connected_subchannel) { |
| 608 | cpa->connected_subchannel = NULL; |
Craig Tiller | 332f1b3 | 2016-05-24 13:21:21 -0700 | [diff] [blame] | 609 | grpc_exec_ctx_sched(exec_ctx, cpa->on_ready, |
| 610 | GRPC_ERROR_CREATE("Pick cancelled"), NULL); |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 611 | } |
| 612 | } |
Mark D. Roth | ff4df06 | 2016-08-22 15:02:49 -0700 | [diff] [blame] | 613 | gpr_mu_unlock(&chand->mu); |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 614 | GPR_TIMER_END("pick_subchannel", 0); |
Mark D. Roth | 4c0fe49 | 2016-08-31 13:51:55 -0700 | [diff] [blame] | 615 | return true; |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 616 | } |
| 617 | if (chand->lb_policy != NULL) { |
Craig Tiller | 86c0f8a | 2015-12-01 20:05:40 -0800 | [diff] [blame] | 618 | grpc_lb_policy *lb_policy = chand->lb_policy; |
| 619 | int r; |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 620 | GRPC_LB_POLICY_REF(lb_policy, "pick_subchannel"); |
Mark D. Roth | ff4df06 | 2016-08-22 15:02:49 -0700 | [diff] [blame] | 621 | gpr_mu_unlock(&chand->mu); |
David Garcia Quintas | 8aace51 | 2016-08-15 14:55:12 -0700 | [diff] [blame] | 622 | const grpc_lb_policy_pick_args inputs = {calld->pollent, initial_metadata, |
David Garcia Quintas | 5b0e946 | 2016-08-15 19:38:39 -0700 | [diff] [blame] | 623 | initial_metadata_flags, |
| 624 | &calld->lb_token_mdelem}; |
David Garcia Quintas | 8aace51 | 2016-08-15 14:55:12 -0700 | [diff] [blame] | 625 | r = grpc_lb_policy_pick(exec_ctx, lb_policy, &inputs, connected_subchannel, |
David Garcia Quintas | 331b9c0 | 2016-09-12 18:37:05 -0700 | [diff] [blame] | 626 | NULL, on_ready); |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 627 | GRPC_LB_POLICY_UNREF(exec_ctx, lb_policy, "pick_subchannel"); |
| 628 | GPR_TIMER_END("pick_subchannel", 0); |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 629 | return r; |
| 630 | } |
| 631 | if (chand->resolver != NULL && !chand->started_resolving) { |
Mark D. Roth | 4c0fe49 | 2016-08-31 13:51:55 -0700 | [diff] [blame] | 632 | chand->started_resolving = true; |
Craig Tiller | 906e3bc | 2015-11-24 07:31:31 -0800 | [diff] [blame] | 633 | GRPC_CHANNEL_STACK_REF(chand->owning_stack, "resolver"); |
Mark D. Roth | 046cf76 | 2016-09-26 11:13:51 -0700 | [diff] [blame^] | 634 | grpc_resolver_next(exec_ctx, chand->resolver, &chand->resolver_result, |
Mark D. Roth | ff4df06 | 2016-08-22 15:02:49 -0700 | [diff] [blame] | 635 | &chand->on_resolver_result_changed); |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 636 | } |
Craig Tiller | 0eab697 | 2016-04-23 12:59:57 -0700 | [diff] [blame] | 637 | if (chand->resolver != NULL) { |
| 638 | cpa = gpr_malloc(sizeof(*cpa)); |
| 639 | cpa->initial_metadata = initial_metadata; |
| 640 | cpa->initial_metadata_flags = initial_metadata_flags; |
| 641 | cpa->connected_subchannel = connected_subchannel; |
| 642 | cpa->on_ready = on_ready; |
| 643 | cpa->elem = elem; |
| 644 | grpc_closure_init(&cpa->closure, continue_picking, cpa); |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 645 | grpc_closure_list_append(&chand->waiting_for_config_closures, &cpa->closure, |
| 646 | GRPC_ERROR_NONE); |
Craig Tiller | 0eab697 | 2016-04-23 12:59:57 -0700 | [diff] [blame] | 647 | } else { |
Craig Tiller | 332f1b3 | 2016-05-24 13:21:21 -0700 | [diff] [blame] | 648 | grpc_exec_ctx_sched(exec_ctx, on_ready, GRPC_ERROR_CREATE("Disconnected"), |
| 649 | NULL); |
Craig Tiller | 0eab697 | 2016-04-23 12:59:57 -0700 | [diff] [blame] | 650 | } |
Mark D. Roth | ff4df06 | 2016-08-22 15:02:49 -0700 | [diff] [blame] | 651 | gpr_mu_unlock(&chand->mu); |
Craig Tiller | bfc9adc | 2016-06-27 13:16:22 -0700 | [diff] [blame] | 652 | |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 653 | GPR_TIMER_END("pick_subchannel", 0); |
Mark D. Roth | 4c0fe49 | 2016-08-31 13:51:55 -0700 | [diff] [blame] | 654 | return false; |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 655 | } |
| 656 | |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 657 | // The logic here is fairly complicated, due to (a) the fact that we |
| 658 | // need to handle the case where we receive the send op before the |
| 659 | // initial metadata op, and (b) the need for efficiency, especially in |
| 660 | // the streaming case. |
| 661 | // TODO(ctiller): Explain this more thoroughly. |
| 662 | static void cc_start_transport_stream_op(grpc_exec_ctx *exec_ctx, |
| 663 | grpc_call_element *elem, |
| 664 | grpc_transport_stream_op *op) { |
| 665 | call_data *calld = elem->call_data; |
| 666 | GRPC_CALL_LOG_OP(GPR_INFO, elem, op); |
| 667 | /* try to (atomically) get the call */ |
| 668 | grpc_subchannel_call *call = GET_CALL(calld); |
| 669 | GPR_TIMER_BEGIN("cc_start_transport_stream_op", 0); |
| 670 | if (call == CANCELLED_CALL) { |
| 671 | grpc_transport_stream_op_finish_with_failure(exec_ctx, op, |
| 672 | GRPC_ERROR_CANCELLED); |
| 673 | GPR_TIMER_END("cc_start_transport_stream_op", 0); |
| 674 | return; |
| 675 | } |
| 676 | if (call != NULL) { |
| 677 | grpc_subchannel_call_process_op(exec_ctx, call, op); |
| 678 | GPR_TIMER_END("cc_start_transport_stream_op", 0); |
| 679 | return; |
| 680 | } |
| 681 | /* we failed; lock and figure out what to do */ |
| 682 | gpr_mu_lock(&calld->mu); |
| 683 | retry: |
| 684 | /* need to recheck that another thread hasn't set the call */ |
| 685 | call = GET_CALL(calld); |
| 686 | if (call == CANCELLED_CALL) { |
| 687 | gpr_mu_unlock(&calld->mu); |
| 688 | grpc_transport_stream_op_finish_with_failure(exec_ctx, op, |
| 689 | GRPC_ERROR_CANCELLED); |
| 690 | GPR_TIMER_END("cc_start_transport_stream_op", 0); |
| 691 | return; |
| 692 | } |
| 693 | if (call != NULL) { |
| 694 | gpr_mu_unlock(&calld->mu); |
| 695 | grpc_subchannel_call_process_op(exec_ctx, call, op); |
| 696 | GPR_TIMER_END("cc_start_transport_stream_op", 0); |
| 697 | return; |
| 698 | } |
| 699 | /* if this is a cancellation, then we can raise our cancelled flag */ |
| 700 | if (op->cancel_error != GRPC_ERROR_NONE) { |
| 701 | if (!gpr_atm_rel_cas(&calld->subchannel_call, 0, |
| 702 | (gpr_atm)(uintptr_t)CANCELLED_CALL)) { |
| 703 | goto retry; |
| 704 | } else { |
| 705 | switch (calld->creation_phase) { |
| 706 | case GRPC_SUBCHANNEL_CALL_HOLDER_NOT_CREATING: |
| 707 | fail_locked(exec_ctx, calld, GRPC_ERROR_REF(op->cancel_error)); |
| 708 | break; |
| 709 | case GRPC_SUBCHANNEL_CALL_HOLDER_PICKING_SUBCHANNEL: |
Mark D. Roth | d4c0f55 | 2016-09-01 09:25:32 -0700 | [diff] [blame] | 710 | pick_subchannel(exec_ctx, elem, NULL, 0, &calld->connected_subchannel, |
| 711 | NULL); |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 712 | break; |
| 713 | } |
| 714 | gpr_mu_unlock(&calld->mu); |
| 715 | grpc_transport_stream_op_finish_with_failure(exec_ctx, op, |
| 716 | GRPC_ERROR_CANCELLED); |
| 717 | GPR_TIMER_END("cc_start_transport_stream_op", 0); |
| 718 | return; |
| 719 | } |
| 720 | } |
| 721 | /* if we don't have a subchannel, try to get one */ |
| 722 | if (calld->creation_phase == GRPC_SUBCHANNEL_CALL_HOLDER_NOT_CREATING && |
| 723 | calld->connected_subchannel == NULL && |
| 724 | op->send_initial_metadata != NULL) { |
Mark D. Roth | 9fe284e | 2016-09-12 11:22:27 -0700 | [diff] [blame] | 725 | for (grpc_linked_mdelem *mdelem = op->send_initial_metadata->list.head; |
| 726 | mdelem != NULL; mdelem = mdelem->next) { |
| 727 | if (mdelem->md->key == GRPC_MDSTR_PATH) { |
| 728 | calld->path = GRPC_MDSTR_REF(mdelem->md->value); |
| 729 | break; |
| 730 | } |
| 731 | } |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 732 | calld->creation_phase = GRPC_SUBCHANNEL_CALL_HOLDER_PICKING_SUBCHANNEL; |
Mark D. Roth | 9fe284e | 2016-09-12 11:22:27 -0700 | [diff] [blame] | 733 | grpc_closure_init(&calld->next_step, subchannel_ready, elem); |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 734 | GRPC_CALL_STACK_REF(calld->owning_call, "pick_subchannel"); |
Mark D. Roth | d4c0f55 | 2016-09-01 09:25:32 -0700 | [diff] [blame] | 735 | if (pick_subchannel(exec_ctx, elem, op->send_initial_metadata, |
| 736 | op->send_initial_metadata_flags, |
| 737 | &calld->connected_subchannel, &calld->next_step)) { |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 738 | calld->creation_phase = GRPC_SUBCHANNEL_CALL_HOLDER_NOT_CREATING; |
| 739 | GRPC_CALL_STACK_UNREF(exec_ctx, calld->owning_call, "pick_subchannel"); |
| 740 | } |
| 741 | } |
| 742 | /* if we've got a subchannel, then let's ask it to create a call */ |
| 743 | if (calld->creation_phase == GRPC_SUBCHANNEL_CALL_HOLDER_NOT_CREATING && |
| 744 | calld->connected_subchannel != NULL) { |
| 745 | grpc_subchannel_call *subchannel_call = NULL; |
| 746 | grpc_error *error = grpc_connected_subchannel_create_call( |
| 747 | exec_ctx, calld->connected_subchannel, calld->pollent, |
| 748 | &subchannel_call); |
| 749 | if (error != GRPC_ERROR_NONE) { |
| 750 | subchannel_call = CANCELLED_CALL; |
| 751 | fail_locked(exec_ctx, calld, GRPC_ERROR_REF(error)); |
| 752 | grpc_transport_stream_op_finish_with_failure(exec_ctx, op, error); |
| 753 | } |
| 754 | gpr_atm_rel_store(&calld->subchannel_call, |
| 755 | (gpr_atm)(uintptr_t)subchannel_call); |
| 756 | retry_waiting_locked(exec_ctx, calld); |
| 757 | goto retry; |
| 758 | } |
| 759 | /* nothing to be done but wait */ |
| 760 | add_waiting_locked(calld, op); |
| 761 | gpr_mu_unlock(&calld->mu); |
| 762 | GPR_TIMER_END("cc_start_transport_stream_op", 0); |
| 763 | } |
| 764 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 765 | /* Constructor for call_data */ |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 766 | static grpc_error *cc_init_call_elem(grpc_exec_ctx *exec_ctx, |
| 767 | grpc_call_element *elem, |
| 768 | grpc_call_element_args *args) { |
Mark D. Roth | 4c0fe49 | 2016-08-31 13:51:55 -0700 | [diff] [blame] | 769 | call_data *calld = elem->call_data; |
| 770 | gpr_atm_rel_store(&calld->subchannel_call, 0); |
Mark D. Roth | 4c0fe49 | 2016-08-31 13:51:55 -0700 | [diff] [blame] | 771 | gpr_mu_init(&calld->mu); |
| 772 | calld->connected_subchannel = NULL; |
Mark D. Roth | 9fe284e | 2016-09-12 11:22:27 -0700 | [diff] [blame] | 773 | calld->path = NULL; |
| 774 | calld->method_config = NULL; |
Mark D. Roth | 4c0fe49 | 2016-08-31 13:51:55 -0700 | [diff] [blame] | 775 | calld->waiting_ops = NULL; |
| 776 | calld->waiting_ops_count = 0; |
| 777 | calld->waiting_ops_capacity = 0; |
| 778 | calld->creation_phase = GRPC_SUBCHANNEL_CALL_HOLDER_NOT_CREATING; |
| 779 | calld->owning_call = args->call_stack; |
| 780 | calld->pollent = NULL; |
Mark D. Roth | 0badbe8 | 2016-06-23 10:15:12 -0700 | [diff] [blame] | 781 | return GRPC_ERROR_NONE; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 782 | } |
| 783 | |
| 784 | /* Destructor for call_data */ |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 785 | static void cc_destroy_call_elem(grpc_exec_ctx *exec_ctx, |
| 786 | grpc_call_element *elem, |
| 787 | const grpc_call_final_info *final_info, |
| 788 | void *and_free_memory) { |
Mark D. Roth | 4c0fe49 | 2016-08-31 13:51:55 -0700 | [diff] [blame] | 789 | call_data *calld = elem->call_data; |
Mark D. Roth | 9fe284e | 2016-09-12 11:22:27 -0700 | [diff] [blame] | 790 | if (calld->path != NULL) GRPC_MDSTR_UNREF(calld->path); |
Mark D. Roth | 4c0fe49 | 2016-08-31 13:51:55 -0700 | [diff] [blame] | 791 | grpc_subchannel_call *call = GET_CALL(calld); |
| 792 | if (call != NULL && call != CANCELLED_CALL) { |
| 793 | GRPC_SUBCHANNEL_CALL_UNREF(exec_ctx, call, "client_channel_destroy_call"); |
| 794 | } |
| 795 | GPR_ASSERT(calld->creation_phase == GRPC_SUBCHANNEL_CALL_HOLDER_NOT_CREATING); |
| 796 | gpr_mu_destroy(&calld->mu); |
| 797 | GPR_ASSERT(calld->waiting_ops_count == 0); |
| 798 | gpr_free(calld->waiting_ops); |
Craig Tiller | 2c8063c | 2016-03-22 22:12:15 -0700 | [diff] [blame] | 799 | gpr_free(and_free_memory); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 800 | } |
| 801 | |
David Garcia Quintas | f72eb97 | 2016-05-03 18:28:09 -0700 | [diff] [blame] | 802 | static void cc_set_pollset_or_pollset_set(grpc_exec_ctx *exec_ctx, |
| 803 | grpc_call_element *elem, |
David Garcia Quintas | 2a50dfe | 2016-05-31 15:09:12 -0700 | [diff] [blame] | 804 | grpc_polling_entity *pollent) { |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 805 | call_data *calld = elem->call_data; |
David Garcia Quintas | 2a50dfe | 2016-05-31 15:09:12 -0700 | [diff] [blame] | 806 | calld->pollent = pollent; |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 807 | } |
| 808 | |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 809 | /************************************************************************* |
| 810 | * EXPORTED SYMBOLS |
| 811 | */ |
| 812 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 813 | const grpc_channel_filter grpc_client_channel_filter = { |
Craig Tiller | f40df23 | 2016-03-25 13:38:14 -0700 | [diff] [blame] | 814 | cc_start_transport_stream_op, |
| 815 | cc_start_transport_op, |
| 816 | sizeof(call_data), |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 817 | cc_init_call_elem, |
David Garcia Quintas | 4afce7e | 2016-04-18 16:25:17 -0700 | [diff] [blame] | 818 | cc_set_pollset_or_pollset_set, |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 819 | cc_destroy_call_elem, |
Craig Tiller | f40df23 | 2016-03-25 13:38:14 -0700 | [diff] [blame] | 820 | sizeof(channel_data), |
Mark D. Roth | 2a5959f | 2016-09-01 08:20:27 -0700 | [diff] [blame] | 821 | cc_init_channel_elem, |
| 822 | cc_destroy_channel_elem, |
Craig Tiller | f40df23 | 2016-03-25 13:38:14 -0700 | [diff] [blame] | 823 | cc_get_peer, |
| 824 | "client-channel", |
Craig Tiller | 87d5b19 | 2015-04-16 14:37:57 -0700 | [diff] [blame] | 825 | }; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 826 | |
Mark D. Roth | 6053497 | 2016-09-20 08:37:12 -0700 | [diff] [blame] | 827 | void grpc_client_channel_finish_initialization( |
Mark D. Roth | 0e48a9a | 2016-09-08 14:14:39 -0700 | [diff] [blame] | 828 | grpc_exec_ctx *exec_ctx, grpc_channel_stack *channel_stack, |
| 829 | grpc_resolver *resolver, |
| 830 | grpc_client_channel_factory *client_channel_factory) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 831 | /* post construction initialization: set the transport setup pointer */ |
Mark D. Roth | 7f7d165 | 2016-09-20 10:46:15 -0700 | [diff] [blame] | 832 | GPR_ASSERT(client_channel_factory != NULL); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 833 | grpc_channel_element *elem = grpc_channel_stack_last_element(channel_stack); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 834 | channel_data *chand = elem->channel_data; |
Mark D. Roth | ff4df06 | 2016-08-22 15:02:49 -0700 | [diff] [blame] | 835 | gpr_mu_lock(&chand->mu); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 836 | GPR_ASSERT(!chand->resolver); |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 837 | chand->resolver = resolver; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 838 | GRPC_RESOLVER_REF(resolver, "channel"); |
| 839 | if (!grpc_closure_list_empty(chand->waiting_for_config_closures) || |
| 840 | chand->exit_idle_when_lb_policy_arrives) { |
Mark D. Roth | 4c0fe49 | 2016-08-31 13:51:55 -0700 | [diff] [blame] | 841 | chand->started_resolving = true; |
Craig Tiller | 906e3bc | 2015-11-24 07:31:31 -0800 | [diff] [blame] | 842 | GRPC_CHANNEL_STACK_REF(chand->owning_stack, "resolver"); |
Mark D. Roth | 046cf76 | 2016-09-26 11:13:51 -0700 | [diff] [blame^] | 843 | grpc_resolver_next(exec_ctx, resolver, &chand->resolver_result, |
Mark D. Roth | ff4df06 | 2016-08-22 15:02:49 -0700 | [diff] [blame] | 844 | &chand->on_resolver_result_changed); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 845 | } |
Mark D. Roth | 0e48a9a | 2016-09-08 14:14:39 -0700 | [diff] [blame] | 846 | chand->client_channel_factory = client_channel_factory; |
| 847 | grpc_client_channel_factory_ref(client_channel_factory); |
Mark D. Roth | ff4df06 | 2016-08-22 15:02:49 -0700 | [diff] [blame] | 848 | gpr_mu_unlock(&chand->mu); |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 849 | } |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 850 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 851 | grpc_connectivity_state grpc_client_channel_check_connectivity_state( |
| 852 | 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] | 853 | channel_data *chand = elem->channel_data; |
| 854 | grpc_connectivity_state out; |
Mark D. Roth | ff4df06 | 2016-08-22 15:02:49 -0700 | [diff] [blame] | 855 | gpr_mu_lock(&chand->mu); |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 856 | out = grpc_connectivity_state_check(&chand->state_tracker, NULL); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 857 | if (out == GRPC_CHANNEL_IDLE && try_to_connect) { |
| 858 | if (chand->lb_policy != NULL) { |
| 859 | grpc_lb_policy_exit_idle(exec_ctx, chand->lb_policy); |
| 860 | } else { |
Mark D. Roth | 4c0fe49 | 2016-08-31 13:51:55 -0700 | [diff] [blame] | 861 | chand->exit_idle_when_lb_policy_arrives = true; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 862 | if (!chand->started_resolving && chand->resolver != NULL) { |
Craig Tiller | 906e3bc | 2015-11-24 07:31:31 -0800 | [diff] [blame] | 863 | GRPC_CHANNEL_STACK_REF(chand->owning_stack, "resolver"); |
Mark D. Roth | 4c0fe49 | 2016-08-31 13:51:55 -0700 | [diff] [blame] | 864 | chand->started_resolving = true; |
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 | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 867 | } |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 868 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 869 | } |
Mark D. Roth | ff4df06 | 2016-08-22 15:02:49 -0700 | [diff] [blame] | 870 | gpr_mu_unlock(&chand->mu); |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 871 | return out; |
| 872 | } |
| 873 | |
Craig Tiller | 86c9958 | 2015-11-25 15:22:26 -0800 | [diff] [blame] | 874 | typedef struct { |
| 875 | channel_data *chand; |
| 876 | grpc_pollset *pollset; |
| 877 | grpc_closure *on_complete; |
| 878 | grpc_closure my_closure; |
| 879 | } external_connectivity_watcher; |
| 880 | |
Craig Tiller | 1d881fb | 2015-12-01 07:39:04 -0800 | [diff] [blame] | 881 | 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] | 882 | grpc_error *error) { |
Craig Tiller | 86c9958 | 2015-11-25 15:22:26 -0800 | [diff] [blame] | 883 | external_connectivity_watcher *w = arg; |
| 884 | grpc_closure *follow_up = w->on_complete; |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 885 | grpc_pollset_set_del_pollset(exec_ctx, w->chand->interested_parties, |
Craig Tiller | 1d881fb | 2015-12-01 07:39:04 -0800 | [diff] [blame] | 886 | w->pollset); |
| 887 | GRPC_CHANNEL_STACK_UNREF(exec_ctx, w->chand->owning_stack, |
| 888 | "external_connectivity_watcher"); |
Craig Tiller | 86c9958 | 2015-11-25 15:22:26 -0800 | [diff] [blame] | 889 | gpr_free(w); |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 890 | follow_up->cb(exec_ctx, follow_up->cb_arg, error); |
Craig Tiller | 86c9958 | 2015-11-25 15:22:26 -0800 | [diff] [blame] | 891 | } |
| 892 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 893 | void grpc_client_channel_watch_connectivity_state( |
Craig Tiller | 906e3bc | 2015-11-24 07:31:31 -0800 | [diff] [blame] | 894 | grpc_exec_ctx *exec_ctx, grpc_channel_element *elem, grpc_pollset *pollset, |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 895 | grpc_connectivity_state *state, grpc_closure *on_complete) { |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 896 | channel_data *chand = elem->channel_data; |
Craig Tiller | 86c9958 | 2015-11-25 15:22:26 -0800 | [diff] [blame] | 897 | external_connectivity_watcher *w = gpr_malloc(sizeof(*w)); |
| 898 | w->chand = chand; |
| 899 | w->pollset = pollset; |
| 900 | w->on_complete = on_complete; |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 901 | grpc_pollset_set_add_pollset(exec_ctx, chand->interested_parties, pollset); |
Craig Tiller | 86c9958 | 2015-11-25 15:22:26 -0800 | [diff] [blame] | 902 | grpc_closure_init(&w->my_closure, on_external_watch_complete, w); |
Craig Tiller | 1d881fb | 2015-12-01 07:39:04 -0800 | [diff] [blame] | 903 | GRPC_CHANNEL_STACK_REF(w->chand->owning_stack, |
| 904 | "external_connectivity_watcher"); |
Mark D. Roth | ff4df06 | 2016-08-22 15:02:49 -0700 | [diff] [blame] | 905 | gpr_mu_lock(&chand->mu); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 906 | grpc_connectivity_state_notify_on_state_change( |
Craig Tiller | 86c9958 | 2015-11-25 15:22:26 -0800 | [diff] [blame] | 907 | exec_ctx, &chand->state_tracker, state, &w->my_closure); |
Mark D. Roth | ff4df06 | 2016-08-22 15:02:49 -0700 | [diff] [blame] | 908 | gpr_mu_unlock(&chand->mu); |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 909 | } |