Craig Tiller | af69180 | 2015-06-23 14:57:07 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright 2015, Google Inc. |
| 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/client_config/subchannel.h" |
Craig Tiller | 2595ab7 | 2015-06-25 15:26:00 -0700 | [diff] [blame] | 35 | |
Craig Tiller | f7afa1f | 2015-06-26 09:02:20 -0700 | [diff] [blame] | 36 | #include <string.h> |
| 37 | |
Craig Tiller | 2595ab7 | 2015-06-25 15:26:00 -0700 | [diff] [blame] | 38 | #include <grpc/support/alloc.h> |
| 39 | |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 40 | #include "src/core/channel/channel_args.h" |
Craig Tiller | ff54c92 | 2015-06-26 16:57:20 -0700 | [diff] [blame] | 41 | #include "src/core/channel/connected_channel.h" |
Craig Tiller | 08a1cf8 | 2015-06-29 09:37:52 -0700 | [diff] [blame] | 42 | #include "src/core/transport/connectivity_state.h" |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 43 | |
| 44 | typedef struct { |
Craig Tiller | 4ab82d2 | 2015-06-29 09:40:33 -0700 | [diff] [blame] | 45 | /* all fields protected by subchannel->mu */ |
| 46 | /** refcount */ |
| 47 | int refs; |
| 48 | /** parent subchannel */ |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 49 | grpc_subchannel *subchannel; |
| 50 | } connection; |
| 51 | |
Craig Tiller | df91ba5 | 2015-06-29 10:55:46 -0700 | [diff] [blame^] | 52 | typedef struct { |
| 53 | grpc_iomgr_closure closure; |
| 54 | size_t version; |
| 55 | grpc_subchannel *subchannel; |
| 56 | grpc_connectivity_state connectivity_state; |
| 57 | } state_watcher; |
| 58 | |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 59 | typedef struct waiting_for_connect { |
| 60 | struct waiting_for_connect *next; |
| 61 | grpc_iomgr_closure *notify; |
Craig Tiller | df91ba5 | 2015-06-29 10:55:46 -0700 | [diff] [blame^] | 62 | grpc_transport_stream_op initial_op; |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 63 | grpc_subchannel_call **target; |
Craig Tiller | df91ba5 | 2015-06-29 10:55:46 -0700 | [diff] [blame^] | 64 | grpc_subchannel *subchannel; |
| 65 | grpc_iomgr_closure continuation; |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 66 | } waiting_for_connect; |
| 67 | |
Craig Tiller | 2595ab7 | 2015-06-25 15:26:00 -0700 | [diff] [blame] | 68 | struct grpc_subchannel { |
Craig Tiller | 9162466 | 2015-06-25 16:31:02 -0700 | [diff] [blame] | 69 | grpc_connector *connector; |
Craig Tiller | f7afa1f | 2015-06-26 09:02:20 -0700 | [diff] [blame] | 70 | |
| 71 | /** non-transport related channel filters */ |
| 72 | const grpc_channel_filter **filters; |
Craig Tiller | 5945ee1 | 2015-06-27 10:36:09 -0700 | [diff] [blame] | 73 | size_t num_filters; |
Craig Tiller | f7afa1f | 2015-06-26 09:02:20 -0700 | [diff] [blame] | 74 | /** channel arguments */ |
| 75 | grpc_channel_args *args; |
| 76 | /** address to connect to */ |
| 77 | struct sockaddr *addr; |
| 78 | size_t addr_len; |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 79 | /** metadata context */ |
| 80 | grpc_mdctx *mdctx; |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 81 | |
| 82 | /** set during connection */ |
Craig Tiller | 04c5d4b | 2015-06-26 17:21:41 -0700 | [diff] [blame] | 83 | grpc_connect_out_args connecting_result; |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 84 | |
| 85 | /** callback for connection finishing */ |
| 86 | grpc_iomgr_closure connected; |
| 87 | |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 88 | /** pollset_set tracking who's interested in a connection |
| 89 | being setup */ |
| 90 | grpc_pollset_set pollset_set; |
| 91 | |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 92 | /** mutex protecting remaining elements */ |
| 93 | gpr_mu mu; |
| 94 | |
| 95 | /** active connection */ |
| 96 | connection *active; |
Craig Tiller | df91ba5 | 2015-06-29 10:55:46 -0700 | [diff] [blame^] | 97 | /** version number for the active connection */ |
| 98 | size_t active_version; |
Craig Tiller | d7b68e7 | 2015-06-28 11:41:09 -0700 | [diff] [blame] | 99 | /** refcount */ |
| 100 | int refs; |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 101 | /** are we connecting */ |
| 102 | int connecting; |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 103 | /** things waiting for a connection */ |
| 104 | waiting_for_connect *waiting; |
Craig Tiller | c7b5f76 | 2015-06-27 11:48:42 -0700 | [diff] [blame] | 105 | /** connectivity state tracking */ |
| 106 | grpc_connectivity_state_tracker state_tracker; |
Craig Tiller | 2595ab7 | 2015-06-25 15:26:00 -0700 | [diff] [blame] | 107 | }; |
| 108 | |
| 109 | struct grpc_subchannel_call { |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 110 | connection *connection; |
Craig Tiller | 2595ab7 | 2015-06-25 15:26:00 -0700 | [diff] [blame] | 111 | gpr_refcount refs; |
| 112 | }; |
| 113 | |
Craig Tiller | 04c5d4b | 2015-06-26 17:21:41 -0700 | [diff] [blame] | 114 | #define SUBCHANNEL_CALL_TO_CALL_STACK(call) ((grpc_call_stack *)((call) + 1)) |
| 115 | #define CHANNEL_STACK_FROM_CONNECTION(con) ((grpc_channel_stack *)((con) + 1)) |
Craig Tiller | 2595ab7 | 2015-06-25 15:26:00 -0700 | [diff] [blame] | 116 | |
Craig Tiller | 4ab82d2 | 2015-06-29 09:40:33 -0700 | [diff] [blame] | 117 | static grpc_subchannel_call *create_call(connection *con, |
| 118 | grpc_transport_stream_op *initial_op); |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 119 | static void connectivity_state_changed_locked(grpc_subchannel *c); |
| 120 | static grpc_connectivity_state compute_connectivity_locked(grpc_subchannel *c); |
| 121 | static gpr_timespec compute_connect_deadline(grpc_subchannel *c); |
Craig Tiller | ff54c92 | 2015-06-26 16:57:20 -0700 | [diff] [blame] | 122 | static void subchannel_connected(void *subchannel, int iomgr_success); |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 123 | |
Craig Tiller | d7b68e7 | 2015-06-28 11:41:09 -0700 | [diff] [blame] | 124 | static void subchannel_ref_locked(grpc_subchannel *c); |
| 125 | static int subchannel_unref_locked(grpc_subchannel *c) GRPC_MUST_USE_RESULT; |
| 126 | static void connection_ref_locked(connection *c); |
Craig Tiller | 4ab82d2 | 2015-06-29 09:40:33 -0700 | [diff] [blame] | 127 | static grpc_subchannel *connection_unref_locked(connection *c) |
| 128 | GRPC_MUST_USE_RESULT; |
Craig Tiller | d7b68e7 | 2015-06-28 11:41:09 -0700 | [diff] [blame] | 129 | static void subchannel_destroy(grpc_subchannel *c); |
| 130 | |
Craig Tiller | 2595ab7 | 2015-06-25 15:26:00 -0700 | [diff] [blame] | 131 | /* |
Craig Tiller | ca3e9d3 | 2015-06-27 18:37:27 -0700 | [diff] [blame] | 132 | * connection implementation |
| 133 | */ |
| 134 | |
Craig Tiller | d7b68e7 | 2015-06-28 11:41:09 -0700 | [diff] [blame] | 135 | static void connection_destroy(connection *c) { |
Craig Tiller | 4ab82d2 | 2015-06-29 09:40:33 -0700 | [diff] [blame] | 136 | GPR_ASSERT(c->refs == 0); |
| 137 | grpc_channel_stack_destroy(CHANNEL_STACK_FROM_CONNECTION(c)); |
Craig Tiller | d7b68e7 | 2015-06-28 11:41:09 -0700 | [diff] [blame] | 138 | gpr_free(c); |
Craig Tiller | ca3e9d3 | 2015-06-27 18:37:27 -0700 | [diff] [blame] | 139 | } |
| 140 | |
Craig Tiller | 4ab82d2 | 2015-06-29 09:40:33 -0700 | [diff] [blame] | 141 | static void connection_ref_locked(connection *c) { |
| 142 | subchannel_ref_locked(c->subchannel); |
| 143 | ++c->refs; |
Craig Tiller | d7b68e7 | 2015-06-28 11:41:09 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | static grpc_subchannel *connection_unref_locked(connection *c) { |
Craig Tiller | 4ab82d2 | 2015-06-29 09:40:33 -0700 | [diff] [blame] | 147 | grpc_subchannel *destroy = NULL; |
| 148 | if (subchannel_unref_locked(c->subchannel)) { |
| 149 | destroy = c->subchannel; |
| 150 | } |
Craig Tiller | d7b68e7 | 2015-06-28 11:41:09 -0700 | [diff] [blame] | 151 | if (--c->refs == 0 && c->subchannel->active != c) { |
Craig Tiller | 4ab82d2 | 2015-06-29 09:40:33 -0700 | [diff] [blame] | 152 | connection_destroy(c); |
Craig Tiller | ca3e9d3 | 2015-06-27 18:37:27 -0700 | [diff] [blame] | 153 | } |
Craig Tiller | d7b68e7 | 2015-06-28 11:41:09 -0700 | [diff] [blame] | 154 | return destroy; |
Craig Tiller | ca3e9d3 | 2015-06-27 18:37:27 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | /* |
Craig Tiller | 2595ab7 | 2015-06-25 15:26:00 -0700 | [diff] [blame] | 158 | * grpc_subchannel implementation |
| 159 | */ |
| 160 | |
Craig Tiller | 4ab82d2 | 2015-06-29 09:40:33 -0700 | [diff] [blame] | 161 | static void subchannel_ref_locked(grpc_subchannel *c) { ++c->refs; } |
Craig Tiller | 2595ab7 | 2015-06-25 15:26:00 -0700 | [diff] [blame] | 162 | |
Craig Tiller | d7b68e7 | 2015-06-28 11:41:09 -0700 | [diff] [blame] | 163 | static int subchannel_unref_locked(grpc_subchannel *c) { |
Craig Tiller | 4ab82d2 | 2015-06-29 09:40:33 -0700 | [diff] [blame] | 164 | return --c->refs == 0; |
Craig Tiller | d7b68e7 | 2015-06-28 11:41:09 -0700 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | void grpc_subchannel_ref(grpc_subchannel *c) { |
Craig Tiller | 4ab82d2 | 2015-06-29 09:40:33 -0700 | [diff] [blame] | 168 | gpr_mu_lock(&c->mu); |
| 169 | subchannel_ref_locked(c); |
| 170 | gpr_mu_unlock(&c->mu); |
Craig Tiller | d7b68e7 | 2015-06-28 11:41:09 -0700 | [diff] [blame] | 171 | } |
| 172 | |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 173 | void grpc_subchannel_unref(grpc_subchannel *c) { |
Craig Tiller | 4ab82d2 | 2015-06-29 09:40:33 -0700 | [diff] [blame] | 174 | int destroy; |
| 175 | gpr_mu_lock(&c->mu); |
| 176 | destroy = subchannel_unref_locked(c); |
| 177 | gpr_mu_unlock(&c->mu); |
| 178 | if (destroy) subchannel_destroy(c); |
Craig Tiller | d7b68e7 | 2015-06-28 11:41:09 -0700 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | static void subchannel_destroy(grpc_subchannel *c) { |
Craig Tiller | 4ab82d2 | 2015-06-29 09:40:33 -0700 | [diff] [blame] | 182 | if (c->active != NULL) { |
| 183 | connection_destroy(c->active); |
| 184 | } |
Craig Tiller | d7b68e7 | 2015-06-28 11:41:09 -0700 | [diff] [blame] | 185 | gpr_free(c->filters); |
| 186 | grpc_channel_args_destroy(c->args); |
| 187 | gpr_free(c->addr); |
| 188 | grpc_mdctx_unref(c->mdctx); |
| 189 | grpc_pollset_set_destroy(&c->pollset_set); |
| 190 | grpc_connectivity_state_destroy(&c->state_tracker); |
| 191 | gpr_free(c); |
Craig Tiller | 2595ab7 | 2015-06-25 15:26:00 -0700 | [diff] [blame] | 192 | } |
| 193 | |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 194 | void grpc_subchannel_add_interested_party(grpc_subchannel *c, |
| 195 | grpc_pollset *pollset) { |
| 196 | grpc_pollset_set_add_pollset(&c->pollset_set, pollset); |
| 197 | } |
| 198 | |
| 199 | void grpc_subchannel_del_interested_party(grpc_subchannel *c, |
| 200 | grpc_pollset *pollset) { |
| 201 | grpc_pollset_set_del_pollset(&c->pollset_set, pollset); |
| 202 | } |
| 203 | |
Craig Tiller | f7afa1f | 2015-06-26 09:02:20 -0700 | [diff] [blame] | 204 | grpc_subchannel *grpc_subchannel_create(grpc_connector *connector, |
| 205 | grpc_subchannel_args *args) { |
| 206 | grpc_subchannel *c = gpr_malloc(sizeof(*c)); |
| 207 | memset(c, 0, sizeof(*c)); |
Craig Tiller | d7b68e7 | 2015-06-28 11:41:09 -0700 | [diff] [blame] | 208 | c->refs = 1; |
Craig Tiller | f7afa1f | 2015-06-26 09:02:20 -0700 | [diff] [blame] | 209 | c->connector = connector; |
| 210 | grpc_connector_ref(c->connector); |
Craig Tiller | 5945ee1 | 2015-06-27 10:36:09 -0700 | [diff] [blame] | 211 | c->num_filters = args->filter_count; |
| 212 | c->filters = gpr_malloc(sizeof(grpc_channel_filter *) * c->num_filters); |
Craig Tiller | f7afa1f | 2015-06-26 09:02:20 -0700 | [diff] [blame] | 213 | memcpy(c->filters, args->filters, |
Craig Tiller | 5945ee1 | 2015-06-27 10:36:09 -0700 | [diff] [blame] | 214 | sizeof(grpc_channel_filter *) * c->num_filters); |
Craig Tiller | f7afa1f | 2015-06-26 09:02:20 -0700 | [diff] [blame] | 215 | c->addr = gpr_malloc(args->addr_len); |
| 216 | memcpy(c->addr, args->addr, args->addr_len); |
| 217 | c->addr_len = args->addr_len; |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 218 | c->args = grpc_channel_args_copy(args->args); |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 219 | c->mdctx = args->mdctx; |
| 220 | grpc_mdctx_ref(c->mdctx); |
Craig Tiller | ff54c92 | 2015-06-26 16:57:20 -0700 | [diff] [blame] | 221 | grpc_pollset_set_init(&c->pollset_set); |
| 222 | grpc_iomgr_closure_init(&c->connected, subchannel_connected, c); |
Craig Tiller | c7b5f76 | 2015-06-27 11:48:42 -0700 | [diff] [blame] | 223 | grpc_connectivity_state_init(&c->state_tracker, GRPC_CHANNEL_IDLE); |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 224 | gpr_mu_init(&c->mu); |
Craig Tiller | f7afa1f | 2015-06-26 09:02:20 -0700 | [diff] [blame] | 225 | return c; |
| 226 | } |
| 227 | |
Craig Tiller | 04c5d4b | 2015-06-26 17:21:41 -0700 | [diff] [blame] | 228 | static void start_connect(grpc_subchannel *c) { |
Craig Tiller | 4ab82d2 | 2015-06-29 09:40:33 -0700 | [diff] [blame] | 229 | grpc_connect_in_args args; |
Craig Tiller | 04c5d4b | 2015-06-26 17:21:41 -0700 | [diff] [blame] | 230 | |
Craig Tiller | 4ab82d2 | 2015-06-29 09:40:33 -0700 | [diff] [blame] | 231 | args.interested_parties = &c->pollset_set; |
| 232 | args.addr = c->addr; |
| 233 | args.addr_len = c->addr_len; |
| 234 | args.deadline = compute_connect_deadline(c); |
| 235 | args.channel_args = c->args; |
| 236 | args.metadata_context = c->mdctx; |
Craig Tiller | 04c5d4b | 2015-06-26 17:21:41 -0700 | [diff] [blame] | 237 | |
Craig Tiller | 4ab82d2 | 2015-06-29 09:40:33 -0700 | [diff] [blame] | 238 | grpc_connector_connect(c->connector, &args, &c->connecting_result, |
| 239 | &c->connected); |
Craig Tiller | 04c5d4b | 2015-06-26 17:21:41 -0700 | [diff] [blame] | 240 | } |
| 241 | |
Craig Tiller | df91ba5 | 2015-06-29 10:55:46 -0700 | [diff] [blame^] | 242 | static void continue_creating_call(void *arg, int iomgr_success) { |
| 243 | waiting_for_connect *w4c = arg; |
| 244 | grpc_subchannel_create_call(w4c->subchannel, |
| 245 | &w4c->initial_op, |
| 246 | w4c->target, |
| 247 | w4c->notify); |
| 248 | grpc_subchannel_unref(w4c->subchannel); |
| 249 | gpr_free(w4c); |
| 250 | } |
| 251 | |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 252 | void grpc_subchannel_create_call(grpc_subchannel *c, |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 253 | grpc_transport_stream_op *initial_op, |
| 254 | grpc_subchannel_call **target, |
| 255 | grpc_iomgr_closure *notify) { |
| 256 | connection *con; |
| 257 | gpr_mu_lock(&c->mu); |
| 258 | if (c->active != NULL) { |
| 259 | con = c->active; |
Craig Tiller | d7b68e7 | 2015-06-28 11:41:09 -0700 | [diff] [blame] | 260 | connection_ref_locked(con); |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 261 | gpr_mu_unlock(&c->mu); |
| 262 | |
| 263 | *target = create_call(con, initial_op); |
| 264 | notify->cb(notify->cb_arg, 1); |
| 265 | } else { |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 266 | waiting_for_connect *w4c = gpr_malloc(sizeof(*w4c)); |
| 267 | w4c->next = c->waiting; |
| 268 | w4c->notify = notify; |
Craig Tiller | df91ba5 | 2015-06-29 10:55:46 -0700 | [diff] [blame^] | 269 | w4c->initial_op = *initial_op; |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 270 | w4c->target = target; |
Craig Tiller | df91ba5 | 2015-06-29 10:55:46 -0700 | [diff] [blame^] | 271 | w4c->subchannel = c; |
| 272 | subchannel_ref_locked(c); |
| 273 | grpc_iomgr_closure_init(&w4c->continuation, continue_creating_call, w4c); |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 274 | c->waiting = w4c; |
| 275 | grpc_subchannel_add_interested_party(c, initial_op->bind_pollset); |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 276 | if (!c->connecting) { |
| 277 | c->connecting = 1; |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 278 | connectivity_state_changed_locked(c); |
Craig Tiller | d7b68e7 | 2015-06-28 11:41:09 -0700 | [diff] [blame] | 279 | subchannel_ref_locked(c); |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 280 | gpr_mu_unlock(&c->mu); |
| 281 | |
Craig Tiller | 04c5d4b | 2015-06-26 17:21:41 -0700 | [diff] [blame] | 282 | start_connect(c); |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 283 | } else { |
| 284 | gpr_mu_unlock(&c->mu); |
| 285 | } |
| 286 | } |
| 287 | } |
| 288 | |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 289 | grpc_connectivity_state grpc_subchannel_check_connectivity(grpc_subchannel *c) { |
| 290 | grpc_connectivity_state state; |
| 291 | gpr_mu_lock(&c->mu); |
Craig Tiller | c7b5f76 | 2015-06-27 11:48:42 -0700 | [diff] [blame] | 292 | state = grpc_connectivity_state_check(&c->state_tracker); |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 293 | gpr_mu_unlock(&c->mu); |
| 294 | return state; |
| 295 | } |
| 296 | |
| 297 | void grpc_subchannel_notify_on_state_change(grpc_subchannel *c, |
| 298 | grpc_connectivity_state *state, |
| 299 | grpc_iomgr_closure *notify) { |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 300 | int do_connect = 0; |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 301 | gpr_mu_lock(&c->mu); |
Craig Tiller | 4ab82d2 | 2015-06-29 09:40:33 -0700 | [diff] [blame] | 302 | if (grpc_connectivity_state_notify_on_state_change(&c->state_tracker, state, |
| 303 | notify)) { |
| 304 | do_connect = 1; |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 305 | c->connecting = 1; |
Craig Tiller | d7b68e7 | 2015-06-28 11:41:09 -0700 | [diff] [blame] | 306 | subchannel_ref_locked(c); |
Craig Tiller | 4ab82d2 | 2015-06-29 09:40:33 -0700 | [diff] [blame] | 307 | grpc_connectivity_state_set(&c->state_tracker, |
| 308 | compute_connectivity_locked(c)); |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 309 | } |
Craig Tiller | c7b5f76 | 2015-06-27 11:48:42 -0700 | [diff] [blame] | 310 | gpr_mu_unlock(&c->mu); |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 311 | if (do_connect) { |
Craig Tiller | 4ab82d2 | 2015-06-29 09:40:33 -0700 | [diff] [blame] | 312 | start_connect(c); |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 313 | } |
| 314 | } |
| 315 | |
Craig Tiller | 4ab82d2 | 2015-06-29 09:40:33 -0700 | [diff] [blame] | 316 | void grpc_subchannel_process_transport_op(grpc_subchannel *c, |
| 317 | grpc_transport_op *op) { |
Craig Tiller | df91ba5 | 2015-06-29 10:55:46 -0700 | [diff] [blame^] | 318 | abort(); /* not implemented */ |
| 319 | } |
| 320 | |
| 321 | static void on_state_changed(void *p, int iomgr_success) { |
| 322 | state_watcher *sw = p; |
| 323 | grpc_subchannel *c = sw->subchannel; |
| 324 | gpr_mu *mu = &c->mu; |
| 325 | int destroy; |
| 326 | grpc_transport_op op; |
| 327 | grpc_channel_element *elem; |
| 328 | connection *destroy_connection = NULL; |
| 329 | int do_connect = 0; |
| 330 | |
| 331 | gpr_mu_lock(mu); |
| 332 | |
| 333 | /* if we failed or there is a version number mismatch, just leave |
| 334 | this closure */ |
| 335 | if (!iomgr_success || sw->subchannel->active_version != sw->version) { |
| 336 | goto done; |
| 337 | } |
| 338 | |
| 339 | switch (sw->connectivity_state) { |
| 340 | case GRPC_CHANNEL_CONNECTING: |
| 341 | case GRPC_CHANNEL_READY: |
| 342 | case GRPC_CHANNEL_IDLE: |
| 343 | /* all is still good: keep watching */ |
| 344 | memset(&op, 0, sizeof(op)); |
| 345 | op.connectivity_state = &sw->connectivity_state; |
| 346 | op.on_connectivity_state_change = &sw->closure; |
| 347 | elem = grpc_channel_stack_element(CHANNEL_STACK_FROM_CONNECTION(c->active), 0); |
| 348 | elem->filter->start_transport_op(elem, &op); |
| 349 | /* early out */ |
| 350 | gpr_mu_unlock(mu); |
| 351 | return; |
| 352 | case GRPC_CHANNEL_FATAL_FAILURE: |
| 353 | /* things have gone wrong, deactivate and enter idle */ |
| 354 | if (sw->subchannel->active->refs == 0) { |
| 355 | destroy_connection = sw->subchannel->active; |
| 356 | } |
| 357 | sw->subchannel->active = NULL; |
| 358 | break; |
| 359 | case GRPC_CHANNEL_TRANSIENT_FAILURE: |
| 360 | /* things are starting to go wrong, reconnect but don't deactivate */ |
| 361 | subchannel_ref_locked(c); |
| 362 | do_connect = 1; |
| 363 | c->connecting = 1; |
| 364 | break; |
| 365 | } |
| 366 | |
| 367 | done: |
| 368 | grpc_connectivity_state_set(&c->state_tracker, |
| 369 | compute_connectivity_locked(c)); |
| 370 | destroy = subchannel_unref_locked(c); |
| 371 | gpr_free(sw); |
| 372 | gpr_mu_unlock(mu); |
| 373 | if (do_connect) { |
| 374 | start_connect(c); |
| 375 | } |
| 376 | if (destroy) { |
| 377 | subchannel_destroy(c); |
| 378 | } |
| 379 | if (destroy_connection != NULL) { |
| 380 | connection_destroy(destroy_connection); |
| 381 | } |
Craig Tiller | c7b5f76 | 2015-06-27 11:48:42 -0700 | [diff] [blame] | 382 | } |
| 383 | |
Craig Tiller | ff54c92 | 2015-06-26 16:57:20 -0700 | [diff] [blame] | 384 | static void publish_transport(grpc_subchannel *c) { |
Craig Tiller | 4ab82d2 | 2015-06-29 09:40:33 -0700 | [diff] [blame] | 385 | size_t channel_stack_size; |
| 386 | connection *con; |
| 387 | grpc_channel_stack *stk; |
| 388 | size_t num_filters; |
| 389 | const grpc_channel_filter **filters; |
| 390 | waiting_for_connect *w4c; |
Craig Tiller | df91ba5 | 2015-06-29 10:55:46 -0700 | [diff] [blame^] | 391 | grpc_transport_op op; |
| 392 | state_watcher *sw; |
| 393 | connection *destroy_connection = NULL; |
| 394 | grpc_channel_element *elem; |
Craig Tiller | 5945ee1 | 2015-06-27 10:36:09 -0700 | [diff] [blame] | 395 | |
Craig Tiller | df91ba5 | 2015-06-29 10:55:46 -0700 | [diff] [blame^] | 396 | /* build final filter list */ |
Craig Tiller | 4ab82d2 | 2015-06-29 09:40:33 -0700 | [diff] [blame] | 397 | num_filters = c->num_filters + c->connecting_result.num_filters + 1; |
| 398 | filters = gpr_malloc(sizeof(*filters) * num_filters); |
| 399 | memcpy(filters, c->filters, sizeof(*filters) * c->num_filters); |
| 400 | memcpy(filters + c->num_filters, c->connecting_result.filters, |
| 401 | sizeof(*filters) * c->connecting_result.num_filters); |
| 402 | filters[num_filters - 1] = &grpc_connected_channel_filter; |
Craig Tiller | 5945ee1 | 2015-06-27 10:36:09 -0700 | [diff] [blame] | 403 | |
Craig Tiller | df91ba5 | 2015-06-29 10:55:46 -0700 | [diff] [blame^] | 404 | /* construct channel stack */ |
Craig Tiller | 4ab82d2 | 2015-06-29 09:40:33 -0700 | [diff] [blame] | 405 | channel_stack_size = grpc_channel_stack_size(filters, num_filters); |
| 406 | con = gpr_malloc(sizeof(connection) + channel_stack_size); |
| 407 | stk = (grpc_channel_stack *)(con + 1); |
Craig Tiller | 4ab82d2 | 2015-06-29 09:40:33 -0700 | [diff] [blame] | 408 | con->refs = 0; |
| 409 | con->subchannel = c; |
| 410 | grpc_channel_stack_init(filters, num_filters, c->args, c->mdctx, stk); |
| 411 | grpc_connected_channel_bind_transport(stk, c->connecting_result.transport); |
| 412 | memset(&c->connecting_result, 0, sizeof(c->connecting_result)); |
Craig Tiller | ff54c92 | 2015-06-26 16:57:20 -0700 | [diff] [blame] | 413 | |
Craig Tiller | df91ba5 | 2015-06-29 10:55:46 -0700 | [diff] [blame^] | 414 | /* initialize state watcher */ |
| 415 | sw = gpr_malloc(sizeof(*sw)); |
| 416 | grpc_iomgr_closure_init(&sw->closure, on_state_changed, sw); |
| 417 | sw->subchannel = c; |
| 418 | sw->connectivity_state = GRPC_CHANNEL_READY; |
| 419 | |
Craig Tiller | 4ab82d2 | 2015-06-29 09:40:33 -0700 | [diff] [blame] | 420 | gpr_mu_lock(&c->mu); |
Craig Tiller | df91ba5 | 2015-06-29 10:55:46 -0700 | [diff] [blame^] | 421 | |
| 422 | /* publish */ |
| 423 | if (c->active != NULL && c->active->refs == 0) { |
| 424 | destroy_connection = c->active; |
| 425 | } |
Craig Tiller | 4ab82d2 | 2015-06-29 09:40:33 -0700 | [diff] [blame] | 426 | c->active = con; |
Craig Tiller | df91ba5 | 2015-06-29 10:55:46 -0700 | [diff] [blame^] | 427 | c->active_version++; |
| 428 | sw->version = c->active_version; |
Craig Tiller | 4ab82d2 | 2015-06-29 09:40:33 -0700 | [diff] [blame] | 429 | c->connecting = 0; |
Craig Tiller | df91ba5 | 2015-06-29 10:55:46 -0700 | [diff] [blame^] | 430 | |
| 431 | /* watch for changes; subchannel ref for connecting is donated |
| 432 | to the state watcher */ |
| 433 | memset(&op, 0, sizeof(op)); |
| 434 | op.connectivity_state = &sw->connectivity_state; |
| 435 | op.on_connectivity_state_change = &sw->closure; |
| 436 | elem = grpc_channel_stack_element(CHANNEL_STACK_FROM_CONNECTION(c->active), 0); |
| 437 | elem->filter->start_transport_op(elem, &op); |
| 438 | |
| 439 | /* signal completion */ |
Craig Tiller | 4ab82d2 | 2015-06-29 09:40:33 -0700 | [diff] [blame] | 440 | connectivity_state_changed_locked(c); |
| 441 | while ((w4c = c->waiting)) { |
Craig Tiller | df91ba5 | 2015-06-29 10:55:46 -0700 | [diff] [blame^] | 442 | c->waiting = w4c->next; |
| 443 | grpc_iomgr_add_callback(&w4c->continuation); |
Craig Tiller | 4ab82d2 | 2015-06-29 09:40:33 -0700 | [diff] [blame] | 444 | } |
Craig Tiller | df91ba5 | 2015-06-29 10:55:46 -0700 | [diff] [blame^] | 445 | |
Craig Tiller | 4ab82d2 | 2015-06-29 09:40:33 -0700 | [diff] [blame] | 446 | gpr_mu_unlock(&c->mu); |
Craig Tiller | 5945ee1 | 2015-06-27 10:36:09 -0700 | [diff] [blame] | 447 | |
Craig Tiller | 4ab82d2 | 2015-06-29 09:40:33 -0700 | [diff] [blame] | 448 | gpr_free(filters); |
Craig Tiller | d7b68e7 | 2015-06-28 11:41:09 -0700 | [diff] [blame] | 449 | |
Craig Tiller | df91ba5 | 2015-06-29 10:55:46 -0700 | [diff] [blame^] | 450 | if (destroy_connection != NULL) { |
| 451 | connection_destroy(destroy_connection); |
Craig Tiller | 4ab82d2 | 2015-06-29 09:40:33 -0700 | [diff] [blame] | 452 | } |
| 453 | } |
Craig Tiller | ff54c92 | 2015-06-26 16:57:20 -0700 | [diff] [blame] | 454 | |
| 455 | static void subchannel_connected(void *arg, int iomgr_success) { |
Craig Tiller | 4ab82d2 | 2015-06-29 09:40:33 -0700 | [diff] [blame] | 456 | grpc_subchannel *c = arg; |
| 457 | if (c->connecting_result.transport) { |
| 458 | publish_transport(c); |
| 459 | } else { |
| 460 | int destroy; |
| 461 | gpr_mu_lock(&c->mu); |
| 462 | destroy = subchannel_unref_locked(c); |
| 463 | gpr_mu_unlock(&c->mu); |
| 464 | if (destroy) subchannel_destroy(c); |
| 465 | /* TODO(ctiller): retry after sleeping */ |
| 466 | abort(); |
| 467 | } |
Craig Tiller | ff54c92 | 2015-06-26 16:57:20 -0700 | [diff] [blame] | 468 | } |
| 469 | |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 470 | static gpr_timespec compute_connect_deadline(grpc_subchannel *c) { |
| 471 | return gpr_time_add(gpr_now(), gpr_time_from_seconds(60)); |
| 472 | } |
| 473 | |
| 474 | static grpc_connectivity_state compute_connectivity_locked(grpc_subchannel *c) { |
| 475 | if (c->connecting) { |
| 476 | return GRPC_CHANNEL_CONNECTING; |
| 477 | } |
| 478 | if (c->active) { |
| 479 | return GRPC_CHANNEL_READY; |
| 480 | } |
| 481 | return GRPC_CHANNEL_IDLE; |
| 482 | } |
| 483 | |
| 484 | static void connectivity_state_changed_locked(grpc_subchannel *c) { |
| 485 | grpc_connectivity_state current = compute_connectivity_locked(c); |
Craig Tiller | c7b5f76 | 2015-06-27 11:48:42 -0700 | [diff] [blame] | 486 | grpc_connectivity_state_set(&c->state_tracker, current); |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 487 | } |
| 488 | |
Craig Tiller | 2595ab7 | 2015-06-25 15:26:00 -0700 | [diff] [blame] | 489 | /* |
| 490 | * grpc_subchannel_call implementation |
| 491 | */ |
| 492 | |
Craig Tiller | 4ab82d2 | 2015-06-29 09:40:33 -0700 | [diff] [blame] | 493 | void grpc_subchannel_call_ref(grpc_subchannel_call *c) { gpr_ref(&c->refs); } |
Craig Tiller | 2595ab7 | 2015-06-25 15:26:00 -0700 | [diff] [blame] | 494 | |
Craig Tiller | ca3e9d3 | 2015-06-27 18:37:27 -0700 | [diff] [blame] | 495 | void grpc_subchannel_call_unref(grpc_subchannel_call *c) { |
Craig Tiller | ca3e9d3 | 2015-06-27 18:37:27 -0700 | [diff] [blame] | 496 | if (gpr_unref(&c->refs)) { |
Craig Tiller | 4ab82d2 | 2015-06-29 09:40:33 -0700 | [diff] [blame] | 497 | gpr_mu *mu = &c->connection->subchannel->mu; |
| 498 | grpc_subchannel *destroy; |
Craig Tiller | ca3e9d3 | 2015-06-27 18:37:27 -0700 | [diff] [blame] | 499 | grpc_call_stack_destroy(SUBCHANNEL_CALL_TO_CALL_STACK(c)); |
Craig Tiller | d7b68e7 | 2015-06-28 11:41:09 -0700 | [diff] [blame] | 500 | gpr_mu_lock(mu); |
| 501 | destroy = connection_unref_locked(c->connection); |
| 502 | gpr_mu_unlock(mu); |
Craig Tiller | ca3e9d3 | 2015-06-27 18:37:27 -0700 | [diff] [blame] | 503 | gpr_free(c); |
Craig Tiller | d7b68e7 | 2015-06-28 11:41:09 -0700 | [diff] [blame] | 504 | if (destroy) { |
Craig Tiller | 4ab82d2 | 2015-06-29 09:40:33 -0700 | [diff] [blame] | 505 | subchannel_destroy(destroy); |
Craig Tiller | d7b68e7 | 2015-06-28 11:41:09 -0700 | [diff] [blame] | 506 | } |
Craig Tiller | 2595ab7 | 2015-06-25 15:26:00 -0700 | [diff] [blame] | 507 | } |
| 508 | } |
| 509 | |
| 510 | void grpc_subchannel_call_process_op(grpc_subchannel_call *call, |
| 511 | grpc_transport_stream_op *op) { |
| 512 | grpc_call_stack *call_stack = SUBCHANNEL_CALL_TO_CALL_STACK(call); |
| 513 | grpc_call_element *top_elem = grpc_call_stack_element(call_stack, 0); |
| 514 | top_elem->filter->start_transport_stream_op(top_elem, op); |
| 515 | } |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 516 | |
Craig Tiller | 4ab82d2 | 2015-06-29 09:40:33 -0700 | [diff] [blame] | 517 | grpc_subchannel_call *create_call(connection *con, |
| 518 | grpc_transport_stream_op *initial_op) { |
| 519 | grpc_channel_stack *chanstk = CHANNEL_STACK_FROM_CONNECTION(con); |
| 520 | grpc_subchannel_call *call = |
| 521 | gpr_malloc(sizeof(grpc_subchannel_call) + chanstk->call_stack_size); |
Craig Tiller | 04c5d4b | 2015-06-26 17:21:41 -0700 | [diff] [blame] | 522 | grpc_call_stack *callstk = SUBCHANNEL_CALL_TO_CALL_STACK(call); |
| 523 | call->connection = con; |
| 524 | gpr_ref_init(&call->refs, 1); |
| 525 | grpc_call_stack_init(chanstk, NULL, initial_op, callstk); |
| 526 | return call; |
Craig Tiller | eb3b12e | 2015-06-26 14:42:49 -0700 | [diff] [blame] | 527 | } |