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" |
Craig Tiller | ca3e9d3 | 2015-06-27 18:37:27 -0700 | [diff] [blame^] | 41 | #include "src/core/channel/connectivity_state.h" |
ctiller | c6d61c4 | 2014-12-15 14:52:08 -0800 | [diff] [blame] | 42 | #include "src/core/iomgr/iomgr.h" |
Craig Tiller | fe4ba36 | 2015-05-08 09:47:18 -0700 | [diff] [blame] | 43 | #include "src/core/iomgr/pollset_set.h" |
Craig Tiller | 485d776 | 2015-01-23 12:54:05 -0800 | [diff] [blame] | 44 | #include "src/core/support/string.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 45 | #include <grpc/support/alloc.h> |
| 46 | #include <grpc/support/log.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 47 | #include <grpc/support/sync.h> |
| 48 | #include <grpc/support/useful.h> |
| 49 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 50 | /* Client channel implementation */ |
| 51 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 52 | typedef struct call_data call_data; |
| 53 | |
| 54 | typedef struct { |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 55 | /** metadata context for this channel */ |
Craig Tiller | e70413c | 2015-04-24 10:12:34 -0700 | [diff] [blame] | 56 | grpc_mdctx *mdctx; |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 57 | /** resolver for this channel */ |
| 58 | grpc_resolver *resolver; |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 59 | |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 60 | /** mutex protecting client configuration, resolution state */ |
| 61 | gpr_mu mu_config; |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 62 | /** currently active load balancer - guarded by mu_config */ |
| 63 | grpc_lb_policy *lb_policy; |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 64 | /** incoming configuration - set by resolver.next |
| 65 | guarded by mu_config */ |
| 66 | grpc_client_config *incoming_configuration; |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 67 | /** a list of closures that are all waiting for config to come in */ |
| 68 | grpc_iomgr_closure *waiting_for_config_closures; |
| 69 | /** resolver callback */ |
| 70 | grpc_iomgr_closure on_config_changed; |
| 71 | /** connectivity state being tracked */ |
Craig Tiller | ca3e9d3 | 2015-06-27 18:37:27 -0700 | [diff] [blame^] | 72 | grpc_connectivity_state_tracker state_tracker; |
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 | 1a727fd | 2015-04-24 13:21:22 -0700 | [diff] [blame] | 101 | static void handle_op_after_cancellation(grpc_call_element *elem, |
Craig Tiller | b7959a0 | 2015-06-25 08:50:54 -0700 | [diff] [blame] | 102 | grpc_transport_stream_op *op) { |
Craig Tiller | e70413c | 2015-04-24 10:12:34 -0700 | [diff] [blame] | 103 | call_data *calld = elem->call_data; |
| 104 | channel_data *chand = elem->channel_data; |
| 105 | if (op->send_ops) { |
Yang Gao | dbf8fdc | 2015-05-28 00:52:31 -0700 | [diff] [blame] | 106 | 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] | 107 | op->on_done_send->cb(op->on_done_send->cb_arg, 0); |
Craig Tiller | e70413c | 2015-04-24 10:12:34 -0700 | [diff] [blame] | 108 | } |
| 109 | if (op->recv_ops) { |
| 110 | char status[GPR_LTOA_MIN_BUFSIZE]; |
| 111 | grpc_metadata_batch mdb; |
| 112 | gpr_ltoa(GRPC_STATUS_CANCELLED, status); |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 113 | calld->status.md = |
Craig Tiller | 1a727fd | 2015-04-24 13:21:22 -0700 | [diff] [blame] | 114 | grpc_mdelem_from_strings(chand->mdctx, "grpc-status", status); |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 115 | calld->details.md = |
Craig Tiller | 1a727fd | 2015-04-24 13:21:22 -0700 | [diff] [blame] | 116 | grpc_mdelem_from_strings(chand->mdctx, "grpc-message", "Cancelled"); |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 117 | calld->status.prev = calld->details.next = NULL; |
| 118 | calld->status.next = &calld->details; |
| 119 | calld->details.prev = &calld->status; |
| 120 | mdb.list.head = &calld->status; |
| 121 | mdb.list.tail = &calld->details; |
Craig Tiller | e70413c | 2015-04-24 10:12:34 -0700 | [diff] [blame] | 122 | mdb.garbage.head = mdb.garbage.tail = NULL; |
| 123 | mdb.deadline = gpr_inf_future; |
| 124 | grpc_sopb_add_metadata(op->recv_ops, mdb); |
| 125 | *op->recv_state = GRPC_STREAM_CLOSED; |
Craig Tiller | 1e6facb | 2015-06-11 22:47:11 -0700 | [diff] [blame] | 126 | op->on_done_recv->cb(op->on_done_recv->cb_arg, 1); |
Craig Tiller | e70413c | 2015-04-24 10:12:34 -0700 | [diff] [blame] | 127 | } |
Craig Tiller | 5dde66e | 2015-06-02 09:05:23 -0700 | [diff] [blame] | 128 | if (op->on_consumed) { |
Craig Tiller | 1e6facb | 2015-06-11 22:47:11 -0700 | [diff] [blame] | 129 | op->on_consumed->cb(op->on_consumed->cb_arg, 0); |
Craig Tiller | 5dde66e | 2015-06-02 09:05:23 -0700 | [diff] [blame] | 130 | } |
Craig Tiller | e70413c | 2015-04-24 10:12:34 -0700 | [diff] [blame] | 131 | } |
Craig Tiller | 8b976d0 | 2015-02-05 21:41:23 -0800 | [diff] [blame] | 132 | |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 133 | typedef struct { |
| 134 | grpc_iomgr_closure closure; |
| 135 | grpc_call_element *elem; |
| 136 | } waiting_call; |
| 137 | |
| 138 | static void perform_transport_stream_op(grpc_call_element *elem, grpc_transport_stream_op *op, int continuation); |
| 139 | |
| 140 | static void continue_with_pick(void *arg, int iomgr_success) { |
| 141 | waiting_call *wc = arg; |
| 142 | call_data *calld = wc->elem->call_data; |
| 143 | perform_transport_stream_op(wc->elem, &calld->waiting_op, 1); |
| 144 | gpr_free(wc); |
| 145 | } |
| 146 | |
| 147 | static void add_to_lb_policy_wait_queue_locked_state_config(grpc_call_element *elem) { |
| 148 | channel_data *chand = elem->channel_data; |
| 149 | waiting_call *wc = gpr_malloc(sizeof(*wc)); |
| 150 | grpc_iomgr_closure_init(&wc->closure, continue_with_pick, wc); |
| 151 | wc->elem = elem; |
| 152 | wc->closure.next = chand->waiting_for_config_closures; |
| 153 | chand->waiting_for_config_closures = &wc->closure; |
| 154 | } |
| 155 | |
| 156 | static int is_empty(void *p, int len) { |
| 157 | char *ptr = p; |
| 158 | int i; |
| 159 | for (i = 0; i < len; i++) { |
| 160 | if (ptr[i] != 0) return 0; |
| 161 | } |
| 162 | return 1; |
| 163 | } |
| 164 | |
| 165 | static void started_call(void *arg, int iomgr_success) { |
| 166 | call_data *calld = arg; |
| 167 | grpc_transport_stream_op op; |
| 168 | int have_waiting; |
| 169 | |
| 170 | gpr_mu_lock(&calld->mu_state); |
| 171 | if (calld->state == CALL_CANCELLED && calld->subchannel_call != NULL) { |
| 172 | memset(&op, 0, sizeof(op)); |
| 173 | op.cancel_with_status = GRPC_STATUS_CANCELLED; |
| 174 | gpr_mu_unlock(&calld->mu_state); |
| 175 | grpc_subchannel_call_process_op(calld->subchannel_call, &op); |
| 176 | } else if (calld->state == CALL_WAITING_FOR_CALL) { |
| 177 | have_waiting = !is_empty(&calld->waiting_op, sizeof(calld->waiting_op)); |
| 178 | if (calld->subchannel_call != NULL) { |
| 179 | calld->state = CALL_ACTIVE; |
| 180 | gpr_mu_unlock(&calld->mu_state); |
| 181 | if (have_waiting) { |
| 182 | grpc_subchannel_call_process_op(calld->subchannel_call, &calld->waiting_op); |
| 183 | } |
| 184 | } else { |
| 185 | calld->state = CALL_CANCELLED; |
| 186 | gpr_mu_unlock(&calld->mu_state); |
| 187 | if (have_waiting) { |
| 188 | handle_op_after_cancellation(calld->elem, &calld->waiting_op); |
| 189 | } |
| 190 | } |
| 191 | } else { |
| 192 | GPR_ASSERT(calld->state == CALL_CANCELLED); |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | static void picked_target(void *arg, int iomgr_success) { |
| 197 | call_data *calld = arg; |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 198 | grpc_transport_stream_op op; |
| 199 | |
| 200 | if (calld->picked_channel == NULL) { |
| 201 | /* treat this like a cancellation */ |
| 202 | calld->waiting_op.cancel_with_status = GRPC_STATUS_UNAVAILABLE; |
| 203 | perform_transport_stream_op(calld->elem, &calld->waiting_op, 1); |
| 204 | } else { |
| 205 | gpr_mu_lock(&calld->mu_state); |
| 206 | if (calld->state == CALL_CANCELLED) { |
| 207 | gpr_mu_unlock(&calld->mu_state); |
| 208 | handle_op_after_cancellation(calld->elem, &calld->waiting_op); |
| 209 | } else { |
| 210 | GPR_ASSERT(calld->state == CALL_WAITING_FOR_PICK); |
| 211 | calld->state = CALL_WAITING_FOR_CALL; |
| 212 | op = calld->waiting_op; |
| 213 | memset(&calld->waiting_op, 0, sizeof(calld->waiting_op)); |
| 214 | gpr_mu_unlock(&calld->mu_state); |
| 215 | grpc_iomgr_closure_init(&calld->async_setup_task, started_call, calld); |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 216 | grpc_subchannel_create_call(calld->picked_channel, &op, |
| 217 | &calld->subchannel_call, |
| 218 | &calld->async_setup_task); |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 219 | } |
| 220 | } |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | static void pick_target(grpc_lb_policy *lb_policy, call_data *calld) { |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 224 | grpc_metadata_batch *initial_metadata; |
| 225 | grpc_transport_stream_op *op = &calld->waiting_op; |
| 226 | |
| 227 | GPR_ASSERT(op->bind_pollset); |
| 228 | GPR_ASSERT(op->send_ops); |
| 229 | GPR_ASSERT(op->send_ops->nops >= 1); |
| 230 | GPR_ASSERT(op->send_ops->ops[0].type == GRPC_OP_METADATA); |
| 231 | initial_metadata = &op->send_ops->ops[0].data.metadata; |
| 232 | |
| 233 | grpc_iomgr_closure_init(&calld->async_setup_task, picked_target, calld); |
| 234 | grpc_lb_policy_pick(lb_policy, op->bind_pollset, |
| 235 | initial_metadata, &calld->picked_channel, &calld->async_setup_task); |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 236 | } |
| 237 | |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 238 | 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] | 239 | call_data *calld = elem->call_data; |
| 240 | channel_data *chand = elem->channel_data; |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 241 | grpc_subchannel_call *subchannel_call; |
| 242 | grpc_lb_policy *lb_policy; |
Craig Tiller | a940752 | 2015-04-24 10:37:57 -0700 | [diff] [blame] | 243 | GPR_ASSERT(elem->filter == &grpc_client_channel_filter); |
| 244 | GRPC_CALL_LOG_OP(GPR_INFO, elem, op); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 245 | |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 246 | gpr_mu_lock(&calld->mu_state); |
| 247 | switch (calld->state) { |
| 248 | case CALL_ACTIVE: |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 249 | GPR_ASSERT(!continuation); |
| 250 | subchannel_call = calld->subchannel_call; |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 251 | gpr_mu_unlock(&calld->mu_state); |
| 252 | grpc_subchannel_call_process_op(subchannel_call, op); |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 253 | break; |
| 254 | case CALL_CANCELLED: |
| 255 | gpr_mu_unlock(&calld->mu_state); |
| 256 | handle_op_after_cancellation(elem, op); |
| 257 | break; |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 258 | case CALL_WAITING_FOR_CONFIG: |
| 259 | case CALL_WAITING_FOR_PICK: |
| 260 | case CALL_WAITING_FOR_CALL: |
| 261 | if (!continuation) { |
| 262 | if (op->cancel_with_status != GRPC_STATUS_OK) { |
| 263 | calld->state = CALL_CANCELLED; |
| 264 | gpr_mu_unlock(&calld->mu_state); |
| 265 | handle_op_after_cancellation(elem, op); |
| 266 | } else { |
| 267 | GPR_ASSERT((calld->waiting_op.send_ops == NULL) != |
| 268 | (op->send_ops == NULL)); |
| 269 | GPR_ASSERT((calld->waiting_op.recv_ops == NULL) != |
| 270 | (op->recv_ops == NULL)); |
| 271 | if (op->send_ops != NULL) { |
| 272 | calld->waiting_op.send_ops = op->send_ops; |
| 273 | calld->waiting_op.is_last_send = op->is_last_send; |
| 274 | calld->waiting_op.on_done_send = op->on_done_send; |
| 275 | } |
| 276 | if (op->recv_ops != NULL) { |
| 277 | calld->waiting_op.recv_ops = op->recv_ops; |
| 278 | calld->waiting_op.recv_state = op->recv_state; |
| 279 | calld->waiting_op.on_done_recv = op->on_done_recv; |
| 280 | } |
| 281 | gpr_mu_unlock(&calld->mu_state); |
| 282 | if (op->on_consumed != NULL) { |
| 283 | op->on_consumed->cb(op->on_consumed->cb_arg, 0); |
| 284 | } |
| 285 | } |
| 286 | break; |
| 287 | } |
| 288 | /* fall through */ |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 289 | case CALL_CREATED: |
| 290 | if (op->cancel_with_status != GRPC_STATUS_OK) { |
| 291 | calld->state = CALL_CANCELLED; |
| 292 | gpr_mu_unlock(&calld->mu_state); |
| 293 | handle_op_after_cancellation(elem, op); |
| 294 | } else { |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 295 | calld->waiting_op = *op; |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 296 | |
| 297 | gpr_mu_lock(&chand->mu_config); |
| 298 | lb_policy = chand->lb_policy; |
| 299 | if (lb_policy) { |
| 300 | grpc_lb_policy_ref(lb_policy); |
| 301 | gpr_mu_unlock(&chand->mu_config); |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 302 | calld->state = CALL_WAITING_FOR_PICK; |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 303 | gpr_mu_unlock(&calld->mu_state); |
| 304 | |
| 305 | pick_target(lb_policy, calld); |
| 306 | |
| 307 | grpc_lb_policy_unref(lb_policy); |
| 308 | } else { |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 309 | calld->state = CALL_WAITING_FOR_CONFIG; |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 310 | add_to_lb_policy_wait_queue_locked_state_config(elem); |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 311 | gpr_mu_unlock(&chand->mu_config); |
| 312 | gpr_mu_unlock(&calld->mu_state); |
| 313 | } |
| 314 | } |
| 315 | break; |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 316 | } |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 317 | } |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 318 | |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 319 | static void cc_start_transport_stream_op(grpc_call_element *elem, |
| 320 | grpc_transport_stream_op *op) { |
| 321 | perform_transport_stream_op(elem, op, 0); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 322 | } |
| 323 | |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 324 | static void update_state_locked(channel_data *chand) { |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 325 | gpr_log(GPR_ERROR, "update_state_locked not implemented"); |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | static void cc_on_config_changed(void *arg, int iomgr_success) { |
| 329 | channel_data *chand = arg; |
| 330 | grpc_lb_policy *lb_policy = NULL; |
| 331 | grpc_lb_policy *old_lb_policy; |
| 332 | grpc_resolver *old_resolver; |
| 333 | grpc_iomgr_closure *wakeup_closures = NULL; |
| 334 | |
| 335 | if (chand->incoming_configuration) { |
| 336 | lb_policy = grpc_client_config_get_lb_policy(chand->incoming_configuration); |
| 337 | grpc_lb_policy_ref(lb_policy); |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 338 | |
| 339 | grpc_client_config_unref(chand->incoming_configuration); |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 340 | } |
| 341 | |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 342 | chand->incoming_configuration = NULL; |
| 343 | |
| 344 | gpr_mu_lock(&chand->mu_config); |
| 345 | old_lb_policy = chand->lb_policy; |
| 346 | chand->lb_policy = lb_policy; |
| 347 | if (lb_policy != NULL) { |
| 348 | wakeup_closures = chand->waiting_for_config_closures; |
| 349 | chand->waiting_for_config_closures = NULL; |
| 350 | } |
| 351 | gpr_mu_unlock(&chand->mu_config); |
| 352 | |
| 353 | while (wakeup_closures) { |
| 354 | grpc_iomgr_closure *next = wakeup_closures->next; |
| 355 | grpc_iomgr_add_callback(wakeup_closures); |
| 356 | wakeup_closures = next; |
| 357 | } |
| 358 | |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 359 | if (old_lb_policy) { |
| 360 | grpc_lb_policy_unref(old_lb_policy); |
| 361 | } |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 362 | |
| 363 | if (iomgr_success) { |
| 364 | grpc_resolver_next(chand->resolver, &chand->incoming_configuration, &chand->on_config_changed); |
| 365 | } else { |
| 366 | gpr_mu_lock(&chand->mu_config); |
| 367 | old_resolver = chand->resolver; |
| 368 | chand->resolver = NULL; |
| 369 | update_state_locked(chand); |
| 370 | gpr_mu_unlock(&chand->mu_config); |
| 371 | grpc_resolver_unref(old_resolver); |
| 372 | } |
| 373 | } |
| 374 | |
Craig Tiller | ca3e9d3 | 2015-06-27 18:37:27 -0700 | [diff] [blame^] | 375 | static void cc_start_transport_op(grpc_channel_element *elem, grpc_transport_op *op) { |
| 376 | grpc_lb_policy *lb_policy = NULL; |
| 377 | channel_data *chand = elem->channel_data; |
| 378 | grpc_iomgr_closure *on_consumed = op->on_consumed; |
| 379 | op->on_consumed = NULL; |
| 380 | |
| 381 | GPR_ASSERT(op->set_accept_stream == NULL); |
| 382 | GPR_ASSERT(op->bind_pollset == NULL); |
| 383 | |
| 384 | gpr_mu_lock(&chand->mu_config); |
| 385 | if (op->on_connectivity_state_change != NULL) { |
| 386 | grpc_connectivity_state_notify_on_state_change(&chand->state_tracker, op->connectivity_state, op->on_connectivity_state_change); |
| 387 | op->on_connectivity_state_change = NULL; |
| 388 | op->connectivity_state = NULL; |
| 389 | } |
| 390 | |
| 391 | if (!is_empty(op, sizeof(*op))) { |
| 392 | lb_policy = chand->lb_policy; |
| 393 | if (lb_policy) { |
| 394 | grpc_lb_policy_ref(lb_policy); |
| 395 | } |
| 396 | } |
| 397 | gpr_mu_unlock(&chand->mu_config); |
| 398 | |
| 399 | if (lb_policy) { |
| 400 | grpc_lb_policy_broadcast(lb_policy, op); |
| 401 | grpc_lb_policy_unref(lb_policy); |
| 402 | } |
| 403 | |
| 404 | if (on_consumed) { |
| 405 | grpc_iomgr_add_callback(on_consumed); |
| 406 | } |
| 407 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 408 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 409 | /* Constructor for call_data */ |
| 410 | static void init_call_elem(grpc_call_element *elem, |
Craig Tiller | 06aeea7 | 2015-04-23 10:54:45 -0700 | [diff] [blame] | 411 | const void *server_transport_data, |
Craig Tiller | b7959a0 | 2015-06-25 08:50:54 -0700 | [diff] [blame] | 412 | grpc_transport_stream_op *initial_op) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 413 | call_data *calld = elem->call_data; |
| 414 | |
Craig Tiller | 50d9db5 | 2015-04-23 10:52:14 -0700 | [diff] [blame] | 415 | /* TODO(ctiller): is there something useful we can do here? */ |
| 416 | GPR_ASSERT(initial_op == NULL); |
| 417 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 418 | GPR_ASSERT(elem->filter == &grpc_client_channel_filter); |
| 419 | GPR_ASSERT(server_transport_data == NULL); |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 420 | gpr_mu_init(&calld->mu_state); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 421 | calld->elem = elem; |
| 422 | calld->state = CALL_CREATED; |
| 423 | calld->deadline = gpr_inf_future; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | /* Destructor for call_data */ |
| 427 | static void destroy_call_elem(grpc_call_element *elem) { |
| 428 | call_data *calld = elem->call_data; |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 429 | grpc_subchannel_call *subchannel_call; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 430 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 431 | /* if the call got activated, we need to destroy the child stack also, and |
| 432 | remove it from the in-flight requests tracked by the child_entry we |
| 433 | picked */ |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 434 | gpr_mu_lock(&calld->mu_state); |
Craig Tiller | f93fd05 | 2015-06-02 08:15:33 -0700 | [diff] [blame] | 435 | switch (calld->state) { |
| 436 | case CALL_ACTIVE: |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 437 | subchannel_call = calld->subchannel_call; |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 438 | gpr_mu_unlock(&calld->mu_state); |
Craig Tiller | ca3e9d3 | 2015-06-27 18:37:27 -0700 | [diff] [blame^] | 439 | GRPC_SUBCHANNEL_CALL_UNREF(subchannel_call, "channel"); |
Craig Tiller | f93fd05 | 2015-06-02 08:15:33 -0700 | [diff] [blame] | 440 | break; |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 441 | case CALL_CREATED: |
| 442 | case CALL_CANCELLED: |
| 443 | gpr_mu_unlock(&calld->mu_state); |
Craig Tiller | f93fd05 | 2015-06-02 08:15:33 -0700 | [diff] [blame] | 444 | break; |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 445 | case CALL_WAITING_FOR_PICK: |
| 446 | case CALL_WAITING_FOR_CONFIG: |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 447 | case CALL_WAITING_FOR_CALL: |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 448 | gpr_log(GPR_ERROR, "should never reach here"); |
| 449 | abort(); |
Craig Tiller | f93fd05 | 2015-06-02 08:15:33 -0700 | [diff] [blame] | 450 | break; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 451 | } |
| 452 | } |
| 453 | |
| 454 | /* Constructor for channel_data */ |
| 455 | static void init_channel_elem(grpc_channel_element *elem, |
| 456 | const grpc_channel_args *args, |
| 457 | grpc_mdctx *metadata_context, int is_first, |
| 458 | int is_last) { |
| 459 | channel_data *chand = elem->channel_data; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 460 | |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 461 | memset(chand, 0, sizeof(*chand)); |
| 462 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 463 | GPR_ASSERT(is_last); |
| 464 | GPR_ASSERT(elem->filter == &grpc_client_channel_filter); |
| 465 | |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 466 | gpr_mu_init(&chand->mu_config); |
Craig Tiller | e70413c | 2015-04-24 10:12:34 -0700 | [diff] [blame] | 467 | chand->mdctx = metadata_context; |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 468 | 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] | 469 | } |
| 470 | |
| 471 | /* Destructor for channel_data */ |
| 472 | static void destroy_channel_elem(grpc_channel_element *elem) { |
| 473 | channel_data *chand = elem->channel_data; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 474 | |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 475 | if (chand->resolver) { |
| 476 | grpc_resolver_unref(chand->resolver); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 477 | } |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 478 | if (chand->lb_policy) { |
| 479 | grpc_lb_policy_unref(chand->lb_policy); |
| 480 | } |
| 481 | gpr_mu_destroy(&chand->mu_config); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | const grpc_channel_filter grpc_client_channel_filter = { |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 485 | cc_start_transport_stream_op, cc_start_transport_op, sizeof(call_data), |
Craig Tiller | 83b826a | 2015-05-13 13:43:01 -0700 | [diff] [blame] | 486 | init_call_elem, destroy_call_elem, sizeof(channel_data), |
| 487 | init_channel_elem, destroy_channel_elem, "client-channel", |
Craig Tiller | 87d5b19 | 2015-04-16 14:37:57 -0700 | [diff] [blame] | 488 | }; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 489 | |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 490 | void grpc_client_channel_set_resolver(grpc_channel_stack *channel_stack, |
| 491 | grpc_resolver *resolver) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 492 | /* post construction initialization: set the transport setup pointer */ |
| 493 | grpc_channel_element *elem = grpc_channel_stack_last_element(channel_stack); |
| 494 | channel_data *chand = elem->channel_data; |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 495 | GPR_ASSERT(!chand->resolver); |
| 496 | chand->resolver = resolver; |
Craig Tiller | 3f47542 | 2015-06-25 10:43:05 -0700 | [diff] [blame] | 497 | grpc_resolver_ref(resolver); |
Craig Tiller | f5f1712 | 2015-06-25 08:47:26 -0700 | [diff] [blame] | 498 | grpc_resolver_next(resolver, &chand->incoming_configuration, &chand->on_config_changed); |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 499 | } |