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 | |
| 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 | 9781615 | 2016-04-05 13:39:00 -0700 | [diff] [blame] | 44 | #include "src/core/ext/client_config/subchannel_call_holder.h" |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 45 | #include "src/core/lib/channel/channel_args.h" |
| 46 | #include "src/core/lib/channel/connected_channel.h" |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 47 | #include "src/core/lib/iomgr/iomgr.h" |
| 48 | #include "src/core/lib/profiling/timers.h" |
| 49 | #include "src/core/lib/support/string.h" |
| 50 | #include "src/core/lib/surface/channel.h" |
| 51 | #include "src/core/lib/transport/connectivity_state.h" |
Craig Tiller | 8910ac6 | 2015-10-08 16:49:15 -0700 | [diff] [blame] | 52 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 53 | /* Client channel implementation */ |
| 54 | |
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 | /** resolver for this channel */ |
| 59 | grpc_resolver *resolver; |
Craig Tiller | 20a3c35 | 2015-08-05 08:39:50 -0700 | [diff] [blame] | 60 | /** have we started resolving this channel */ |
| 61 | int started_resolving; |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 62 | |
Craig Tiller | 9d94b60 | 2015-07-01 14:23:18 -0700 | [diff] [blame] | 63 | /** mutex protecting client configuration, including all |
| 64 | variables below in this data structure */ |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 65 | gpr_mu mu_config; |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 66 | /** currently active load balancer - guarded by mu_config */ |
| 67 | grpc_lb_policy *lb_policy; |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 68 | /** incoming configuration - set by resolver.next |
| 69 | guarded by mu_config */ |
| 70 | grpc_client_config *incoming_configuration; |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 71 | /** 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] | 72 | grpc_closure_list waiting_for_config_closures; |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 73 | /** resolver callback */ |
Craig Tiller | 3382511 | 2015-09-18 07:44:19 -0700 | [diff] [blame] | 74 | grpc_closure on_config_changed; |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 75 | /** connectivity state being tracked */ |
Craig Tiller | ca3e9d3 | 2015-06-27 18:37:27 -0700 | [diff] [blame] | 76 | grpc_connectivity_state_tracker state_tracker; |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 77 | /** when an lb_policy arrives, should we try to exit idle */ |
| 78 | int exit_idle_when_lb_policy_arrives; |
Craig Tiller | 906e3bc | 2015-11-24 07:31:31 -0800 | [diff] [blame] | 79 | /** owning stack */ |
| 80 | grpc_channel_stack *owning_stack; |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 81 | /** interested parties (owned) */ |
| 82 | grpc_pollset_set *interested_parties; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 83 | } channel_data; |
| 84 | |
Craig Tiller | d6c98df | 2015-08-18 09:33:44 -0700 | [diff] [blame] | 85 | /** We create one watcher for each new lb_policy that is returned from a |
| 86 | resolver, |
| 87 | to watch for state changes from the lb_policy. When a state change is seen, |
| 88 | we |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 89 | update the channel, and create a new watcher */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 90 | typedef struct { |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 91 | channel_data *chand; |
Craig Tiller | 3382511 | 2015-09-18 07:44:19 -0700 | [diff] [blame] | 92 | grpc_closure on_changed; |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 93 | grpc_connectivity_state state; |
| 94 | grpc_lb_policy *lb_policy; |
| 95 | } lb_policy_connectivity_watcher; |
| 96 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 97 | typedef struct { |
Craig Tiller | 3382511 | 2015-09-18 07:44:19 -0700 | [diff] [blame] | 98 | grpc_closure closure; |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 99 | grpc_call_element *elem; |
| 100 | } waiting_call; |
| 101 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 102 | static char *cc_get_peer(grpc_exec_ctx *exec_ctx, grpc_call_element *elem) { |
Craig Tiller | 906e3bc | 2015-11-24 07:31:31 -0800 | [diff] [blame] | 103 | return grpc_subchannel_call_holder_get_peer(exec_ctx, elem->call_data); |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 104 | } |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 105 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 106 | static void cc_start_transport_stream_op(grpc_exec_ctx *exec_ctx, |
| 107 | grpc_call_element *elem, |
| 108 | grpc_transport_stream_op *op) { |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 109 | GRPC_CALL_LOG_OP(GPR_INFO, elem, op); |
| 110 | grpc_subchannel_call_holder_perform_op(exec_ctx, elem->call_data, op); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 111 | } |
| 112 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 113 | static void watch_lb_policy(grpc_exec_ctx *exec_ctx, channel_data *chand, |
| 114 | grpc_lb_policy *lb_policy, |
| 115 | grpc_connectivity_state current_state); |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 116 | |
Craig Tiller | 8c0d96f | 2016-03-11 14:27:52 -0800 | [diff] [blame] | 117 | static void set_channel_connectivity_state_locked(grpc_exec_ctx *exec_ctx, |
| 118 | channel_data *chand, |
| 119 | grpc_connectivity_state state, |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 120 | grpc_error *error, |
Craig Tiller | 8c0d96f | 2016-03-11 14:27:52 -0800 | [diff] [blame] | 121 | const char *reason) { |
| 122 | if ((state == GRPC_CHANNEL_TRANSIENT_FAILURE || |
| 123 | state == GRPC_CHANNEL_FATAL_FAILURE) && |
| 124 | chand->lb_policy != NULL) { |
| 125 | /* cancel fail-fast picks */ |
| 126 | grpc_lb_policy_cancel_picks( |
| 127 | exec_ctx, chand->lb_policy, |
| 128 | /* mask= */ GRPC_INITIAL_METADATA_IGNORE_CONNECTIVITY, |
| 129 | /* check= */ 0); |
| 130 | } |
Craig Tiller | 94f8453 | 2016-05-06 21:03:29 -0700 | [diff] [blame] | 131 | grpc_connectivity_state_set(exec_ctx, &chand->state_tracker, state, |
Craig Tiller | 1c51edc | 2016-05-07 16:18:43 -0700 | [diff] [blame^] | 132 | error, reason); |
Craig Tiller | 8c0d96f | 2016-03-11 14:27:52 -0800 | [diff] [blame] | 133 | } |
| 134 | |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 135 | static void on_lb_policy_state_changed_locked(grpc_exec_ctx *exec_ctx, |
| 136 | lb_policy_connectivity_watcher *w, |
| 137 | grpc_error *error) { |
Craig Tiller | cb2609f | 2015-11-24 17:19:19 -0800 | [diff] [blame] | 138 | grpc_connectivity_state publish_state = w->state; |
Craig Tiller | 5795da7 | 2015-09-17 15:27:13 -0700 | [diff] [blame] | 139 | /* check if the notification is for a stale policy */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 140 | if (w->lb_policy != w->chand->lb_policy) return; |
Craig Tiller | 5795da7 | 2015-09-17 15:27:13 -0700 | [diff] [blame] | 141 | |
Craig Tiller | 1d881fb | 2015-12-01 07:39:04 -0800 | [diff] [blame] | 142 | if (publish_state == GRPC_CHANNEL_FATAL_FAILURE && |
| 143 | w->chand->resolver != NULL) { |
Craig Tiller | cb2609f | 2015-11-24 17:19:19 -0800 | [diff] [blame] | 144 | publish_state = GRPC_CHANNEL_TRANSIENT_FAILURE; |
| 145 | grpc_resolver_channel_saw_error(exec_ctx, w->chand->resolver); |
Craig Tiller | f62c4d5 | 2015-12-04 07:43:07 -0800 | [diff] [blame] | 146 | GRPC_LB_POLICY_UNREF(exec_ctx, w->chand->lb_policy, "channel"); |
| 147 | w->chand->lb_policy = NULL; |
Craig Tiller | cb2609f | 2015-11-24 17:19:19 -0800 | [diff] [blame] | 148 | } |
Craig Tiller | 8c0d96f | 2016-03-11 14:27:52 -0800 | [diff] [blame] | 149 | set_channel_connectivity_state_locked(exec_ctx, w->chand, publish_state, |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 150 | error, "lb_changed"); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 151 | if (w->state != GRPC_CHANNEL_FATAL_FAILURE) { |
| 152 | watch_lb_policy(exec_ctx, w->chand, w->lb_policy, w->state); |
| 153 | } |
Craig Tiller | 5795da7 | 2015-09-17 15:27:13 -0700 | [diff] [blame] | 154 | } |
| 155 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 156 | 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] | 157 | grpc_error *error) { |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 158 | lb_policy_connectivity_watcher *w = arg; |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 159 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 160 | gpr_mu_lock(&w->chand->mu_config); |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 161 | on_lb_policy_state_changed_locked(exec_ctx, w, error); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 162 | gpr_mu_unlock(&w->chand->mu_config); |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 163 | |
Craig Tiller | 906e3bc | 2015-11-24 07:31:31 -0800 | [diff] [blame] | 164 | 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] | 165 | gpr_free(w); |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 168 | static void watch_lb_policy(grpc_exec_ctx *exec_ctx, channel_data *chand, |
| 169 | grpc_lb_policy *lb_policy, |
| 170 | grpc_connectivity_state current_state) { |
| 171 | lb_policy_connectivity_watcher *w = gpr_malloc(sizeof(*w)); |
Craig Tiller | 906e3bc | 2015-11-24 07:31:31 -0800 | [diff] [blame] | 172 | GRPC_CHANNEL_STACK_REF(chand->owning_stack, "watch_lb_policy"); |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 173 | |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 174 | w->chand = chand; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 175 | grpc_closure_init(&w->on_changed, on_lb_policy_state_changed, w); |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 176 | w->state = current_state; |
| 177 | w->lb_policy = lb_policy; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 178 | grpc_lb_policy_notify_on_state_change(exec_ctx, lb_policy, &w->state, |
| 179 | &w->on_changed); |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 180 | } |
| 181 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 182 | static void cc_on_config_changed(grpc_exec_ctx *exec_ctx, void *arg, |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 183 | grpc_error *error) { |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 184 | channel_data *chand = arg; |
| 185 | grpc_lb_policy *lb_policy = NULL; |
| 186 | grpc_lb_policy *old_lb_policy; |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 187 | grpc_connectivity_state state = GRPC_CHANNEL_TRANSIENT_FAILURE; |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 188 | int exit_idle = 0; |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 189 | grpc_error *state_error = GRPC_ERROR_CREATE("No load balancing policy"); |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 190 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 191 | if (chand->incoming_configuration != NULL) { |
| 192 | lb_policy = grpc_client_config_get_lb_policy(chand->incoming_configuration); |
| 193 | if (lb_policy != NULL) { |
| 194 | GRPC_LB_POLICY_REF(lb_policy, "channel"); |
| 195 | GRPC_LB_POLICY_REF(lb_policy, "config_change"); |
Craig Tiller | f707d62 | 2016-05-06 14:26:12 -0700 | [diff] [blame] | 196 | GRPC_ERROR_UNREF(state_error); |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 197 | state = |
| 198 | grpc_lb_policy_check_connectivity(exec_ctx, lb_policy, &state_error); |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 199 | } |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 200 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 201 | grpc_client_config_unref(exec_ctx, chand->incoming_configuration); |
| 202 | } |
| 203 | |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 204 | chand->incoming_configuration = NULL; |
| 205 | |
Craig Tiller | 86c9958 | 2015-11-25 15:22:26 -0800 | [diff] [blame] | 206 | if (lb_policy != NULL) { |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 207 | grpc_pollset_set_add_pollset_set(exec_ctx, lb_policy->interested_parties, |
| 208 | chand->interested_parties); |
Craig Tiller | 86c9958 | 2015-11-25 15:22:26 -0800 | [diff] [blame] | 209 | } |
| 210 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 211 | gpr_mu_lock(&chand->mu_config); |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 212 | old_lb_policy = chand->lb_policy; |
| 213 | chand->lb_policy = lb_policy; |
Craig Tiller | 0ede545 | 2016-04-23 12:21:45 -0700 | [diff] [blame] | 214 | if (lb_policy != NULL) { |
| 215 | grpc_exec_ctx_enqueue_list(exec_ctx, &chand->waiting_for_config_closures, |
| 216 | NULL); |
| 217 | } else if (chand->resolver == NULL /* disconnected */) { |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 218 | grpc_closure_list_fail_all( |
| 219 | &chand->waiting_for_config_closures, |
| 220 | GRPC_ERROR_CREATE_REFERENCING("Channel disconnected", &error, 1)); |
Craig Tiller | 6c39686 | 2016-01-28 13:53:40 -0800 | [diff] [blame] | 221 | grpc_exec_ctx_enqueue_list(exec_ctx, &chand->waiting_for_config_closures, |
| 222 | NULL); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 223 | } |
| 224 | if (lb_policy != NULL && chand->exit_idle_when_lb_policy_arrives) { |
| 225 | GRPC_LB_POLICY_REF(lb_policy, "exit_idle"); |
| 226 | exit_idle = 1; |
| 227 | chand->exit_idle_when_lb_policy_arrives = 0; |
| 228 | } |
Craig Tiller | 9846503 | 2015-06-29 14:36:42 -0700 | [diff] [blame] | 229 | |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 230 | if (error == GRPC_ERROR_NONE && chand->resolver) { |
Craig Tiller | 1c51edc | 2016-05-07 16:18:43 -0700 | [diff] [blame^] | 231 | set_channel_connectivity_state_locked(exec_ctx, chand, state, GRPC_ERROR_REF(state_error), |
Craig Tiller | 8c0d96f | 2016-03-11 14:27:52 -0800 | [diff] [blame] | 232 | "new_lb+resolver"); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 233 | if (lb_policy != NULL) { |
| 234 | watch_lb_policy(exec_ctx, chand, lb_policy, state); |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 235 | } |
Craig Tiller | 906e3bc | 2015-11-24 07:31:31 -0800 | [diff] [blame] | 236 | GRPC_CHANNEL_STACK_REF(chand->owning_stack, "resolver"); |
Craig Tiller | 76a5c0e | 2016-03-09 09:05:30 -0800 | [diff] [blame] | 237 | grpc_resolver_next(exec_ctx, chand->resolver, |
| 238 | &chand->incoming_configuration, |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 239 | &chand->on_config_changed); |
Craig Tiller | 76a5c0e | 2016-03-09 09:05:30 -0800 | [diff] [blame] | 240 | gpr_mu_unlock(&chand->mu_config); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 241 | } else { |
Craig Tiller | 76a5c0e | 2016-03-09 09:05:30 -0800 | [diff] [blame] | 242 | if (chand->resolver != NULL) { |
| 243 | grpc_resolver_shutdown(exec_ctx, chand->resolver); |
| 244 | GRPC_RESOLVER_UNREF(exec_ctx, chand->resolver, "channel"); |
| 245 | chand->resolver = NULL; |
| 246 | } |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 247 | grpc_error *refs[] = {error, state_error}; |
Craig Tiller | 8c0d96f | 2016-03-11 14:27:52 -0800 | [diff] [blame] | 248 | set_channel_connectivity_state_locked( |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 249 | exec_ctx, chand, GRPC_CHANNEL_FATAL_FAILURE, |
| 250 | GRPC_ERROR_CREATE_REFERENCING("Got config after disconnection", refs, |
| 251 | GPR_ARRAY_SIZE(refs)), |
| 252 | "resolver_gone"); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 253 | gpr_mu_unlock(&chand->mu_config); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 254 | } |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 255 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 256 | if (exit_idle) { |
| 257 | grpc_lb_policy_exit_idle(exec_ctx, lb_policy); |
| 258 | GRPC_LB_POLICY_UNREF(exec_ctx, lb_policy, "exit_idle"); |
| 259 | } |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 260 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 261 | if (old_lb_policy != NULL) { |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 262 | grpc_pollset_set_del_pollset_set( |
| 263 | exec_ctx, old_lb_policy->interested_parties, chand->interested_parties); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 264 | GRPC_LB_POLICY_UNREF(exec_ctx, old_lb_policy, "channel"); |
| 265 | } |
Craig Tiller | 000cd8f | 2015-09-18 07:20:29 -0700 | [diff] [blame] | 266 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 267 | if (lb_policy != NULL) { |
| 268 | GRPC_LB_POLICY_UNREF(exec_ctx, lb_policy, "config_change"); |
| 269 | } |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 270 | |
Craig Tiller | 906e3bc | 2015-11-24 07:31:31 -0800 | [diff] [blame] | 271 | GRPC_CHANNEL_STACK_UNREF(exec_ctx, chand->owning_stack, "resolver"); |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 272 | } |
| 273 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 274 | static void cc_start_transport_op(grpc_exec_ctx *exec_ctx, |
| 275 | grpc_channel_element *elem, |
| 276 | grpc_transport_op *op) { |
Craig Tiller | ca3e9d3 | 2015-06-27 18:37:27 -0700 | [diff] [blame] | 277 | channel_data *chand = elem->channel_data; |
Craig Tiller | 000cd8f | 2015-09-18 07:20:29 -0700 | [diff] [blame] | 278 | |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 279 | grpc_exec_ctx_push(exec_ctx, op->on_consumed, GRPC_ERROR_NONE, NULL); |
Craig Tiller | ca3e9d3 | 2015-06-27 18:37:27 -0700 | [diff] [blame] | 280 | |
Craig Tiller | d7f12e3 | 2016-03-03 10:08:31 -0800 | [diff] [blame] | 281 | GPR_ASSERT(op->set_accept_stream == false); |
Craig Tiller | 28bf891 | 2015-12-07 16:07:04 -0800 | [diff] [blame] | 282 | if (op->bind_pollset != NULL) { |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 283 | grpc_pollset_set_add_pollset(exec_ctx, chand->interested_parties, |
Craig Tiller | e2c6237 | 2015-12-07 16:11:03 -0800 | [diff] [blame] | 284 | op->bind_pollset); |
Craig Tiller | 28bf891 | 2015-12-07 16:07:04 -0800 | [diff] [blame] | 285 | } |
Craig Tiller | ca3e9d3 | 2015-06-27 18:37:27 -0700 | [diff] [blame] | 286 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 287 | gpr_mu_lock(&chand->mu_config); |
| 288 | if (op->on_connectivity_state_change != NULL) { |
| 289 | grpc_connectivity_state_notify_on_state_change( |
| 290 | exec_ctx, &chand->state_tracker, op->connectivity_state, |
| 291 | op->on_connectivity_state_change); |
| 292 | op->on_connectivity_state_change = NULL; |
| 293 | op->connectivity_state = NULL; |
| 294 | } |
| 295 | |
Craig Tiller | 26dab31 | 2015-12-07 14:43:47 -0800 | [diff] [blame] | 296 | if (op->send_ping != NULL) { |
Craig Tiller | 87b71e2 | 2015-12-07 15:14:14 -0800 | [diff] [blame] | 297 | if (chand->lb_policy == NULL) { |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 298 | grpc_exec_ctx_push(exec_ctx, op->send_ping, |
| 299 | GRPC_ERROR_CREATE("Ping with no load balancing"), |
| 300 | NULL); |
Craig Tiller | 26dab31 | 2015-12-07 14:43:47 -0800 | [diff] [blame] | 301 | } else { |
Craig Tiller | 28bf891 | 2015-12-07 16:07:04 -0800 | [diff] [blame] | 302 | 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] | 303 | op->bind_pollset = NULL; |
| 304 | } |
| 305 | op->send_ping = NULL; |
| 306 | } |
| 307 | |
Craig Tiller | 1c51edc | 2016-05-07 16:18:43 -0700 | [diff] [blame^] | 308 | if (op->disconnect_with_error != GRPC_ERROR_NONE) { |
| 309 | if (chand->resolver != NULL) { |
| 310 | set_channel_connectivity_state_locked( |
| 311 | exec_ctx, chand, GRPC_CHANNEL_FATAL_FAILURE, |
| 312 | GRPC_ERROR_REF(op->disconnect_with_error), "disconnect"); |
| 313 | grpc_resolver_shutdown(exec_ctx, chand->resolver); |
| 314 | GRPC_RESOLVER_UNREF(exec_ctx, chand->resolver, "channel"); |
| 315 | chand->resolver = NULL; |
| 316 | if (!chand->started_resolving) { |
| 317 | grpc_closure_list_fail_all(&chand->waiting_for_config_closures, |
| 318 | GRPC_ERROR_REF(op->disconnect_with_error)); |
| 319 | grpc_exec_ctx_enqueue_list(exec_ctx, &chand->waiting_for_config_closures, |
| 320 | NULL); |
| 321 | } |
| 322 | if (chand->lb_policy != NULL) { |
| 323 | grpc_pollset_set_del_pollset_set(exec_ctx, |
| 324 | chand->lb_policy->interested_parties, |
| 325 | chand->interested_parties); |
| 326 | GRPC_LB_POLICY_UNREF(exec_ctx, chand->lb_policy, "channel"); |
| 327 | chand->lb_policy = NULL; |
| 328 | } |
Craig Tiller | b12d22a | 2016-04-23 12:50:21 -0700 | [diff] [blame] | 329 | } |
Craig Tiller | 1c51edc | 2016-05-07 16:18:43 -0700 | [diff] [blame^] | 330 | GRPC_ERROR_UNREF(op->disconnect_with_error); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 331 | } |
| 332 | gpr_mu_unlock(&chand->mu_config); |
Craig Tiller | ca3e9d3 | 2015-06-27 18:37:27 -0700 | [diff] [blame] | 333 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 334 | |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 335 | typedef struct { |
| 336 | grpc_metadata_batch *initial_metadata; |
Craig Tiller | 8c0d96f | 2016-03-11 14:27:52 -0800 | [diff] [blame] | 337 | uint32_t initial_metadata_flags; |
Craig Tiller | b5585d4 | 2015-11-17 07:18:31 -0800 | [diff] [blame] | 338 | grpc_connected_subchannel **connected_subchannel; |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 339 | grpc_closure *on_ready; |
| 340 | grpc_call_element *elem; |
| 341 | grpc_closure closure; |
| 342 | } continue_picking_args; |
| 343 | |
| 344 | static int cc_pick_subchannel(grpc_exec_ctx *exec_ctx, void *arg, |
| 345 | grpc_metadata_batch *initial_metadata, |
Craig Tiller | 8c0d96f | 2016-03-11 14:27:52 -0800 | [diff] [blame] | 346 | uint32_t initial_metadata_flags, |
Craig Tiller | b5585d4 | 2015-11-17 07:18:31 -0800 | [diff] [blame] | 347 | grpc_connected_subchannel **connected_subchannel, |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 348 | grpc_closure *on_ready); |
| 349 | |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 350 | static void continue_picking(grpc_exec_ctx *exec_ctx, void *arg, |
| 351 | grpc_error *error) { |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 352 | continue_picking_args *cpa = arg; |
Craig Tiller | 0ede545 | 2016-04-23 12:21:45 -0700 | [diff] [blame] | 353 | if (cpa->connected_subchannel == NULL) { |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 354 | /* cancelled, do nothing */ |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 355 | } else if (error != GRPC_ERROR_NONE) { |
Craig Tiller | f707d62 | 2016-05-06 14:26:12 -0700 | [diff] [blame] | 356 | grpc_exec_ctx_push(exec_ctx, cpa->on_ready, GRPC_ERROR_REF(error), NULL); |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 357 | } else if (cc_pick_subchannel(exec_ctx, cpa->elem, cpa->initial_metadata, |
Craig Tiller | 8c0d96f | 2016-03-11 14:27:52 -0800 | [diff] [blame] | 358 | cpa->initial_metadata_flags, |
Craig Tiller | b5585d4 | 2015-11-17 07:18:31 -0800 | [diff] [blame] | 359 | cpa->connected_subchannel, cpa->on_ready)) { |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 360 | grpc_exec_ctx_push(exec_ctx, cpa->on_ready, GRPC_ERROR_NONE, NULL); |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 361 | } |
| 362 | gpr_free(cpa); |
| 363 | } |
| 364 | |
| 365 | static int cc_pick_subchannel(grpc_exec_ctx *exec_ctx, void *elemp, |
| 366 | grpc_metadata_batch *initial_metadata, |
Craig Tiller | 8c0d96f | 2016-03-11 14:27:52 -0800 | [diff] [blame] | 367 | uint32_t initial_metadata_flags, |
Craig Tiller | b5585d4 | 2015-11-17 07:18:31 -0800 | [diff] [blame] | 368 | grpc_connected_subchannel **connected_subchannel, |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 369 | grpc_closure *on_ready) { |
| 370 | grpc_call_element *elem = elemp; |
| 371 | channel_data *chand = elem->channel_data; |
| 372 | call_data *calld = elem->call_data; |
| 373 | continue_picking_args *cpa; |
| 374 | grpc_closure *closure; |
| 375 | |
Craig Tiller | b5585d4 | 2015-11-17 07:18:31 -0800 | [diff] [blame] | 376 | GPR_ASSERT(connected_subchannel); |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 377 | |
| 378 | gpr_mu_lock(&chand->mu_config); |
| 379 | if (initial_metadata == NULL) { |
| 380 | if (chand->lb_policy != NULL) { |
Craig Tiller | ab33b48 | 2015-11-21 08:11:04 -0800 | [diff] [blame] | 381 | grpc_lb_policy_cancel_pick(exec_ctx, chand->lb_policy, |
| 382 | connected_subchannel); |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 383 | } |
| 384 | for (closure = chand->waiting_for_config_closures.head; closure != NULL; |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 385 | closure = closure->next_data.next) { |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 386 | cpa = closure->cb_arg; |
Craig Tiller | b5585d4 | 2015-11-17 07:18:31 -0800 | [diff] [blame] | 387 | if (cpa->connected_subchannel == connected_subchannel) { |
| 388 | cpa->connected_subchannel = NULL; |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 389 | grpc_exec_ctx_push(exec_ctx, cpa->on_ready, |
| 390 | GRPC_ERROR_CREATE("Pick cancelled"), NULL); |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 391 | } |
| 392 | } |
| 393 | gpr_mu_unlock(&chand->mu_config); |
| 394 | return 1; |
| 395 | } |
| 396 | if (chand->lb_policy != NULL) { |
Craig Tiller | 86c0f8a | 2015-12-01 20:05:40 -0800 | [diff] [blame] | 397 | grpc_lb_policy *lb_policy = chand->lb_policy; |
| 398 | int r; |
| 399 | GRPC_LB_POLICY_REF(lb_policy, "cc_pick_subchannel"); |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 400 | gpr_mu_unlock(&chand->mu_config); |
Craig Tiller | 86c0f8a | 2015-12-01 20:05:40 -0800 | [diff] [blame] | 401 | r = grpc_lb_policy_pick(exec_ctx, lb_policy, calld->pollset, |
Craig Tiller | 8c0d96f | 2016-03-11 14:27:52 -0800 | [diff] [blame] | 402 | initial_metadata, initial_metadata_flags, |
| 403 | connected_subchannel, on_ready); |
Craig Tiller | 86c0f8a | 2015-12-01 20:05:40 -0800 | [diff] [blame] | 404 | GRPC_LB_POLICY_UNREF(exec_ctx, lb_policy, "cc_pick_subchannel"); |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 405 | return r; |
| 406 | } |
| 407 | if (chand->resolver != NULL && !chand->started_resolving) { |
| 408 | chand->started_resolving = 1; |
Craig Tiller | 906e3bc | 2015-11-24 07:31:31 -0800 | [diff] [blame] | 409 | GRPC_CHANNEL_STACK_REF(chand->owning_stack, "resolver"); |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 410 | grpc_resolver_next(exec_ctx, chand->resolver, |
| 411 | &chand->incoming_configuration, |
| 412 | &chand->on_config_changed); |
| 413 | } |
Craig Tiller | 0eab697 | 2016-04-23 12:59:57 -0700 | [diff] [blame] | 414 | if (chand->resolver != NULL) { |
| 415 | cpa = gpr_malloc(sizeof(*cpa)); |
| 416 | cpa->initial_metadata = initial_metadata; |
| 417 | cpa->initial_metadata_flags = initial_metadata_flags; |
| 418 | cpa->connected_subchannel = connected_subchannel; |
| 419 | cpa->on_ready = on_ready; |
| 420 | cpa->elem = elem; |
| 421 | grpc_closure_init(&cpa->closure, continue_picking, cpa); |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 422 | grpc_closure_list_append(&chand->waiting_for_config_closures, &cpa->closure, |
| 423 | GRPC_ERROR_NONE); |
Craig Tiller | 0eab697 | 2016-04-23 12:59:57 -0700 | [diff] [blame] | 424 | } else { |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 425 | grpc_exec_ctx_push(exec_ctx, on_ready, GRPC_ERROR_CREATE("Disconnected"), |
| 426 | NULL); |
Craig Tiller | 0eab697 | 2016-04-23 12:59:57 -0700 | [diff] [blame] | 427 | } |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 428 | gpr_mu_unlock(&chand->mu_config); |
| 429 | return 0; |
| 430 | } |
| 431 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 432 | /* Constructor for call_data */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 433 | 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] | 434 | grpc_call_element_args *args) { |
Craig Tiller | 11beb9a | 2015-11-24 10:29:32 -0800 | [diff] [blame] | 435 | grpc_subchannel_call_holder_init(elem->call_data, cc_pick_subchannel, elem, |
| 436 | args->call_stack); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | /* Destructor for call_data */ |
Craig Tiller | 2c8063c | 2016-03-22 22:12:15 -0700 | [diff] [blame] | 440 | static void destroy_call_elem(grpc_exec_ctx *exec_ctx, grpc_call_element *elem, |
| 441 | void *and_free_memory) { |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 442 | grpc_subchannel_call_holder_destroy(exec_ctx, elem->call_data); |
Craig Tiller | 2c8063c | 2016-03-22 22:12:15 -0700 | [diff] [blame] | 443 | gpr_free(and_free_memory); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | /* Constructor for channel_data */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 447 | static void init_channel_elem(grpc_exec_ctx *exec_ctx, |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 448 | grpc_channel_element *elem, |
| 449 | grpc_channel_element_args *args) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 450 | channel_data *chand = elem->channel_data; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 451 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 452 | memset(chand, 0, sizeof(*chand)); |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 453 | |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 454 | GPR_ASSERT(args->is_last); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 455 | GPR_ASSERT(elem->filter == &grpc_client_channel_filter); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 456 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 457 | gpr_mu_init(&chand->mu_config); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 458 | grpc_closure_init(&chand->on_config_changed, cc_on_config_changed, chand); |
Craig Tiller | 11beb9a | 2015-11-24 10:29:32 -0800 | [diff] [blame] | 459 | chand->owning_stack = args->channel_stack; |
Craig Tiller | 9846503 | 2015-06-29 14:36:42 -0700 | [diff] [blame] | 460 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 461 | grpc_connectivity_state_init(&chand->state_tracker, GRPC_CHANNEL_IDLE, |
| 462 | "client_channel"); |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 463 | chand->interested_parties = grpc_pollset_set_create(); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | /* Destructor for channel_data */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 467 | static void destroy_channel_elem(grpc_exec_ctx *exec_ctx, |
| 468 | grpc_channel_element *elem) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 469 | channel_data *chand = elem->channel_data; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 470 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 471 | if (chand->resolver != NULL) { |
| 472 | grpc_resolver_shutdown(exec_ctx, chand->resolver); |
| 473 | GRPC_RESOLVER_UNREF(exec_ctx, chand->resolver, "channel"); |
| 474 | } |
| 475 | if (chand->lb_policy != NULL) { |
Craig Tiller | 1d881fb | 2015-12-01 07:39:04 -0800 | [diff] [blame] | 476 | grpc_pollset_set_del_pollset_set(exec_ctx, |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 477 | chand->lb_policy->interested_parties, |
| 478 | chand->interested_parties); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 479 | GRPC_LB_POLICY_UNREF(exec_ctx, chand->lb_policy, "channel"); |
| 480 | } |
| 481 | grpc_connectivity_state_destroy(exec_ctx, &chand->state_tracker); |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 482 | grpc_pollset_set_destroy(chand->interested_parties); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 483 | gpr_mu_destroy(&chand->mu_config); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 484 | } |
| 485 | |
Craig Tiller | 577c9b2 | 2015-11-02 14:11:15 -0800 | [diff] [blame] | 486 | static void cc_set_pollset(grpc_exec_ctx *exec_ctx, grpc_call_element *elem, |
| 487 | grpc_pollset *pollset) { |
| 488 | call_data *calld = elem->call_data; |
| 489 | calld->pollset = pollset; |
| 490 | } |
| 491 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 492 | const grpc_channel_filter grpc_client_channel_filter = { |
Craig Tiller | f40df23 | 2016-03-25 13:38:14 -0700 | [diff] [blame] | 493 | cc_start_transport_stream_op, |
| 494 | cc_start_transport_op, |
| 495 | sizeof(call_data), |
| 496 | init_call_elem, |
| 497 | cc_set_pollset, |
| 498 | destroy_call_elem, |
| 499 | sizeof(channel_data), |
| 500 | init_channel_elem, |
| 501 | destroy_channel_elem, |
| 502 | cc_get_peer, |
| 503 | "client-channel", |
Craig Tiller | 87d5b19 | 2015-04-16 14:37:57 -0700 | [diff] [blame] | 504 | }; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 505 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 506 | void grpc_client_channel_set_resolver(grpc_exec_ctx *exec_ctx, |
| 507 | grpc_channel_stack *channel_stack, |
| 508 | grpc_resolver *resolver) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 509 | /* post construction initialization: set the transport setup pointer */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 510 | grpc_channel_element *elem = grpc_channel_stack_last_element(channel_stack); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 511 | channel_data *chand = elem->channel_data; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 512 | gpr_mu_lock(&chand->mu_config); |
| 513 | GPR_ASSERT(!chand->resolver); |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 514 | chand->resolver = resolver; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 515 | GRPC_RESOLVER_REF(resolver, "channel"); |
| 516 | if (!grpc_closure_list_empty(chand->waiting_for_config_closures) || |
| 517 | chand->exit_idle_when_lb_policy_arrives) { |
| 518 | chand->started_resolving = 1; |
Craig Tiller | 906e3bc | 2015-11-24 07:31:31 -0800 | [diff] [blame] | 519 | GRPC_CHANNEL_STACK_REF(chand->owning_stack, "resolver"); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 520 | grpc_resolver_next(exec_ctx, resolver, &chand->incoming_configuration, |
| 521 | &chand->on_config_changed); |
| 522 | } |
| 523 | gpr_mu_unlock(&chand->mu_config); |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 524 | } |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 525 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 526 | grpc_connectivity_state grpc_client_channel_check_connectivity_state( |
| 527 | 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] | 528 | channel_data *chand = elem->channel_data; |
| 529 | grpc_connectivity_state out; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 530 | gpr_mu_lock(&chand->mu_config); |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 531 | out = grpc_connectivity_state_check(&chand->state_tracker, NULL); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 532 | if (out == GRPC_CHANNEL_IDLE && try_to_connect) { |
| 533 | if (chand->lb_policy != NULL) { |
| 534 | grpc_lb_policy_exit_idle(exec_ctx, chand->lb_policy); |
| 535 | } else { |
| 536 | chand->exit_idle_when_lb_policy_arrives = 1; |
| 537 | if (!chand->started_resolving && chand->resolver != NULL) { |
Craig Tiller | 906e3bc | 2015-11-24 07:31:31 -0800 | [diff] [blame] | 538 | GRPC_CHANNEL_STACK_REF(chand->owning_stack, "resolver"); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 539 | chand->started_resolving = 1; |
| 540 | grpc_resolver_next(exec_ctx, chand->resolver, |
| 541 | &chand->incoming_configuration, |
| 542 | &chand->on_config_changed); |
| 543 | } |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 544 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 545 | } |
| 546 | gpr_mu_unlock(&chand->mu_config); |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 547 | return out; |
| 548 | } |
| 549 | |
Craig Tiller | 86c9958 | 2015-11-25 15:22:26 -0800 | [diff] [blame] | 550 | typedef struct { |
| 551 | channel_data *chand; |
| 552 | grpc_pollset *pollset; |
| 553 | grpc_closure *on_complete; |
| 554 | grpc_closure my_closure; |
| 555 | } external_connectivity_watcher; |
| 556 | |
Craig Tiller | 1d881fb | 2015-12-01 07:39:04 -0800 | [diff] [blame] | 557 | 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] | 558 | grpc_error *error) { |
Craig Tiller | 86c9958 | 2015-11-25 15:22:26 -0800 | [diff] [blame] | 559 | external_connectivity_watcher *w = arg; |
| 560 | grpc_closure *follow_up = w->on_complete; |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 561 | grpc_pollset_set_del_pollset(exec_ctx, w->chand->interested_parties, |
Craig Tiller | 1d881fb | 2015-12-01 07:39:04 -0800 | [diff] [blame] | 562 | w->pollset); |
| 563 | GRPC_CHANNEL_STACK_UNREF(exec_ctx, w->chand->owning_stack, |
| 564 | "external_connectivity_watcher"); |
Craig Tiller | 86c9958 | 2015-11-25 15:22:26 -0800 | [diff] [blame] | 565 | gpr_free(w); |
Craig Tiller | 804ff71 | 2016-05-05 16:25:40 -0700 | [diff] [blame] | 566 | follow_up->cb(exec_ctx, follow_up->cb_arg, error); |
Craig Tiller | 86c9958 | 2015-11-25 15:22:26 -0800 | [diff] [blame] | 567 | } |
| 568 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 569 | void grpc_client_channel_watch_connectivity_state( |
Craig Tiller | 906e3bc | 2015-11-24 07:31:31 -0800 | [diff] [blame] | 570 | grpc_exec_ctx *exec_ctx, grpc_channel_element *elem, grpc_pollset *pollset, |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 571 | grpc_connectivity_state *state, grpc_closure *on_complete) { |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 572 | channel_data *chand = elem->channel_data; |
Craig Tiller | 86c9958 | 2015-11-25 15:22:26 -0800 | [diff] [blame] | 573 | external_connectivity_watcher *w = gpr_malloc(sizeof(*w)); |
| 574 | w->chand = chand; |
| 575 | w->pollset = pollset; |
| 576 | w->on_complete = on_complete; |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 577 | grpc_pollset_set_add_pollset(exec_ctx, chand->interested_parties, pollset); |
Craig Tiller | 86c9958 | 2015-11-25 15:22:26 -0800 | [diff] [blame] | 578 | grpc_closure_init(&w->my_closure, on_external_watch_complete, w); |
Craig Tiller | 1d881fb | 2015-12-01 07:39:04 -0800 | [diff] [blame] | 579 | GRPC_CHANNEL_STACK_REF(w->chand->owning_stack, |
| 580 | "external_connectivity_watcher"); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 581 | gpr_mu_lock(&chand->mu_config); |
| 582 | grpc_connectivity_state_notify_on_state_change( |
Craig Tiller | 86c9958 | 2015-11-25 15:22:26 -0800 | [diff] [blame] | 583 | exec_ctx, &chand->state_tracker, state, &w->my_closure); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 584 | gpr_mu_unlock(&chand->mu_config); |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 585 | } |