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 | |
| 39 | #include "src/core/channel/channel_args.h" |
| 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 | fe4ba36 | 2015-05-08 09:47:18 -0700 | [diff] [blame] | 42 | #include "src/core/iomgr/pollset_set.h" |
Craig Tiller | 485d776 | 2015-01-23 12:54:05 -0800 | [diff] [blame] | 43 | #include "src/core/support/string.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 44 | #include <grpc/support/alloc.h> |
| 45 | #include <grpc/support/log.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 46 | #include <grpc/support/sync.h> |
| 47 | #include <grpc/support/useful.h> |
| 48 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 49 | /* Client channel implementation */ |
| 50 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 51 | typedef struct call_data call_data; |
| 52 | |
| 53 | typedef struct { |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 54 | /** metadata context for this channel */ |
Craig Tiller | e70413c | 2015-04-24 10:12:34 -0700 | [diff] [blame] | 55 | grpc_mdctx *mdctx; |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 56 | /** resolver for this channel */ |
| 57 | grpc_resolver *resolver; |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 58 | |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 59 | /** mutex protecting client configuration, resolution state */ |
| 60 | gpr_mu mu_config; |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 61 | /** currently active load balancer - guarded by mu_config */ |
| 62 | grpc_lb_policy *lb_policy; |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 63 | /** incoming configuration - set by resolver.next |
| 64 | guarded by mu_config */ |
| 65 | grpc_client_config *incoming_configuration; |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 66 | /** a list of closures that are all waiting for config to come in */ |
| 67 | grpc_iomgr_closure *waiting_for_config_closures; |
| 68 | /** resolver callback */ |
| 69 | grpc_iomgr_closure on_config_changed; |
| 70 | /** connectivity state being tracked */ |
| 71 | grpc_iomgr_closure *on_connectivity_state_change; |
| 72 | grpc_connectivity_state *connectivity_state; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 73 | } channel_data; |
| 74 | |
| 75 | typedef enum { |
| 76 | CALL_CREATED, |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 77 | CALL_WAITING_FOR_CONFIG, |
| 78 | CALL_WAITING_FOR_PICK, |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame^] | 79 | CALL_WAITING_FOR_CALL, |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 80 | CALL_ACTIVE, |
| 81 | CALL_CANCELLED |
| 82 | } call_state; |
| 83 | |
| 84 | struct call_data { |
| 85 | /* owning element */ |
| 86 | grpc_call_element *elem; |
| 87 | |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 88 | gpr_mu mu_state; |
| 89 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 90 | call_state state; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 91 | gpr_timespec deadline; |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame^] | 92 | grpc_subchannel *picked_channel; |
| 93 | grpc_iomgr_closure async_setup_task; |
| 94 | grpc_transport_stream_op waiting_op; |
| 95 | /* our child call stack */ |
| 96 | grpc_subchannel_call *subchannel_call; |
| 97 | grpc_linked_mdelem status; |
| 98 | grpc_linked_mdelem details; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 99 | }; |
| 100 | |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 101 | #if 0 |
ctiller | 82e275f | 2014-12-12 08:43:28 -0800 | [diff] [blame] | 102 | static int prepare_activate(grpc_call_element *elem, |
| 103 | grpc_child_channel *on_child) { |
| 104 | call_data *calld = elem->call_data; |
Craig Tiller | 4cf08fb | 2015-06-10 12:56:24 -0700 | [diff] [blame] | 105 | channel_data *chand = elem->channel_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); |
Craig Tiller | 4cf08fb | 2015-06-10 12:56:24 -0700 | [diff] [blame] | 110 | |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame^] | 111 | if (calld->waiting_op.bind_pollset) { |
Craig Tiller | 4cf08fb | 2015-06-10 12:56:24 -0700 | [diff] [blame] | 112 | grpc_transport_setup_del_interested_party(chand->transport_setup, |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame^] | 113 | calld->waiting_op.bind_pollset); |
Craig Tiller | 4cf08fb | 2015-06-10 12:56:24 -0700 | [diff] [blame] | 114 | } |
| 115 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 116 | calld->state = CALL_ACTIVE; |
| 117 | |
ctiller | 82e275f | 2014-12-12 08:43:28 -0800 | [diff] [blame] | 118 | /* create a child call */ |
Craig Tiller | 3f2c221 | 2015-04-23 07:56:33 -0700 | [diff] [blame] | 119 | /* TODO(ctiller): pass the waiting op down here */ |
Craig Tiller | 06aeea7 | 2015-04-23 10:54:45 -0700 | [diff] [blame] | 120 | calld->s.active.child_call = |
| 121 | grpc_child_channel_create_call(on_child, elem, NULL); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 122 | |
| 123 | return 1; |
| 124 | } |
| 125 | |
Craig Tiller | b7959a0 | 2015-06-25 08:50:54 -0700 | [diff] [blame] | 126 | static void complete_activate(grpc_call_element *elem, grpc_transport_stream_op *op) { |
ctiller | f962f52 | 2014-12-10 15:28:27 -0800 | [diff] [blame] | 127 | call_data *calld = elem->call_data; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 128 | grpc_call_element *child_elem = |
ctiller | 82e275f | 2014-12-12 08:43:28 -0800 | [diff] [blame] | 129 | grpc_child_call_get_top_element(calld->s.active.child_call); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 130 | |
| 131 | GPR_ASSERT(calld->state == CALL_ACTIVE); |
| 132 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 133 | /* continue the start call down the stack, this nees to happen after metadata |
| 134 | are flushed*/ |
Craig Tiller | 83f88d9 | 2015-04-21 16:02:05 -0700 | [diff] [blame] | 135 | child_elem->filter->start_transport_op(child_elem, op); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 136 | } |
| 137 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 138 | static void remove_waiting_child(channel_data *chand, call_data *calld) { |
| 139 | size_t new_count; |
| 140 | size_t i; |
| 141 | for (i = 0, new_count = 0; i < chand->waiting_child_count; i++) { |
Craig Tiller | 6e51180 | 2015-05-13 12:48:36 -0700 | [diff] [blame] | 142 | if (chand->waiting_children[i] == calld) { |
Craig Tiller | 83b826a | 2015-05-13 13:43:01 -0700 | [diff] [blame] | 143 | grpc_transport_setup_del_interested_party( |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame^] | 144 | chand->transport_setup, calld->waiting_op.bind_pollset); |
Craig Tiller | 6e51180 | 2015-05-13 12:48:36 -0700 | [diff] [blame] | 145 | continue; |
| 146 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 147 | chand->waiting_children[new_count++] = chand->waiting_children[i]; |
| 148 | } |
| 149 | GPR_ASSERT(new_count == chand->waiting_child_count - 1 || |
| 150 | new_count == chand->waiting_child_count); |
| 151 | chand->waiting_child_count = new_count; |
| 152 | } |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 153 | #endif |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 154 | |
Craig Tiller | 1a727fd | 2015-04-24 13:21:22 -0700 | [diff] [blame] | 155 | static void handle_op_after_cancellation(grpc_call_element *elem, |
Craig Tiller | b7959a0 | 2015-06-25 08:50:54 -0700 | [diff] [blame] | 156 | grpc_transport_stream_op *op) { |
Craig Tiller | e70413c | 2015-04-24 10:12:34 -0700 | [diff] [blame] | 157 | call_data *calld = elem->call_data; |
| 158 | channel_data *chand = elem->channel_data; |
| 159 | if (op->send_ops) { |
Yang Gao | dbf8fdc | 2015-05-28 00:52:31 -0700 | [diff] [blame] | 160 | grpc_stream_ops_unref_owned_objects(op->send_ops->ops, op->send_ops->nops); |
Craig Tiller | 1e6facb | 2015-06-11 22:47:11 -0700 | [diff] [blame] | 161 | op->on_done_send->cb(op->on_done_send->cb_arg, 0); |
Craig Tiller | e70413c | 2015-04-24 10:12:34 -0700 | [diff] [blame] | 162 | } |
| 163 | if (op->recv_ops) { |
| 164 | char status[GPR_LTOA_MIN_BUFSIZE]; |
| 165 | grpc_metadata_batch mdb; |
| 166 | gpr_ltoa(GRPC_STATUS_CANCELLED, status); |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame^] | 167 | calld->status.md = |
Craig Tiller | 1a727fd | 2015-04-24 13:21:22 -0700 | [diff] [blame] | 168 | grpc_mdelem_from_strings(chand->mdctx, "grpc-status", status); |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame^] | 169 | calld->details.md = |
Craig Tiller | 1a727fd | 2015-04-24 13:21:22 -0700 | [diff] [blame] | 170 | grpc_mdelem_from_strings(chand->mdctx, "grpc-message", "Cancelled"); |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame^] | 171 | calld->status.prev = calld->details.next = NULL; |
| 172 | calld->status.next = &calld->details; |
| 173 | calld->details.prev = &calld->status; |
| 174 | mdb.list.head = &calld->status; |
| 175 | mdb.list.tail = &calld->details; |
Craig Tiller | e70413c | 2015-04-24 10:12:34 -0700 | [diff] [blame] | 176 | mdb.garbage.head = mdb.garbage.tail = NULL; |
| 177 | mdb.deadline = gpr_inf_future; |
| 178 | grpc_sopb_add_metadata(op->recv_ops, mdb); |
| 179 | *op->recv_state = GRPC_STREAM_CLOSED; |
Craig Tiller | 1e6facb | 2015-06-11 22:47:11 -0700 | [diff] [blame] | 180 | op->on_done_recv->cb(op->on_done_recv->cb_arg, 1); |
Craig Tiller | e70413c | 2015-04-24 10:12:34 -0700 | [diff] [blame] | 181 | } |
Craig Tiller | 5dde66e | 2015-06-02 09:05:23 -0700 | [diff] [blame] | 182 | if (op->on_consumed) { |
Craig Tiller | 1e6facb | 2015-06-11 22:47:11 -0700 | [diff] [blame] | 183 | op->on_consumed->cb(op->on_consumed->cb_arg, 0); |
Craig Tiller | 5dde66e | 2015-06-02 09:05:23 -0700 | [diff] [blame] | 184 | } |
Craig Tiller | e70413c | 2015-04-24 10:12:34 -0700 | [diff] [blame] | 185 | } |
Craig Tiller | 8b976d0 | 2015-02-05 21:41:23 -0800 | [diff] [blame] | 186 | |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame^] | 187 | typedef struct { |
| 188 | grpc_iomgr_closure closure; |
| 189 | grpc_call_element *elem; |
| 190 | } waiting_call; |
| 191 | |
| 192 | static void perform_transport_stream_op(grpc_call_element *elem, grpc_transport_stream_op *op, int continuation); |
| 193 | |
| 194 | static void continue_with_pick(void *arg, int iomgr_success) { |
| 195 | waiting_call *wc = arg; |
| 196 | call_data *calld = wc->elem->call_data; |
| 197 | perform_transport_stream_op(wc->elem, &calld->waiting_op, 1); |
| 198 | gpr_free(wc); |
| 199 | } |
| 200 | |
| 201 | static void add_to_lb_policy_wait_queue_locked_state_config(grpc_call_element *elem) { |
| 202 | channel_data *chand = elem->channel_data; |
| 203 | waiting_call *wc = gpr_malloc(sizeof(*wc)); |
| 204 | grpc_iomgr_closure_init(&wc->closure, continue_with_pick, wc); |
| 205 | wc->elem = elem; |
| 206 | wc->closure.next = chand->waiting_for_config_closures; |
| 207 | chand->waiting_for_config_closures = &wc->closure; |
| 208 | } |
| 209 | |
| 210 | static int is_empty(void *p, int len) { |
| 211 | char *ptr = p; |
| 212 | int i; |
| 213 | for (i = 0; i < len; i++) { |
| 214 | if (ptr[i] != 0) return 0; |
| 215 | } |
| 216 | return 1; |
| 217 | } |
| 218 | |
| 219 | static void started_call(void *arg, int iomgr_success) { |
| 220 | call_data *calld = arg; |
| 221 | grpc_transport_stream_op op; |
| 222 | int have_waiting; |
| 223 | |
| 224 | gpr_mu_lock(&calld->mu_state); |
| 225 | if (calld->state == CALL_CANCELLED && calld->subchannel_call != NULL) { |
| 226 | memset(&op, 0, sizeof(op)); |
| 227 | op.cancel_with_status = GRPC_STATUS_CANCELLED; |
| 228 | gpr_mu_unlock(&calld->mu_state); |
| 229 | grpc_subchannel_call_process_op(calld->subchannel_call, &op); |
| 230 | } else if (calld->state == CALL_WAITING_FOR_CALL) { |
| 231 | have_waiting = !is_empty(&calld->waiting_op, sizeof(calld->waiting_op)); |
| 232 | if (calld->subchannel_call != NULL) { |
| 233 | calld->state = CALL_ACTIVE; |
| 234 | gpr_mu_unlock(&calld->mu_state); |
| 235 | if (have_waiting) { |
| 236 | grpc_subchannel_call_process_op(calld->subchannel_call, &calld->waiting_op); |
| 237 | } |
| 238 | } else { |
| 239 | calld->state = CALL_CANCELLED; |
| 240 | gpr_mu_unlock(&calld->mu_state); |
| 241 | if (have_waiting) { |
| 242 | handle_op_after_cancellation(calld->elem, &calld->waiting_op); |
| 243 | } |
| 244 | } |
| 245 | } else { |
| 246 | GPR_ASSERT(calld->state == CALL_CANCELLED); |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | static void picked_target(void *arg, int iomgr_success) { |
| 251 | call_data *calld = arg; |
| 252 | channel_data *chand = calld->elem->channel_data; |
| 253 | grpc_transport_stream_op op; |
| 254 | |
| 255 | if (calld->picked_channel == NULL) { |
| 256 | /* treat this like a cancellation */ |
| 257 | calld->waiting_op.cancel_with_status = GRPC_STATUS_UNAVAILABLE; |
| 258 | perform_transport_stream_op(calld->elem, &calld->waiting_op, 1); |
| 259 | } else { |
| 260 | gpr_mu_lock(&calld->mu_state); |
| 261 | if (calld->state == CALL_CANCELLED) { |
| 262 | gpr_mu_unlock(&calld->mu_state); |
| 263 | handle_op_after_cancellation(calld->elem, &calld->waiting_op); |
| 264 | } else { |
| 265 | GPR_ASSERT(calld->state == CALL_WAITING_FOR_PICK); |
| 266 | calld->state = CALL_WAITING_FOR_CALL; |
| 267 | op = calld->waiting_op; |
| 268 | memset(&calld->waiting_op, 0, sizeof(calld->waiting_op)); |
| 269 | gpr_mu_unlock(&calld->mu_state); |
| 270 | grpc_iomgr_closure_init(&calld->async_setup_task, started_call, calld); |
| 271 | grpc_subchannel_create_call(calld->picked_channel, chand->mdctx, &op, &calld->subchannel_call, &calld->async_setup_task); |
| 272 | } |
| 273 | } |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | static void pick_target(grpc_lb_policy *lb_policy, call_data *calld) { |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame^] | 277 | grpc_metadata_batch *initial_metadata; |
| 278 | grpc_transport_stream_op *op = &calld->waiting_op; |
| 279 | |
| 280 | GPR_ASSERT(op->bind_pollset); |
| 281 | GPR_ASSERT(op->send_ops); |
| 282 | GPR_ASSERT(op->send_ops->nops >= 1); |
| 283 | GPR_ASSERT(op->send_ops->ops[0].type == GRPC_OP_METADATA); |
| 284 | initial_metadata = &op->send_ops->ops[0].data.metadata; |
| 285 | |
| 286 | grpc_iomgr_closure_init(&calld->async_setup_task, picked_target, calld); |
| 287 | grpc_lb_policy_pick(lb_policy, op->bind_pollset, |
| 288 | initial_metadata, &calld->picked_channel, &calld->async_setup_task); |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 289 | } |
| 290 | |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame^] | 291 | static void perform_transport_stream_op(grpc_call_element *elem, grpc_transport_stream_op *op, int continuation) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 292 | call_data *calld = elem->call_data; |
| 293 | channel_data *chand = elem->channel_data; |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 294 | grpc_subchannel_call *subchannel_call; |
| 295 | grpc_lb_policy *lb_policy; |
Craig Tiller | a940752 | 2015-04-24 10:37:57 -0700 | [diff] [blame] | 296 | GPR_ASSERT(elem->filter == &grpc_client_channel_filter); |
| 297 | GRPC_CALL_LOG_OP(GPR_INFO, elem, op); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 298 | |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 299 | gpr_mu_lock(&calld->mu_state); |
| 300 | switch (calld->state) { |
| 301 | case CALL_ACTIVE: |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame^] | 302 | GPR_ASSERT(!continuation); |
| 303 | subchannel_call = calld->subchannel_call; |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 304 | gpr_mu_unlock(&calld->mu_state); |
| 305 | grpc_subchannel_call_process_op(subchannel_call, op); |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 306 | break; |
| 307 | case CALL_CANCELLED: |
| 308 | gpr_mu_unlock(&calld->mu_state); |
| 309 | handle_op_after_cancellation(elem, op); |
| 310 | break; |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame^] | 311 | case CALL_WAITING_FOR_CONFIG: |
| 312 | case CALL_WAITING_FOR_PICK: |
| 313 | case CALL_WAITING_FOR_CALL: |
| 314 | if (!continuation) { |
| 315 | if (op->cancel_with_status != GRPC_STATUS_OK) { |
| 316 | calld->state = CALL_CANCELLED; |
| 317 | gpr_mu_unlock(&calld->mu_state); |
| 318 | handle_op_after_cancellation(elem, op); |
| 319 | } else { |
| 320 | GPR_ASSERT((calld->waiting_op.send_ops == NULL) != |
| 321 | (op->send_ops == NULL)); |
| 322 | GPR_ASSERT((calld->waiting_op.recv_ops == NULL) != |
| 323 | (op->recv_ops == NULL)); |
| 324 | if (op->send_ops != NULL) { |
| 325 | calld->waiting_op.send_ops = op->send_ops; |
| 326 | calld->waiting_op.is_last_send = op->is_last_send; |
| 327 | calld->waiting_op.on_done_send = op->on_done_send; |
| 328 | } |
| 329 | if (op->recv_ops != NULL) { |
| 330 | calld->waiting_op.recv_ops = op->recv_ops; |
| 331 | calld->waiting_op.recv_state = op->recv_state; |
| 332 | calld->waiting_op.on_done_recv = op->on_done_recv; |
| 333 | } |
| 334 | gpr_mu_unlock(&calld->mu_state); |
| 335 | if (op->on_consumed != NULL) { |
| 336 | op->on_consumed->cb(op->on_consumed->cb_arg, 0); |
| 337 | } |
| 338 | } |
| 339 | break; |
| 340 | } |
| 341 | /* fall through */ |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 342 | case CALL_CREATED: |
| 343 | if (op->cancel_with_status != GRPC_STATUS_OK) { |
| 344 | calld->state = CALL_CANCELLED; |
| 345 | gpr_mu_unlock(&calld->mu_state); |
| 346 | handle_op_after_cancellation(elem, op); |
| 347 | } else { |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame^] | 348 | calld->waiting_op = *op; |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 349 | |
| 350 | gpr_mu_lock(&chand->mu_config); |
| 351 | lb_policy = chand->lb_policy; |
| 352 | if (lb_policy) { |
| 353 | grpc_lb_policy_ref(lb_policy); |
| 354 | gpr_mu_unlock(&chand->mu_config); |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 355 | calld->state = CALL_WAITING_FOR_PICK; |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 356 | gpr_mu_unlock(&calld->mu_state); |
| 357 | |
| 358 | pick_target(lb_policy, calld); |
| 359 | |
| 360 | grpc_lb_policy_unref(lb_policy); |
| 361 | } else { |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 362 | calld->state = CALL_WAITING_FOR_CONFIG; |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame^] | 363 | add_to_lb_policy_wait_queue_locked_state_config(elem); |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 364 | gpr_mu_unlock(&chand->mu_config); |
| 365 | gpr_mu_unlock(&calld->mu_state); |
| 366 | } |
| 367 | } |
| 368 | break; |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 369 | } |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame^] | 370 | } |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 371 | |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame^] | 372 | static void cc_start_transport_stream_op(grpc_call_element *elem, |
| 373 | grpc_transport_stream_op *op) { |
| 374 | perform_transport_stream_op(elem, op, 0); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 375 | } |
| 376 | |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 377 | static void update_state_locked(channel_data *chand) { |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame^] | 378 | gpr_log(GPR_ERROR, "update_state_locked not implemented"); |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | static void cc_on_config_changed(void *arg, int iomgr_success) { |
| 382 | channel_data *chand = arg; |
| 383 | grpc_lb_policy *lb_policy = NULL; |
| 384 | grpc_lb_policy *old_lb_policy; |
| 385 | grpc_resolver *old_resolver; |
| 386 | grpc_iomgr_closure *wakeup_closures = NULL; |
| 387 | |
| 388 | if (chand->incoming_configuration) { |
| 389 | lb_policy = grpc_client_config_get_lb_policy(chand->incoming_configuration); |
| 390 | grpc_lb_policy_ref(lb_policy); |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame^] | 391 | |
| 392 | grpc_client_config_unref(chand->incoming_configuration); |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 393 | } |
| 394 | |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 395 | chand->incoming_configuration = NULL; |
| 396 | |
| 397 | gpr_mu_lock(&chand->mu_config); |
| 398 | old_lb_policy = chand->lb_policy; |
| 399 | chand->lb_policy = lb_policy; |
| 400 | if (lb_policy != NULL) { |
| 401 | wakeup_closures = chand->waiting_for_config_closures; |
| 402 | chand->waiting_for_config_closures = NULL; |
| 403 | } |
| 404 | gpr_mu_unlock(&chand->mu_config); |
| 405 | |
| 406 | while (wakeup_closures) { |
| 407 | grpc_iomgr_closure *next = wakeup_closures->next; |
| 408 | grpc_iomgr_add_callback(wakeup_closures); |
| 409 | wakeup_closures = next; |
| 410 | } |
| 411 | |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame^] | 412 | if (old_lb_policy) { |
| 413 | grpc_lb_policy_unref(old_lb_policy); |
| 414 | } |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 415 | |
| 416 | if (iomgr_success) { |
| 417 | grpc_resolver_next(chand->resolver, &chand->incoming_configuration, &chand->on_config_changed); |
| 418 | } else { |
| 419 | gpr_mu_lock(&chand->mu_config); |
| 420 | old_resolver = chand->resolver; |
| 421 | chand->resolver = NULL; |
| 422 | update_state_locked(chand); |
| 423 | gpr_mu_unlock(&chand->mu_config); |
| 424 | grpc_resolver_unref(old_resolver); |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | #if 0 |
ctiller | f962f52 | 2014-12-10 15:28:27 -0800 | [diff] [blame] | 429 | static void channel_op(grpc_channel_element *elem, |
| 430 | grpc_channel_element *from_elem, grpc_channel_op *op) { |
ctiller | 82e275f | 2014-12-12 08:43:28 -0800 | [diff] [blame] | 431 | channel_data *chand = elem->channel_data; |
| 432 | grpc_child_channel *child_channel; |
Craig Tiller | da66937 | 2015-02-05 10:10:15 -0800 | [diff] [blame] | 433 | grpc_channel_op rop; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 434 | GPR_ASSERT(elem->filter == &grpc_client_channel_filter); |
| 435 | |
| 436 | switch (op->type) { |
ctiller | 82e275f | 2014-12-12 08:43:28 -0800 | [diff] [blame] | 437 | case GRPC_CHANNEL_GOAWAY: |
ctiller | c6d61c4 | 2014-12-15 14:52:08 -0800 | [diff] [blame] | 438 | /* sending goaway: clear out the active child on the way through */ |
ctiller | 82e275f | 2014-12-12 08:43:28 -0800 | [diff] [blame] | 439 | gpr_mu_lock(&chand->mu); |
| 440 | child_channel = chand->active_child; |
| 441 | chand->active_child = NULL; |
| 442 | gpr_mu_unlock(&chand->mu); |
| 443 | if (child_channel) { |
| 444 | grpc_child_channel_handle_op(child_channel, op); |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 445 | grpc_child_channel_destroy(child_channel, 1); |
ctiller | 82e275f | 2014-12-12 08:43:28 -0800 | [diff] [blame] | 446 | } else { |
| 447 | gpr_slice_unref(op->data.goaway.message); |
| 448 | } |
| 449 | break; |
| 450 | case GRPC_CHANNEL_DISCONNECT: |
ctiller | c6d61c4 | 2014-12-15 14:52:08 -0800 | [diff] [blame] | 451 | /* sending disconnect: clear out the active child on the way through */ |
ctiller | 82e275f | 2014-12-12 08:43:28 -0800 | [diff] [blame] | 452 | gpr_mu_lock(&chand->mu); |
| 453 | child_channel = chand->active_child; |
| 454 | chand->active_child = NULL; |
| 455 | gpr_mu_unlock(&chand->mu); |
| 456 | if (child_channel) { |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 457 | grpc_child_channel_destroy(child_channel, 1); |
ctiller | c6d61c4 | 2014-12-15 14:52:08 -0800 | [diff] [blame] | 458 | } |
Craig Tiller | da66937 | 2015-02-05 10:10:15 -0800 | [diff] [blame] | 459 | /* fake a transport closed to satisfy the refcounting in client */ |
| 460 | rop.type = GRPC_TRANSPORT_CLOSED; |
| 461 | rop.dir = GRPC_CALL_UP; |
| 462 | grpc_channel_next_op(elem, &rop); |
ctiller | c6d61c4 | 2014-12-15 14:52:08 -0800 | [diff] [blame] | 463 | break; |
| 464 | case GRPC_TRANSPORT_GOAWAY: |
| 465 | /* receiving goaway: if it's from our active child, drop the active child; |
| 466 | in all cases consume the event here */ |
| 467 | gpr_mu_lock(&chand->mu); |
| 468 | child_channel = grpc_channel_stack_from_top_element(from_elem); |
| 469 | if (child_channel == chand->active_child) { |
| 470 | chand->active_child = NULL; |
| 471 | } else { |
| 472 | child_channel = NULL; |
| 473 | } |
| 474 | gpr_mu_unlock(&chand->mu); |
| 475 | if (child_channel) { |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 476 | grpc_child_channel_destroy(child_channel, 0); |
ctiller | c6d61c4 | 2014-12-15 14:52:08 -0800 | [diff] [blame] | 477 | } |
| 478 | gpr_slice_unref(op->data.goaway.message); |
| 479 | break; |
| 480 | case GRPC_TRANSPORT_CLOSED: |
| 481 | /* receiving disconnect: if it's from our active child, drop the active |
| 482 | child; in all cases consume the event here */ |
| 483 | gpr_mu_lock(&chand->mu); |
| 484 | child_channel = grpc_channel_stack_from_top_element(from_elem); |
| 485 | if (child_channel == chand->active_child) { |
| 486 | chand->active_child = NULL; |
| 487 | } else { |
| 488 | child_channel = NULL; |
| 489 | } |
| 490 | gpr_mu_unlock(&chand->mu); |
| 491 | if (child_channel) { |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 492 | grpc_child_channel_destroy(child_channel, 0); |
ctiller | 82e275f | 2014-12-12 08:43:28 -0800 | [diff] [blame] | 493 | } |
| 494 | break; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 495 | default: |
| 496 | switch (op->dir) { |
| 497 | case GRPC_CALL_UP: |
| 498 | grpc_channel_next_op(elem, op); |
| 499 | break; |
| 500 | case GRPC_CALL_DOWN: |
ctiller | 82e275f | 2014-12-12 08:43:28 -0800 | [diff] [blame] | 501 | gpr_log(GPR_ERROR, "unhandled channel op: %d", op->type); |
| 502 | abort(); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 503 | break; |
| 504 | } |
| 505 | break; |
| 506 | } |
| 507 | } |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 508 | #endif |
| 509 | |
| 510 | static void cc_start_transport_op(grpc_channel_element *elem, grpc_transport_op *op) {} |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 511 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 512 | /* Constructor for call_data */ |
| 513 | static void init_call_elem(grpc_call_element *elem, |
Craig Tiller | 06aeea7 | 2015-04-23 10:54:45 -0700 | [diff] [blame] | 514 | const void *server_transport_data, |
Craig Tiller | b7959a0 | 2015-06-25 08:50:54 -0700 | [diff] [blame] | 515 | grpc_transport_stream_op *initial_op) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 516 | call_data *calld = elem->call_data; |
| 517 | |
Craig Tiller | 50d9db5 | 2015-04-23 10:52:14 -0700 | [diff] [blame] | 518 | /* TODO(ctiller): is there something useful we can do here? */ |
| 519 | GPR_ASSERT(initial_op == NULL); |
| 520 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 521 | GPR_ASSERT(elem->filter == &grpc_client_channel_filter); |
| 522 | GPR_ASSERT(server_transport_data == NULL); |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame^] | 523 | gpr_mu_init(&calld->mu_state); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 524 | calld->elem = elem; |
| 525 | calld->state = CALL_CREATED; |
| 526 | calld->deadline = gpr_inf_future; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | /* Destructor for call_data */ |
| 530 | static void destroy_call_elem(grpc_call_element *elem) { |
| 531 | call_data *calld = elem->call_data; |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 532 | grpc_subchannel_call *subchannel_call; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 533 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 534 | /* if the call got activated, we need to destroy the child stack also, and |
| 535 | remove it from the in-flight requests tracked by the child_entry we |
| 536 | picked */ |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 537 | gpr_mu_lock(&calld->mu_state); |
Craig Tiller | f93fd05 | 2015-06-02 08:15:33 -0700 | [diff] [blame] | 538 | switch (calld->state) { |
| 539 | case CALL_ACTIVE: |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame^] | 540 | subchannel_call = calld->subchannel_call; |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 541 | gpr_mu_unlock(&calld->mu_state); |
| 542 | grpc_subchannel_call_unref(subchannel_call); |
Craig Tiller | f93fd05 | 2015-06-02 08:15:33 -0700 | [diff] [blame] | 543 | break; |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 544 | case CALL_CREATED: |
| 545 | case CALL_CANCELLED: |
| 546 | gpr_mu_unlock(&calld->mu_state); |
Craig Tiller | f93fd05 | 2015-06-02 08:15:33 -0700 | [diff] [blame] | 547 | break; |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 548 | case CALL_WAITING_FOR_PICK: |
| 549 | case CALL_WAITING_FOR_CONFIG: |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame^] | 550 | case CALL_WAITING_FOR_CALL: |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 551 | gpr_log(GPR_ERROR, "should never reach here"); |
| 552 | abort(); |
Craig Tiller | f93fd05 | 2015-06-02 08:15:33 -0700 | [diff] [blame] | 553 | break; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 554 | } |
| 555 | } |
| 556 | |
| 557 | /* Constructor for channel_data */ |
| 558 | static void init_channel_elem(grpc_channel_element *elem, |
| 559 | const grpc_channel_args *args, |
| 560 | grpc_mdctx *metadata_context, int is_first, |
| 561 | int is_last) { |
| 562 | channel_data *chand = elem->channel_data; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 563 | |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame^] | 564 | memset(chand, 0, sizeof(*chand)); |
| 565 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 566 | GPR_ASSERT(is_last); |
| 567 | GPR_ASSERT(elem->filter == &grpc_client_channel_filter); |
| 568 | |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 569 | gpr_mu_init(&chand->mu_config); |
Craig Tiller | e70413c | 2015-04-24 10:12:34 -0700 | [diff] [blame] | 570 | chand->mdctx = metadata_context; |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 571 | grpc_iomgr_closure_init(&chand->on_config_changed, cc_on_config_changed, chand); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | /* Destructor for channel_data */ |
| 575 | static void destroy_channel_elem(grpc_channel_element *elem) { |
| 576 | channel_data *chand = elem->channel_data; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 577 | |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 578 | if (chand->resolver) { |
| 579 | grpc_resolver_unref(chand->resolver); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 580 | } |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 581 | if (chand->lb_policy) { |
| 582 | grpc_lb_policy_unref(chand->lb_policy); |
| 583 | } |
| 584 | gpr_mu_destroy(&chand->mu_config); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | const grpc_channel_filter grpc_client_channel_filter = { |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 588 | cc_start_transport_stream_op, cc_start_transport_op, sizeof(call_data), |
Craig Tiller | 83b826a | 2015-05-13 13:43:01 -0700 | [diff] [blame] | 589 | init_call_elem, destroy_call_elem, sizeof(channel_data), |
| 590 | init_channel_elem, destroy_channel_elem, "client-channel", |
Craig Tiller | 87d5b19 | 2015-04-16 14:37:57 -0700 | [diff] [blame] | 591 | }; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 592 | |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 593 | #if 0 |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 594 | grpc_transport_setup_result grpc_client_channel_transport_setup_complete( |
| 595 | grpc_channel_stack *channel_stack, grpc_transport *transport, |
| 596 | grpc_channel_filter const **channel_filters, size_t num_channel_filters, |
| 597 | grpc_mdctx *mdctx) { |
| 598 | /* we just got a new transport: lets create a child channel stack for it */ |
| 599 | grpc_channel_element *elem = grpc_channel_stack_last_element(channel_stack); |
| 600 | channel_data *chand = elem->channel_data; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 601 | size_t num_child_filters = 2 + num_channel_filters; |
| 602 | grpc_channel_filter const **child_filters; |
| 603 | grpc_transport_setup_result result; |
ctiller | 82e275f | 2014-12-12 08:43:28 -0800 | [diff] [blame] | 604 | grpc_child_channel *old_active = NULL; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 605 | call_data **waiting_children; |
| 606 | size_t waiting_child_count; |
| 607 | size_t i; |
Craig Tiller | b7959a0 | 2015-06-25 08:50:54 -0700 | [diff] [blame] | 608 | grpc_transport_stream_op *call_ops; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 609 | |
| 610 | /* build the child filter stack */ |
| 611 | child_filters = gpr_malloc(sizeof(grpc_channel_filter *) * num_child_filters); |
| 612 | /* 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] | 613 | child_filters[0] = &grpc_child_channel_top_filter; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 614 | for (i = 0; i < num_channel_filters; i++) { |
| 615 | child_filters[i + 1] = channel_filters[i]; |
| 616 | } |
| 617 | /* and we always need a connected channel to talk to the transport */ |
| 618 | child_filters[num_child_filters - 1] = &grpc_connected_channel_filter; |
| 619 | |
| 620 | GPR_ASSERT(elem->filter == &grpc_client_channel_filter); |
| 621 | |
| 622 | /* BEGIN LOCKING CHANNEL */ |
| 623 | gpr_mu_lock(&chand->mu); |
| 624 | chand->transport_setup_initiated = 0; |
| 625 | |
ctiller | 82e275f | 2014-12-12 08:43:28 -0800 | [diff] [blame] | 626 | if (chand->active_child) { |
| 627 | old_active = chand->active_child; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 628 | } |
ctiller | 82e275f | 2014-12-12 08:43:28 -0800 | [diff] [blame] | 629 | chand->active_child = grpc_child_channel_create( |
| 630 | elem, child_filters, num_child_filters, chand->args, mdctx); |
| 631 | result = |
| 632 | grpc_connected_channel_bind_transport(chand->active_child, transport); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 633 | |
| 634 | /* capture the waiting children - we'll activate them outside the lock |
| 635 | to avoid re-entrancy problems */ |
| 636 | waiting_children = chand->waiting_children; |
| 637 | waiting_child_count = chand->waiting_child_count; |
| 638 | /* bumping up inflight_requests here avoids taking a lock per rpc below */ |
| 639 | |
| 640 | chand->waiting_children = NULL; |
| 641 | chand->waiting_child_count = 0; |
| 642 | chand->waiting_child_capacity = 0; |
| 643 | |
Craig Tiller | 83f88d9 | 2015-04-21 16:02:05 -0700 | [diff] [blame] | 644 | call_ops = gpr_malloc(sizeof(*call_ops) * waiting_child_count); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 645 | |
| 646 | for (i = 0; i < waiting_child_count; i++) { |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame^] | 647 | call_ops[i] = waiting_children[i]->waiting_op; |
ctiller | 82e275f | 2014-12-12 08:43:28 -0800 | [diff] [blame] | 648 | if (!prepare_activate(waiting_children[i]->elem, chand->active_child)) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 649 | waiting_children[i] = NULL; |
Craig Tiller | b7959a0 | 2015-06-25 08:50:54 -0700 | [diff] [blame] | 650 | grpc_transport_stream_op_finish_with_failure(&call_ops[i]); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 651 | } |
| 652 | } |
| 653 | |
| 654 | /* END LOCKING CHANNEL */ |
| 655 | gpr_mu_unlock(&chand->mu); |
| 656 | |
| 657 | /* activate any pending operations - this is safe to do as we guarantee one |
| 658 | and only one write operation per request at the surface api - if we lose |
| 659 | that guarantee we need to do some curly locking here */ |
| 660 | for (i = 0; i < waiting_child_count; i++) { |
| 661 | if (waiting_children[i]) { |
ctiller | 82e275f | 2014-12-12 08:43:28 -0800 | [diff] [blame] | 662 | complete_activate(waiting_children[i]->elem, &call_ops[i]); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 663 | } |
| 664 | } |
| 665 | gpr_free(waiting_children); |
| 666 | gpr_free(call_ops); |
| 667 | gpr_free(child_filters); |
| 668 | |
ctiller | 82e275f | 2014-12-12 08:43:28 -0800 | [diff] [blame] | 669 | if (old_active) { |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 670 | grpc_child_channel_destroy(old_active, 1); |
ctiller | 82e275f | 2014-12-12 08:43:28 -0800 | [diff] [blame] | 671 | } |
| 672 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 673 | return result; |
| 674 | } |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 675 | #endif |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 676 | |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 677 | void grpc_client_channel_set_resolver(grpc_channel_stack *channel_stack, |
| 678 | grpc_resolver *resolver) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 679 | /* post construction initialization: set the transport setup pointer */ |
| 680 | grpc_channel_element *elem = grpc_channel_stack_last_element(channel_stack); |
| 681 | channel_data *chand = elem->channel_data; |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 682 | GPR_ASSERT(!chand->resolver); |
| 683 | chand->resolver = resolver; |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 684 | grpc_resolver_ref(resolver); |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 685 | grpc_resolver_next(resolver, &chand->incoming_configuration, &chand->on_config_changed); |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 686 | } |