blob: e229a15260eb752b8501838f524a3627bedaf22d [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Craig Tillera93a25f2016-01-28 13:55:49 -08003 * Copyright 2015-2016, Google Inc.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08004 * 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 Tillereb3b12e2015-06-26 14:42:49 -070037#include <string.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080038
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080039#include <grpc/support/alloc.h>
40#include <grpc/support/log.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080041#include <grpc/support/sync.h>
42#include <grpc/support/useful.h>
43
Craig Tiller8910ac62015-10-08 16:49:15 -070044#include "src/core/channel/channel_args.h"
45#include "src/core/channel/connected_channel.h"
Craig Tiller577c9b22015-11-02 14:11:15 -080046#include "src/core/channel/subchannel_call_holder.h"
Craig Tiller8910ac62015-10-08 16:49:15 -070047#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 Nobleb7ebd3b2014-11-26 16:33:03 -080053/* Client channel implementation */
54
Craig Tiller577c9b22015-11-02 14:11:15 -080055typedef grpc_subchannel_call_holder call_data;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080056
Craig Tiller800dacb2015-10-06 09:10:26 -070057typedef struct client_channel_channel_data {
Craig Tillerf5f17122015-06-25 08:47:26 -070058 /** resolver for this channel */
59 grpc_resolver *resolver;
Craig Tiller20a3c352015-08-05 08:39:50 -070060 /** have we started resolving this channel */
61 int started_resolving;
Craig Tillerf5f17122015-06-25 08:47:26 -070062
Craig Tiller9d94b602015-07-01 14:23:18 -070063 /** mutex protecting client configuration, including all
64 variables below in this data structure */
Craig Tillerf5f17122015-06-25 08:47:26 -070065 gpr_mu mu_config;
Craig Tillerf5f17122015-06-25 08:47:26 -070066 /** currently active load balancer - guarded by mu_config */
67 grpc_lb_policy *lb_policy;
Craig Tillerf5f17122015-06-25 08:47:26 -070068 /** incoming configuration - set by resolver.next
69 guarded by mu_config */
70 grpc_client_config *incoming_configuration;
Craig Tiller3f475422015-06-25 10:43:05 -070071 /** a list of closures that are all waiting for config to come in */
Craig Tillerd9ccbbf2015-09-22 09:30:00 -070072 grpc_closure_list waiting_for_config_closures;
Craig Tiller3f475422015-06-25 10:43:05 -070073 /** resolver callback */
Craig Tiller33825112015-09-18 07:44:19 -070074 grpc_closure on_config_changed;
Craig Tiller3f475422015-06-25 10:43:05 -070075 /** connectivity state being tracked */
Craig Tillerca3e9d32015-06-27 18:37:27 -070076 grpc_connectivity_state_tracker state_tracker;
Craig Tiller48cb07c2015-07-15 16:16:15 -070077 /** when an lb_policy arrives, should we try to exit idle */
78 int exit_idle_when_lb_policy_arrives;
Craig Tiller906e3bc2015-11-24 07:31:31 -080079 /** owning stack */
80 grpc_channel_stack *owning_stack;
Craig Tiller69b093b2016-02-25 19:04:07 -080081 /** interested parties (owned) */
82 grpc_pollset_set *interested_parties;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080083} channel_data;
84
Craig Tillerd6c98df2015-08-18 09:33:44 -070085/** 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 Tiller1ada6ad2015-07-16 16:19:14 -070089 update the channel, and create a new watcher */
Craig Tillera82950e2015-09-22 12:33:20 -070090typedef struct {
Craig Tiller1ada6ad2015-07-16 16:19:14 -070091 channel_data *chand;
Craig Tiller33825112015-09-18 07:44:19 -070092 grpc_closure on_changed;
Craig Tiller1ada6ad2015-07-16 16:19:14 -070093 grpc_connectivity_state state;
94 grpc_lb_policy *lb_policy;
95} lb_policy_connectivity_watcher;
96
Craig Tillera82950e2015-09-22 12:33:20 -070097typedef struct {
Craig Tiller33825112015-09-18 07:44:19 -070098 grpc_closure closure;
Craig Tillereb3b12e2015-06-26 14:42:49 -070099 grpc_call_element *elem;
100} waiting_call;
101
Craig Tillera82950e2015-09-22 12:33:20 -0700102static char *cc_get_peer(grpc_exec_ctx *exec_ctx, grpc_call_element *elem) {
Craig Tiller906e3bc2015-11-24 07:31:31 -0800103 return grpc_subchannel_call_holder_get_peer(exec_ctx, elem->call_data);
Craig Tillereb3b12e2015-06-26 14:42:49 -0700104}
Craig Tillerf5f17122015-06-25 08:47:26 -0700105
Craig Tillera82950e2015-09-22 12:33:20 -0700106static void cc_start_transport_stream_op(grpc_exec_ctx *exec_ctx,
107 grpc_call_element *elem,
108 grpc_transport_stream_op *op) {
Craig Tiller577c9b22015-11-02 14:11:15 -0800109 GRPC_CALL_LOG_OP(GPR_INFO, elem, op);
110 grpc_subchannel_call_holder_perform_op(exec_ctx, elem->call_data, op);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800111}
112
Craig Tillera82950e2015-09-22 12:33:20 -0700113static 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 Tiller1ada6ad2015-07-16 16:19:14 -0700116
Craig Tiller8c0d96f2016-03-11 14:27:52 -0800117static void set_channel_connectivity_state_locked(grpc_exec_ctx *exec_ctx,
118 channel_data *chand,
119 grpc_connectivity_state state,
120 const char *reason) {
121 if ((state == GRPC_CHANNEL_TRANSIENT_FAILURE ||
122 state == GRPC_CHANNEL_FATAL_FAILURE) &&
123 chand->lb_policy != NULL) {
124 /* cancel fail-fast picks */
125 grpc_lb_policy_cancel_picks(
126 exec_ctx, chand->lb_policy,
127 /* mask= */ GRPC_INITIAL_METADATA_IGNORE_CONNECTIVITY,
128 /* check= */ 0);
129 }
130 grpc_connectivity_state_set(exec_ctx, &chand->state_tracker, state, reason);
131}
132
Craig Tillera82950e2015-09-22 12:33:20 -0700133static void on_lb_policy_state_changed_locked(
134 grpc_exec_ctx *exec_ctx, lb_policy_connectivity_watcher *w) {
Craig Tillercb2609f2015-11-24 17:19:19 -0800135 grpc_connectivity_state publish_state = w->state;
Craig Tiller5795da72015-09-17 15:27:13 -0700136 /* check if the notification is for a stale policy */
Craig Tillera82950e2015-09-22 12:33:20 -0700137 if (w->lb_policy != w->chand->lb_policy) return;
Craig Tiller5795da72015-09-17 15:27:13 -0700138
Craig Tiller1d881fb2015-12-01 07:39:04 -0800139 if (publish_state == GRPC_CHANNEL_FATAL_FAILURE &&
140 w->chand->resolver != NULL) {
Craig Tillercb2609f2015-11-24 17:19:19 -0800141 publish_state = GRPC_CHANNEL_TRANSIENT_FAILURE;
142 grpc_resolver_channel_saw_error(exec_ctx, w->chand->resolver);
Craig Tillerf62c4d52015-12-04 07:43:07 -0800143 GRPC_LB_POLICY_UNREF(exec_ctx, w->chand->lb_policy, "channel");
144 w->chand->lb_policy = NULL;
Craig Tillercb2609f2015-11-24 17:19:19 -0800145 }
Craig Tiller8c0d96f2016-03-11 14:27:52 -0800146 set_channel_connectivity_state_locked(exec_ctx, w->chand, publish_state,
147 "lb_changed");
Craig Tillera82950e2015-09-22 12:33:20 -0700148 if (w->state != GRPC_CHANNEL_FATAL_FAILURE) {
149 watch_lb_policy(exec_ctx, w->chand, w->lb_policy, w->state);
150 }
Craig Tiller5795da72015-09-17 15:27:13 -0700151}
152
Craig Tillera82950e2015-09-22 12:33:20 -0700153static void on_lb_policy_state_changed(grpc_exec_ctx *exec_ctx, void *arg,
Craig Tiller6c396862016-01-28 13:53:40 -0800154 bool iomgr_success) {
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700155 lb_policy_connectivity_watcher *w = arg;
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700156
Craig Tillera82950e2015-09-22 12:33:20 -0700157 gpr_mu_lock(&w->chand->mu_config);
158 on_lb_policy_state_changed_locked(exec_ctx, w);
159 gpr_mu_unlock(&w->chand->mu_config);
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700160
Craig Tiller906e3bc2015-11-24 07:31:31 -0800161 GRPC_CHANNEL_STACK_UNREF(exec_ctx, w->chand->owning_stack, "watch_lb_policy");
Craig Tillera82950e2015-09-22 12:33:20 -0700162 gpr_free(w);
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700163}
164
Craig Tillera82950e2015-09-22 12:33:20 -0700165static void watch_lb_policy(grpc_exec_ctx *exec_ctx, channel_data *chand,
166 grpc_lb_policy *lb_policy,
167 grpc_connectivity_state current_state) {
168 lb_policy_connectivity_watcher *w = gpr_malloc(sizeof(*w));
Craig Tiller906e3bc2015-11-24 07:31:31 -0800169 GRPC_CHANNEL_STACK_REF(chand->owning_stack, "watch_lb_policy");
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700170
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700171 w->chand = chand;
Craig Tillera82950e2015-09-22 12:33:20 -0700172 grpc_closure_init(&w->on_changed, on_lb_policy_state_changed, w);
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700173 w->state = current_state;
174 w->lb_policy = lb_policy;
Craig Tillera82950e2015-09-22 12:33:20 -0700175 grpc_lb_policy_notify_on_state_change(exec_ctx, lb_policy, &w->state,
176 &w->on_changed);
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700177}
178
Craig Tillera82950e2015-09-22 12:33:20 -0700179static void cc_on_config_changed(grpc_exec_ctx *exec_ctx, void *arg,
Craig Tiller6c396862016-01-28 13:53:40 -0800180 bool iomgr_success) {
Craig Tiller3f475422015-06-25 10:43:05 -0700181 channel_data *chand = arg;
182 grpc_lb_policy *lb_policy = NULL;
183 grpc_lb_policy *old_lb_policy;
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700184 grpc_connectivity_state state = GRPC_CHANNEL_TRANSIENT_FAILURE;
Craig Tiller48cb07c2015-07-15 16:16:15 -0700185 int exit_idle = 0;
Craig Tiller3f475422015-06-25 10:43:05 -0700186
Craig Tillera82950e2015-09-22 12:33:20 -0700187 if (chand->incoming_configuration != NULL) {
188 lb_policy = grpc_client_config_get_lb_policy(chand->incoming_configuration);
189 if (lb_policy != NULL) {
190 GRPC_LB_POLICY_REF(lb_policy, "channel");
191 GRPC_LB_POLICY_REF(lb_policy, "config_change");
192 state = grpc_lb_policy_check_connectivity(exec_ctx, lb_policy);
Craig Tiller45724b32015-09-22 10:42:19 -0700193 }
Craig Tiller3f475422015-06-25 10:43:05 -0700194
Craig Tillera82950e2015-09-22 12:33:20 -0700195 grpc_client_config_unref(exec_ctx, chand->incoming_configuration);
196 }
197
Craig Tiller3f475422015-06-25 10:43:05 -0700198 chand->incoming_configuration = NULL;
199
Craig Tiller86c99582015-11-25 15:22:26 -0800200 if (lb_policy != NULL) {
Craig Tiller69b093b2016-02-25 19:04:07 -0800201 grpc_pollset_set_add_pollset_set(exec_ctx, lb_policy->interested_parties,
202 chand->interested_parties);
Craig Tiller86c99582015-11-25 15:22:26 -0800203 }
204
Craig Tillera82950e2015-09-22 12:33:20 -0700205 gpr_mu_lock(&chand->mu_config);
Craig Tiller3f475422015-06-25 10:43:05 -0700206 old_lb_policy = chand->lb_policy;
207 chand->lb_policy = lb_policy;
Craig Tillera82950e2015-09-22 12:33:20 -0700208 if (lb_policy != NULL || chand->resolver == NULL /* disconnected */) {
Craig Tiller6c396862016-01-28 13:53:40 -0800209 grpc_exec_ctx_enqueue_list(exec_ctx, &chand->waiting_for_config_closures,
210 NULL);
Craig Tillera82950e2015-09-22 12:33:20 -0700211 }
212 if (lb_policy != NULL && chand->exit_idle_when_lb_policy_arrives) {
213 GRPC_LB_POLICY_REF(lb_policy, "exit_idle");
214 exit_idle = 1;
215 chand->exit_idle_when_lb_policy_arrives = 0;
216 }
Craig Tiller98465032015-06-29 14:36:42 -0700217
Craig Tillera82950e2015-09-22 12:33:20 -0700218 if (iomgr_success && chand->resolver) {
Craig Tiller8c0d96f2016-03-11 14:27:52 -0800219 set_channel_connectivity_state_locked(exec_ctx, chand, state,
220 "new_lb+resolver");
Craig Tillera82950e2015-09-22 12:33:20 -0700221 if (lb_policy != NULL) {
222 watch_lb_policy(exec_ctx, chand, lb_policy, state);
Craig Tiller45724b32015-09-22 10:42:19 -0700223 }
Craig Tiller906e3bc2015-11-24 07:31:31 -0800224 GRPC_CHANNEL_STACK_REF(chand->owning_stack, "resolver");
Craig Tiller76a5c0e2016-03-09 09:05:30 -0800225 grpc_resolver_next(exec_ctx, chand->resolver,
226 &chand->incoming_configuration,
Craig Tillera82950e2015-09-22 12:33:20 -0700227 &chand->on_config_changed);
Craig Tiller76a5c0e2016-03-09 09:05:30 -0800228 gpr_mu_unlock(&chand->mu_config);
Craig Tillera82950e2015-09-22 12:33:20 -0700229 } else {
Craig Tiller76a5c0e2016-03-09 09:05:30 -0800230 if (chand->resolver != NULL) {
231 grpc_resolver_shutdown(exec_ctx, chand->resolver);
232 GRPC_RESOLVER_UNREF(exec_ctx, chand->resolver, "channel");
233 chand->resolver = NULL;
234 }
Craig Tiller8c0d96f2016-03-11 14:27:52 -0800235 set_channel_connectivity_state_locked(
236 exec_ctx, chand, GRPC_CHANNEL_FATAL_FAILURE, "resolver_gone");
Craig Tillera82950e2015-09-22 12:33:20 -0700237 gpr_mu_unlock(&chand->mu_config);
Craig Tillera82950e2015-09-22 12:33:20 -0700238 }
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700239
Craig Tillera82950e2015-09-22 12:33:20 -0700240 if (exit_idle) {
241 grpc_lb_policy_exit_idle(exec_ctx, lb_policy);
242 GRPC_LB_POLICY_UNREF(exec_ctx, lb_policy, "exit_idle");
243 }
Craig Tiller1ada6ad2015-07-16 16:19:14 -0700244
Craig Tillera82950e2015-09-22 12:33:20 -0700245 if (old_lb_policy != NULL) {
Craig Tiller69b093b2016-02-25 19:04:07 -0800246 grpc_pollset_set_del_pollset_set(
247 exec_ctx, old_lb_policy->interested_parties, chand->interested_parties);
Craig Tillera82950e2015-09-22 12:33:20 -0700248 GRPC_LB_POLICY_UNREF(exec_ctx, old_lb_policy, "channel");
249 }
Craig Tiller000cd8f2015-09-18 07:20:29 -0700250
Craig Tillera82950e2015-09-22 12:33:20 -0700251 if (lb_policy != NULL) {
252 GRPC_LB_POLICY_UNREF(exec_ctx, lb_policy, "config_change");
253 }
Craig Tiller45724b32015-09-22 10:42:19 -0700254
Craig Tiller906e3bc2015-11-24 07:31:31 -0800255 GRPC_CHANNEL_STACK_UNREF(exec_ctx, chand->owning_stack, "resolver");
Craig Tiller3f475422015-06-25 10:43:05 -0700256}
257
Craig Tillera82950e2015-09-22 12:33:20 -0700258static void cc_start_transport_op(grpc_exec_ctx *exec_ctx,
259 grpc_channel_element *elem,
260 grpc_transport_op *op) {
Craig Tillerca3e9d32015-06-27 18:37:27 -0700261 channel_data *chand = elem->channel_data;
Craig Tiller000cd8f2015-09-18 07:20:29 -0700262
Craig Tiller6c396862016-01-28 13:53:40 -0800263 grpc_exec_ctx_enqueue(exec_ctx, op->on_consumed, true, NULL);
Craig Tillerca3e9d32015-06-27 18:37:27 -0700264
Craig Tillerd7f12e32016-03-03 10:08:31 -0800265 GPR_ASSERT(op->set_accept_stream == false);
Craig Tiller28bf8912015-12-07 16:07:04 -0800266 if (op->bind_pollset != NULL) {
Craig Tiller69b093b2016-02-25 19:04:07 -0800267 grpc_pollset_set_add_pollset(exec_ctx, chand->interested_parties,
Craig Tillere2c62372015-12-07 16:11:03 -0800268 op->bind_pollset);
Craig Tiller28bf8912015-12-07 16:07:04 -0800269 }
Craig Tillerca3e9d32015-06-27 18:37:27 -0700270
Craig Tillera82950e2015-09-22 12:33:20 -0700271 gpr_mu_lock(&chand->mu_config);
272 if (op->on_connectivity_state_change != NULL) {
273 grpc_connectivity_state_notify_on_state_change(
274 exec_ctx, &chand->state_tracker, op->connectivity_state,
275 op->on_connectivity_state_change);
276 op->on_connectivity_state_change = NULL;
277 op->connectivity_state = NULL;
278 }
279
Craig Tiller26dab312015-12-07 14:43:47 -0800280 if (op->send_ping != NULL) {
Craig Tiller87b71e22015-12-07 15:14:14 -0800281 if (chand->lb_policy == NULL) {
Craig Tiller6c396862016-01-28 13:53:40 -0800282 grpc_exec_ctx_enqueue(exec_ctx, op->send_ping, false, NULL);
Craig Tiller26dab312015-12-07 14:43:47 -0800283 } else {
Craig Tiller28bf8912015-12-07 16:07:04 -0800284 grpc_lb_policy_ping_one(exec_ctx, chand->lb_policy, op->send_ping);
Craig Tiller26dab312015-12-07 14:43:47 -0800285 op->bind_pollset = NULL;
286 }
287 op->send_ping = NULL;
288 }
289
Craig Tillera82950e2015-09-22 12:33:20 -0700290 if (op->disconnect && chand->resolver != NULL) {
Craig Tiller8c0d96f2016-03-11 14:27:52 -0800291 set_channel_connectivity_state_locked(
292 exec_ctx, chand, GRPC_CHANNEL_FATAL_FAILURE, "disconnect");
Craig Tiller76a5c0e2016-03-09 09:05:30 -0800293 grpc_resolver_shutdown(exec_ctx, chand->resolver);
294 GRPC_RESOLVER_UNREF(exec_ctx, chand->resolver, "channel");
Craig Tillera82950e2015-09-22 12:33:20 -0700295 chand->resolver = NULL;
296 if (chand->lb_policy != NULL) {
Craig Tiller1d881fb2015-12-01 07:39:04 -0800297 grpc_pollset_set_del_pollset_set(exec_ctx,
Craig Tiller69b093b2016-02-25 19:04:07 -0800298 chand->lb_policy->interested_parties,
299 chand->interested_parties);
Craig Tillera82950e2015-09-22 12:33:20 -0700300 GRPC_LB_POLICY_UNREF(exec_ctx, chand->lb_policy, "channel");
301 chand->lb_policy = NULL;
Craig Tillerd2cc4592015-07-01 07:50:47 -0700302 }
Craig Tillera82950e2015-09-22 12:33:20 -0700303 }
304 gpr_mu_unlock(&chand->mu_config);
Craig Tillerca3e9d32015-06-27 18:37:27 -0700305}
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800306
Craig Tiller577c9b22015-11-02 14:11:15 -0800307typedef struct {
308 grpc_metadata_batch *initial_metadata;
Craig Tiller8c0d96f2016-03-11 14:27:52 -0800309 uint32_t initial_metadata_flags;
Craig Tillerb5585d42015-11-17 07:18:31 -0800310 grpc_connected_subchannel **connected_subchannel;
Craig Tiller577c9b22015-11-02 14:11:15 -0800311 grpc_closure *on_ready;
312 grpc_call_element *elem;
313 grpc_closure closure;
314} continue_picking_args;
315
316static int cc_pick_subchannel(grpc_exec_ctx *exec_ctx, void *arg,
317 grpc_metadata_batch *initial_metadata,
Craig Tiller8c0d96f2016-03-11 14:27:52 -0800318 uint32_t initial_metadata_flags,
Craig Tillerb5585d42015-11-17 07:18:31 -0800319 grpc_connected_subchannel **connected_subchannel,
Craig Tiller577c9b22015-11-02 14:11:15 -0800320 grpc_closure *on_ready);
321
Craig Tiller6c396862016-01-28 13:53:40 -0800322static void continue_picking(grpc_exec_ctx *exec_ctx, void *arg, bool success) {
Craig Tiller577c9b22015-11-02 14:11:15 -0800323 continue_picking_args *cpa = arg;
324 if (!success) {
Craig Tiller6c396862016-01-28 13:53:40 -0800325 grpc_exec_ctx_enqueue(exec_ctx, cpa->on_ready, false, NULL);
Craig Tillerb5585d42015-11-17 07:18:31 -0800326 } else if (cpa->connected_subchannel == NULL) {
Craig Tiller577c9b22015-11-02 14:11:15 -0800327 /* cancelled, do nothing */
328 } else if (cc_pick_subchannel(exec_ctx, cpa->elem, cpa->initial_metadata,
Craig Tiller8c0d96f2016-03-11 14:27:52 -0800329 cpa->initial_metadata_flags,
Craig Tillerb5585d42015-11-17 07:18:31 -0800330 cpa->connected_subchannel, cpa->on_ready)) {
Craig Tiller6c396862016-01-28 13:53:40 -0800331 grpc_exec_ctx_enqueue(exec_ctx, cpa->on_ready, true, NULL);
Craig Tiller577c9b22015-11-02 14:11:15 -0800332 }
333 gpr_free(cpa);
334}
335
336static int cc_pick_subchannel(grpc_exec_ctx *exec_ctx, void *elemp,
337 grpc_metadata_batch *initial_metadata,
Craig Tiller8c0d96f2016-03-11 14:27:52 -0800338 uint32_t initial_metadata_flags,
Craig Tillerb5585d42015-11-17 07:18:31 -0800339 grpc_connected_subchannel **connected_subchannel,
Craig Tiller577c9b22015-11-02 14:11:15 -0800340 grpc_closure *on_ready) {
341 grpc_call_element *elem = elemp;
342 channel_data *chand = elem->channel_data;
343 call_data *calld = elem->call_data;
344 continue_picking_args *cpa;
345 grpc_closure *closure;
346
Craig Tillerb5585d42015-11-17 07:18:31 -0800347 GPR_ASSERT(connected_subchannel);
Craig Tiller577c9b22015-11-02 14:11:15 -0800348
349 gpr_mu_lock(&chand->mu_config);
350 if (initial_metadata == NULL) {
351 if (chand->lb_policy != NULL) {
Craig Tillerab33b482015-11-21 08:11:04 -0800352 grpc_lb_policy_cancel_pick(exec_ctx, chand->lb_policy,
353 connected_subchannel);
Craig Tiller577c9b22015-11-02 14:11:15 -0800354 }
355 for (closure = chand->waiting_for_config_closures.head; closure != NULL;
356 closure = grpc_closure_next(closure)) {
357 cpa = closure->cb_arg;
Craig Tillerb5585d42015-11-17 07:18:31 -0800358 if (cpa->connected_subchannel == connected_subchannel) {
359 cpa->connected_subchannel = NULL;
Craig Tiller6c396862016-01-28 13:53:40 -0800360 grpc_exec_ctx_enqueue(exec_ctx, cpa->on_ready, false, NULL);
Craig Tiller577c9b22015-11-02 14:11:15 -0800361 }
362 }
363 gpr_mu_unlock(&chand->mu_config);
364 return 1;
365 }
366 if (chand->lb_policy != NULL) {
Craig Tiller86c0f8a2015-12-01 20:05:40 -0800367 grpc_lb_policy *lb_policy = chand->lb_policy;
368 int r;
369 GRPC_LB_POLICY_REF(lb_policy, "cc_pick_subchannel");
Craig Tiller577c9b22015-11-02 14:11:15 -0800370 gpr_mu_unlock(&chand->mu_config);
Craig Tiller86c0f8a2015-12-01 20:05:40 -0800371 r = grpc_lb_policy_pick(exec_ctx, lb_policy, calld->pollset,
Craig Tiller8c0d96f2016-03-11 14:27:52 -0800372 initial_metadata, initial_metadata_flags,
373 connected_subchannel, on_ready);
Craig Tiller86c0f8a2015-12-01 20:05:40 -0800374 GRPC_LB_POLICY_UNREF(exec_ctx, lb_policy, "cc_pick_subchannel");
Craig Tiller577c9b22015-11-02 14:11:15 -0800375 return r;
376 }
377 if (chand->resolver != NULL && !chand->started_resolving) {
378 chand->started_resolving = 1;
Craig Tiller906e3bc2015-11-24 07:31:31 -0800379 GRPC_CHANNEL_STACK_REF(chand->owning_stack, "resolver");
Craig Tiller577c9b22015-11-02 14:11:15 -0800380 grpc_resolver_next(exec_ctx, chand->resolver,
381 &chand->incoming_configuration,
382 &chand->on_config_changed);
383 }
384 cpa = gpr_malloc(sizeof(*cpa));
385 cpa->initial_metadata = initial_metadata;
Craig Tiller8c0d96f2016-03-11 14:27:52 -0800386 cpa->initial_metadata_flags = initial_metadata_flags;
Craig Tillerb5585d42015-11-17 07:18:31 -0800387 cpa->connected_subchannel = connected_subchannel;
Craig Tiller577c9b22015-11-02 14:11:15 -0800388 cpa->on_ready = on_ready;
389 cpa->elem = elem;
390 grpc_closure_init(&cpa->closure, continue_picking, cpa);
391 grpc_closure_list_add(&chand->waiting_for_config_closures, &cpa->closure, 1);
392 gpr_mu_unlock(&chand->mu_config);
393 return 0;
394}
395
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800396/* Constructor for call_data */
Craig Tillera82950e2015-09-22 12:33:20 -0700397static void init_call_elem(grpc_exec_ctx *exec_ctx, grpc_call_element *elem,
Craig Tiller577c9b22015-11-02 14:11:15 -0800398 grpc_call_element_args *args) {
Craig Tiller11beb9a2015-11-24 10:29:32 -0800399 grpc_subchannel_call_holder_init(elem->call_data, cc_pick_subchannel, elem,
400 args->call_stack);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800401}
402
403/* Destructor for call_data */
Craig Tillera82950e2015-09-22 12:33:20 -0700404static void destroy_call_elem(grpc_exec_ctx *exec_ctx,
405 grpc_call_element *elem) {
Craig Tiller577c9b22015-11-02 14:11:15 -0800406 grpc_subchannel_call_holder_destroy(exec_ctx, elem->call_data);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800407}
408
409/* Constructor for channel_data */
Craig Tillera82950e2015-09-22 12:33:20 -0700410static void init_channel_elem(grpc_exec_ctx *exec_ctx,
Craig Tiller577c9b22015-11-02 14:11:15 -0800411 grpc_channel_element *elem,
412 grpc_channel_element_args *args) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800413 channel_data *chand = elem->channel_data;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800414
Craig Tillera82950e2015-09-22 12:33:20 -0700415 memset(chand, 0, sizeof(*chand));
Craig Tillereb3b12e2015-06-26 14:42:49 -0700416
Craig Tiller577c9b22015-11-02 14:11:15 -0800417 GPR_ASSERT(args->is_last);
Craig Tillera82950e2015-09-22 12:33:20 -0700418 GPR_ASSERT(elem->filter == &grpc_client_channel_filter);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800419
Craig Tillera82950e2015-09-22 12:33:20 -0700420 gpr_mu_init(&chand->mu_config);
Craig Tillera82950e2015-09-22 12:33:20 -0700421 grpc_closure_init(&chand->on_config_changed, cc_on_config_changed, chand);
Craig Tiller11beb9a2015-11-24 10:29:32 -0800422 chand->owning_stack = args->channel_stack;
Craig Tiller98465032015-06-29 14:36:42 -0700423
Craig Tillera82950e2015-09-22 12:33:20 -0700424 grpc_connectivity_state_init(&chand->state_tracker, GRPC_CHANNEL_IDLE,
425 "client_channel");
Craig Tiller69b093b2016-02-25 19:04:07 -0800426 chand->interested_parties = grpc_pollset_set_create();
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800427}
428
429/* Destructor for channel_data */
Craig Tillera82950e2015-09-22 12:33:20 -0700430static void destroy_channel_elem(grpc_exec_ctx *exec_ctx,
431 grpc_channel_element *elem) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800432 channel_data *chand = elem->channel_data;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800433
Craig Tillera82950e2015-09-22 12:33:20 -0700434 if (chand->resolver != NULL) {
435 grpc_resolver_shutdown(exec_ctx, chand->resolver);
436 GRPC_RESOLVER_UNREF(exec_ctx, chand->resolver, "channel");
437 }
438 if (chand->lb_policy != NULL) {
Craig Tiller1d881fb2015-12-01 07:39:04 -0800439 grpc_pollset_set_del_pollset_set(exec_ctx,
Craig Tiller69b093b2016-02-25 19:04:07 -0800440 chand->lb_policy->interested_parties,
441 chand->interested_parties);
Craig Tillera82950e2015-09-22 12:33:20 -0700442 GRPC_LB_POLICY_UNREF(exec_ctx, chand->lb_policy, "channel");
443 }
444 grpc_connectivity_state_destroy(exec_ctx, &chand->state_tracker);
Craig Tiller69b093b2016-02-25 19:04:07 -0800445 grpc_pollset_set_destroy(chand->interested_parties);
Craig Tillera82950e2015-09-22 12:33:20 -0700446 gpr_mu_destroy(&chand->mu_config);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800447}
448
Craig Tiller577c9b22015-11-02 14:11:15 -0800449static void cc_set_pollset(grpc_exec_ctx *exec_ctx, grpc_call_element *elem,
450 grpc_pollset *pollset) {
451 call_data *calld = elem->call_data;
452 calld->pollset = pollset;
453}
454
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800455const grpc_channel_filter grpc_client_channel_filter = {
Craig Tiller81416962016-02-25 20:22:11 -0800456 cc_start_transport_stream_op, cc_start_transport_op, sizeof(call_data),
457 init_call_elem, cc_set_pollset, destroy_call_elem, sizeof(channel_data),
458 init_channel_elem, destroy_channel_elem, cc_get_peer, "client-channel",
Craig Tiller87d5b192015-04-16 14:37:57 -0700459};
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800460
Craig Tillera82950e2015-09-22 12:33:20 -0700461void grpc_client_channel_set_resolver(grpc_exec_ctx *exec_ctx,
462 grpc_channel_stack *channel_stack,
463 grpc_resolver *resolver) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800464 /* post construction initialization: set the transport setup pointer */
Craig Tillera82950e2015-09-22 12:33:20 -0700465 grpc_channel_element *elem = grpc_channel_stack_last_element(channel_stack);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800466 channel_data *chand = elem->channel_data;
Craig Tillera82950e2015-09-22 12:33:20 -0700467 gpr_mu_lock(&chand->mu_config);
468 GPR_ASSERT(!chand->resolver);
Craig Tillerf5f17122015-06-25 08:47:26 -0700469 chand->resolver = resolver;
Craig Tillera82950e2015-09-22 12:33:20 -0700470 GRPC_RESOLVER_REF(resolver, "channel");
471 if (!grpc_closure_list_empty(chand->waiting_for_config_closures) ||
472 chand->exit_idle_when_lb_policy_arrives) {
473 chand->started_resolving = 1;
Craig Tiller906e3bc2015-11-24 07:31:31 -0800474 GRPC_CHANNEL_STACK_REF(chand->owning_stack, "resolver");
Craig Tillera82950e2015-09-22 12:33:20 -0700475 grpc_resolver_next(exec_ctx, resolver, &chand->incoming_configuration,
476 &chand->on_config_changed);
477 }
478 gpr_mu_unlock(&chand->mu_config);
Craig Tiller190d3602015-02-18 09:23:38 -0800479}
Craig Tiller48cb07c2015-07-15 16:16:15 -0700480
Craig Tillera82950e2015-09-22 12:33:20 -0700481grpc_connectivity_state grpc_client_channel_check_connectivity_state(
482 grpc_exec_ctx *exec_ctx, grpc_channel_element *elem, int try_to_connect) {
Craig Tiller48cb07c2015-07-15 16:16:15 -0700483 channel_data *chand = elem->channel_data;
484 grpc_connectivity_state out;
Craig Tillera82950e2015-09-22 12:33:20 -0700485 gpr_mu_lock(&chand->mu_config);
486 out = grpc_connectivity_state_check(&chand->state_tracker);
487 if (out == GRPC_CHANNEL_IDLE && try_to_connect) {
488 if (chand->lb_policy != NULL) {
489 grpc_lb_policy_exit_idle(exec_ctx, chand->lb_policy);
490 } else {
491 chand->exit_idle_when_lb_policy_arrives = 1;
492 if (!chand->started_resolving && chand->resolver != NULL) {
Craig Tiller906e3bc2015-11-24 07:31:31 -0800493 GRPC_CHANNEL_STACK_REF(chand->owning_stack, "resolver");
Craig Tillera82950e2015-09-22 12:33:20 -0700494 chand->started_resolving = 1;
495 grpc_resolver_next(exec_ctx, chand->resolver,
496 &chand->incoming_configuration,
497 &chand->on_config_changed);
498 }
Craig Tiller48cb07c2015-07-15 16:16:15 -0700499 }
Craig Tillera82950e2015-09-22 12:33:20 -0700500 }
501 gpr_mu_unlock(&chand->mu_config);
Craig Tiller48cb07c2015-07-15 16:16:15 -0700502 return out;
503}
504
Craig Tiller86c99582015-11-25 15:22:26 -0800505typedef struct {
506 channel_data *chand;
507 grpc_pollset *pollset;
508 grpc_closure *on_complete;
509 grpc_closure my_closure;
510} external_connectivity_watcher;
511
Craig Tiller1d881fb2015-12-01 07:39:04 -0800512static void on_external_watch_complete(grpc_exec_ctx *exec_ctx, void *arg,
Craig Tiller6c396862016-01-28 13:53:40 -0800513 bool iomgr_success) {
Craig Tiller86c99582015-11-25 15:22:26 -0800514 external_connectivity_watcher *w = arg;
515 grpc_closure *follow_up = w->on_complete;
Craig Tiller69b093b2016-02-25 19:04:07 -0800516 grpc_pollset_set_del_pollset(exec_ctx, w->chand->interested_parties,
Craig Tiller1d881fb2015-12-01 07:39:04 -0800517 w->pollset);
518 GRPC_CHANNEL_STACK_UNREF(exec_ctx, w->chand->owning_stack,
519 "external_connectivity_watcher");
Craig Tiller86c99582015-11-25 15:22:26 -0800520 gpr_free(w);
521 follow_up->cb(exec_ctx, follow_up->cb_arg, iomgr_success);
522}
523
Craig Tillera82950e2015-09-22 12:33:20 -0700524void grpc_client_channel_watch_connectivity_state(
Craig Tiller906e3bc2015-11-24 07:31:31 -0800525 grpc_exec_ctx *exec_ctx, grpc_channel_element *elem, grpc_pollset *pollset,
Craig Tillera82950e2015-09-22 12:33:20 -0700526 grpc_connectivity_state *state, grpc_closure *on_complete) {
Craig Tiller48cb07c2015-07-15 16:16:15 -0700527 channel_data *chand = elem->channel_data;
Craig Tiller86c99582015-11-25 15:22:26 -0800528 external_connectivity_watcher *w = gpr_malloc(sizeof(*w));
529 w->chand = chand;
530 w->pollset = pollset;
531 w->on_complete = on_complete;
Craig Tiller69b093b2016-02-25 19:04:07 -0800532 grpc_pollset_set_add_pollset(exec_ctx, chand->interested_parties, pollset);
Craig Tiller86c99582015-11-25 15:22:26 -0800533 grpc_closure_init(&w->my_closure, on_external_watch_complete, w);
Craig Tiller1d881fb2015-12-01 07:39:04 -0800534 GRPC_CHANNEL_STACK_REF(w->chand->owning_stack,
535 "external_connectivity_watcher");
Craig Tillera82950e2015-09-22 12:33:20 -0700536 gpr_mu_lock(&chand->mu_config);
537 grpc_connectivity_state_notify_on_state_change(
Craig Tiller86c99582015-11-25 15:22:26 -0800538 exec_ctx, &chand->state_tracker, state, &w->my_closure);
Craig Tillera82950e2015-09-22 12:33:20 -0700539 gpr_mu_unlock(&chand->mu_config);
Craig Tiller48cb07c2015-07-15 16:16:15 -0700540}