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" |
| 46 | #include "src/core/iomgr/iomgr.h" |
| 47 | #include "src/core/profiling/timers.h" |
| 48 | #include "src/core/support/string.h" |
| 49 | #include "src/core/surface/channel.h" |
| 50 | #include "src/core/transport/connectivity_state.h" |
| 51 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 52 | /* Client channel implementation */ |
| 53 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 54 | typedef struct call_data call_data; |
| 55 | |
Craig Tiller | 800dacb | 2015-10-06 09:10:26 -0700 | [diff] [blame] | 56 | typedef struct client_channel_channel_data { |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 57 | /** metadata context for this channel */ |
Craig Tiller | e70413c | 2015-04-24 10:12:34 -0700 | [diff] [blame] | 58 | grpc_mdctx *mdctx; |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 59 | /** resolver for this channel */ |
| 60 | grpc_resolver *resolver; |
Craig Tiller | 20a3c35 | 2015-08-05 08:39:50 -0700 | [diff] [blame] | 61 | /** have we started resolving this channel */ |
| 62 | int started_resolving; |
Craig Tiller | 50bc609 | 2015-07-01 14:25:19 -0700 | [diff] [blame] | 63 | /** master channel - the grpc_channel instance that ultimately owns |
| 64 | this channel_data via its channel stack. |
| 65 | We occasionally use this to bump the refcount on the master channel |
| 66 | to keep ourselves alive through an asynchronous operation. */ |
Craig Tiller | 9846503 | 2015-06-29 14:36:42 -0700 | [diff] [blame] | 67 | grpc_channel *master; |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 68 | |
Craig Tiller | 9d94b60 | 2015-07-01 14:23:18 -0700 | [diff] [blame] | 69 | /** mutex protecting client configuration, including all |
| 70 | variables below in this data structure */ |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 71 | gpr_mu mu_config; |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 72 | /** currently active load balancer - guarded by mu_config */ |
| 73 | grpc_lb_policy *lb_policy; |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 74 | /** incoming configuration - set by resolver.next |
| 75 | guarded by mu_config */ |
| 76 | grpc_client_config *incoming_configuration; |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 77 | /** 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] | 78 | grpc_closure_list waiting_for_config_closures; |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 79 | /** resolver callback */ |
Craig Tiller | 3382511 | 2015-09-18 07:44:19 -0700 | [diff] [blame] | 80 | grpc_closure on_config_changed; |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 81 | /** connectivity state being tracked */ |
Craig Tiller | ca3e9d3 | 2015-06-27 18:37:27 -0700 | [diff] [blame] | 82 | grpc_connectivity_state_tracker state_tracker; |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 83 | /** when an lb_policy arrives, should we try to exit idle */ |
| 84 | int exit_idle_when_lb_policy_arrives; |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 85 | /** pollset_set of interested parties in a new connection */ |
| 86 | grpc_pollset_set pollset_set; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 87 | } channel_data; |
| 88 | |
Craig Tiller | d6c98df | 2015-08-18 09:33:44 -0700 | [diff] [blame] | 89 | /** We create one watcher for each new lb_policy that is returned from a |
| 90 | resolver, |
| 91 | to watch for state changes from the lb_policy. When a state change is seen, |
| 92 | we |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 93 | update the channel, and create a new watcher */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 94 | typedef struct { |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 95 | channel_data *chand; |
Craig Tiller | 3382511 | 2015-09-18 07:44:19 -0700 | [diff] [blame] | 96 | grpc_closure on_changed; |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 97 | grpc_connectivity_state state; |
| 98 | grpc_lb_policy *lb_policy; |
| 99 | } lb_policy_connectivity_watcher; |
| 100 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 101 | typedef enum { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 102 | CALL_CREATED, |
Craig Tiller | 5d44c06 | 2015-07-01 08:55:28 -0700 | [diff] [blame] | 103 | CALL_WAITING_FOR_SEND, |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 104 | CALL_WAITING_FOR_CONFIG, |
| 105 | CALL_WAITING_FOR_PICK, |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 106 | CALL_WAITING_FOR_CALL, |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 107 | CALL_ACTIVE, |
| 108 | CALL_CANCELLED |
| 109 | } call_state; |
| 110 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 111 | struct call_data { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 112 | /* owning element */ |
| 113 | grpc_call_element *elem; |
| 114 | |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 115 | gpr_mu mu_state; |
| 116 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 117 | call_state state; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 118 | gpr_timespec deadline; |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 119 | grpc_subchannel *picked_channel; |
Craig Tiller | 3382511 | 2015-09-18 07:44:19 -0700 | [diff] [blame] | 120 | grpc_closure async_setup_task; |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 121 | grpc_transport_stream_op waiting_op; |
| 122 | /* our child call stack */ |
| 123 | grpc_subchannel_call *subchannel_call; |
| 124 | grpc_linked_mdelem status; |
| 125 | grpc_linked_mdelem details; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 126 | }; |
| 127 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 128 | static grpc_closure *merge_into_waiting_op(grpc_call_element *elem, |
| 129 | grpc_transport_stream_op *new_op) |
| 130 | GRPC_MUST_USE_RESULT; |
Craig Tiller | 5d44c06 | 2015-07-01 08:55:28 -0700 | [diff] [blame] | 131 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 132 | static void handle_op_after_cancellation(grpc_exec_ctx *exec_ctx, |
| 133 | grpc_call_element *elem, |
| 134 | grpc_transport_stream_op *op) { |
Craig Tiller | e70413c | 2015-04-24 10:12:34 -0700 | [diff] [blame] | 135 | call_data *calld = elem->call_data; |
| 136 | channel_data *chand = elem->channel_data; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 137 | if (op->send_ops) { |
| 138 | grpc_stream_ops_unref_owned_objects(op->send_ops->ops, op->send_ops->nops); |
| 139 | op->on_done_send->cb(exec_ctx, op->on_done_send->cb_arg, 0); |
| 140 | } |
| 141 | if (op->recv_ops) { |
| 142 | char status[GPR_LTOA_MIN_BUFSIZE]; |
| 143 | grpc_metadata_batch mdb; |
| 144 | gpr_ltoa(GRPC_STATUS_CANCELLED, status); |
| 145 | calld->status.md = |
| 146 | grpc_mdelem_from_strings(chand->mdctx, "grpc-status", status); |
| 147 | calld->details.md = |
| 148 | grpc_mdelem_from_strings(chand->mdctx, "grpc-message", "Cancelled"); |
| 149 | calld->status.prev = calld->details.next = NULL; |
| 150 | calld->status.next = &calld->details; |
| 151 | calld->details.prev = &calld->status; |
| 152 | mdb.list.head = &calld->status; |
| 153 | mdb.list.tail = &calld->details; |
| 154 | mdb.garbage.head = mdb.garbage.tail = NULL; |
| 155 | mdb.deadline = gpr_inf_future(GPR_CLOCK_REALTIME); |
| 156 | grpc_sopb_add_metadata(op->recv_ops, mdb); |
| 157 | *op->recv_state = GRPC_STREAM_CLOSED; |
| 158 | op->on_done_recv->cb(exec_ctx, op->on_done_recv->cb_arg, 1); |
| 159 | } |
| 160 | if (op->on_consumed) { |
| 161 | op->on_consumed->cb(exec_ctx, op->on_consumed->cb_arg, 0); |
| 162 | } |
Craig Tiller | e70413c | 2015-04-24 10:12:34 -0700 | [diff] [blame] | 163 | } |
Craig Tiller | 8b976d0 | 2015-02-05 21:41:23 -0800 | [diff] [blame] | 164 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 165 | typedef struct { |
Craig Tiller | 3382511 | 2015-09-18 07:44:19 -0700 | [diff] [blame] | 166 | grpc_closure closure; |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 167 | grpc_call_element *elem; |
| 168 | } waiting_call; |
| 169 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 170 | static void perform_transport_stream_op(grpc_exec_ctx *exec_ctx, |
| 171 | grpc_call_element *elem, |
| 172 | grpc_transport_stream_op *op, |
| 173 | int continuation); |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 174 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 175 | static void continue_with_pick(grpc_exec_ctx *exec_ctx, void *arg, |
| 176 | int iomgr_success) { |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 177 | waiting_call *wc = arg; |
| 178 | call_data *calld = wc->elem->call_data; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 179 | perform_transport_stream_op(exec_ctx, wc->elem, &calld->waiting_op, 1); |
| 180 | gpr_free(wc); |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 181 | } |
| 182 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 183 | static void add_to_lb_policy_wait_queue_locked_state_config( |
| 184 | grpc_call_element *elem) { |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 185 | channel_data *chand = elem->channel_data; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 186 | waiting_call *wc = gpr_malloc(sizeof(*wc)); |
| 187 | grpc_closure_init(&wc->closure, continue_with_pick, wc); |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 188 | wc->elem = elem; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 189 | grpc_closure_list_add(&chand->waiting_for_config_closures, &wc->closure, 1); |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 190 | } |
| 191 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 192 | static int is_empty(void *p, int len) { |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 193 | char *ptr = p; |
| 194 | int i; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 195 | for (i = 0; i < len; i++) { |
| 196 | if (ptr[i] != 0) return 0; |
| 197 | } |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 198 | return 1; |
| 199 | } |
| 200 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 201 | static void started_call(grpc_exec_ctx *exec_ctx, void *arg, |
| 202 | int iomgr_success) { |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 203 | call_data *calld = arg; |
| 204 | grpc_transport_stream_op op; |
| 205 | int have_waiting; |
| 206 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 207 | gpr_mu_lock(&calld->mu_state); |
| 208 | if (calld->state == CALL_CANCELLED && calld->subchannel_call != NULL) { |
| 209 | memset(&op, 0, sizeof(op)); |
| 210 | op.cancel_with_status = GRPC_STATUS_CANCELLED; |
| 211 | gpr_mu_unlock(&calld->mu_state); |
| 212 | grpc_subchannel_call_process_op(exec_ctx, calld->subchannel_call, &op); |
| 213 | } else if (calld->state == CALL_WAITING_FOR_CALL) { |
| 214 | have_waiting = !is_empty(&calld->waiting_op, sizeof(calld->waiting_op)); |
| 215 | if (calld->subchannel_call != NULL) { |
| 216 | calld->state = CALL_ACTIVE; |
| 217 | gpr_mu_unlock(&calld->mu_state); |
| 218 | if (have_waiting) { |
| 219 | grpc_subchannel_call_process_op(exec_ctx, calld->subchannel_call, |
| 220 | &calld->waiting_op); |
| 221 | } |
| 222 | } else { |
| 223 | calld->state = CALL_CANCELLED; |
| 224 | gpr_mu_unlock(&calld->mu_state); |
| 225 | if (have_waiting) { |
| 226 | handle_op_after_cancellation(exec_ctx, calld->elem, &calld->waiting_op); |
| 227 | } |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 228 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 229 | } else { |
| 230 | GPR_ASSERT(calld->state == CALL_CANCELLED); |
| 231 | gpr_mu_unlock(&calld->mu_state); |
| 232 | } |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 233 | } |
| 234 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 235 | static void picked_target(grpc_exec_ctx *exec_ctx, void *arg, |
| 236 | int iomgr_success) { |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 237 | call_data *calld = arg; |
Craig Tiller | abf3638 | 2015-06-29 16:13:27 -0700 | [diff] [blame] | 238 | grpc_pollset *pollset; |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 239 | |
Craig Tiller | 8910ac6 | 2015-10-08 16:49:15 -0700 | [diff] [blame^] | 240 | GRPC_TIMER_BEGIN(GRPC_PTAG_CHANNEL_PICKED_TARGET, 0); |
| 241 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 242 | if (calld->picked_channel == NULL) { |
| 243 | /* treat this like a cancellation */ |
| 244 | calld->waiting_op.cancel_with_status = GRPC_STATUS_UNAVAILABLE; |
| 245 | perform_transport_stream_op(exec_ctx, calld->elem, &calld->waiting_op, 1); |
| 246 | } else { |
| 247 | gpr_mu_lock(&calld->mu_state); |
| 248 | if (calld->state == CALL_CANCELLED) { |
| 249 | gpr_mu_unlock(&calld->mu_state); |
| 250 | handle_op_after_cancellation(exec_ctx, calld->elem, &calld->waiting_op); |
| 251 | } else { |
| 252 | GPR_ASSERT(calld->state == CALL_WAITING_FOR_PICK); |
| 253 | calld->state = CALL_WAITING_FOR_CALL; |
| 254 | pollset = calld->waiting_op.bind_pollset; |
| 255 | gpr_mu_unlock(&calld->mu_state); |
| 256 | grpc_closure_init(&calld->async_setup_task, started_call, calld); |
| 257 | grpc_subchannel_create_call(exec_ctx, calld->picked_channel, pollset, |
| 258 | &calld->subchannel_call, |
| 259 | &calld->async_setup_task); |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 260 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 261 | } |
Craig Tiller | 8910ac6 | 2015-10-08 16:49:15 -0700 | [diff] [blame^] | 262 | |
| 263 | GRPC_TIMER_END(GRPC_PTAG_CHANNEL_PICKED_TARGET, 0); |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 264 | } |
| 265 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 266 | static grpc_closure *merge_into_waiting_op(grpc_call_element *elem, |
| 267 | grpc_transport_stream_op *new_op) { |
Craig Tiller | bb32ba3 | 2015-06-30 09:31:48 -0700 | [diff] [blame] | 268 | call_data *calld = elem->call_data; |
Craig Tiller | 3382511 | 2015-09-18 07:44:19 -0700 | [diff] [blame] | 269 | grpc_closure *consumed_op = NULL; |
Craig Tiller | bb32ba3 | 2015-06-30 09:31:48 -0700 | [diff] [blame] | 270 | grpc_transport_stream_op *waiting_op = &calld->waiting_op; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 271 | GPR_ASSERT((waiting_op->send_ops != NULL) + (new_op->send_ops != NULL) <= 1); |
| 272 | GPR_ASSERT((waiting_op->recv_ops != NULL) + (new_op->recv_ops != NULL) <= 1); |
| 273 | if (new_op->send_ops != NULL) { |
| 274 | waiting_op->send_ops = new_op->send_ops; |
| 275 | waiting_op->is_last_send = new_op->is_last_send; |
| 276 | waiting_op->on_done_send = new_op->on_done_send; |
| 277 | } |
| 278 | if (new_op->recv_ops != NULL) { |
| 279 | waiting_op->recv_ops = new_op->recv_ops; |
| 280 | waiting_op->recv_state = new_op->recv_state; |
| 281 | waiting_op->on_done_recv = new_op->on_done_recv; |
| 282 | } |
| 283 | if (new_op->on_consumed != NULL) { |
| 284 | if (waiting_op->on_consumed != NULL) { |
| 285 | consumed_op = waiting_op->on_consumed; |
Craig Tiller | 5d44c06 | 2015-07-01 08:55:28 -0700 | [diff] [blame] | 286 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 287 | waiting_op->on_consumed = new_op->on_consumed; |
| 288 | } |
| 289 | if (new_op->cancel_with_status != GRPC_STATUS_OK) { |
| 290 | waiting_op->cancel_with_status = new_op->cancel_with_status; |
| 291 | } |
Craig Tiller | 5d44c06 | 2015-07-01 08:55:28 -0700 | [diff] [blame] | 292 | return consumed_op; |
Craig Tiller | bb32ba3 | 2015-06-30 09:31:48 -0700 | [diff] [blame] | 293 | } |
| 294 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 295 | 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] | 296 | call_data *calld = elem->call_data; |
| 297 | channel_data *chand = elem->channel_data; |
| 298 | grpc_subchannel_call *subchannel_call; |
| 299 | char *result; |
| 300 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 301 | gpr_mu_lock(&calld->mu_state); |
| 302 | if (calld->state == CALL_ACTIVE) { |
| 303 | subchannel_call = calld->subchannel_call; |
| 304 | GRPC_SUBCHANNEL_CALL_REF(subchannel_call, "get_peer"); |
| 305 | gpr_mu_unlock(&calld->mu_state); |
| 306 | result = grpc_subchannel_call_get_peer(exec_ctx, subchannel_call); |
| 307 | GRPC_SUBCHANNEL_CALL_UNREF(exec_ctx, subchannel_call, "get_peer"); |
| 308 | return result; |
| 309 | } else { |
| 310 | gpr_mu_unlock(&calld->mu_state); |
| 311 | return grpc_channel_get_target(chand->master); |
| 312 | } |
Craig Tiller | 1b22b9d | 2015-07-20 13:42:22 -0700 | [diff] [blame] | 313 | } |
| 314 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 315 | static void perform_transport_stream_op(grpc_exec_ctx *exec_ctx, |
| 316 | grpc_call_element *elem, |
| 317 | grpc_transport_stream_op *op, |
| 318 | int continuation) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 319 | call_data *calld = elem->call_data; |
| 320 | channel_data *chand = elem->channel_data; |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 321 | grpc_subchannel_call *subchannel_call; |
| 322 | grpc_lb_policy *lb_policy; |
Craig Tiller | 49924e0 | 2015-06-29 22:42:33 -0700 | [diff] [blame] | 323 | grpc_transport_stream_op op2; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 324 | GPR_ASSERT(elem->filter == &grpc_client_channel_filter); |
| 325 | GRPC_CALL_LOG_OP(GPR_INFO, elem, op); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 326 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 327 | gpr_mu_lock(&calld->mu_state); |
| 328 | switch (calld->state) { |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 329 | case CALL_ACTIVE: |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 330 | GPR_ASSERT(!continuation); |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 331 | subchannel_call = calld->subchannel_call; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 332 | gpr_mu_unlock(&calld->mu_state); |
| 333 | grpc_subchannel_call_process_op(exec_ctx, subchannel_call, op); |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 334 | break; |
| 335 | case CALL_CANCELLED: |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 336 | gpr_mu_unlock(&calld->mu_state); |
| 337 | handle_op_after_cancellation(exec_ctx, elem, op); |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 338 | break; |
Craig Tiller | 5d44c06 | 2015-07-01 08:55:28 -0700 | [diff] [blame] | 339 | case CALL_WAITING_FOR_SEND: |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 340 | GPR_ASSERT(!continuation); |
| 341 | grpc_exec_ctx_enqueue(exec_ctx, merge_into_waiting_op(elem, op), 1); |
| 342 | if (!calld->waiting_op.send_ops && |
| 343 | calld->waiting_op.cancel_with_status == GRPC_STATUS_OK) { |
| 344 | gpr_mu_unlock(&calld->mu_state); |
| 345 | break; |
| 346 | } |
Craig Tiller | 5d44c06 | 2015-07-01 08:55:28 -0700 | [diff] [blame] | 347 | *op = calld->waiting_op; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 348 | memset(&calld->waiting_op, 0, sizeof(calld->waiting_op)); |
Craig Tiller | 5d44c06 | 2015-07-01 08:55:28 -0700 | [diff] [blame] | 349 | continuation = 1; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 350 | /* fall through */ |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 351 | case CALL_WAITING_FOR_CONFIG: |
| 352 | case CALL_WAITING_FOR_PICK: |
| 353 | case CALL_WAITING_FOR_CALL: |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 354 | if (!continuation) { |
| 355 | if (op->cancel_with_status != GRPC_STATUS_OK) { |
| 356 | calld->state = CALL_CANCELLED; |
| 357 | op2 = calld->waiting_op; |
| 358 | memset(&calld->waiting_op, 0, sizeof(calld->waiting_op)); |
| 359 | if (op->on_consumed) { |
| 360 | calld->waiting_op.on_consumed = op->on_consumed; |
| 361 | op->on_consumed = NULL; |
| 362 | } else if (op2.on_consumed) { |
| 363 | calld->waiting_op.on_consumed = op2.on_consumed; |
| 364 | op2.on_consumed = NULL; |
| 365 | } |
| 366 | gpr_mu_unlock(&calld->mu_state); |
| 367 | handle_op_after_cancellation(exec_ctx, elem, op); |
| 368 | handle_op_after_cancellation(exec_ctx, elem, &op2); |
| 369 | } else { |
| 370 | grpc_exec_ctx_enqueue(exec_ctx, merge_into_waiting_op(elem, op), 1); |
| 371 | gpr_mu_unlock(&calld->mu_state); |
| 372 | } |
| 373 | break; |
| 374 | } |
| 375 | /* fall through */ |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 376 | case CALL_CREATED: |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 377 | if (op->cancel_with_status != GRPC_STATUS_OK) { |
| 378 | calld->state = CALL_CANCELLED; |
| 379 | gpr_mu_unlock(&calld->mu_state); |
| 380 | handle_op_after_cancellation(exec_ctx, elem, op); |
| 381 | } else { |
| 382 | calld->waiting_op = *op; |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 383 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 384 | if (op->send_ops == NULL) { |
| 385 | /* need to have some send ops before we can select the |
| 386 | lb target */ |
| 387 | calld->state = CALL_WAITING_FOR_SEND; |
| 388 | gpr_mu_unlock(&calld->mu_state); |
| 389 | } else { |
| 390 | gpr_mu_lock(&chand->mu_config); |
| 391 | lb_policy = chand->lb_policy; |
| 392 | if (lb_policy) { |
Craig Tiller | b9d3596 | 2015-09-11 13:31:16 -0700 | [diff] [blame] | 393 | grpc_transport_stream_op *waiting_op = &calld->waiting_op; |
| 394 | grpc_pollset *bind_pollset = waiting_op->bind_pollset; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 395 | grpc_metadata_batch *initial_metadata = |
Craig Tiller | b9d3596 | 2015-09-11 13:31:16 -0700 | [diff] [blame] | 396 | &waiting_op->send_ops->ops[0].data.metadata; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 397 | GRPC_LB_POLICY_REF(lb_policy, "pick"); |
| 398 | gpr_mu_unlock(&chand->mu_config); |
| 399 | calld->state = CALL_WAITING_FOR_PICK; |
Craig Tiller | 3d57871 | 2015-07-20 22:00:24 -0700 | [diff] [blame] | 400 | |
Craig Tiller | b9d3596 | 2015-09-11 13:31:16 -0700 | [diff] [blame] | 401 | GPR_ASSERT(waiting_op->bind_pollset); |
| 402 | GPR_ASSERT(waiting_op->send_ops); |
| 403 | GPR_ASSERT(waiting_op->send_ops->nops >= 1); |
| 404 | GPR_ASSERT(waiting_op->send_ops->ops[0].type == GRPC_OP_METADATA); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 405 | gpr_mu_unlock(&calld->mu_state); |
Craig Tiller | 5d44c06 | 2015-07-01 08:55:28 -0700 | [diff] [blame] | 406 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 407 | grpc_closure_init(&calld->async_setup_task, picked_target, calld); |
| 408 | grpc_lb_policy_pick(exec_ctx, lb_policy, bind_pollset, |
| 409 | initial_metadata, &calld->picked_channel, |
| 410 | &calld->async_setup_task); |
Craig Tiller | 5d44c06 | 2015-07-01 08:55:28 -0700 | [diff] [blame] | 411 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 412 | GRPC_LB_POLICY_UNREF(exec_ctx, lb_policy, "pick"); |
| 413 | } else if (chand->resolver != NULL) { |
| 414 | calld->state = CALL_WAITING_FOR_CONFIG; |
| 415 | add_to_lb_policy_wait_queue_locked_state_config(elem); |
| 416 | if (!chand->started_resolving && chand->resolver != NULL) { |
| 417 | GRPC_CHANNEL_INTERNAL_REF(chand->master, "resolver"); |
| 418 | chand->started_resolving = 1; |
| 419 | grpc_resolver_next(exec_ctx, chand->resolver, |
| 420 | &chand->incoming_configuration, |
| 421 | &chand->on_config_changed); |
| 422 | } |
| 423 | gpr_mu_unlock(&chand->mu_config); |
| 424 | gpr_mu_unlock(&calld->mu_state); |
| 425 | } else { |
| 426 | calld->state = CALL_CANCELLED; |
| 427 | gpr_mu_unlock(&chand->mu_config); |
| 428 | gpr_mu_unlock(&calld->mu_state); |
| 429 | handle_op_after_cancellation(exec_ctx, elem, op); |
| 430 | } |
| 431 | } |
| 432 | } |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 433 | break; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 434 | } |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 435 | } |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 436 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 437 | static void cc_start_transport_stream_op(grpc_exec_ctx *exec_ctx, |
| 438 | grpc_call_element *elem, |
| 439 | grpc_transport_stream_op *op) { |
| 440 | perform_transport_stream_op(exec_ctx, elem, op, 0); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 441 | } |
| 442 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 443 | static void watch_lb_policy(grpc_exec_ctx *exec_ctx, channel_data *chand, |
| 444 | grpc_lb_policy *lb_policy, |
| 445 | grpc_connectivity_state current_state); |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 446 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 447 | static void on_lb_policy_state_changed_locked( |
| 448 | grpc_exec_ctx *exec_ctx, lb_policy_connectivity_watcher *w) { |
Craig Tiller | 5795da7 | 2015-09-17 15:27:13 -0700 | [diff] [blame] | 449 | /* check if the notification is for a stale policy */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 450 | if (w->lb_policy != w->chand->lb_policy) return; |
Craig Tiller | 5795da7 | 2015-09-17 15:27:13 -0700 | [diff] [blame] | 451 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 452 | grpc_connectivity_state_set(exec_ctx, &w->chand->state_tracker, w->state, |
| 453 | "lb_changed"); |
| 454 | if (w->state != GRPC_CHANNEL_FATAL_FAILURE) { |
| 455 | watch_lb_policy(exec_ctx, w->chand, w->lb_policy, w->state); |
| 456 | } |
Craig Tiller | 5795da7 | 2015-09-17 15:27:13 -0700 | [diff] [blame] | 457 | } |
| 458 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 459 | static void on_lb_policy_state_changed(grpc_exec_ctx *exec_ctx, void *arg, |
| 460 | int iomgr_success) { |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 461 | lb_policy_connectivity_watcher *w = arg; |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 462 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 463 | gpr_mu_lock(&w->chand->mu_config); |
| 464 | on_lb_policy_state_changed_locked(exec_ctx, w); |
| 465 | gpr_mu_unlock(&w->chand->mu_config); |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 466 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 467 | GRPC_CHANNEL_INTERNAL_UNREF(exec_ctx, w->chand->master, "watch_lb_policy"); |
| 468 | gpr_free(w); |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 469 | } |
| 470 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 471 | static void watch_lb_policy(grpc_exec_ctx *exec_ctx, channel_data *chand, |
| 472 | grpc_lb_policy *lb_policy, |
| 473 | grpc_connectivity_state current_state) { |
| 474 | lb_policy_connectivity_watcher *w = gpr_malloc(sizeof(*w)); |
| 475 | GRPC_CHANNEL_INTERNAL_REF(chand->master, "watch_lb_policy"); |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 476 | |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 477 | w->chand = chand; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 478 | grpc_closure_init(&w->on_changed, on_lb_policy_state_changed, w); |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 479 | w->state = current_state; |
| 480 | w->lb_policy = lb_policy; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 481 | grpc_lb_policy_notify_on_state_change(exec_ctx, lb_policy, &w->state, |
| 482 | &w->on_changed); |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 483 | } |
| 484 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 485 | static void cc_on_config_changed(grpc_exec_ctx *exec_ctx, void *arg, |
| 486 | int iomgr_success) { |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 487 | channel_data *chand = arg; |
| 488 | grpc_lb_policy *lb_policy = NULL; |
| 489 | grpc_lb_policy *old_lb_policy; |
| 490 | grpc_resolver *old_resolver; |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 491 | grpc_connectivity_state state = GRPC_CHANNEL_TRANSIENT_FAILURE; |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 492 | int exit_idle = 0; |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 493 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 494 | if (chand->incoming_configuration != NULL) { |
| 495 | lb_policy = grpc_client_config_get_lb_policy(chand->incoming_configuration); |
| 496 | if (lb_policy != NULL) { |
| 497 | GRPC_LB_POLICY_REF(lb_policy, "channel"); |
| 498 | GRPC_LB_POLICY_REF(lb_policy, "config_change"); |
| 499 | state = grpc_lb_policy_check_connectivity(exec_ctx, lb_policy); |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 500 | } |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 501 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 502 | grpc_client_config_unref(exec_ctx, chand->incoming_configuration); |
| 503 | } |
| 504 | |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 505 | chand->incoming_configuration = NULL; |
| 506 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 507 | gpr_mu_lock(&chand->mu_config); |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 508 | old_lb_policy = chand->lb_policy; |
| 509 | chand->lb_policy = lb_policy; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 510 | if (lb_policy != NULL || chand->resolver == NULL /* disconnected */) { |
| 511 | grpc_exec_ctx_enqueue_list(exec_ctx, &chand->waiting_for_config_closures); |
| 512 | } |
| 513 | if (lb_policy != NULL && chand->exit_idle_when_lb_policy_arrives) { |
| 514 | GRPC_LB_POLICY_REF(lb_policy, "exit_idle"); |
| 515 | exit_idle = 1; |
| 516 | chand->exit_idle_when_lb_policy_arrives = 0; |
| 517 | } |
Craig Tiller | 9846503 | 2015-06-29 14:36:42 -0700 | [diff] [blame] | 518 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 519 | if (iomgr_success && chand->resolver) { |
| 520 | grpc_resolver *resolver = chand->resolver; |
| 521 | GRPC_RESOLVER_REF(resolver, "channel-next"); |
| 522 | grpc_connectivity_state_set(exec_ctx, &chand->state_tracker, state, |
| 523 | "new_lb+resolver"); |
| 524 | if (lb_policy != NULL) { |
| 525 | watch_lb_policy(exec_ctx, chand, lb_policy, state); |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 526 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 527 | gpr_mu_unlock(&chand->mu_config); |
| 528 | GRPC_CHANNEL_INTERNAL_REF(chand->master, "resolver"); |
| 529 | grpc_resolver_next(exec_ctx, resolver, &chand->incoming_configuration, |
| 530 | &chand->on_config_changed); |
| 531 | GRPC_RESOLVER_UNREF(exec_ctx, resolver, "channel-next"); |
| 532 | } else { |
| 533 | old_resolver = chand->resolver; |
| 534 | chand->resolver = NULL; |
| 535 | grpc_connectivity_state_set(exec_ctx, &chand->state_tracker, |
| 536 | GRPC_CHANNEL_FATAL_FAILURE, "resolver_gone"); |
| 537 | gpr_mu_unlock(&chand->mu_config); |
| 538 | if (old_resolver != NULL) { |
| 539 | grpc_resolver_shutdown(exec_ctx, old_resolver); |
| 540 | GRPC_RESOLVER_UNREF(exec_ctx, old_resolver, "channel"); |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 541 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 542 | } |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 543 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 544 | if (exit_idle) { |
| 545 | grpc_lb_policy_exit_idle(exec_ctx, lb_policy); |
| 546 | GRPC_LB_POLICY_UNREF(exec_ctx, lb_policy, "exit_idle"); |
| 547 | } |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 548 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 549 | if (old_lb_policy != NULL) { |
| 550 | grpc_lb_policy_shutdown(exec_ctx, old_lb_policy); |
| 551 | GRPC_LB_POLICY_UNREF(exec_ctx, old_lb_policy, "channel"); |
| 552 | } |
Craig Tiller | 000cd8f | 2015-09-18 07:20:29 -0700 | [diff] [blame] | 553 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 554 | if (lb_policy != NULL) { |
| 555 | GRPC_LB_POLICY_UNREF(exec_ctx, lb_policy, "config_change"); |
| 556 | } |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 557 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 558 | GRPC_CHANNEL_INTERNAL_UNREF(exec_ctx, chand->master, "resolver"); |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 559 | } |
| 560 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 561 | static void cc_start_transport_op(grpc_exec_ctx *exec_ctx, |
| 562 | grpc_channel_element *elem, |
| 563 | grpc_transport_op *op) { |
Craig Tiller | ca3e9d3 | 2015-06-27 18:37:27 -0700 | [diff] [blame] | 564 | grpc_lb_policy *lb_policy = NULL; |
| 565 | channel_data *chand = elem->channel_data; |
Craig Tiller | 9846503 | 2015-06-29 14:36:42 -0700 | [diff] [blame] | 566 | grpc_resolver *destroy_resolver = NULL; |
Craig Tiller | 000cd8f | 2015-09-18 07:20:29 -0700 | [diff] [blame] | 567 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 568 | grpc_exec_ctx_enqueue(exec_ctx, op->on_consumed, 1); |
Craig Tiller | ca3e9d3 | 2015-06-27 18:37:27 -0700 | [diff] [blame] | 569 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 570 | GPR_ASSERT(op->set_accept_stream == NULL); |
| 571 | GPR_ASSERT(op->bind_pollset == NULL); |
Craig Tiller | ca3e9d3 | 2015-06-27 18:37:27 -0700 | [diff] [blame] | 572 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 573 | gpr_mu_lock(&chand->mu_config); |
| 574 | if (op->on_connectivity_state_change != NULL) { |
| 575 | grpc_connectivity_state_notify_on_state_change( |
| 576 | exec_ctx, &chand->state_tracker, op->connectivity_state, |
| 577 | op->on_connectivity_state_change); |
| 578 | op->on_connectivity_state_change = NULL; |
| 579 | op->connectivity_state = NULL; |
| 580 | } |
| 581 | |
| 582 | if (!is_empty(op, sizeof(*op))) { |
| 583 | lb_policy = chand->lb_policy; |
| 584 | if (lb_policy) { |
| 585 | GRPC_LB_POLICY_REF(lb_policy, "broadcast"); |
Craig Tiller | 03dc655 | 2015-07-17 23:12:34 -0700 | [diff] [blame] | 586 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 587 | } |
Craig Tiller | 03dc655 | 2015-07-17 23:12:34 -0700 | [diff] [blame] | 588 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 589 | if (op->disconnect && chand->resolver != NULL) { |
| 590 | grpc_connectivity_state_set(exec_ctx, &chand->state_tracker, |
| 591 | GRPC_CHANNEL_FATAL_FAILURE, "disconnect"); |
| 592 | destroy_resolver = chand->resolver; |
| 593 | chand->resolver = NULL; |
| 594 | if (chand->lb_policy != NULL) { |
| 595 | grpc_lb_policy_shutdown(exec_ctx, chand->lb_policy); |
| 596 | GRPC_LB_POLICY_UNREF(exec_ctx, chand->lb_policy, "channel"); |
| 597 | chand->lb_policy = NULL; |
Craig Tiller | d2cc459 | 2015-07-01 07:50:47 -0700 | [diff] [blame] | 598 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 599 | } |
| 600 | gpr_mu_unlock(&chand->mu_config); |
Craig Tiller | ca3e9d3 | 2015-06-27 18:37:27 -0700 | [diff] [blame] | 601 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 602 | if (destroy_resolver) { |
| 603 | grpc_resolver_shutdown(exec_ctx, destroy_resolver); |
| 604 | GRPC_RESOLVER_UNREF(exec_ctx, destroy_resolver, "channel"); |
| 605 | } |
Craig Tiller | 9846503 | 2015-06-29 14:36:42 -0700 | [diff] [blame] | 606 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 607 | if (lb_policy) { |
| 608 | grpc_lb_policy_broadcast(exec_ctx, lb_policy, op); |
| 609 | GRPC_LB_POLICY_UNREF(exec_ctx, lb_policy, "broadcast"); |
| 610 | } |
Craig Tiller | ca3e9d3 | 2015-06-27 18:37:27 -0700 | [diff] [blame] | 611 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 612 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 613 | /* Constructor for call_data */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 614 | static void init_call_elem(grpc_exec_ctx *exec_ctx, grpc_call_element *elem, |
| 615 | const void *server_transport_data, |
| 616 | grpc_transport_stream_op *initial_op) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 617 | call_data *calld = elem->call_data; |
| 618 | |
Craig Tiller | 50d9db5 | 2015-04-23 10:52:14 -0700 | [diff] [blame] | 619 | /* TODO(ctiller): is there something useful we can do here? */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 620 | GPR_ASSERT(initial_op == NULL); |
Craig Tiller | 50d9db5 | 2015-04-23 10:52:14 -0700 | [diff] [blame] | 621 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 622 | GPR_ASSERT(elem->filter == &grpc_client_channel_filter); |
| 623 | GPR_ASSERT(server_transport_data == NULL); |
| 624 | gpr_mu_init(&calld->mu_state); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 625 | calld->elem = elem; |
| 626 | calld->state = CALL_CREATED; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 627 | calld->deadline = gpr_inf_future(GPR_CLOCK_REALTIME); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 628 | } |
| 629 | |
| 630 | /* Destructor for call_data */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 631 | static void destroy_call_elem(grpc_exec_ctx *exec_ctx, |
| 632 | grpc_call_element *elem) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 633 | call_data *calld = elem->call_data; |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 634 | grpc_subchannel_call *subchannel_call; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 635 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 636 | /* if the call got activated, we need to destroy the child stack also, and |
| 637 | remove it from the in-flight requests tracked by the child_entry we |
| 638 | picked */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 639 | gpr_mu_lock(&calld->mu_state); |
| 640 | switch (calld->state) { |
Craig Tiller | f93fd05 | 2015-06-02 08:15:33 -0700 | [diff] [blame] | 641 | case CALL_ACTIVE: |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 642 | subchannel_call = calld->subchannel_call; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 643 | gpr_mu_unlock(&calld->mu_state); |
| 644 | GRPC_SUBCHANNEL_CALL_UNREF(exec_ctx, subchannel_call, "client_channel"); |
Craig Tiller | f93fd05 | 2015-06-02 08:15:33 -0700 | [diff] [blame] | 645 | break; |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 646 | case CALL_CREATED: |
| 647 | case CALL_CANCELLED: |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 648 | gpr_mu_unlock(&calld->mu_state); |
Craig Tiller | f93fd05 | 2015-06-02 08:15:33 -0700 | [diff] [blame] | 649 | break; |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 650 | case CALL_WAITING_FOR_PICK: |
| 651 | case CALL_WAITING_FOR_CONFIG: |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 652 | case CALL_WAITING_FOR_CALL: |
Craig Tiller | 5d44c06 | 2015-07-01 08:55:28 -0700 | [diff] [blame] | 653 | case CALL_WAITING_FOR_SEND: |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 654 | gpr_log(GPR_ERROR, "should never reach here"); |
| 655 | abort(); |
Craig Tiller | f93fd05 | 2015-06-02 08:15:33 -0700 | [diff] [blame] | 656 | break; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 657 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 658 | } |
| 659 | |
| 660 | /* Constructor for channel_data */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 661 | static void init_channel_elem(grpc_exec_ctx *exec_ctx, |
| 662 | grpc_channel_element *elem, grpc_channel *master, |
| 663 | const grpc_channel_args *args, |
| 664 | grpc_mdctx *metadata_context, int is_first, |
| 665 | int is_last) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 666 | channel_data *chand = elem->channel_data; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 667 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 668 | memset(chand, 0, sizeof(*chand)); |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 669 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 670 | GPR_ASSERT(is_last); |
| 671 | GPR_ASSERT(elem->filter == &grpc_client_channel_filter); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 672 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 673 | gpr_mu_init(&chand->mu_config); |
Craig Tiller | e70413c | 2015-04-24 10:12:34 -0700 | [diff] [blame] | 674 | chand->mdctx = metadata_context; |
Craig Tiller | 9846503 | 2015-06-29 14:36:42 -0700 | [diff] [blame] | 675 | chand->master = master; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 676 | grpc_pollset_set_init(&chand->pollset_set); |
| 677 | grpc_closure_init(&chand->on_config_changed, cc_on_config_changed, chand); |
Craig Tiller | 9846503 | 2015-06-29 14:36:42 -0700 | [diff] [blame] | 678 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 679 | grpc_connectivity_state_init(&chand->state_tracker, GRPC_CHANNEL_IDLE, |
| 680 | "client_channel"); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 681 | } |
| 682 | |
| 683 | /* Destructor for channel_data */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 684 | static void destroy_channel_elem(grpc_exec_ctx *exec_ctx, |
| 685 | grpc_channel_element *elem) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 686 | channel_data *chand = elem->channel_data; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 687 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 688 | if (chand->resolver != NULL) { |
| 689 | grpc_resolver_shutdown(exec_ctx, chand->resolver); |
| 690 | GRPC_RESOLVER_UNREF(exec_ctx, chand->resolver, "channel"); |
| 691 | } |
| 692 | if (chand->lb_policy != NULL) { |
| 693 | GRPC_LB_POLICY_UNREF(exec_ctx, chand->lb_policy, "channel"); |
| 694 | } |
| 695 | grpc_connectivity_state_destroy(exec_ctx, &chand->state_tracker); |
| 696 | grpc_pollset_set_destroy(&chand->pollset_set); |
| 697 | gpr_mu_destroy(&chand->mu_config); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 698 | } |
| 699 | |
| 700 | const grpc_channel_filter grpc_client_channel_filter = { |
Craig Tiller | 71a0f9d | 2015-09-28 17:22:01 -0700 | [diff] [blame] | 701 | cc_start_transport_stream_op, cc_start_transport_op, sizeof(call_data), |
| 702 | init_call_elem, destroy_call_elem, sizeof(channel_data), init_channel_elem, |
| 703 | destroy_channel_elem, cc_get_peer, "client-channel", |
Craig Tiller | 87d5b19 | 2015-04-16 14:37:57 -0700 | [diff] [blame] | 704 | }; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 705 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 706 | void grpc_client_channel_set_resolver(grpc_exec_ctx *exec_ctx, |
| 707 | grpc_channel_stack *channel_stack, |
| 708 | grpc_resolver *resolver) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 709 | /* post construction initialization: set the transport setup pointer */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 710 | grpc_channel_element *elem = grpc_channel_stack_last_element(channel_stack); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 711 | channel_data *chand = elem->channel_data; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 712 | gpr_mu_lock(&chand->mu_config); |
| 713 | GPR_ASSERT(!chand->resolver); |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 714 | chand->resolver = resolver; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 715 | GRPC_RESOLVER_REF(resolver, "channel"); |
| 716 | if (!grpc_closure_list_empty(chand->waiting_for_config_closures) || |
| 717 | chand->exit_idle_when_lb_policy_arrives) { |
| 718 | chand->started_resolving = 1; |
| 719 | GRPC_CHANNEL_INTERNAL_REF(chand->master, "resolver"); |
| 720 | grpc_resolver_next(exec_ctx, resolver, &chand->incoming_configuration, |
| 721 | &chand->on_config_changed); |
| 722 | } |
| 723 | gpr_mu_unlock(&chand->mu_config); |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 724 | } |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 725 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 726 | grpc_connectivity_state grpc_client_channel_check_connectivity_state( |
| 727 | 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] | 728 | channel_data *chand = elem->channel_data; |
| 729 | grpc_connectivity_state out; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 730 | gpr_mu_lock(&chand->mu_config); |
| 731 | out = grpc_connectivity_state_check(&chand->state_tracker); |
| 732 | if (out == GRPC_CHANNEL_IDLE && try_to_connect) { |
| 733 | if (chand->lb_policy != NULL) { |
| 734 | grpc_lb_policy_exit_idle(exec_ctx, chand->lb_policy); |
| 735 | } else { |
| 736 | chand->exit_idle_when_lb_policy_arrives = 1; |
| 737 | if (!chand->started_resolving && chand->resolver != NULL) { |
| 738 | GRPC_CHANNEL_INTERNAL_REF(chand->master, "resolver"); |
| 739 | chand->started_resolving = 1; |
| 740 | grpc_resolver_next(exec_ctx, chand->resolver, |
| 741 | &chand->incoming_configuration, |
| 742 | &chand->on_config_changed); |
| 743 | } |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 744 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 745 | } |
| 746 | gpr_mu_unlock(&chand->mu_config); |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 747 | return out; |
| 748 | } |
| 749 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 750 | void grpc_client_channel_watch_connectivity_state( |
| 751 | grpc_exec_ctx *exec_ctx, grpc_channel_element *elem, |
| 752 | grpc_connectivity_state *state, grpc_closure *on_complete) { |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 753 | channel_data *chand = elem->channel_data; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 754 | gpr_mu_lock(&chand->mu_config); |
| 755 | grpc_connectivity_state_notify_on_state_change( |
| 756 | exec_ctx, &chand->state_tracker, state, on_complete); |
| 757 | gpr_mu_unlock(&chand->mu_config); |
Craig Tiller | 48cb07c | 2015-07-15 16:16:15 -0700 | [diff] [blame] | 758 | } |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 759 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 760 | grpc_pollset_set *grpc_client_channel_get_connecting_pollset_set( |
| 761 | grpc_channel_element *elem) { |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 762 | channel_data *chand = elem->channel_data; |
| 763 | return &chand->pollset_set; |
| 764 | } |
| 765 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 766 | void grpc_client_channel_add_interested_party(grpc_exec_ctx *exec_ctx, |
| 767 | grpc_channel_element *elem, |
| 768 | grpc_pollset *pollset) { |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 769 | channel_data *chand = elem->channel_data; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 770 | grpc_pollset_set_add_pollset(exec_ctx, &chand->pollset_set, pollset); |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 771 | } |
| 772 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 773 | void grpc_client_channel_del_interested_party(grpc_exec_ctx *exec_ctx, |
| 774 | grpc_channel_element *elem, |
| 775 | grpc_pollset *pollset) { |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 776 | channel_data *chand = elem->channel_data; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 777 | grpc_pollset_set_del_pollset(exec_ctx, &chand->pollset_set, pollset); |
Craig Tiller | 1ada6ad | 2015-07-16 16:19:14 -0700 | [diff] [blame] | 778 | } |