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> |
| 37 | |
| 38 | #include "src/core/channel/channel_args.h" |
ctiller | 82e275f | 2014-12-12 08:43:28 -0800 | [diff] [blame] | 39 | #include "src/core/channel/child_channel.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 40 | #include "src/core/channel/connected_channel.h" |
ctiller | c6d61c4 | 2014-12-15 14:52:08 -0800 | [diff] [blame] | 41 | #include "src/core/iomgr/iomgr.h" |
Craig Tiller | 485d776 | 2015-01-23 12:54:05 -0800 | [diff] [blame] | 42 | #include "src/core/support/string.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 43 | #include <grpc/support/alloc.h> |
| 44 | #include <grpc/support/log.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 45 | #include <grpc/support/sync.h> |
| 46 | #include <grpc/support/useful.h> |
| 47 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 48 | /* Client channel implementation */ |
| 49 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 50 | typedef struct call_data call_data; |
| 51 | |
| 52 | typedef struct { |
| 53 | /* protects children, child_count, child_capacity, active_child, |
| 54 | transport_setup_initiated |
| 55 | does not protect channel stacks held by children |
| 56 | transport_setup is assumed to be set once during construction */ |
| 57 | gpr_mu mu; |
| 58 | |
ctiller | 82e275f | 2014-12-12 08:43:28 -0800 | [diff] [blame] | 59 | /* the sending child (may be null) */ |
| 60 | grpc_child_channel *active_child; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 61 | |
| 62 | /* calls waiting for a channel to be ready */ |
| 63 | call_data **waiting_children; |
| 64 | size_t waiting_child_count; |
| 65 | size_t waiting_child_capacity; |
| 66 | |
| 67 | /* transport setup for this channel */ |
| 68 | grpc_transport_setup *transport_setup; |
| 69 | int transport_setup_initiated; |
| 70 | |
| 71 | grpc_channel_args *args; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 72 | } channel_data; |
| 73 | |
| 74 | typedef enum { |
| 75 | CALL_CREATED, |
| 76 | CALL_WAITING, |
| 77 | CALL_ACTIVE, |
| 78 | CALL_CANCELLED |
| 79 | } call_state; |
| 80 | |
| 81 | struct call_data { |
| 82 | /* owning element */ |
| 83 | grpc_call_element *elem; |
| 84 | |
| 85 | call_state state; |
Craig Tiller | 6902ad2 | 2015-04-16 08:01:49 -0700 | [diff] [blame] | 86 | grpc_call_op_metadata pending_metadata; |
| 87 | gpr_uint32 pending_metadata_flags; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 88 | gpr_timespec deadline; |
| 89 | union { |
| 90 | struct { |
| 91 | /* our child call stack */ |
ctiller | 82e275f | 2014-12-12 08:43:28 -0800 | [diff] [blame] | 92 | grpc_child_call *child_call; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 93 | } active; |
| 94 | struct { |
| 95 | void (*on_complete)(void *user_data, grpc_op_error error); |
| 96 | void *on_complete_user_data; |
| 97 | gpr_uint32 start_flags; |
ctiller | d79b486 | 2014-12-17 16:36:59 -0800 | [diff] [blame] | 98 | grpc_pollset *pollset; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 99 | } waiting; |
| 100 | } s; |
| 101 | }; |
| 102 | |
ctiller | 82e275f | 2014-12-12 08:43:28 -0800 | [diff] [blame] | 103 | static int prepare_activate(grpc_call_element *elem, |
| 104 | grpc_child_channel *on_child) { |
| 105 | call_data *calld = elem->call_data; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 106 | if (calld->state == CALL_CANCELLED) return 0; |
| 107 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 108 | /* no more access to calld->s.waiting allowed */ |
| 109 | GPR_ASSERT(calld->state == CALL_WAITING); |
| 110 | calld->state = CALL_ACTIVE; |
| 111 | |
ctiller | 82e275f | 2014-12-12 08:43:28 -0800 | [diff] [blame] | 112 | /* create a child call */ |
| 113 | calld->s.active.child_call = grpc_child_channel_create_call(on_child, elem); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 114 | |
| 115 | return 1; |
| 116 | } |
| 117 | |
| 118 | static void do_nothing(void *ignored, grpc_op_error error) {} |
| 119 | |
ctiller | 82e275f | 2014-12-12 08:43:28 -0800 | [diff] [blame] | 120 | static void complete_activate(grpc_call_element *elem, grpc_call_op *op) { |
ctiller | f962f52 | 2014-12-10 15:28:27 -0800 | [diff] [blame] | 121 | call_data *calld = elem->call_data; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 122 | grpc_call_element *child_elem = |
ctiller | 82e275f | 2014-12-12 08:43:28 -0800 | [diff] [blame] | 123 | grpc_child_call_get_top_element(calld->s.active.child_call); |
Craig Tiller | 6902ad2 | 2015-04-16 08:01:49 -0700 | [diff] [blame] | 124 | grpc_call_op mop; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 125 | |
| 126 | GPR_ASSERT(calld->state == CALL_ACTIVE); |
| 127 | |
| 128 | /* sending buffered metadata down the stack before the start call */ |
Craig Tiller | 6902ad2 | 2015-04-16 08:01:49 -0700 | [diff] [blame] | 129 | mop.type = GRPC_SEND_METADATA; |
| 130 | mop.dir = GRPC_CALL_DOWN; |
| 131 | mop.flags = calld->pending_metadata_flags; |
| 132 | mop.data.metadata = calld->pending_metadata; |
| 133 | mop.done_cb = do_nothing; |
| 134 | mop.user_data = NULL; |
| 135 | child_elem->filter->call_op(child_elem, elem, &mop); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 136 | |
| 137 | /* continue the start call down the stack, this nees to happen after metadata |
| 138 | are flushed*/ |
ctiller | f962f52 | 2014-12-10 15:28:27 -0800 | [diff] [blame] | 139 | child_elem->filter->call_op(child_elem, elem, op); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 140 | } |
| 141 | |
ctiller | f962f52 | 2014-12-10 15:28:27 -0800 | [diff] [blame] | 142 | static void start_rpc(grpc_call_element *elem, grpc_call_op *op) { |
| 143 | call_data *calld = elem->call_data; |
| 144 | channel_data *chand = elem->channel_data; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 145 | gpr_mu_lock(&chand->mu); |
| 146 | if (calld->state == CALL_CANCELLED) { |
| 147 | gpr_mu_unlock(&chand->mu); |
| 148 | op->done_cb(op->user_data, GRPC_OP_ERROR); |
| 149 | return; |
| 150 | } |
| 151 | GPR_ASSERT(calld->state == CALL_CREATED); |
| 152 | calld->state = CALL_WAITING; |
| 153 | if (chand->active_child) { |
| 154 | /* channel is connected - use the connected stack */ |
ctiller | 82e275f | 2014-12-12 08:43:28 -0800 | [diff] [blame] | 155 | if (prepare_activate(elem, chand->active_child)) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 156 | gpr_mu_unlock(&chand->mu); |
| 157 | /* activate the request (pass it down) outside the lock */ |
ctiller | 82e275f | 2014-12-12 08:43:28 -0800 | [diff] [blame] | 158 | complete_activate(elem, op); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 159 | } else { |
| 160 | gpr_mu_unlock(&chand->mu); |
| 161 | } |
| 162 | } else { |
| 163 | /* check to see if we should initiate a connection (if we're not already), |
| 164 | but don't do so until outside the lock to avoid re-entrancy problems if |
| 165 | the callback is immediate */ |
| 166 | int initiate_transport_setup = 0; |
| 167 | if (!chand->transport_setup_initiated) { |
| 168 | chand->transport_setup_initiated = 1; |
| 169 | initiate_transport_setup = 1; |
| 170 | } |
| 171 | /* add this call to the waiting set to be resumed once we have a child |
| 172 | channel stack, growing the waiting set if needed */ |
| 173 | if (chand->waiting_child_count == chand->waiting_child_capacity) { |
| 174 | chand->waiting_child_capacity = |
| 175 | GPR_MAX(chand->waiting_child_capacity * 2, 8); |
| 176 | chand->waiting_children = |
| 177 | gpr_realloc(chand->waiting_children, |
| 178 | chand->waiting_child_capacity * sizeof(call_data *)); |
| 179 | } |
| 180 | calld->s.waiting.on_complete = op->done_cb; |
| 181 | calld->s.waiting.on_complete_user_data = op->user_data; |
| 182 | calld->s.waiting.start_flags = op->flags; |
ctiller | d79b486 | 2014-12-17 16:36:59 -0800 | [diff] [blame] | 183 | calld->s.waiting.pollset = op->data.start.pollset; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 184 | chand->waiting_children[chand->waiting_child_count++] = calld; |
| 185 | gpr_mu_unlock(&chand->mu); |
| 186 | |
| 187 | /* finally initiate transport setup if needed */ |
| 188 | if (initiate_transport_setup) { |
| 189 | grpc_transport_setup_initiate(chand->transport_setup); |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | static void remove_waiting_child(channel_data *chand, call_data *calld) { |
| 195 | size_t new_count; |
| 196 | size_t i; |
| 197 | for (i = 0, new_count = 0; i < chand->waiting_child_count; i++) { |
| 198 | if (chand->waiting_children[i] == calld) continue; |
| 199 | chand->waiting_children[new_count++] = chand->waiting_children[i]; |
| 200 | } |
| 201 | GPR_ASSERT(new_count == chand->waiting_child_count - 1 || |
| 202 | new_count == chand->waiting_child_count); |
| 203 | chand->waiting_child_count = new_count; |
| 204 | } |
| 205 | |
Craig Tiller | 8b976d0 | 2015-02-05 21:41:23 -0800 | [diff] [blame] | 206 | static void send_up_cancelled_ops(grpc_call_element *elem) { |
| 207 | grpc_call_op finish_op; |
Craig Tiller | 8b976d0 | 2015-02-05 21:41:23 -0800 | [diff] [blame] | 208 | /* send up a synthesized status */ |
Craig Tiller | 6902ad2 | 2015-04-16 08:01:49 -0700 | [diff] [blame] | 209 | grpc_call_element_recv_status(elem, GRPC_STATUS_CANCELLED, "Cancelled"); |
Craig Tiller | 8b976d0 | 2015-02-05 21:41:23 -0800 | [diff] [blame] | 210 | /* send up a finish */ |
| 211 | finish_op.type = GRPC_RECV_FINISH; |
| 212 | finish_op.dir = GRPC_CALL_UP; |
| 213 | finish_op.flags = 0; |
| 214 | finish_op.done_cb = do_nothing; |
| 215 | finish_op.user_data = NULL; |
| 216 | grpc_call_next_op(elem, &finish_op); |
| 217 | } |
| 218 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 219 | static void cancel_rpc(grpc_call_element *elem, grpc_call_op *op) { |
| 220 | call_data *calld = elem->call_data; |
| 221 | channel_data *chand = elem->channel_data; |
| 222 | grpc_call_element *child_elem; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 223 | |
| 224 | gpr_mu_lock(&chand->mu); |
| 225 | switch (calld->state) { |
| 226 | case CALL_ACTIVE: |
ctiller | 82e275f | 2014-12-12 08:43:28 -0800 | [diff] [blame] | 227 | child_elem = grpc_child_call_get_top_element(calld->s.active.child_call); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 228 | gpr_mu_unlock(&chand->mu); |
ctiller | f962f52 | 2014-12-10 15:28:27 -0800 | [diff] [blame] | 229 | child_elem->filter->call_op(child_elem, elem, op); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 230 | return; /* early out */ |
| 231 | case CALL_WAITING: |
| 232 | remove_waiting_child(chand, calld); |
Craig Tiller | 696fda9 | 2015-02-06 10:59:44 -0800 | [diff] [blame] | 233 | calld->state = CALL_CANCELLED; |
Craig Tiller | 8b976d0 | 2015-02-05 21:41:23 -0800 | [diff] [blame] | 234 | gpr_mu_unlock(&chand->mu); |
| 235 | send_up_cancelled_ops(elem); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 236 | calld->s.waiting.on_complete(calld->s.waiting.on_complete_user_data, |
| 237 | GRPC_OP_ERROR); |
Craig Tiller | 8b976d0 | 2015-02-05 21:41:23 -0800 | [diff] [blame] | 238 | return; /* early out */ |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 239 | case CALL_CREATED: |
| 240 | calld->state = CALL_CANCELLED; |
| 241 | gpr_mu_unlock(&chand->mu); |
Craig Tiller | 8b976d0 | 2015-02-05 21:41:23 -0800 | [diff] [blame] | 242 | send_up_cancelled_ops(elem); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 243 | return; /* early out */ |
| 244 | case CALL_CANCELLED: |
| 245 | gpr_mu_unlock(&chand->mu); |
| 246 | return; /* early out */ |
| 247 | } |
| 248 | gpr_log(GPR_ERROR, "should never reach here"); |
| 249 | abort(); |
| 250 | } |
| 251 | |
ctiller | f962f52 | 2014-12-10 15:28:27 -0800 | [diff] [blame] | 252 | static void call_op(grpc_call_element *elem, grpc_call_element *from_elem, |
| 253 | grpc_call_op *op) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 254 | call_data *calld = elem->call_data; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 255 | GPR_ASSERT(elem->filter == &grpc_client_channel_filter); |
| 256 | GRPC_CALL_LOG_OP(GPR_INFO, elem, op); |
| 257 | |
| 258 | switch (op->type) { |
| 259 | case GRPC_SEND_METADATA: |
Craig Tiller | 6902ad2 | 2015-04-16 08:01:49 -0700 | [diff] [blame] | 260 | grpc_call_op_metadata_merge(&calld->pending_metadata, &op->data.metadata); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 261 | op->done_cb(op->user_data, GRPC_OP_OK); |
| 262 | break; |
| 263 | case GRPC_SEND_START: |
| 264 | /* filter out the start event to find which child to send on */ |
ctiller | f962f52 | 2014-12-10 15:28:27 -0800 | [diff] [blame] | 265 | start_rpc(elem, op); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 266 | break; |
| 267 | case GRPC_CANCEL_OP: |
| 268 | cancel_rpc(elem, op); |
| 269 | break; |
ctiller | bcd6259 | 2014-12-17 21:23:05 -0800 | [diff] [blame] | 270 | case GRPC_SEND_MESSAGE: |
| 271 | case GRPC_SEND_FINISH: |
| 272 | case GRPC_REQUEST_DATA: |
| 273 | if (calld->state == CALL_ACTIVE) { |
| 274 | grpc_call_element *child_elem = |
| 275 | grpc_child_call_get_top_element(calld->s.active.child_call); |
| 276 | child_elem->filter->call_op(child_elem, elem, op); |
| 277 | } else { |
| 278 | op->done_cb(op->user_data, GRPC_OP_ERROR); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 279 | } |
| 280 | break; |
ctiller | bcd6259 | 2014-12-17 21:23:05 -0800 | [diff] [blame] | 281 | default: |
| 282 | GPR_ASSERT(op->dir == GRPC_CALL_UP); |
| 283 | grpc_call_next_op(elem, op); |
| 284 | break; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 285 | } |
| 286 | } |
| 287 | |
ctiller | f962f52 | 2014-12-10 15:28:27 -0800 | [diff] [blame] | 288 | static void channel_op(grpc_channel_element *elem, |
| 289 | grpc_channel_element *from_elem, grpc_channel_op *op) { |
ctiller | 82e275f | 2014-12-12 08:43:28 -0800 | [diff] [blame] | 290 | channel_data *chand = elem->channel_data; |
| 291 | grpc_child_channel *child_channel; |
Craig Tiller | da66937 | 2015-02-05 10:10:15 -0800 | [diff] [blame] | 292 | grpc_channel_op rop; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 293 | GPR_ASSERT(elem->filter == &grpc_client_channel_filter); |
| 294 | |
| 295 | switch (op->type) { |
ctiller | 82e275f | 2014-12-12 08:43:28 -0800 | [diff] [blame] | 296 | case GRPC_CHANNEL_GOAWAY: |
ctiller | c6d61c4 | 2014-12-15 14:52:08 -0800 | [diff] [blame] | 297 | /* sending goaway: clear out the active child on the way through */ |
ctiller | 82e275f | 2014-12-12 08:43:28 -0800 | [diff] [blame] | 298 | gpr_mu_lock(&chand->mu); |
| 299 | child_channel = chand->active_child; |
| 300 | chand->active_child = NULL; |
| 301 | gpr_mu_unlock(&chand->mu); |
| 302 | if (child_channel) { |
| 303 | grpc_child_channel_handle_op(child_channel, op); |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 304 | grpc_child_channel_destroy(child_channel, 1); |
ctiller | 82e275f | 2014-12-12 08:43:28 -0800 | [diff] [blame] | 305 | } else { |
| 306 | gpr_slice_unref(op->data.goaway.message); |
| 307 | } |
| 308 | break; |
| 309 | case GRPC_CHANNEL_DISCONNECT: |
ctiller | c6d61c4 | 2014-12-15 14:52:08 -0800 | [diff] [blame] | 310 | /* sending disconnect: clear out the active child on the way through */ |
ctiller | 82e275f | 2014-12-12 08:43:28 -0800 | [diff] [blame] | 311 | gpr_mu_lock(&chand->mu); |
| 312 | child_channel = chand->active_child; |
| 313 | chand->active_child = NULL; |
| 314 | gpr_mu_unlock(&chand->mu); |
| 315 | if (child_channel) { |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 316 | grpc_child_channel_destroy(child_channel, 1); |
ctiller | c6d61c4 | 2014-12-15 14:52:08 -0800 | [diff] [blame] | 317 | } |
Craig Tiller | da66937 | 2015-02-05 10:10:15 -0800 | [diff] [blame] | 318 | /* fake a transport closed to satisfy the refcounting in client */ |
| 319 | rop.type = GRPC_TRANSPORT_CLOSED; |
| 320 | rop.dir = GRPC_CALL_UP; |
| 321 | grpc_channel_next_op(elem, &rop); |
ctiller | c6d61c4 | 2014-12-15 14:52:08 -0800 | [diff] [blame] | 322 | break; |
| 323 | case GRPC_TRANSPORT_GOAWAY: |
| 324 | /* receiving goaway: if it's from our active child, drop the active child; |
| 325 | in all cases consume the event here */ |
| 326 | gpr_mu_lock(&chand->mu); |
| 327 | child_channel = grpc_channel_stack_from_top_element(from_elem); |
| 328 | if (child_channel == chand->active_child) { |
| 329 | chand->active_child = NULL; |
| 330 | } else { |
| 331 | child_channel = NULL; |
| 332 | } |
| 333 | gpr_mu_unlock(&chand->mu); |
| 334 | if (child_channel) { |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 335 | grpc_child_channel_destroy(child_channel, 0); |
ctiller | c6d61c4 | 2014-12-15 14:52:08 -0800 | [diff] [blame] | 336 | } |
| 337 | gpr_slice_unref(op->data.goaway.message); |
| 338 | break; |
| 339 | case GRPC_TRANSPORT_CLOSED: |
| 340 | /* receiving disconnect: if it's from our active child, drop the active |
| 341 | child; in all cases consume the event here */ |
| 342 | gpr_mu_lock(&chand->mu); |
| 343 | child_channel = grpc_channel_stack_from_top_element(from_elem); |
| 344 | if (child_channel == chand->active_child) { |
| 345 | chand->active_child = NULL; |
| 346 | } else { |
| 347 | child_channel = NULL; |
| 348 | } |
| 349 | gpr_mu_unlock(&chand->mu); |
| 350 | if (child_channel) { |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 351 | grpc_child_channel_destroy(child_channel, 0); |
ctiller | 82e275f | 2014-12-12 08:43:28 -0800 | [diff] [blame] | 352 | } |
| 353 | break; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 354 | default: |
| 355 | switch (op->dir) { |
| 356 | case GRPC_CALL_UP: |
| 357 | grpc_channel_next_op(elem, op); |
| 358 | break; |
| 359 | case GRPC_CALL_DOWN: |
ctiller | 82e275f | 2014-12-12 08:43:28 -0800 | [diff] [blame] | 360 | gpr_log(GPR_ERROR, "unhandled channel op: %d", op->type); |
| 361 | abort(); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 362 | break; |
| 363 | } |
| 364 | break; |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | static void error_bad_on_complete(void *arg, grpc_op_error error) { |
| 369 | gpr_log(GPR_ERROR, |
| 370 | "Waiting finished but not started? Bad on_complete callback"); |
| 371 | abort(); |
| 372 | } |
| 373 | |
| 374 | /* Constructor for call_data */ |
| 375 | static void init_call_elem(grpc_call_element *elem, |
| 376 | const void *server_transport_data) { |
| 377 | call_data *calld = elem->call_data; |
| 378 | |
| 379 | GPR_ASSERT(elem->filter == &grpc_client_channel_filter); |
| 380 | GPR_ASSERT(server_transport_data == NULL); |
| 381 | calld->elem = elem; |
| 382 | calld->state = CALL_CREATED; |
| 383 | calld->deadline = gpr_inf_future; |
| 384 | calld->s.waiting.on_complete = error_bad_on_complete; |
| 385 | calld->s.waiting.on_complete_user_data = NULL; |
Craig Tiller | 6902ad2 | 2015-04-16 08:01:49 -0700 | [diff] [blame] | 386 | grpc_call_op_metadata_init(&calld->pending_metadata); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | /* Destructor for call_data */ |
| 390 | static void destroy_call_elem(grpc_call_element *elem) { |
| 391 | call_data *calld = elem->call_data; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 392 | |
| 393 | /* if the metadata buffer is not flushed, destroy it here. */ |
Craig Tiller | 6902ad2 | 2015-04-16 08:01:49 -0700 | [diff] [blame] | 394 | grpc_call_op_metadata_destroy(&calld->pending_metadata); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 395 | /* if the call got activated, we need to destroy the child stack also, and |
| 396 | remove it from the in-flight requests tracked by the child_entry we |
| 397 | picked */ |
| 398 | if (calld->state == CALL_ACTIVE) { |
ctiller | 82e275f | 2014-12-12 08:43:28 -0800 | [diff] [blame] | 399 | grpc_child_call_destroy(calld->s.active.child_call); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 400 | } |
| 401 | } |
| 402 | |
| 403 | /* Constructor for channel_data */ |
| 404 | static void init_channel_elem(grpc_channel_element *elem, |
| 405 | const grpc_channel_args *args, |
| 406 | grpc_mdctx *metadata_context, int is_first, |
| 407 | int is_last) { |
| 408 | channel_data *chand = elem->channel_data; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 409 | |
| 410 | GPR_ASSERT(!is_first); |
| 411 | GPR_ASSERT(is_last); |
| 412 | GPR_ASSERT(elem->filter == &grpc_client_channel_filter); |
| 413 | |
| 414 | gpr_mu_init(&chand->mu); |
| 415 | chand->active_child = NULL; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 416 | chand->waiting_children = NULL; |
| 417 | chand->waiting_child_count = 0; |
| 418 | chand->waiting_child_capacity = 0; |
| 419 | chand->transport_setup = NULL; |
| 420 | chand->transport_setup_initiated = 0; |
| 421 | chand->args = grpc_channel_args_copy(args); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | /* Destructor for channel_data */ |
| 425 | static void destroy_channel_elem(grpc_channel_element *elem) { |
| 426 | channel_data *chand = elem->channel_data; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 427 | |
| 428 | grpc_transport_setup_cancel(chand->transport_setup); |
| 429 | |
ctiller | 82e275f | 2014-12-12 08:43:28 -0800 | [diff] [blame] | 430 | if (chand->active_child) { |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 431 | grpc_child_channel_destroy(chand->active_child, 1); |
ctiller | 82e275f | 2014-12-12 08:43:28 -0800 | [diff] [blame] | 432 | chand->active_child = NULL; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | grpc_channel_args_destroy(chand->args); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 436 | |
| 437 | gpr_mu_destroy(&chand->mu); |
| 438 | GPR_ASSERT(chand->waiting_child_count == 0); |
| 439 | gpr_free(chand->waiting_children); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | const grpc_channel_filter grpc_client_channel_filter = { |
Craig Tiller | 87d5b19 | 2015-04-16 14:37:57 -0700 | [diff] [blame^] | 443 | call_op, channel_op, sizeof(call_data), init_call_elem, destroy_call_elem, |
| 444 | sizeof(channel_data), init_channel_elem, destroy_channel_elem, |
| 445 | "client-channel", |
| 446 | }; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 447 | |
| 448 | grpc_transport_setup_result grpc_client_channel_transport_setup_complete( |
| 449 | grpc_channel_stack *channel_stack, grpc_transport *transport, |
| 450 | grpc_channel_filter const **channel_filters, size_t num_channel_filters, |
| 451 | grpc_mdctx *mdctx) { |
| 452 | /* we just got a new transport: lets create a child channel stack for it */ |
| 453 | grpc_channel_element *elem = grpc_channel_stack_last_element(channel_stack); |
| 454 | channel_data *chand = elem->channel_data; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 455 | size_t num_child_filters = 2 + num_channel_filters; |
| 456 | grpc_channel_filter const **child_filters; |
| 457 | grpc_transport_setup_result result; |
ctiller | 82e275f | 2014-12-12 08:43:28 -0800 | [diff] [blame] | 458 | grpc_child_channel *old_active = NULL; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 459 | call_data **waiting_children; |
| 460 | size_t waiting_child_count; |
| 461 | size_t i; |
| 462 | grpc_call_op *call_ops; |
| 463 | |
| 464 | /* build the child filter stack */ |
| 465 | child_filters = gpr_malloc(sizeof(grpc_channel_filter *) * num_child_filters); |
| 466 | /* we always need a link back filter to get back to the connected channel */ |
ctiller | 82e275f | 2014-12-12 08:43:28 -0800 | [diff] [blame] | 467 | child_filters[0] = &grpc_child_channel_top_filter; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 468 | for (i = 0; i < num_channel_filters; i++) { |
| 469 | child_filters[i + 1] = channel_filters[i]; |
| 470 | } |
| 471 | /* and we always need a connected channel to talk to the transport */ |
| 472 | child_filters[num_child_filters - 1] = &grpc_connected_channel_filter; |
| 473 | |
| 474 | GPR_ASSERT(elem->filter == &grpc_client_channel_filter); |
| 475 | |
| 476 | /* BEGIN LOCKING CHANNEL */ |
| 477 | gpr_mu_lock(&chand->mu); |
| 478 | chand->transport_setup_initiated = 0; |
| 479 | |
ctiller | 82e275f | 2014-12-12 08:43:28 -0800 | [diff] [blame] | 480 | if (chand->active_child) { |
| 481 | old_active = chand->active_child; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 482 | } |
ctiller | 82e275f | 2014-12-12 08:43:28 -0800 | [diff] [blame] | 483 | chand->active_child = grpc_child_channel_create( |
| 484 | elem, child_filters, num_child_filters, chand->args, mdctx); |
| 485 | result = |
| 486 | grpc_connected_channel_bind_transport(chand->active_child, transport); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 487 | |
| 488 | /* capture the waiting children - we'll activate them outside the lock |
| 489 | to avoid re-entrancy problems */ |
| 490 | waiting_children = chand->waiting_children; |
| 491 | waiting_child_count = chand->waiting_child_count; |
| 492 | /* bumping up inflight_requests here avoids taking a lock per rpc below */ |
| 493 | |
| 494 | chand->waiting_children = NULL; |
| 495 | chand->waiting_child_count = 0; |
| 496 | chand->waiting_child_capacity = 0; |
| 497 | |
| 498 | call_ops = gpr_malloc(sizeof(grpc_call_op) * waiting_child_count); |
| 499 | |
| 500 | for (i = 0; i < waiting_child_count; i++) { |
| 501 | call_ops[i].type = GRPC_SEND_START; |
| 502 | call_ops[i].dir = GRPC_CALL_DOWN; |
| 503 | call_ops[i].flags = waiting_children[i]->s.waiting.start_flags; |
| 504 | call_ops[i].done_cb = waiting_children[i]->s.waiting.on_complete; |
| 505 | call_ops[i].user_data = |
| 506 | waiting_children[i]->s.waiting.on_complete_user_data; |
ctiller | d79b486 | 2014-12-17 16:36:59 -0800 | [diff] [blame] | 507 | call_ops[i].data.start.pollset = waiting_children[i]->s.waiting.pollset; |
ctiller | 82e275f | 2014-12-12 08:43:28 -0800 | [diff] [blame] | 508 | if (!prepare_activate(waiting_children[i]->elem, chand->active_child)) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 509 | waiting_children[i] = NULL; |
| 510 | call_ops[i].done_cb(call_ops[i].user_data, GRPC_OP_ERROR); |
| 511 | } |
| 512 | } |
| 513 | |
| 514 | /* END LOCKING CHANNEL */ |
| 515 | gpr_mu_unlock(&chand->mu); |
| 516 | |
| 517 | /* activate any pending operations - this is safe to do as we guarantee one |
| 518 | and only one write operation per request at the surface api - if we lose |
| 519 | that guarantee we need to do some curly locking here */ |
| 520 | for (i = 0; i < waiting_child_count; i++) { |
| 521 | if (waiting_children[i]) { |
ctiller | 82e275f | 2014-12-12 08:43:28 -0800 | [diff] [blame] | 522 | complete_activate(waiting_children[i]->elem, &call_ops[i]); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 523 | } |
| 524 | } |
| 525 | gpr_free(waiting_children); |
| 526 | gpr_free(call_ops); |
| 527 | gpr_free(child_filters); |
| 528 | |
ctiller | 82e275f | 2014-12-12 08:43:28 -0800 | [diff] [blame] | 529 | if (old_active) { |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 530 | grpc_child_channel_destroy(old_active, 1); |
ctiller | 82e275f | 2014-12-12 08:43:28 -0800 | [diff] [blame] | 531 | } |
| 532 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 533 | return result; |
| 534 | } |
| 535 | |
| 536 | void grpc_client_channel_set_transport_setup(grpc_channel_stack *channel_stack, |
| 537 | grpc_transport_setup *setup) { |
| 538 | /* post construction initialization: set the transport setup pointer */ |
| 539 | grpc_channel_element *elem = grpc_channel_stack_last_element(channel_stack); |
| 540 | channel_data *chand = elem->channel_data; |
| 541 | GPR_ASSERT(!chand->transport_setup); |
| 542 | chand->transport_setup = setup; |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 543 | } |