Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 0605995 | 2015-02-18 08:34:56 -0800 | [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 | |
| 34 | #include "src/core/channel/client_channel.h" |
| 35 | |
| 36 | #include <stdio.h> |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 37 | #include <string.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 38 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 39 | #include <grpc/support/alloc.h> |
| 40 | #include <grpc/support/log.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 41 | #include <grpc/support/sync.h> |
| 42 | #include <grpc/support/useful.h> |
| 43 | |
Craig Tiller | 8910ac6 | 2015-10-08 16:49:15 -0700 | [diff] [blame] | 44 | #include "src/core/channel/channel_args.h" |
| 45 | #include "src/core/channel/connected_channel.h" |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame^] | 46 | #include "src/core/channel/subchannel_call_holder.h" |
Craig Tiller | 8910ac6 | 2015-10-08 16:49:15 -0700 | [diff] [blame] | 47 | #include "src/core/iomgr/iomgr.h" |
| 48 | #include "src/core/profiling/timers.h" |
| 49 | #include "src/core/support/string.h" |
| 50 | #include "src/core/surface/channel.h" |
| 51 | #include "src/core/transport/connectivity_state.h" |
| 52 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 53 | /* Client channel implementation */ |
| 54 | |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame^] | 55 | typedef grpc_subchannel_call_holder call_data; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 56 | |
Craig Tiller | 800dacb | 2015-10-06 09:10:26 -0700 | [diff] [blame] | 57 | typedef struct client_channel_channel_data { |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 58 | /** metadata context for this channel */ |
Craig Tiller | e70413c | 2015-04-24 10:12:34 -0700 | [diff] [blame] | 59 | grpc_mdctx *mdctx; |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 60 | /** resolver for this channel */ |
| 61 | grpc_resolver *resolver; |
Craig Tiller | 20a3c35 | 2015-08-05 08:39:50 -0700 | [diff] [blame] | 62 | /** have we started resolving this channel */ |
| 63 | int started_resolving; |
Craig Tiller | 50bc609 | 2015-07-01 14:25:19 -0700 | [diff] [blame] | 64 | /** master channel - the grpc_channel instance that ultimately owns |
| 65 | this channel_data via its channel stack. |
| 66 | We occasionally use this to bump the refcount on the master channel |
| 67 | to keep ourselves alive through an asynchronous operation. */ |
Craig Tiller | 9846503 | 2015-06-29 14:36:42 -0700 | [diff] [blame] | 68 | grpc_channel *master; |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 69 | |
Craig Tiller | 9d94b60 | 2015-07-01 14:23:18 -0700 | [diff] [blame] | 70 | /** mutex protecting client configuration, including all |
| 71 | variables below in this data structure */ |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 72 | gpr_mu mu_config; |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 73 | /** currently active load balancer - guarded by mu_config */ |
| 74 | grpc_lb_policy *lb_policy; |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 75 | /** incoming configuration - set by resolver.next |
| 76 | guarded by mu_config */ |
| 77 | grpc_client_config *incoming_configuration; |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 78 | /** 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] | 79 | grpc_closure_list waiting_for_config_closures; |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 80 | /** resolver callback */ |
Craig Tiller | 3382511 | 2015-09-18 07:44:19 -0700 | [diff] [blame] | 81 | grpc_closure on_config_changed; |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 82 | /** connectivity state being tracked */ |
Craig Tiller | ca3e9d3 | 2015-06-27 18:37:27 -0700 | [diff] [blame] | 83 | grpc_connectivity_state_tracker state_tracker; |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 84 | /** when an lb_policy arrives, should we try to exit idle */ |
| 85 | int exit_idle_when_lb_policy_arrives; |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 86 | /** pollset_set of interested parties in a new connection */ |
| 87 | grpc_pollset_set pollset_set; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 88 | } channel_data; |
| 89 | |
Craig Tiller | d6c98df | 2015-08-18 09:33:44 -0700 | [diff] [blame] | 90 | /** We create one watcher for each new lb_policy that is returned from a |
| 91 | resolver, |
| 92 | to watch for state changes from the lb_policy. When a state change is seen, |
| 93 | we |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 94 | update the channel, and create a new watcher */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 95 | typedef struct { |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 96 | channel_data *chand; |
Craig Tiller | 3382511 | 2015-09-18 07:44:19 -0700 | [diff] [blame] | 97 | grpc_closure on_changed; |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 98 | grpc_connectivity_state state; |
| 99 | grpc_lb_policy *lb_policy; |
| 100 | } lb_policy_connectivity_watcher; |
| 101 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 102 | typedef struct { |
Craig Tiller | 3382511 | 2015-09-18 07:44:19 -0700 | [diff] [blame] | 103 | grpc_closure closure; |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 104 | grpc_call_element *elem; |
| 105 | } waiting_call; |
| 106 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 107 | static char *cc_get_peer(grpc_exec_ctx *exec_ctx, grpc_call_element *elem) { |
Craig Tiller | 1b22b9d | 2015-07-20 13:42:22 -0700 | [diff] [blame] | 108 | channel_data *chand = elem->channel_data; |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame^] | 109 | return grpc_subchannel_call_holder_get_peer(exec_ctx, elem->call_data, |
| 110 | chand->master); |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 111 | } |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 112 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 113 | static void cc_start_transport_stream_op(grpc_exec_ctx *exec_ctx, |
| 114 | grpc_call_element *elem, |
| 115 | grpc_transport_stream_op *op) { |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame^] | 116 | GRPC_CALL_LOG_OP(GPR_INFO, elem, op); |
| 117 | grpc_subchannel_call_holder_perform_op(exec_ctx, elem->call_data, op); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 118 | } |
| 119 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 120 | static void watch_lb_policy(grpc_exec_ctx *exec_ctx, channel_data *chand, |
| 121 | grpc_lb_policy *lb_policy, |
| 122 | grpc_connectivity_state current_state); |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 123 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 124 | static void on_lb_policy_state_changed_locked( |
| 125 | grpc_exec_ctx *exec_ctx, lb_policy_connectivity_watcher *w) { |
Craig Tiller | 5795da7 | 2015-09-17 15:27:13 -0700 | [diff] [blame] | 126 | /* check if the notification is for a stale policy */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 127 | if (w->lb_policy != w->chand->lb_policy) return; |
Craig Tiller | 5795da7 | 2015-09-17 15:27:13 -0700 | [diff] [blame] | 128 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 129 | grpc_connectivity_state_set(exec_ctx, &w->chand->state_tracker, w->state, |
| 130 | "lb_changed"); |
| 131 | if (w->state != GRPC_CHANNEL_FATAL_FAILURE) { |
| 132 | watch_lb_policy(exec_ctx, w->chand, w->lb_policy, w->state); |
| 133 | } |
Craig Tiller | 5795da7 | 2015-09-17 15:27:13 -0700 | [diff] [blame] | 134 | } |
| 135 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 136 | static void on_lb_policy_state_changed(grpc_exec_ctx *exec_ctx, void *arg, |
| 137 | int iomgr_success) { |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 138 | lb_policy_connectivity_watcher *w = arg; |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 139 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 140 | gpr_mu_lock(&w->chand->mu_config); |
| 141 | on_lb_policy_state_changed_locked(exec_ctx, w); |
| 142 | gpr_mu_unlock(&w->chand->mu_config); |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 143 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 144 | GRPC_CHANNEL_INTERNAL_UNREF(exec_ctx, w->chand->master, "watch_lb_policy"); |
| 145 | gpr_free(w); |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 146 | } |
| 147 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 148 | static void watch_lb_policy(grpc_exec_ctx *exec_ctx, channel_data *chand, |
| 149 | grpc_lb_policy *lb_policy, |
| 150 | grpc_connectivity_state current_state) { |
| 151 | lb_policy_connectivity_watcher *w = gpr_malloc(sizeof(*w)); |
| 152 | GRPC_CHANNEL_INTERNAL_REF(chand->master, "watch_lb_policy"); |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 153 | |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 154 | w->chand = chand; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 155 | grpc_closure_init(&w->on_changed, on_lb_policy_state_changed, w); |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 156 | w->state = current_state; |
| 157 | w->lb_policy = lb_policy; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 158 | grpc_lb_policy_notify_on_state_change(exec_ctx, lb_policy, &w->state, |
| 159 | &w->on_changed); |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 160 | } |
| 161 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 162 | static void cc_on_config_changed(grpc_exec_ctx *exec_ctx, void *arg, |
| 163 | int iomgr_success) { |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 164 | channel_data *chand = arg; |
| 165 | grpc_lb_policy *lb_policy = NULL; |
| 166 | grpc_lb_policy *old_lb_policy; |
| 167 | grpc_resolver *old_resolver; |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 168 | grpc_connectivity_state state = GRPC_CHANNEL_TRANSIENT_FAILURE; |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 169 | int exit_idle = 0; |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 170 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 171 | if (chand->incoming_configuration != NULL) { |
| 172 | lb_policy = grpc_client_config_get_lb_policy(chand->incoming_configuration); |
| 173 | if (lb_policy != NULL) { |
| 174 | GRPC_LB_POLICY_REF(lb_policy, "channel"); |
| 175 | GRPC_LB_POLICY_REF(lb_policy, "config_change"); |
| 176 | state = grpc_lb_policy_check_connectivity(exec_ctx, lb_policy); |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 177 | } |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 178 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 179 | grpc_client_config_unref(exec_ctx, chand->incoming_configuration); |
| 180 | } |
| 181 | |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 182 | chand->incoming_configuration = NULL; |
| 183 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 184 | gpr_mu_lock(&chand->mu_config); |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 185 | old_lb_policy = chand->lb_policy; |
| 186 | chand->lb_policy = lb_policy; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 187 | if (lb_policy != NULL || chand->resolver == NULL /* disconnected */) { |
| 188 | grpc_exec_ctx_enqueue_list(exec_ctx, &chand->waiting_for_config_closures); |
| 189 | } |
| 190 | if (lb_policy != NULL && chand->exit_idle_when_lb_policy_arrives) { |
| 191 | GRPC_LB_POLICY_REF(lb_policy, "exit_idle"); |
| 192 | exit_idle = 1; |
| 193 | chand->exit_idle_when_lb_policy_arrives = 0; |
| 194 | } |
Craig Tiller | 9846503 | 2015-06-29 14:36:42 -0700 | [diff] [blame] | 195 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 196 | if (iomgr_success && chand->resolver) { |
| 197 | grpc_resolver *resolver = chand->resolver; |
| 198 | GRPC_RESOLVER_REF(resolver, "channel-next"); |
| 199 | grpc_connectivity_state_set(exec_ctx, &chand->state_tracker, state, |
| 200 | "new_lb+resolver"); |
| 201 | if (lb_policy != NULL) { |
| 202 | watch_lb_policy(exec_ctx, chand, lb_policy, state); |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 203 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 204 | gpr_mu_unlock(&chand->mu_config); |
| 205 | GRPC_CHANNEL_INTERNAL_REF(chand->master, "resolver"); |
| 206 | grpc_resolver_next(exec_ctx, resolver, &chand->incoming_configuration, |
| 207 | &chand->on_config_changed); |
| 208 | GRPC_RESOLVER_UNREF(exec_ctx, resolver, "channel-next"); |
| 209 | } else { |
| 210 | old_resolver = chand->resolver; |
| 211 | chand->resolver = NULL; |
| 212 | grpc_connectivity_state_set(exec_ctx, &chand->state_tracker, |
| 213 | GRPC_CHANNEL_FATAL_FAILURE, "resolver_gone"); |
| 214 | gpr_mu_unlock(&chand->mu_config); |
| 215 | if (old_resolver != NULL) { |
| 216 | grpc_resolver_shutdown(exec_ctx, old_resolver); |
| 217 | GRPC_RESOLVER_UNREF(exec_ctx, old_resolver, "channel"); |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 218 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 219 | } |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 220 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 221 | if (exit_idle) { |
| 222 | grpc_lb_policy_exit_idle(exec_ctx, lb_policy); |
| 223 | GRPC_LB_POLICY_UNREF(exec_ctx, lb_policy, "exit_idle"); |
| 224 | } |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 225 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 226 | if (old_lb_policy != NULL) { |
| 227 | grpc_lb_policy_shutdown(exec_ctx, old_lb_policy); |
| 228 | GRPC_LB_POLICY_UNREF(exec_ctx, old_lb_policy, "channel"); |
| 229 | } |
Craig Tiller | 000cd8f | 2015-09-18 07:20:29 -0700 | [diff] [blame] | 230 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 231 | if (lb_policy != NULL) { |
| 232 | GRPC_LB_POLICY_UNREF(exec_ctx, lb_policy, "config_change"); |
| 233 | } |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 234 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 235 | GRPC_CHANNEL_INTERNAL_UNREF(exec_ctx, chand->master, "resolver"); |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 236 | } |
| 237 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 238 | static void cc_start_transport_op(grpc_exec_ctx *exec_ctx, |
| 239 | grpc_channel_element *elem, |
| 240 | grpc_transport_op *op) { |
Craig Tiller | ca3e9d3 | 2015-06-27 18:37:27 -0700 | [diff] [blame] | 241 | grpc_lb_policy *lb_policy = NULL; |
| 242 | channel_data *chand = elem->channel_data; |
Craig Tiller | 9846503 | 2015-06-29 14:36:42 -0700 | [diff] [blame] | 243 | grpc_resolver *destroy_resolver = NULL; |
Craig Tiller | 000cd8f | 2015-09-18 07:20:29 -0700 | [diff] [blame] | 244 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 245 | grpc_exec_ctx_enqueue(exec_ctx, op->on_consumed, 1); |
Craig Tiller | ca3e9d3 | 2015-06-27 18:37:27 -0700 | [diff] [blame] | 246 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 247 | GPR_ASSERT(op->set_accept_stream == NULL); |
| 248 | GPR_ASSERT(op->bind_pollset == NULL); |
Craig Tiller | ca3e9d3 | 2015-06-27 18:37:27 -0700 | [diff] [blame] | 249 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 250 | gpr_mu_lock(&chand->mu_config); |
| 251 | if (op->on_connectivity_state_change != NULL) { |
| 252 | grpc_connectivity_state_notify_on_state_change( |
| 253 | exec_ctx, &chand->state_tracker, op->connectivity_state, |
| 254 | op->on_connectivity_state_change); |
| 255 | op->on_connectivity_state_change = NULL; |
| 256 | op->connectivity_state = NULL; |
| 257 | } |
| 258 | |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame^] | 259 | lb_policy = chand->lb_policy; |
| 260 | if (lb_policy) { |
| 261 | GRPC_LB_POLICY_REF(lb_policy, "broadcast"); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 262 | } |
Craig Tiller | 03dc655 | 2015-07-17 23:12:34 -0700 | [diff] [blame] | 263 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 264 | if (op->disconnect && chand->resolver != NULL) { |
| 265 | grpc_connectivity_state_set(exec_ctx, &chand->state_tracker, |
| 266 | GRPC_CHANNEL_FATAL_FAILURE, "disconnect"); |
| 267 | destroy_resolver = chand->resolver; |
| 268 | chand->resolver = NULL; |
| 269 | if (chand->lb_policy != NULL) { |
| 270 | grpc_lb_policy_shutdown(exec_ctx, chand->lb_policy); |
| 271 | GRPC_LB_POLICY_UNREF(exec_ctx, chand->lb_policy, "channel"); |
| 272 | chand->lb_policy = NULL; |
Craig Tiller | d2cc459 | 2015-07-01 07:50:47 -0700 | [diff] [blame] | 273 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 274 | } |
| 275 | gpr_mu_unlock(&chand->mu_config); |
Craig Tiller | ca3e9d3 | 2015-06-27 18:37:27 -0700 | [diff] [blame] | 276 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 277 | if (destroy_resolver) { |
| 278 | grpc_resolver_shutdown(exec_ctx, destroy_resolver); |
| 279 | GRPC_RESOLVER_UNREF(exec_ctx, destroy_resolver, "channel"); |
| 280 | } |
Craig Tiller | 9846503 | 2015-06-29 14:36:42 -0700 | [diff] [blame] | 281 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 282 | if (lb_policy) { |
| 283 | grpc_lb_policy_broadcast(exec_ctx, lb_policy, op); |
| 284 | GRPC_LB_POLICY_UNREF(exec_ctx, lb_policy, "broadcast"); |
| 285 | } |
Craig Tiller | ca3e9d3 | 2015-06-27 18:37:27 -0700 | [diff] [blame] | 286 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 287 | |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame^] | 288 | typedef struct { |
| 289 | grpc_metadata_batch *initial_metadata; |
| 290 | grpc_subchannel **subchannel; |
| 291 | grpc_closure *on_ready; |
| 292 | grpc_call_element *elem; |
| 293 | grpc_closure closure; |
| 294 | } continue_picking_args; |
| 295 | |
| 296 | static int cc_pick_subchannel(grpc_exec_ctx *exec_ctx, void *arg, |
| 297 | grpc_metadata_batch *initial_metadata, |
| 298 | grpc_subchannel **subchannel, |
| 299 | grpc_closure *on_ready); |
| 300 | |
| 301 | static void continue_picking(grpc_exec_ctx *exec_ctx, void *arg, int success) { |
| 302 | continue_picking_args *cpa = arg; |
| 303 | if (!success) { |
| 304 | grpc_exec_ctx_enqueue(exec_ctx, cpa->on_ready, 0); |
| 305 | } else if (cpa->subchannel == NULL) { |
| 306 | /* cancelled, do nothing */ |
| 307 | } else if (cc_pick_subchannel(exec_ctx, cpa->elem, cpa->initial_metadata, |
| 308 | cpa->subchannel, cpa->on_ready)) { |
| 309 | grpc_exec_ctx_enqueue(exec_ctx, cpa->on_ready, 1); |
| 310 | } |
| 311 | gpr_free(cpa); |
| 312 | } |
| 313 | |
| 314 | static int cc_pick_subchannel(grpc_exec_ctx *exec_ctx, void *elemp, |
| 315 | grpc_metadata_batch *initial_metadata, |
| 316 | grpc_subchannel **subchannel, |
| 317 | grpc_closure *on_ready) { |
| 318 | grpc_call_element *elem = elemp; |
| 319 | channel_data *chand = elem->channel_data; |
| 320 | call_data *calld = elem->call_data; |
| 321 | continue_picking_args *cpa; |
| 322 | grpc_closure *closure; |
| 323 | |
| 324 | GPR_ASSERT(subchannel); |
| 325 | |
| 326 | gpr_mu_lock(&chand->mu_config); |
| 327 | if (initial_metadata == NULL) { |
| 328 | if (chand->lb_policy != NULL) { |
| 329 | grpc_lb_policy_cancel_pick(exec_ctx, chand->lb_policy, subchannel); |
| 330 | } |
| 331 | for (closure = chand->waiting_for_config_closures.head; closure != NULL; |
| 332 | closure = grpc_closure_next(closure)) { |
| 333 | cpa = closure->cb_arg; |
| 334 | if (cpa->subchannel == subchannel) { |
| 335 | cpa->subchannel = NULL; |
| 336 | grpc_exec_ctx_enqueue(exec_ctx, cpa->on_ready, 0); |
| 337 | } |
| 338 | } |
| 339 | gpr_mu_unlock(&chand->mu_config); |
| 340 | return 1; |
| 341 | } |
| 342 | if (chand->lb_policy != NULL) { |
| 343 | int r = grpc_lb_policy_pick(exec_ctx, chand->lb_policy, calld->pollset, |
| 344 | initial_metadata, subchannel, on_ready); |
| 345 | gpr_mu_unlock(&chand->mu_config); |
| 346 | return r; |
| 347 | } |
| 348 | if (chand->resolver != NULL && !chand->started_resolving) { |
| 349 | chand->started_resolving = 1; |
| 350 | GRPC_CHANNEL_INTERNAL_REF(chand->master, "resolver"); |
| 351 | grpc_resolver_next(exec_ctx, chand->resolver, |
| 352 | &chand->incoming_configuration, |
| 353 | &chand->on_config_changed); |
| 354 | } |
| 355 | cpa = gpr_malloc(sizeof(*cpa)); |
| 356 | cpa->initial_metadata = initial_metadata; |
| 357 | cpa->subchannel = subchannel; |
| 358 | cpa->on_ready = on_ready; |
| 359 | cpa->elem = elem; |
| 360 | grpc_closure_init(&cpa->closure, continue_picking, cpa); |
| 361 | grpc_closure_list_add(&chand->waiting_for_config_closures, &cpa->closure, 1); |
| 362 | gpr_mu_unlock(&chand->mu_config); |
| 363 | return 0; |
| 364 | } |
| 365 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 366 | /* Constructor for call_data */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 367 | static void init_call_elem(grpc_exec_ctx *exec_ctx, grpc_call_element *elem, |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame^] | 368 | grpc_call_element_args *args) { |
| 369 | grpc_subchannel_call_holder_init(elem->call_data, cc_pick_subchannel, elem); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | /* Destructor for call_data */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 373 | static void destroy_call_elem(grpc_exec_ctx *exec_ctx, |
| 374 | grpc_call_element *elem) { |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame^] | 375 | grpc_subchannel_call_holder_destroy(exec_ctx, elem->call_data); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | /* Constructor for channel_data */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 379 | static void init_channel_elem(grpc_exec_ctx *exec_ctx, |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame^] | 380 | grpc_channel_element *elem, |
| 381 | grpc_channel_element_args *args) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 382 | channel_data *chand = elem->channel_data; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 383 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 384 | memset(chand, 0, sizeof(*chand)); |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 385 | |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame^] | 386 | GPR_ASSERT(args->is_last); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 387 | GPR_ASSERT(elem->filter == &grpc_client_channel_filter); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 388 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 389 | gpr_mu_init(&chand->mu_config); |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame^] | 390 | chand->mdctx = args->metadata_context; |
| 391 | chand->master = args->master; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 392 | grpc_pollset_set_init(&chand->pollset_set); |
| 393 | grpc_closure_init(&chand->on_config_changed, cc_on_config_changed, chand); |
Craig Tiller | 9846503 | 2015-06-29 14:36:42 -0700 | [diff] [blame] | 394 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 395 | grpc_connectivity_state_init(&chand->state_tracker, GRPC_CHANNEL_IDLE, |
| 396 | "client_channel"); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | /* Destructor for channel_data */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 400 | static void destroy_channel_elem(grpc_exec_ctx *exec_ctx, |
| 401 | grpc_channel_element *elem) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 402 | channel_data *chand = elem->channel_data; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 403 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 404 | if (chand->resolver != NULL) { |
| 405 | grpc_resolver_shutdown(exec_ctx, chand->resolver); |
| 406 | GRPC_RESOLVER_UNREF(exec_ctx, chand->resolver, "channel"); |
| 407 | } |
| 408 | if (chand->lb_policy != NULL) { |
| 409 | GRPC_LB_POLICY_UNREF(exec_ctx, chand->lb_policy, "channel"); |
| 410 | } |
| 411 | grpc_connectivity_state_destroy(exec_ctx, &chand->state_tracker); |
| 412 | grpc_pollset_set_destroy(&chand->pollset_set); |
| 413 | gpr_mu_destroy(&chand->mu_config); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 414 | } |
| 415 | |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame^] | 416 | static void cc_set_pollset(grpc_exec_ctx *exec_ctx, grpc_call_element *elem, |
| 417 | grpc_pollset *pollset) { |
| 418 | call_data *calld = elem->call_data; |
| 419 | calld->pollset = pollset; |
| 420 | } |
| 421 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 422 | const grpc_channel_filter grpc_client_channel_filter = { |
Craig Tiller | 71a0f9d | 2015-09-28 17:22:01 -0700 | [diff] [blame] | 423 | cc_start_transport_stream_op, cc_start_transport_op, sizeof(call_data), |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame^] | 424 | init_call_elem, cc_set_pollset, destroy_call_elem, sizeof(channel_data), |
| 425 | init_channel_elem, destroy_channel_elem, cc_get_peer, "client-channel", |
Craig Tiller | 87d5b19 | 2015-04-16 14:37:57 -0700 | [diff] [blame] | 426 | }; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 427 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 428 | void grpc_client_channel_set_resolver(grpc_exec_ctx *exec_ctx, |
| 429 | grpc_channel_stack *channel_stack, |
| 430 | grpc_resolver *resolver) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 431 | /* post construction initialization: set the transport setup pointer */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 432 | grpc_channel_element *elem = grpc_channel_stack_last_element(channel_stack); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 433 | channel_data *chand = elem->channel_data; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 434 | gpr_mu_lock(&chand->mu_config); |
| 435 | GPR_ASSERT(!chand->resolver); |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 436 | chand->resolver = resolver; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 437 | GRPC_RESOLVER_REF(resolver, "channel"); |
| 438 | if (!grpc_closure_list_empty(chand->waiting_for_config_closures) || |
| 439 | chand->exit_idle_when_lb_policy_arrives) { |
| 440 | chand->started_resolving = 1; |
| 441 | GRPC_CHANNEL_INTERNAL_REF(chand->master, "resolver"); |
| 442 | grpc_resolver_next(exec_ctx, resolver, &chand->incoming_configuration, |
| 443 | &chand->on_config_changed); |
| 444 | } |
| 445 | gpr_mu_unlock(&chand->mu_config); |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 446 | } |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 447 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 448 | grpc_connectivity_state grpc_client_channel_check_connectivity_state( |
| 449 | 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] | 450 | channel_data *chand = elem->channel_data; |
| 451 | grpc_connectivity_state out; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 452 | gpr_mu_lock(&chand->mu_config); |
| 453 | out = grpc_connectivity_state_check(&chand->state_tracker); |
| 454 | if (out == GRPC_CHANNEL_IDLE && try_to_connect) { |
| 455 | if (chand->lb_policy != NULL) { |
| 456 | grpc_lb_policy_exit_idle(exec_ctx, chand->lb_policy); |
| 457 | } else { |
| 458 | chand->exit_idle_when_lb_policy_arrives = 1; |
| 459 | if (!chand->started_resolving && chand->resolver != NULL) { |
| 460 | GRPC_CHANNEL_INTERNAL_REF(chand->master, "resolver"); |
| 461 | chand->started_resolving = 1; |
| 462 | grpc_resolver_next(exec_ctx, chand->resolver, |
| 463 | &chand->incoming_configuration, |
| 464 | &chand->on_config_changed); |
| 465 | } |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 466 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 467 | } |
| 468 | gpr_mu_unlock(&chand->mu_config); |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 469 | return out; |
| 470 | } |
| 471 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 472 | void grpc_client_channel_watch_connectivity_state( |
| 473 | grpc_exec_ctx *exec_ctx, grpc_channel_element *elem, |
| 474 | grpc_connectivity_state *state, grpc_closure *on_complete) { |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 475 | channel_data *chand = elem->channel_data; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 476 | gpr_mu_lock(&chand->mu_config); |
| 477 | grpc_connectivity_state_notify_on_state_change( |
| 478 | exec_ctx, &chand->state_tracker, state, on_complete); |
| 479 | gpr_mu_unlock(&chand->mu_config); |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 480 | } |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 481 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 482 | grpc_pollset_set *grpc_client_channel_get_connecting_pollset_set( |
| 483 | grpc_channel_element *elem) { |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 484 | channel_data *chand = elem->channel_data; |
| 485 | return &chand->pollset_set; |
| 486 | } |
| 487 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 488 | void grpc_client_channel_add_interested_party(grpc_exec_ctx *exec_ctx, |
| 489 | grpc_channel_element *elem, |
| 490 | grpc_pollset *pollset) { |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 491 | channel_data *chand = elem->channel_data; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 492 | grpc_pollset_set_add_pollset(exec_ctx, &chand->pollset_set, pollset); |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 493 | } |
| 494 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 495 | void grpc_client_channel_del_interested_party(grpc_exec_ctx *exec_ctx, |
| 496 | grpc_channel_element *elem, |
| 497 | grpc_pollset *pollset) { |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 498 | channel_data *chand = elem->channel_data; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 499 | grpc_pollset_set_del_pollset(exec_ctx, &chand->pollset_set, pollset); |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 500 | } |