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/surface/server.h" |
| 35 | |
Craig Tiller | f96dfc3 | 2015-09-10 14:43:18 -0700 | [diff] [blame^] | 36 | #include <limits.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 37 | #include <stdlib.h> |
| 38 | #include <string.h> |
| 39 | |
Craig Tiller | 6a006ce | 2015-07-13 16:25:40 -0700 | [diff] [blame] | 40 | #include <grpc/support/alloc.h> |
| 41 | #include <grpc/support/log.h> |
| 42 | #include <grpc/support/string_util.h> |
| 43 | #include <grpc/support/useful.h> |
| 44 | |
Hongyu Chen | e09dc78 | 2015-08-21 11:28:33 -0700 | [diff] [blame] | 45 | #include "src/core/census/grpc_filter.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 46 | #include "src/core/channel/channel_args.h" |
| 47 | #include "src/core/channel/connected_channel.h" |
ctiller | 18b49ab | 2014-12-09 14:39:16 -0800 | [diff] [blame] | 48 | #include "src/core/iomgr/iomgr.h" |
Craig Tiller | 6a006ce | 2015-07-13 16:25:40 -0700 | [diff] [blame] | 49 | #include "src/core/support/stack_lockfree.h" |
Craig Tiller | 485d776 | 2015-01-23 12:54:05 -0800 | [diff] [blame] | 50 | #include "src/core/support/string.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 51 | #include "src/core/surface/call.h" |
| 52 | #include "src/core/surface/channel.h" |
| 53 | #include "src/core/surface/completion_queue.h" |
Craig Tiller | 60fd361 | 2015-03-05 16:24:22 -0800 | [diff] [blame] | 54 | #include "src/core/surface/init.h" |
Craig Tiller | cce17ac | 2015-01-20 09:29:28 -0800 | [diff] [blame] | 55 | #include "src/core/transport/metadata.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 56 | |
| 57 | typedef struct listener { |
| 58 | void *arg; |
Craig Tiller | ec3257c | 2015-02-12 15:59:43 -0800 | [diff] [blame] | 59 | void (*start)(grpc_server *server, void *arg, grpc_pollset **pollsets, |
| 60 | size_t pollset_count); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 61 | void (*destroy)(grpc_server *server, void *arg); |
| 62 | struct listener *next; |
| 63 | } listener; |
| 64 | |
| 65 | typedef struct call_data call_data; |
| 66 | typedef struct channel_data channel_data; |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 67 | typedef struct registered_method registered_method; |
| 68 | |
| 69 | typedef struct { |
| 70 | call_data *next; |
| 71 | call_data *prev; |
| 72 | } call_link; |
| 73 | |
Craig Tiller | 0e91956 | 2015-04-28 14:03:47 -0700 | [diff] [blame] | 74 | typedef enum { BATCH_CALL, REGISTERED_CALL } requested_call_type; |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 75 | |
Craig Tiller | 97fc6a3 | 2015-07-08 15:31:35 -0700 | [diff] [blame] | 76 | typedef struct requested_call { |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 77 | requested_call_type type; |
| 78 | void *tag; |
Craig Tiller | 6a006ce | 2015-07-13 16:25:40 -0700 | [diff] [blame] | 79 | grpc_server *server; |
Craig Tiller | f9e6adf | 2015-05-06 11:45:59 -0700 | [diff] [blame] | 80 | grpc_completion_queue *cq_bound_to_call; |
| 81 | grpc_completion_queue *cq_for_notification; |
| 82 | grpc_call **call; |
Craig Tiller | 97fc6a3 | 2015-07-08 15:31:35 -0700 | [diff] [blame] | 83 | grpc_cq_completion completion; |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 84 | union { |
| 85 | struct { |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 86 | grpc_call_details *details; |
| 87 | grpc_metadata_array *initial_metadata; |
| 88 | } batch; |
| 89 | struct { |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 90 | registered_method *registered_method; |
| 91 | gpr_timespec *deadline; |
| 92 | grpc_metadata_array *initial_metadata; |
| 93 | grpc_byte_buffer **optional_payload; |
| 94 | } registered; |
| 95 | } data; |
| 96 | } requested_call; |
| 97 | |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 98 | typedef struct channel_registered_method { |
| 99 | registered_method *server_registered_method; |
| 100 | grpc_mdstr *method; |
| 101 | grpc_mdstr *host; |
| 102 | } channel_registered_method; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 103 | |
| 104 | struct channel_data { |
| 105 | grpc_server *server; |
Craig Tiller | e039f03 | 2015-06-25 12:54:23 -0700 | [diff] [blame] | 106 | grpc_connectivity_state connectivity_state; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 107 | grpc_channel *channel; |
Craig Tiller | cce17ac | 2015-01-20 09:29:28 -0800 | [diff] [blame] | 108 | grpc_mdstr *path_key; |
| 109 | grpc_mdstr *authority_key; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 110 | /* linked list of all channels on a server */ |
| 111 | channel_data *next; |
| 112 | channel_data *prev; |
Craig Tiller | 04cc8be | 2015-02-10 16:11:22 -0800 | [diff] [blame] | 113 | channel_registered_method *registered_methods; |
| 114 | gpr_uint32 registered_method_slots; |
| 115 | gpr_uint32 registered_method_max_probes; |
David Garcia Quintas | 284488b | 2015-05-28 16:27:39 -0700 | [diff] [blame] | 116 | grpc_iomgr_closure finish_destroy_channel_closure; |
Craig Tiller | e039f03 | 2015-06-25 12:54:23 -0700 | [diff] [blame] | 117 | grpc_iomgr_closure channel_connectivity_changed; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 118 | }; |
| 119 | |
Craig Tiller | bce999f | 2015-05-27 09:55:51 -0700 | [diff] [blame] | 120 | typedef struct shutdown_tag { |
| 121 | void *tag; |
| 122 | grpc_completion_queue *cq; |
Craig Tiller | 97fc6a3 | 2015-07-08 15:31:35 -0700 | [diff] [blame] | 123 | grpc_cq_completion completion; |
Craig Tiller | bce999f | 2015-05-27 09:55:51 -0700 | [diff] [blame] | 124 | } shutdown_tag; |
| 125 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 126 | typedef enum { |
| 127 | /* waiting for metadata */ |
| 128 | NOT_STARTED, |
| 129 | /* inital metadata read, not flow controlled in yet */ |
| 130 | PENDING, |
| 131 | /* flow controlled in, on completion queue */ |
| 132 | ACTIVATED, |
| 133 | /* cancelled before being queued */ |
| 134 | ZOMBIED |
| 135 | } call_state; |
| 136 | |
Craig Tiller | 729b35a | 2015-07-13 12:36:47 -0700 | [diff] [blame] | 137 | typedef struct request_matcher request_matcher; |
| 138 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 139 | struct call_data { |
| 140 | grpc_call *call; |
| 141 | |
Craig Tiller | 76d2c3b | 2015-07-07 11:46:01 -0700 | [diff] [blame] | 142 | /** protects state */ |
| 143 | gpr_mu mu_state; |
| 144 | /** the current state of a call - see call_state */ |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 145 | call_state state; |
Craig Tiller | 76d2c3b | 2015-07-07 11:46:01 -0700 | [diff] [blame] | 146 | |
Craig Tiller | cce17ac | 2015-01-20 09:29:28 -0800 | [diff] [blame] | 147 | grpc_mdstr *path; |
| 148 | grpc_mdstr *host; |
Craig Tiller | be18b8d | 2015-04-22 14:00:47 -0700 | [diff] [blame] | 149 | gpr_timespec deadline; |
| 150 | int got_initial_metadata; |
Craig Tiller | cce17ac | 2015-01-20 09:29:28 -0800 | [diff] [blame] | 151 | |
Craig Tiller | 20bc56d | 2015-02-12 09:02:56 -0800 | [diff] [blame] | 152 | grpc_completion_queue *cq_new; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 153 | |
Craig Tiller | be18b8d | 2015-04-22 14:00:47 -0700 | [diff] [blame] | 154 | grpc_stream_op_buffer *recv_ops; |
| 155 | grpc_stream_state *recv_state; |
Craig Tiller | 1e6facb | 2015-06-11 22:47:11 -0700 | [diff] [blame] | 156 | grpc_iomgr_closure *on_done_recv; |
Craig Tiller | be18b8d | 2015-04-22 14:00:47 -0700 | [diff] [blame] | 157 | |
Craig Tiller | 1e6facb | 2015-06-11 22:47:11 -0700 | [diff] [blame] | 158 | grpc_iomgr_closure server_on_recv; |
David Garcia Quintas | 284488b | 2015-05-28 16:27:39 -0700 | [diff] [blame] | 159 | grpc_iomgr_closure kill_zombie_closure; |
| 160 | |
Craig Tiller | 729b35a | 2015-07-13 12:36:47 -0700 | [diff] [blame] | 161 | call_data *pending_next; |
| 162 | }; |
| 163 | |
| 164 | struct request_matcher { |
| 165 | call_data *pending_head; |
| 166 | call_data *pending_tail; |
Craig Tiller | 6a006ce | 2015-07-13 16:25:40 -0700 | [diff] [blame] | 167 | gpr_stack_lockfree *requests; |
Craig Tiller | 729b35a | 2015-07-13 12:36:47 -0700 | [diff] [blame] | 168 | }; |
| 169 | |
| 170 | struct registered_method { |
| 171 | char *method; |
| 172 | char *host; |
| 173 | request_matcher request_matcher; |
| 174 | registered_method *next; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 175 | }; |
| 176 | |
Craig Tiller | ff3ae68 | 2015-06-29 17:44:04 -0700 | [diff] [blame] | 177 | typedef struct { |
| 178 | grpc_channel **channels; |
| 179 | size_t num_channels; |
| 180 | } channel_broadcaster; |
| 181 | |
Craig Tiller | 729b35a | 2015-07-13 12:36:47 -0700 | [diff] [blame] | 182 | struct grpc_server { |
| 183 | size_t channel_filter_count; |
| 184 | const grpc_channel_filter **channel_filters; |
| 185 | grpc_channel_args *channel_args; |
| 186 | |
| 187 | grpc_completion_queue **cqs; |
| 188 | grpc_pollset **pollsets; |
| 189 | size_t cq_count; |
| 190 | |
| 191 | /* The two following mutexes control access to server-state |
| 192 | mu_global controls access to non-call-related state (e.g., channel state) |
| 193 | mu_call controls access to call-related state (e.g., the call lists) |
| 194 | |
| 195 | If they are ever required to be nested, you must lock mu_global |
| 196 | before mu_call. This is currently used in shutdown processing |
| 197 | (grpc_server_shutdown_and_notify and maybe_finish_shutdown) */ |
| 198 | gpr_mu mu_global; /* mutex for server and channel state */ |
| 199 | gpr_mu mu_call; /* mutex for call-specific state */ |
| 200 | |
| 201 | registered_method *registered_methods; |
| 202 | request_matcher unregistered_request_matcher; |
Craig Tiller | 6a006ce | 2015-07-13 16:25:40 -0700 | [diff] [blame] | 203 | /** free list of available requested_calls indices */ |
| 204 | gpr_stack_lockfree *request_freelist; |
| 205 | /** requested call backing data */ |
| 206 | requested_call *requested_calls; |
Craig Tiller | 32ca48c | 2015-09-10 11:47:15 -0700 | [diff] [blame] | 207 | size_t max_requested_calls; |
Craig Tiller | 729b35a | 2015-07-13 12:36:47 -0700 | [diff] [blame] | 208 | |
Craig Tiller | 6a006ce | 2015-07-13 16:25:40 -0700 | [diff] [blame] | 209 | gpr_atm shutdown_flag; |
Craig Tiller | 729b35a | 2015-07-13 12:36:47 -0700 | [diff] [blame] | 210 | gpr_uint8 shutdown_published; |
| 211 | size_t num_shutdown_tags; |
| 212 | shutdown_tag *shutdown_tags; |
| 213 | |
| 214 | channel_data root_channel_data; |
| 215 | |
| 216 | listener *listeners; |
| 217 | int listeners_destroyed; |
| 218 | gpr_refcount internal_refcount; |
| 219 | |
| 220 | /** when did we print the last shutdown progress message */ |
| 221 | gpr_timespec last_shutdown_message_time; |
| 222 | }; |
| 223 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 224 | #define SERVER_FROM_CALL_ELEM(elem) \ |
| 225 | (((channel_data *)(elem)->channel_data)->server) |
| 226 | |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 227 | static void begin_call(grpc_server *server, call_data *calld, |
| 228 | requested_call *rc); |
| 229 | static void fail_call(grpc_server *server, requested_call *rc); |
Vijay Pai | 8931cdd | 2015-06-17 12:42:17 -0700 | [diff] [blame] | 230 | /* Before calling maybe_finish_shutdown, we must hold mu_global and not |
| 231 | hold mu_call */ |
Craig Tiller | 52760dd | 2015-05-29 23:09:26 -0700 | [diff] [blame] | 232 | static void maybe_finish_shutdown(grpc_server *server); |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 233 | |
Craig Tiller | 729b35a | 2015-07-13 12:36:47 -0700 | [diff] [blame] | 234 | /* |
| 235 | * channel broadcaster |
| 236 | */ |
Craig Tiller | ff3ae68 | 2015-06-29 17:44:04 -0700 | [diff] [blame] | 237 | |
| 238 | /* assumes server locked */ |
| 239 | static void channel_broadcaster_init(grpc_server *s, channel_broadcaster *cb) { |
| 240 | channel_data *c; |
| 241 | size_t count = 0; |
Craig Tiller | 079a11b | 2015-06-30 10:07:15 -0700 | [diff] [blame] | 242 | for (c = s->root_channel_data.next; c != &s->root_channel_data; c = c->next) { |
| 243 | count++; |
Craig Tiller | ff3ae68 | 2015-06-29 17:44:04 -0700 | [diff] [blame] | 244 | } |
| 245 | cb->num_channels = count; |
| 246 | cb->channels = gpr_malloc(sizeof(*cb->channels) * cb->num_channels); |
| 247 | count = 0; |
Craig Tiller | 079a11b | 2015-06-30 10:07:15 -0700 | [diff] [blame] | 248 | for (c = s->root_channel_data.next; c != &s->root_channel_data; c = c->next) { |
Craig Tiller | 49924e0 | 2015-06-29 22:42:33 -0700 | [diff] [blame] | 249 | cb->channels[count++] = c->channel; |
Craig Tiller | ff3ae68 | 2015-06-29 17:44:04 -0700 | [diff] [blame] | 250 | GRPC_CHANNEL_INTERNAL_REF(c->channel, "broadcast"); |
Craig Tiller | ff3ae68 | 2015-06-29 17:44:04 -0700 | [diff] [blame] | 251 | } |
| 252 | } |
| 253 | |
| 254 | struct shutdown_cleanup_args { |
| 255 | grpc_iomgr_closure closure; |
| 256 | gpr_slice slice; |
| 257 | }; |
| 258 | |
| 259 | static void shutdown_cleanup(void *arg, int iomgr_status_ignored) { |
| 260 | struct shutdown_cleanup_args *a = arg; |
| 261 | gpr_slice_unref(a->slice); |
| 262 | gpr_free(a); |
| 263 | } |
| 264 | |
Craig Tiller | 079a11b | 2015-06-30 10:07:15 -0700 | [diff] [blame] | 265 | static void send_shutdown(grpc_channel *channel, int send_goaway, |
| 266 | int send_disconnect) { |
Craig Tiller | ff3ae68 | 2015-06-29 17:44:04 -0700 | [diff] [blame] | 267 | grpc_transport_op op; |
| 268 | struct shutdown_cleanup_args *sc; |
| 269 | grpc_channel_element *elem; |
| 270 | |
| 271 | memset(&op, 0, sizeof(op)); |
Craig Tiller | ff3ae68 | 2015-06-29 17:44:04 -0700 | [diff] [blame] | 272 | op.send_goaway = send_goaway; |
| 273 | sc = gpr_malloc(sizeof(*sc)); |
| 274 | sc->slice = gpr_slice_from_copied_string("Server shutdown"); |
| 275 | op.goaway_message = &sc->slice; |
| 276 | op.goaway_status = GRPC_STATUS_OK; |
| 277 | op.disconnect = send_disconnect; |
| 278 | grpc_iomgr_closure_init(&sc->closure, shutdown_cleanup, sc); |
| 279 | op.on_consumed = &sc->closure; |
| 280 | |
Craig Tiller | 079a11b | 2015-06-30 10:07:15 -0700 | [diff] [blame] | 281 | elem = grpc_channel_stack_element(grpc_channel_get_channel_stack(channel), 0); |
Craig Tiller | ff3ae68 | 2015-06-29 17:44:04 -0700 | [diff] [blame] | 282 | elem->filter->start_transport_op(elem, &op); |
| 283 | } |
| 284 | |
Craig Tiller | 079a11b | 2015-06-30 10:07:15 -0700 | [diff] [blame] | 285 | static void channel_broadcaster_shutdown(channel_broadcaster *cb, |
Craig Tiller | 12cf537 | 2015-07-09 13:48:11 -0700 | [diff] [blame] | 286 | int send_goaway, |
| 287 | int force_disconnect) { |
Craig Tiller | ff3ae68 | 2015-06-29 17:44:04 -0700 | [diff] [blame] | 288 | size_t i; |
| 289 | |
| 290 | for (i = 0; i < cb->num_channels; i++) { |
Craig Tiller | 9188d7a | 2015-07-05 12:44:37 -0700 | [diff] [blame] | 291 | send_shutdown(cb->channels[i], send_goaway, force_disconnect); |
Craig Tiller | ff3ae68 | 2015-06-29 17:44:04 -0700 | [diff] [blame] | 292 | GRPC_CHANNEL_INTERNAL_UNREF(cb->channels[i], "broadcast"); |
| 293 | } |
| 294 | gpr_free(cb->channels); |
| 295 | } |
| 296 | |
Craig Tiller | 729b35a | 2015-07-13 12:36:47 -0700 | [diff] [blame] | 297 | /* |
| 298 | * request_matcher |
| 299 | */ |
Craig Tiller | ff3ae68 | 2015-06-29 17:44:04 -0700 | [diff] [blame] | 300 | |
Craig Tiller | 6a006ce | 2015-07-13 16:25:40 -0700 | [diff] [blame] | 301 | static void request_matcher_init(request_matcher *request_matcher, |
Craig Tiller | 32ca48c | 2015-09-10 11:47:15 -0700 | [diff] [blame] | 302 | size_t entries) { |
Craig Tiller | 729b35a | 2015-07-13 12:36:47 -0700 | [diff] [blame] | 303 | memset(request_matcher, 0, sizeof(*request_matcher)); |
Craig Tiller | 6a006ce | 2015-07-13 16:25:40 -0700 | [diff] [blame] | 304 | request_matcher->requests = gpr_stack_lockfree_create(entries); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 305 | } |
| 306 | |
Craig Tiller | 6a006ce | 2015-07-13 16:25:40 -0700 | [diff] [blame] | 307 | static void request_matcher_destroy(request_matcher *request_matcher) { |
| 308 | GPR_ASSERT(gpr_stack_lockfree_pop(request_matcher->requests) == -1); |
| 309 | gpr_stack_lockfree_destroy(request_matcher->requests); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 310 | } |
| 311 | |
Craig Tiller | 729b35a | 2015-07-13 12:36:47 -0700 | [diff] [blame] | 312 | static void kill_zombie(void *elem, int success) { |
| 313 | grpc_call_destroy(grpc_call_from_top_element(elem)); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 314 | } |
| 315 | |
Craig Tiller | 729b35a | 2015-07-13 12:36:47 -0700 | [diff] [blame] | 316 | static void request_matcher_zombify_all_pending_calls( |
| 317 | request_matcher *request_matcher) { |
| 318 | while (request_matcher->pending_head) { |
| 319 | call_data *calld = request_matcher->pending_head; |
| 320 | request_matcher->pending_head = calld->pending_next; |
Craig Tiller | b645026 | 2015-07-14 07:12:19 -0700 | [diff] [blame] | 321 | gpr_mu_lock(&calld->mu_state); |
Craig Tiller | 729b35a | 2015-07-13 12:36:47 -0700 | [diff] [blame] | 322 | calld->state = ZOMBIED; |
Craig Tiller | b645026 | 2015-07-14 07:12:19 -0700 | [diff] [blame] | 323 | gpr_mu_unlock(&calld->mu_state); |
Craig Tiller | 729b35a | 2015-07-13 12:36:47 -0700 | [diff] [blame] | 324 | grpc_iomgr_closure_init( |
| 325 | &calld->kill_zombie_closure, kill_zombie, |
| 326 | grpc_call_stack_element(grpc_call_get_call_stack(calld->call), 0)); |
| 327 | grpc_iomgr_add_callback(&calld->kill_zombie_closure); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 328 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 329 | } |
| 330 | |
Craig Tiller | 1191e21 | 2015-07-30 14:49:02 -0700 | [diff] [blame] | 331 | static void request_matcher_kill_requests(grpc_server *server, |
| 332 | request_matcher *rm) { |
| 333 | int request_id; |
| 334 | while ((request_id = gpr_stack_lockfree_pop(rm->requests)) != -1) { |
| 335 | fail_call(server, &server->requested_calls[request_id]); |
| 336 | } |
| 337 | } |
| 338 | |
Craig Tiller | 729b35a | 2015-07-13 12:36:47 -0700 | [diff] [blame] | 339 | /* |
| 340 | * server proper |
| 341 | */ |
| 342 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 343 | static void server_ref(grpc_server *server) { |
| 344 | gpr_ref(&server->internal_refcount); |
| 345 | } |
| 346 | |
Craig Tiller | ee945e8 | 2015-05-26 16:15:34 -0700 | [diff] [blame] | 347 | static void server_delete(grpc_server *server) { |
Craig Tiller | ec3257c | 2015-02-12 15:59:43 -0800 | [diff] [blame] | 348 | registered_method *rm; |
Craig Tiller | 8950461 | 2015-04-27 11:48:46 -0700 | [diff] [blame] | 349 | size_t i; |
Craig Tiller | ee945e8 | 2015-05-26 16:15:34 -0700 | [diff] [blame] | 350 | grpc_channel_args_destroy(server->channel_args); |
Vijay Pai | 8931cdd | 2015-06-17 12:42:17 -0700 | [diff] [blame] | 351 | gpr_mu_destroy(&server->mu_global); |
| 352 | gpr_mu_destroy(&server->mu_call); |
Craig Tiller | ee945e8 | 2015-05-26 16:15:34 -0700 | [diff] [blame] | 353 | gpr_free(server->channel_filters); |
Craig Tiller | ee945e8 | 2015-05-26 16:15:34 -0700 | [diff] [blame] | 354 | while ((rm = server->registered_methods) != NULL) { |
| 355 | server->registered_methods = rm->next; |
Craig Tiller | 6a006ce | 2015-07-13 16:25:40 -0700 | [diff] [blame] | 356 | request_matcher_destroy(&rm->request_matcher); |
Craig Tiller | ee945e8 | 2015-05-26 16:15:34 -0700 | [diff] [blame] | 357 | gpr_free(rm->method); |
| 358 | gpr_free(rm->host); |
Craig Tiller | ee945e8 | 2015-05-26 16:15:34 -0700 | [diff] [blame] | 359 | gpr_free(rm); |
| 360 | } |
| 361 | for (i = 0; i < server->cq_count; i++) { |
Craig Tiller | 463f237 | 2015-05-28 16:16:15 -0700 | [diff] [blame] | 362 | GRPC_CQ_INTERNAL_UNREF(server->cqs[i], "server"); |
Craig Tiller | ee945e8 | 2015-05-26 16:15:34 -0700 | [diff] [blame] | 363 | } |
Craig Tiller | 6a006ce | 2015-07-13 16:25:40 -0700 | [diff] [blame] | 364 | request_matcher_destroy(&server->unregistered_request_matcher); |
| 365 | gpr_stack_lockfree_destroy(server->request_freelist); |
Craig Tiller | ee945e8 | 2015-05-26 16:15:34 -0700 | [diff] [blame] | 366 | gpr_free(server->cqs); |
| 367 | gpr_free(server->pollsets); |
| 368 | gpr_free(server->shutdown_tags); |
Craig Tiller | 6a006ce | 2015-07-13 16:25:40 -0700 | [diff] [blame] | 369 | gpr_free(server->requested_calls); |
Craig Tiller | ee945e8 | 2015-05-26 16:15:34 -0700 | [diff] [blame] | 370 | gpr_free(server); |
| 371 | } |
| 372 | |
| 373 | static void server_unref(grpc_server *server) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 374 | if (gpr_unref(&server->internal_refcount)) { |
Craig Tiller | ee945e8 | 2015-05-26 16:15:34 -0700 | [diff] [blame] | 375 | server_delete(server); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 376 | } |
| 377 | } |
| 378 | |
| 379 | static int is_channel_orphaned(channel_data *chand) { |
| 380 | return chand->next == chand; |
| 381 | } |
| 382 | |
| 383 | static void orphan_channel(channel_data *chand) { |
| 384 | chand->next->prev = chand->prev; |
| 385 | chand->prev->next = chand->next; |
| 386 | chand->next = chand->prev = chand; |
| 387 | } |
| 388 | |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 389 | static void finish_destroy_channel(void *cd, int success) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 390 | channel_data *chand = cd; |
| 391 | grpc_server *server = chand->server; |
Craig Tiller | 9ec2a52 | 2015-05-29 22:46:54 -0700 | [diff] [blame] | 392 | GRPC_CHANNEL_INTERNAL_UNREF(chand->channel, "server"); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 393 | server_unref(server); |
| 394 | } |
| 395 | |
| 396 | static void destroy_channel(channel_data *chand) { |
| 397 | if (is_channel_orphaned(chand)) return; |
| 398 | GPR_ASSERT(chand->server != NULL); |
| 399 | orphan_channel(chand); |
| 400 | server_ref(chand->server); |
Craig Tiller | 52760dd | 2015-05-29 23:09:26 -0700 | [diff] [blame] | 401 | maybe_finish_shutdown(chand->server); |
David Garcia Quintas | 284488b | 2015-05-28 16:27:39 -0700 | [diff] [blame] | 402 | chand->finish_destroy_channel_closure.cb = finish_destroy_channel; |
| 403 | chand->finish_destroy_channel_closure.cb_arg = chand; |
| 404 | grpc_iomgr_add_callback(&chand->finish_destroy_channel_closure); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 405 | } |
| 406 | |
Craig Tiller | 76d2c3b | 2015-07-07 11:46:01 -0700 | [diff] [blame] | 407 | static void finish_start_new_rpc(grpc_server *server, grpc_call_element *elem, |
Craig Tiller | 729b35a | 2015-07-13 12:36:47 -0700 | [diff] [blame] | 408 | request_matcher *request_matcher) { |
Craig Tiller | 04cc8be | 2015-02-10 16:11:22 -0800 | [diff] [blame] | 409 | call_data *calld = elem->call_data; |
Craig Tiller | 6a006ce | 2015-07-13 16:25:40 -0700 | [diff] [blame] | 410 | int request_id; |
| 411 | |
Craig Tiller | 9835a13 | 2015-07-22 17:37:39 -0700 | [diff] [blame] | 412 | if (gpr_atm_acq_load(&server->shutdown_flag)) { |
| 413 | gpr_mu_lock(&calld->mu_state); |
| 414 | calld->state = ZOMBIED; |
| 415 | gpr_mu_unlock(&calld->mu_state); |
| 416 | grpc_iomgr_closure_init(&calld->kill_zombie_closure, kill_zombie, elem); |
| 417 | grpc_iomgr_add_callback(&calld->kill_zombie_closure); |
| 418 | return; |
| 419 | } |
| 420 | |
Craig Tiller | 6a006ce | 2015-07-13 16:25:40 -0700 | [diff] [blame] | 421 | request_id = gpr_stack_lockfree_pop(request_matcher->requests); |
| 422 | if (request_id == -1) { |
| 423 | gpr_mu_lock(&server->mu_call); |
Craig Tiller | 76d2c3b | 2015-07-07 11:46:01 -0700 | [diff] [blame] | 424 | gpr_mu_lock(&calld->mu_state); |
Craig Tiller | 04cc8be | 2015-02-10 16:11:22 -0800 | [diff] [blame] | 425 | calld->state = PENDING; |
Craig Tiller | 76d2c3b | 2015-07-07 11:46:01 -0700 | [diff] [blame] | 426 | gpr_mu_unlock(&calld->mu_state); |
Craig Tiller | 729b35a | 2015-07-13 12:36:47 -0700 | [diff] [blame] | 427 | if (request_matcher->pending_head == NULL) { |
| 428 | request_matcher->pending_tail = request_matcher->pending_head = calld; |
| 429 | } else { |
| 430 | request_matcher->pending_tail->pending_next = calld; |
| 431 | request_matcher->pending_tail = calld; |
| 432 | } |
| 433 | calld->pending_next = NULL; |
Vijay Pai | 8931cdd | 2015-06-17 12:42:17 -0700 | [diff] [blame] | 434 | gpr_mu_unlock(&server->mu_call); |
Craig Tiller | 04cc8be | 2015-02-10 16:11:22 -0800 | [diff] [blame] | 435 | } else { |
Craig Tiller | 76d2c3b | 2015-07-07 11:46:01 -0700 | [diff] [blame] | 436 | gpr_mu_lock(&calld->mu_state); |
Craig Tiller | 04cc8be | 2015-02-10 16:11:22 -0800 | [diff] [blame] | 437 | calld->state = ACTIVATED; |
Craig Tiller | 76d2c3b | 2015-07-07 11:46:01 -0700 | [diff] [blame] | 438 | gpr_mu_unlock(&calld->mu_state); |
Craig Tiller | 6a006ce | 2015-07-13 16:25:40 -0700 | [diff] [blame] | 439 | begin_call(server, calld, &server->requested_calls[request_id]); |
Craig Tiller | 04cc8be | 2015-02-10 16:11:22 -0800 | [diff] [blame] | 440 | } |
| 441 | } |
| 442 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 443 | static void start_new_rpc(grpc_call_element *elem) { |
| 444 | channel_data *chand = elem->channel_data; |
| 445 | call_data *calld = elem->call_data; |
| 446 | grpc_server *server = chand->server; |
Craig Tiller | 04cc8be | 2015-02-10 16:11:22 -0800 | [diff] [blame] | 447 | gpr_uint32 i; |
| 448 | gpr_uint32 hash; |
| 449 | channel_registered_method *rm; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 450 | |
Craig Tiller | 04cc8be | 2015-02-10 16:11:22 -0800 | [diff] [blame] | 451 | if (chand->registered_methods && calld->path && calld->host) { |
Craig Tiller | a94beff | 2015-02-17 22:02:06 -0800 | [diff] [blame] | 452 | /* TODO(ctiller): unify these two searches */ |
Craig Tiller | 04cc8be | 2015-02-10 16:11:22 -0800 | [diff] [blame] | 453 | /* check for an exact match with host */ |
| 454 | hash = GRPC_MDSTR_KV_HASH(calld->host->hash, calld->path->hash); |
Craig Tiller | c6d6d90 | 2015-07-09 15:53:50 -0700 | [diff] [blame] | 455 | for (i = 0; i <= chand->registered_method_max_probes; i++) { |
Craig Tiller | 3b29b56 | 2015-02-11 12:58:46 -0800 | [diff] [blame] | 456 | rm = &chand->registered_methods[(hash + i) % |
| 457 | chand->registered_method_slots]; |
Craig Tiller | 04cc8be | 2015-02-10 16:11:22 -0800 | [diff] [blame] | 458 | if (!rm) break; |
| 459 | if (rm->host != calld->host) continue; |
| 460 | if (rm->method != calld->path) continue; |
Craig Tiller | 729b35a | 2015-07-13 12:36:47 -0700 | [diff] [blame] | 461 | finish_start_new_rpc(server, elem, |
| 462 | &rm->server_registered_method->request_matcher); |
Craig Tiller | 04cc8be | 2015-02-10 16:11:22 -0800 | [diff] [blame] | 463 | return; |
| 464 | } |
| 465 | /* check for a wildcard method definition (no host set) */ |
| 466 | hash = GRPC_MDSTR_KV_HASH(0, calld->path->hash); |
Craig Tiller | 0ef1a92 | 2015-02-11 16:23:01 -0800 | [diff] [blame] | 467 | for (i = 0; i <= chand->registered_method_max_probes; i++) { |
Craig Tiller | 3b29b56 | 2015-02-11 12:58:46 -0800 | [diff] [blame] | 468 | rm = &chand->registered_methods[(hash + i) % |
| 469 | chand->registered_method_slots]; |
Craig Tiller | 04cc8be | 2015-02-10 16:11:22 -0800 | [diff] [blame] | 470 | if (!rm) break; |
| 471 | if (rm->host != NULL) continue; |
| 472 | if (rm->method != calld->path) continue; |
Craig Tiller | 729b35a | 2015-07-13 12:36:47 -0700 | [diff] [blame] | 473 | finish_start_new_rpc(server, elem, |
| 474 | &rm->server_registered_method->request_matcher); |
Craig Tiller | 04cc8be | 2015-02-10 16:11:22 -0800 | [diff] [blame] | 475 | return; |
| 476 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 477 | } |
Craig Tiller | 729b35a | 2015-07-13 12:36:47 -0700 | [diff] [blame] | 478 | finish_start_new_rpc(server, elem, &server->unregistered_request_matcher); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 479 | } |
| 480 | |
Craig Tiller | ee945e8 | 2015-05-26 16:15:34 -0700 | [diff] [blame] | 481 | static int num_listeners(grpc_server *server) { |
| 482 | listener *l; |
| 483 | int n = 0; |
| 484 | for (l = server->listeners; l; l = l->next) { |
| 485 | n++; |
| 486 | } |
| 487 | return n; |
| 488 | } |
| 489 | |
Craig Tiller | 97fc6a3 | 2015-07-08 15:31:35 -0700 | [diff] [blame] | 490 | static void done_shutdown_event(void *server, grpc_cq_completion *completion) { |
| 491 | server_unref(server); |
| 492 | } |
| 493 | |
Craig Tiller | ab54f79 | 2015-07-08 08:34:20 -0700 | [diff] [blame] | 494 | static int num_channels(grpc_server *server) { |
| 495 | channel_data *chand; |
| 496 | int n = 0; |
| 497 | for (chand = server->root_channel_data.next; |
| 498 | chand != &server->root_channel_data; chand = chand->next) { |
| 499 | n++; |
| 500 | } |
| 501 | return n; |
| 502 | } |
| 503 | |
Craig Tiller | 1191e21 | 2015-07-30 14:49:02 -0700 | [diff] [blame] | 504 | static void kill_pending_work_locked(grpc_server *server) { |
| 505 | registered_method *rm; |
| 506 | request_matcher_kill_requests(server, &server->unregistered_request_matcher); |
| 507 | request_matcher_zombify_all_pending_calls( |
| 508 | &server->unregistered_request_matcher); |
| 509 | for (rm = server->registered_methods; rm; rm = rm->next) { |
| 510 | request_matcher_kill_requests(server, &rm->request_matcher); |
| 511 | request_matcher_zombify_all_pending_calls(&rm->request_matcher); |
| 512 | } |
| 513 | } |
| 514 | |
Craig Tiller | dc62772 | 2015-05-26 15:27:02 -0700 | [diff] [blame] | 515 | static void maybe_finish_shutdown(grpc_server *server) { |
Craig Tiller | bce999f | 2015-05-27 09:55:51 -0700 | [diff] [blame] | 516 | size_t i; |
Craig Tiller | 6a006ce | 2015-07-13 16:25:40 -0700 | [diff] [blame] | 517 | if (!gpr_atm_acq_load(&server->shutdown_flag) || server->shutdown_published) { |
Craig Tiller | c4a1f52 | 2015-05-29 22:32:04 -0700 | [diff] [blame] | 518 | return; |
| 519 | } |
Vijay Pai | 8931cdd | 2015-06-17 12:42:17 -0700 | [diff] [blame] | 520 | |
Craig Tiller | 1191e21 | 2015-07-30 14:49:02 -0700 | [diff] [blame] | 521 | kill_pending_work_locked(server); |
| 522 | |
Craig Tiller | ab54f79 | 2015-07-08 08:34:20 -0700 | [diff] [blame] | 523 | if (server->root_channel_data.next != &server->root_channel_data || |
| 524 | server->listeners_destroyed < num_listeners(server)) { |
Craig Tiller | 58bbc86 | 2015-07-13 09:51:17 -0700 | [diff] [blame] | 525 | if (gpr_time_cmp(gpr_time_sub(gpr_now(GPR_CLOCK_REALTIME), |
| 526 | server->last_shutdown_message_time), |
| 527 | gpr_time_from_seconds(1, GPR_TIMESPAN)) >= 0) { |
Craig Tiller | 080d6c5 | 2015-07-10 10:23:10 -0700 | [diff] [blame] | 528 | server->last_shutdown_message_time = gpr_now(GPR_CLOCK_REALTIME); |
Craig Tiller | ab54f79 | 2015-07-08 08:34:20 -0700 | [diff] [blame] | 529 | gpr_log(GPR_DEBUG, |
| 530 | "Waiting for %d channels and %d/%d listeners to be destroyed" |
| 531 | " before shutting down server", |
| 532 | num_channels(server), |
| 533 | num_listeners(server) - server->listeners_destroyed, |
| 534 | num_listeners(server)); |
| 535 | } |
Craig Tiller | c4a1f52 | 2015-05-29 22:32:04 -0700 | [diff] [blame] | 536 | return; |
| 537 | } |
| 538 | server->shutdown_published = 1; |
| 539 | for (i = 0; i < server->num_shutdown_tags; i++) { |
Craig Tiller | 97fc6a3 | 2015-07-08 15:31:35 -0700 | [diff] [blame] | 540 | server_ref(server); |
Craig Tiller | 12cf537 | 2015-07-09 13:48:11 -0700 | [diff] [blame] | 541 | grpc_cq_end_op(server->shutdown_tags[i].cq, server->shutdown_tags[i].tag, 1, |
| 542 | done_shutdown_event, server, |
| 543 | &server->shutdown_tags[i].completion); |
Craig Tiller | dc62772 | 2015-05-26 15:27:02 -0700 | [diff] [blame] | 544 | } |
| 545 | } |
| 546 | |
Craig Tiller | 6902ad2 | 2015-04-16 08:01:49 -0700 | [diff] [blame] | 547 | static grpc_mdelem *server_filter(void *user_data, grpc_mdelem *md) { |
| 548 | grpc_call_element *elem = user_data; |
Craig Tiller | cce17ac | 2015-01-20 09:29:28 -0800 | [diff] [blame] | 549 | channel_data *chand = elem->channel_data; |
| 550 | call_data *calld = elem->call_data; |
Craig Tiller | 6902ad2 | 2015-04-16 08:01:49 -0700 | [diff] [blame] | 551 | if (md->key == chand->path_key) { |
Craig Tiller | 1a65a23 | 2015-07-06 10:22:32 -0700 | [diff] [blame] | 552 | calld->path = GRPC_MDSTR_REF(md->value); |
Craig Tiller | 6902ad2 | 2015-04-16 08:01:49 -0700 | [diff] [blame] | 553 | return NULL; |
| 554 | } else if (md->key == chand->authority_key) { |
Craig Tiller | 1a65a23 | 2015-07-06 10:22:32 -0700 | [diff] [blame] | 555 | calld->host = GRPC_MDSTR_REF(md->value); |
Craig Tiller | 6902ad2 | 2015-04-16 08:01:49 -0700 | [diff] [blame] | 556 | return NULL; |
| 557 | } |
| 558 | return md; |
| 559 | } |
| 560 | |
Craig Tiller | be18b8d | 2015-04-22 14:00:47 -0700 | [diff] [blame] | 561 | static void server_on_recv(void *ptr, int success) { |
| 562 | grpc_call_element *elem = ptr; |
Craig Tiller | 6902ad2 | 2015-04-16 08:01:49 -0700 | [diff] [blame] | 563 | call_data *calld = elem->call_data; |
Craig Tiller | 94329d0 | 2015-07-23 09:52:11 -0700 | [diff] [blame] | 564 | gpr_timespec op_deadline; |
Craig Tiller | be18b8d | 2015-04-22 14:00:47 -0700 | [diff] [blame] | 565 | |
| 566 | if (success && !calld->got_initial_metadata) { |
| 567 | size_t i; |
| 568 | size_t nops = calld->recv_ops->nops; |
| 569 | grpc_stream_op *ops = calld->recv_ops->ops; |
| 570 | for (i = 0; i < nops; i++) { |
| 571 | grpc_stream_op *op = &ops[i]; |
| 572 | if (op->type != GRPC_OP_METADATA) continue; |
Craig Tiller | 205aee1 | 2015-04-16 14:46:41 -0700 | [diff] [blame] | 573 | grpc_metadata_batch_filter(&op->data.metadata, server_filter, elem); |
Craig Tiller | 94329d0 | 2015-07-23 09:52:11 -0700 | [diff] [blame] | 574 | op_deadline = op->data.metadata.deadline; |
| 575 | if (0 != |
| 576 | gpr_time_cmp(op_deadline, gpr_inf_future(op_deadline.clock_type))) { |
Craig Tiller | 6902ad2 | 2015-04-16 08:01:49 -0700 | [diff] [blame] | 577 | calld->deadline = op->data.metadata.deadline; |
Craig Tiller | cce17ac | 2015-01-20 09:29:28 -0800 | [diff] [blame] | 578 | } |
Craig Tiller | c4b56b6 | 2015-07-23 17:44:11 -0700 | [diff] [blame] | 579 | if (calld->host && calld->path) { |
| 580 | calld->got_initial_metadata = 1; |
| 581 | start_new_rpc(elem); |
| 582 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 583 | break; |
Craig Tiller | be18b8d | 2015-04-22 14:00:47 -0700 | [diff] [blame] | 584 | } |
| 585 | } |
| 586 | |
| 587 | switch (*calld->recv_state) { |
Craig Tiller | 06aeea7 | 2015-04-23 10:54:45 -0700 | [diff] [blame] | 588 | case GRPC_STREAM_OPEN: |
| 589 | break; |
| 590 | case GRPC_STREAM_SEND_CLOSED: |
| 591 | break; |
Craig Tiller | be18b8d | 2015-04-22 14:00:47 -0700 | [diff] [blame] | 592 | case GRPC_STREAM_RECV_CLOSED: |
Craig Tiller | 76d2c3b | 2015-07-07 11:46:01 -0700 | [diff] [blame] | 593 | gpr_mu_lock(&calld->mu_state); |
Craig Tiller | be18b8d | 2015-04-22 14:00:47 -0700 | [diff] [blame] | 594 | if (calld->state == NOT_STARTED) { |
| 595 | calld->state = ZOMBIED; |
Craig Tiller | 76d2c3b | 2015-07-07 11:46:01 -0700 | [diff] [blame] | 596 | gpr_mu_unlock(&calld->mu_state); |
David Garcia Quintas | 284488b | 2015-05-28 16:27:39 -0700 | [diff] [blame] | 597 | grpc_iomgr_closure_init(&calld->kill_zombie_closure, kill_zombie, elem); |
| 598 | grpc_iomgr_add_callback(&calld->kill_zombie_closure); |
Craig Tiller | 76d2c3b | 2015-07-07 11:46:01 -0700 | [diff] [blame] | 599 | } else { |
| 600 | gpr_mu_unlock(&calld->mu_state); |
Craig Tiller | be18b8d | 2015-04-22 14:00:47 -0700 | [diff] [blame] | 601 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 602 | break; |
Craig Tiller | be18b8d | 2015-04-22 14:00:47 -0700 | [diff] [blame] | 603 | case GRPC_STREAM_CLOSED: |
Craig Tiller | 76d2c3b | 2015-07-07 11:46:01 -0700 | [diff] [blame] | 604 | gpr_mu_lock(&calld->mu_state); |
Craig Tiller | be18b8d | 2015-04-22 14:00:47 -0700 | [diff] [blame] | 605 | if (calld->state == NOT_STARTED) { |
| 606 | calld->state = ZOMBIED; |
Craig Tiller | 76d2c3b | 2015-07-07 11:46:01 -0700 | [diff] [blame] | 607 | gpr_mu_unlock(&calld->mu_state); |
David Garcia Quintas | 284488b | 2015-05-28 16:27:39 -0700 | [diff] [blame] | 608 | grpc_iomgr_closure_init(&calld->kill_zombie_closure, kill_zombie, elem); |
| 609 | grpc_iomgr_add_callback(&calld->kill_zombie_closure); |
Craig Tiller | be18b8d | 2015-04-22 14:00:47 -0700 | [diff] [blame] | 610 | } else if (calld->state == PENDING) { |
Craig Tiller | c9d0382 | 2015-05-20 16:08:45 -0700 | [diff] [blame] | 611 | calld->state = ZOMBIED; |
Craig Tiller | 76d2c3b | 2015-07-07 11:46:01 -0700 | [diff] [blame] | 612 | gpr_mu_unlock(&calld->mu_state); |
Craig Tiller | 729b35a | 2015-07-13 12:36:47 -0700 | [diff] [blame] | 613 | /* zombied call will be destroyed when it's removed from the pending |
| 614 | queue... later */ |
Craig Tiller | 76d2c3b | 2015-07-07 11:46:01 -0700 | [diff] [blame] | 615 | } else { |
| 616 | gpr_mu_unlock(&calld->mu_state); |
Craig Tiller | be18b8d | 2015-04-22 14:00:47 -0700 | [diff] [blame] | 617 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 618 | break; |
| 619 | } |
Craig Tiller | be18b8d | 2015-04-22 14:00:47 -0700 | [diff] [blame] | 620 | |
Craig Tiller | 1e6facb | 2015-06-11 22:47:11 -0700 | [diff] [blame] | 621 | calld->on_done_recv->cb(calld->on_done_recv->cb_arg, success); |
Craig Tiller | be18b8d | 2015-04-22 14:00:47 -0700 | [diff] [blame] | 622 | } |
| 623 | |
Craig Tiller | b7959a0 | 2015-06-25 08:50:54 -0700 | [diff] [blame] | 624 | static void server_mutate_op(grpc_call_element *elem, |
| 625 | grpc_transport_stream_op *op) { |
Craig Tiller | be18b8d | 2015-04-22 14:00:47 -0700 | [diff] [blame] | 626 | call_data *calld = elem->call_data; |
Craig Tiller | be18b8d | 2015-04-22 14:00:47 -0700 | [diff] [blame] | 627 | |
| 628 | if (op->recv_ops) { |
| 629 | /* substitute our callback for the higher callback */ |
| 630 | calld->recv_ops = op->recv_ops; |
| 631 | calld->recv_state = op->recv_state; |
| 632 | calld->on_done_recv = op->on_done_recv; |
Craig Tiller | 1e6facb | 2015-06-11 22:47:11 -0700 | [diff] [blame] | 633 | op->on_done_recv = &calld->server_on_recv; |
Craig Tiller | be18b8d | 2015-04-22 14:00:47 -0700 | [diff] [blame] | 634 | } |
Craig Tiller | 50d9db5 | 2015-04-23 10:52:14 -0700 | [diff] [blame] | 635 | } |
Craig Tiller | be18b8d | 2015-04-22 14:00:47 -0700 | [diff] [blame] | 636 | |
Craig Tiller | e039f03 | 2015-06-25 12:54:23 -0700 | [diff] [blame] | 637 | static void server_start_transport_stream_op(grpc_call_element *elem, |
| 638 | grpc_transport_stream_op *op) { |
Craig Tiller | 50d9db5 | 2015-04-23 10:52:14 -0700 | [diff] [blame] | 639 | GRPC_CALL_LOG_OP(GPR_INFO, elem, op); |
| 640 | server_mutate_op(elem, op); |
Craig Tiller | be18b8d | 2015-04-22 14:00:47 -0700 | [diff] [blame] | 641 | grpc_call_next_op(elem, op); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 642 | } |
| 643 | |
Craig Tiller | e039f03 | 2015-06-25 12:54:23 -0700 | [diff] [blame] | 644 | static void accept_stream(void *cd, grpc_transport *transport, |
| 645 | const void *transport_server_data) { |
| 646 | channel_data *chand = cd; |
| 647 | /* create a call */ |
Craig Tiller | 3e7c6a7 | 2015-07-31 16:17:04 -0700 | [diff] [blame] | 648 | grpc_call_create(chand->channel, NULL, 0, NULL, transport_server_data, NULL, |
| 649 | 0, gpr_inf_future(GPR_CLOCK_MONOTONIC)); |
Craig Tiller | e039f03 | 2015-06-25 12:54:23 -0700 | [diff] [blame] | 650 | } |
| 651 | |
| 652 | static void channel_connectivity_changed(void *cd, int iomgr_status_ignored) { |
| 653 | channel_data *chand = cd; |
| 654 | grpc_server *server = chand->server; |
| 655 | if (chand->connectivity_state != GRPC_CHANNEL_FATAL_FAILURE) { |
| 656 | grpc_transport_op op; |
| 657 | memset(&op, 0, sizeof(op)); |
| 658 | op.on_connectivity_state_change = &chand->channel_connectivity_changed, |
| 659 | op.connectivity_state = &chand->connectivity_state; |
| 660 | grpc_channel_next_op(grpc_channel_stack_element( |
| 661 | grpc_channel_get_channel_stack(chand->channel), 0), |
| 662 | &op); |
| 663 | } else { |
| 664 | gpr_mu_lock(&server->mu_global); |
| 665 | destroy_channel(chand); |
| 666 | gpr_mu_unlock(&server->mu_global); |
| 667 | GRPC_CHANNEL_INTERNAL_UNREF(chand->channel, "connectivity"); |
| 668 | } |
| 669 | } |
| 670 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 671 | static void init_call_elem(grpc_call_element *elem, |
Craig Tiller | 06aeea7 | 2015-04-23 10:54:45 -0700 | [diff] [blame] | 672 | const void *server_transport_data, |
Craig Tiller | b7959a0 | 2015-06-25 08:50:54 -0700 | [diff] [blame] | 673 | grpc_transport_stream_op *initial_op) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 674 | call_data *calld = elem->call_data; |
| 675 | channel_data *chand = elem->channel_data; |
| 676 | memset(calld, 0, sizeof(call_data)); |
Craig Tiller | 143e7bf | 2015-07-13 08:41:49 -0700 | [diff] [blame] | 677 | calld->deadline = gpr_inf_future(GPR_CLOCK_REALTIME); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 678 | calld->call = grpc_call_from_top_element(elem); |
Craig Tiller | 76d2c3b | 2015-07-07 11:46:01 -0700 | [diff] [blame] | 679 | gpr_mu_init(&calld->mu_state); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 680 | |
Craig Tiller | 1e6facb | 2015-06-11 22:47:11 -0700 | [diff] [blame] | 681 | grpc_iomgr_closure_init(&calld->server_on_recv, server_on_recv, elem); |
| 682 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 683 | server_ref(chand->server); |
Craig Tiller | 50d9db5 | 2015-04-23 10:52:14 -0700 | [diff] [blame] | 684 | |
Craig Tiller | 482ef8b | 2015-04-23 11:38:20 -0700 | [diff] [blame] | 685 | if (initial_op) server_mutate_op(elem, initial_op); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 686 | } |
| 687 | |
| 688 | static void destroy_call_elem(grpc_call_element *elem) { |
| 689 | channel_data *chand = elem->channel_data; |
Craig Tiller | db7db99 | 2015-01-29 11:19:01 -0800 | [diff] [blame] | 690 | call_data *calld = elem->call_data; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 691 | |
Craig Tiller | 729b35a | 2015-07-13 12:36:47 -0700 | [diff] [blame] | 692 | GPR_ASSERT(calld->state != PENDING); |
Craig Tiller | 092d8d1 | 2015-07-04 22:35:00 -0700 | [diff] [blame] | 693 | |
Craig Tiller | 4df31a6 | 2015-01-30 09:44:31 -0800 | [diff] [blame] | 694 | if (calld->host) { |
Craig Tiller | 1a65a23 | 2015-07-06 10:22:32 -0700 | [diff] [blame] | 695 | GRPC_MDSTR_UNREF(calld->host); |
Craig Tiller | 4df31a6 | 2015-01-30 09:44:31 -0800 | [diff] [blame] | 696 | } |
| 697 | if (calld->path) { |
Craig Tiller | 1a65a23 | 2015-07-06 10:22:32 -0700 | [diff] [blame] | 698 | GRPC_MDSTR_UNREF(calld->path); |
Craig Tiller | 4df31a6 | 2015-01-30 09:44:31 -0800 | [diff] [blame] | 699 | } |
| 700 | |
Craig Tiller | 76d2c3b | 2015-07-07 11:46:01 -0700 | [diff] [blame] | 701 | gpr_mu_destroy(&calld->mu_state); |
| 702 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 703 | server_unref(chand->server); |
| 704 | } |
| 705 | |
Craig Tiller | 079a11b | 2015-06-30 10:07:15 -0700 | [diff] [blame] | 706 | static void init_channel_elem(grpc_channel_element *elem, grpc_channel *master, |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 707 | const grpc_channel_args *args, |
| 708 | grpc_mdctx *metadata_context, int is_first, |
| 709 | int is_last) { |
| 710 | channel_data *chand = elem->channel_data; |
| 711 | GPR_ASSERT(is_first); |
| 712 | GPR_ASSERT(!is_last); |
| 713 | chand->server = NULL; |
| 714 | chand->channel = NULL; |
Craig Tiller | 6999c09 | 2015-07-22 08:14:12 -0700 | [diff] [blame] | 715 | chand->path_key = grpc_mdstr_from_string(metadata_context, ":path", 0); |
Craig Tiller | d6c98df | 2015-08-18 09:33:44 -0700 | [diff] [blame] | 716 | chand->authority_key = |
| 717 | grpc_mdstr_from_string(metadata_context, ":authority", 0); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 718 | chand->next = chand->prev = chand; |
Craig Tiller | 04cc8be | 2015-02-10 16:11:22 -0800 | [diff] [blame] | 719 | chand->registered_methods = NULL; |
Craig Tiller | e039f03 | 2015-06-25 12:54:23 -0700 | [diff] [blame] | 720 | chand->connectivity_state = GRPC_CHANNEL_IDLE; |
| 721 | grpc_iomgr_closure_init(&chand->channel_connectivity_changed, |
| 722 | channel_connectivity_changed, chand); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 723 | } |
| 724 | |
| 725 | static void destroy_channel_elem(grpc_channel_element *elem) { |
Craig Tiller | ec3257c | 2015-02-12 15:59:43 -0800 | [diff] [blame] | 726 | size_t i; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 727 | channel_data *chand = elem->channel_data; |
Craig Tiller | ec3257c | 2015-02-12 15:59:43 -0800 | [diff] [blame] | 728 | if (chand->registered_methods) { |
| 729 | for (i = 0; i < chand->registered_method_slots; i++) { |
| 730 | if (chand->registered_methods[i].method) { |
Craig Tiller | 1a65a23 | 2015-07-06 10:22:32 -0700 | [diff] [blame] | 731 | GRPC_MDSTR_UNREF(chand->registered_methods[i].method); |
Craig Tiller | ec3257c | 2015-02-12 15:59:43 -0800 | [diff] [blame] | 732 | } |
| 733 | if (chand->registered_methods[i].host) { |
Craig Tiller | 1a65a23 | 2015-07-06 10:22:32 -0700 | [diff] [blame] | 734 | GRPC_MDSTR_UNREF(chand->registered_methods[i].host); |
Craig Tiller | ec3257c | 2015-02-12 15:59:43 -0800 | [diff] [blame] | 735 | } |
| 736 | } |
| 737 | gpr_free(chand->registered_methods); |
| 738 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 739 | if (chand->server) { |
Vijay Pai | 8931cdd | 2015-06-17 12:42:17 -0700 | [diff] [blame] | 740 | gpr_mu_lock(&chand->server->mu_global); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 741 | chand->next->prev = chand->prev; |
| 742 | chand->prev->next = chand->next; |
| 743 | chand->next = chand->prev = chand; |
Craig Tiller | b76d05b | 2015-05-29 17:21:56 -0700 | [diff] [blame] | 744 | maybe_finish_shutdown(chand->server); |
Vijay Pai | 8931cdd | 2015-06-17 12:42:17 -0700 | [diff] [blame] | 745 | gpr_mu_unlock(&chand->server->mu_global); |
Craig Tiller | 1a65a23 | 2015-07-06 10:22:32 -0700 | [diff] [blame] | 746 | GRPC_MDSTR_UNREF(chand->path_key); |
| 747 | GRPC_MDSTR_UNREF(chand->authority_key); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 748 | server_unref(chand->server); |
| 749 | } |
| 750 | } |
| 751 | |
| 752 | static const grpc_channel_filter server_surface_filter = { |
Craig Tiller | e039f03 | 2015-06-25 12:54:23 -0700 | [diff] [blame] | 753 | server_start_transport_stream_op, |
| 754 | grpc_channel_next_op, |
Craig Tiller | b76d05b | 2015-05-29 17:21:56 -0700 | [diff] [blame] | 755 | sizeof(call_data), |
| 756 | init_call_elem, |
| 757 | destroy_call_elem, |
| 758 | sizeof(channel_data), |
| 759 | init_channel_elem, |
| 760 | destroy_channel_elem, |
Craig Tiller | 1b22b9d | 2015-07-20 13:42:22 -0700 | [diff] [blame] | 761 | grpc_call_next_get_peer, |
Craig Tiller | b76d05b | 2015-05-29 17:21:56 -0700 | [diff] [blame] | 762 | "server", |
Craig Tiller | 9f28ac2 | 2015-01-27 17:01:29 -0800 | [diff] [blame] | 763 | }; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 764 | |
Craig Tiller | f9e6adf | 2015-05-06 11:45:59 -0700 | [diff] [blame] | 765 | void grpc_server_register_completion_queue(grpc_server *server, |
Nicolas "Pixel" Noble | ebb5140 | 2015-07-23 02:41:33 +0200 | [diff] [blame] | 766 | grpc_completion_queue *cq, |
| 767 | void *reserved) { |
Craig Tiller | 20bc56d | 2015-02-12 09:02:56 -0800 | [diff] [blame] | 768 | size_t i, n; |
Nicolas "Pixel" Noble | 4599288 | 2015-07-29 23:52:14 +0200 | [diff] [blame] | 769 | GPR_ASSERT(!reserved); |
Craig Tiller | 20bc56d | 2015-02-12 09:02:56 -0800 | [diff] [blame] | 770 | for (i = 0; i < server->cq_count; i++) { |
| 771 | if (server->cqs[i] == cq) return; |
| 772 | } |
Craig Tiller | 463f237 | 2015-05-28 16:16:15 -0700 | [diff] [blame] | 773 | GRPC_CQ_INTERNAL_REF(cq, "server"); |
Craig Tiller | b56975c | 2015-06-15 10:11:16 -0700 | [diff] [blame] | 774 | grpc_cq_mark_server_cq(cq); |
Craig Tiller | 20bc56d | 2015-02-12 09:02:56 -0800 | [diff] [blame] | 775 | n = server->cq_count++; |
Craig Tiller | ec3257c | 2015-02-12 15:59:43 -0800 | [diff] [blame] | 776 | server->cqs = gpr_realloc(server->cqs, |
| 777 | server->cq_count * sizeof(grpc_completion_queue *)); |
Craig Tiller | 20bc56d | 2015-02-12 09:02:56 -0800 | [diff] [blame] | 778 | server->cqs[n] = cq; |
| 779 | } |
| 780 | |
David Garcia Quintas | e25e928 | 2015-06-23 10:18:52 -0700 | [diff] [blame] | 781 | grpc_server *grpc_server_create_from_filters( |
| 782 | const grpc_channel_filter **filters, size_t filter_count, |
| 783 | const grpc_channel_args *args) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 784 | size_t i; |
Alistair Veitch | 9d48ebf | 2015-06-01 10:01:03 -0700 | [diff] [blame] | 785 | /* TODO(census): restore this once we finalize census filter etc. |
| 786 | int census_enabled = grpc_channel_args_is_census_enabled(args); */ |
| 787 | int census_enabled = 0; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 788 | |
| 789 | grpc_server *server = gpr_malloc(sizeof(grpc_server)); |
Craig Tiller | 60fd361 | 2015-03-05 16:24:22 -0800 | [diff] [blame] | 790 | |
| 791 | GPR_ASSERT(grpc_is_initialized() && "call grpc_init()"); |
| 792 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 793 | memset(server, 0, sizeof(grpc_server)); |
| 794 | |
Vijay Pai | 8931cdd | 2015-06-17 12:42:17 -0700 | [diff] [blame] | 795 | gpr_mu_init(&server->mu_global); |
| 796 | gpr_mu_init(&server->mu_call); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 797 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 798 | /* decremented by grpc_server_destroy */ |
| 799 | gpr_ref_init(&server->internal_refcount, 1); |
| 800 | server->root_channel_data.next = server->root_channel_data.prev = |
| 801 | &server->root_channel_data; |
| 802 | |
Craig Tiller | 6a006ce | 2015-07-13 16:25:40 -0700 | [diff] [blame] | 803 | /* TODO(ctiller): expose a channel_arg for this */ |
| 804 | server->max_requested_calls = 32768; |
| 805 | server->request_freelist = |
| 806 | gpr_stack_lockfree_create(server->max_requested_calls); |
| 807 | for (i = 0; i < (size_t)server->max_requested_calls; i++) { |
Craig Tiller | f96dfc3 | 2015-09-10 14:43:18 -0700 | [diff] [blame^] | 808 | gpr_stack_lockfree_push(server->request_freelist, (int)i); |
Craig Tiller | 6a006ce | 2015-07-13 16:25:40 -0700 | [diff] [blame] | 809 | } |
| 810 | request_matcher_init(&server->unregistered_request_matcher, |
| 811 | server->max_requested_calls); |
| 812 | server->requested_calls = gpr_malloc(server->max_requested_calls * |
| 813 | sizeof(*server->requested_calls)); |
Craig Tiller | 729b35a | 2015-07-13 12:36:47 -0700 | [diff] [blame] | 814 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 815 | /* Server filter stack is: |
| 816 | |
| 817 | server_surface_filter - for making surface API calls |
| 818 | grpc_server_census_filter (optional) - for stats collection and tracing |
| 819 | {passed in filter stack} |
| 820 | grpc_connected_channel_filter - for interfacing with transports */ |
Craig Tiller | 32ca48c | 2015-09-10 11:47:15 -0700 | [diff] [blame] | 821 | server->channel_filter_count = filter_count + 1u + (census_enabled ? 1u : 0u); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 822 | server->channel_filters = |
| 823 | gpr_malloc(server->channel_filter_count * sizeof(grpc_channel_filter *)); |
| 824 | server->channel_filters[0] = &server_surface_filter; |
| 825 | if (census_enabled) { |
| 826 | server->channel_filters[1] = &grpc_server_census_filter; |
Hongyu Chen | f68e472 | 2015-08-07 18:06:42 -0700 | [diff] [blame] | 827 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 828 | for (i = 0; i < filter_count; i++) { |
Craig Tiller | 32ca48c | 2015-09-10 11:47:15 -0700 | [diff] [blame] | 829 | server->channel_filters[i + 1u + (census_enabled ? 1u : 0u)] = filters[i]; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 830 | } |
| 831 | |
| 832 | server->channel_args = grpc_channel_args_copy(args); |
| 833 | |
| 834 | return server; |
| 835 | } |
| 836 | |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 837 | static int streq(const char *a, const char *b) { |
| 838 | if (a == NULL && b == NULL) return 1; |
| 839 | if (a == NULL) return 0; |
| 840 | if (b == NULL) return 0; |
| 841 | return 0 == strcmp(a, b); |
| 842 | } |
| 843 | |
| 844 | void *grpc_server_register_method(grpc_server *server, const char *method, |
Craig Tiller | f9e6adf | 2015-05-06 11:45:59 -0700 | [diff] [blame] | 845 | const char *host) { |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 846 | registered_method *m; |
| 847 | if (!method) { |
Craig Tiller | b76d05b | 2015-05-29 17:21:56 -0700 | [diff] [blame] | 848 | gpr_log(GPR_ERROR, |
| 849 | "grpc_server_register_method method string cannot be NULL"); |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 850 | return NULL; |
| 851 | } |
| 852 | for (m = server->registered_methods; m; m = m->next) { |
| 853 | if (streq(m->method, method) && streq(m->host, host)) { |
| 854 | gpr_log(GPR_ERROR, "duplicate registration for %s@%s", method, |
| 855 | host ? host : "*"); |
| 856 | return NULL; |
| 857 | } |
| 858 | } |
| 859 | m = gpr_malloc(sizeof(registered_method)); |
| 860 | memset(m, 0, sizeof(*m)); |
Craig Tiller | 6a006ce | 2015-07-13 16:25:40 -0700 | [diff] [blame] | 861 | request_matcher_init(&m->request_matcher, server->max_requested_calls); |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 862 | m->method = gpr_strdup(method); |
| 863 | m->host = gpr_strdup(host); |
| 864 | m->next = server->registered_methods; |
| 865 | server->registered_methods = m; |
| 866 | return m; |
| 867 | } |
| 868 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 869 | void grpc_server_start(grpc_server *server) { |
| 870 | listener *l; |
Craig Tiller | 20bc56d | 2015-02-12 09:02:56 -0800 | [diff] [blame] | 871 | size_t i; |
| 872 | |
Craig Tiller | ec3257c | 2015-02-12 15:59:43 -0800 | [diff] [blame] | 873 | server->pollsets = gpr_malloc(sizeof(grpc_pollset *) * server->cq_count); |
Craig Tiller | 20bc56d | 2015-02-12 09:02:56 -0800 | [diff] [blame] | 874 | for (i = 0; i < server->cq_count; i++) { |
| 875 | server->pollsets[i] = grpc_cq_pollset(server->cqs[i]); |
| 876 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 877 | |
| 878 | for (l = server->listeners; l; l = l->next) { |
Craig Tiller | 20bc56d | 2015-02-12 09:02:56 -0800 | [diff] [blame] | 879 | l->start(server, l->arg, server->pollsets, server->cq_count); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 880 | } |
| 881 | } |
| 882 | |
Craig Tiller | 1064f8b | 2015-06-25 13:52:57 -0700 | [diff] [blame] | 883 | void grpc_server_setup_transport(grpc_server *s, grpc_transport *transport, |
| 884 | grpc_channel_filter const **extra_filters, |
| 885 | size_t num_extra_filters, grpc_mdctx *mdctx, |
| 886 | const grpc_channel_args *args) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 887 | size_t num_filters = s->channel_filter_count + num_extra_filters + 1; |
| 888 | grpc_channel_filter const **filters = |
| 889 | gpr_malloc(sizeof(grpc_channel_filter *) * num_filters); |
| 890 | size_t i; |
Craig Tiller | 04cc8be | 2015-02-10 16:11:22 -0800 | [diff] [blame] | 891 | size_t num_registered_methods; |
| 892 | size_t alloc; |
| 893 | registered_method *rm; |
| 894 | channel_registered_method *crm; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 895 | grpc_channel *channel; |
| 896 | channel_data *chand; |
Craig Tiller | 04cc8be | 2015-02-10 16:11:22 -0800 | [diff] [blame] | 897 | grpc_mdstr *host; |
| 898 | grpc_mdstr *method; |
| 899 | gpr_uint32 hash; |
Craig Tiller | f96dfc3 | 2015-09-10 14:43:18 -0700 | [diff] [blame^] | 900 | size_t slots; |
Craig Tiller | 04cc8be | 2015-02-10 16:11:22 -0800 | [diff] [blame] | 901 | gpr_uint32 probes; |
| 902 | gpr_uint32 max_probes = 0; |
Craig Tiller | e039f03 | 2015-06-25 12:54:23 -0700 | [diff] [blame] | 903 | grpc_transport_op op; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 904 | |
| 905 | for (i = 0; i < s->channel_filter_count; i++) { |
| 906 | filters[i] = s->channel_filters[i]; |
| 907 | } |
| 908 | for (; i < s->channel_filter_count + num_extra_filters; i++) { |
| 909 | filters[i] = extra_filters[i - s->channel_filter_count]; |
| 910 | } |
| 911 | filters[i] = &grpc_connected_channel_filter; |
| 912 | |
Craig Tiller | 20bc56d | 2015-02-12 09:02:56 -0800 | [diff] [blame] | 913 | for (i = 0; i < s->cq_count; i++) { |
Craig Tiller | e039f03 | 2015-06-25 12:54:23 -0700 | [diff] [blame] | 914 | memset(&op, 0, sizeof(op)); |
| 915 | op.bind_pollset = grpc_cq_pollset(s->cqs[i]); |
| 916 | grpc_transport_perform_op(transport, &op); |
Craig Tiller | 20bc56d | 2015-02-12 09:02:56 -0800 | [diff] [blame] | 917 | } |
ctiller | d79b486 | 2014-12-17 16:36:59 -0800 | [diff] [blame] | 918 | |
Craig Tiller | 1b22b9d | 2015-07-20 13:42:22 -0700 | [diff] [blame] | 919 | channel = grpc_channel_create_from_filters(NULL, filters, num_filters, args, |
| 920 | mdctx, 0); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 921 | chand = (channel_data *)grpc_channel_stack_element( |
Craig Tiller | c02c1d8 | 2015-04-07 16:21:55 -0700 | [diff] [blame] | 922 | grpc_channel_get_channel_stack(channel), 0) |
| 923 | ->channel_data; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 924 | chand->server = s; |
| 925 | server_ref(s); |
| 926 | chand->channel = channel; |
| 927 | |
Craig Tiller | 04cc8be | 2015-02-10 16:11:22 -0800 | [diff] [blame] | 928 | num_registered_methods = 0; |
| 929 | for (rm = s->registered_methods; rm; rm = rm->next) { |
| 930 | num_registered_methods++; |
| 931 | } |
| 932 | /* build a lookup table phrased in terms of mdstr's in this channels context |
| 933 | to quickly find registered methods */ |
| 934 | if (num_registered_methods > 0) { |
| 935 | slots = 2 * num_registered_methods; |
| 936 | alloc = sizeof(channel_registered_method) * slots; |
| 937 | chand->registered_methods = gpr_malloc(alloc); |
| 938 | memset(chand->registered_methods, 0, alloc); |
| 939 | for (rm = s->registered_methods; rm; rm = rm->next) { |
Craig Tiller | 6999c09 | 2015-07-22 08:14:12 -0700 | [diff] [blame] | 940 | host = rm->host ? grpc_mdstr_from_string(mdctx, rm->host, 0) : NULL; |
| 941 | method = grpc_mdstr_from_string(mdctx, rm->method, 0); |
Craig Tiller | 04cc8be | 2015-02-10 16:11:22 -0800 | [diff] [blame] | 942 | hash = GRPC_MDSTR_KV_HASH(host ? host->hash : 0, method->hash); |
Craig Tiller | 3b29b56 | 2015-02-11 12:58:46 -0800 | [diff] [blame] | 943 | for (probes = 0; chand->registered_methods[(hash + probes) % slots] |
Craig Tiller | c02c1d8 | 2015-04-07 16:21:55 -0700 | [diff] [blame] | 944 | .server_registered_method != NULL; |
Craig Tiller | 3b29b56 | 2015-02-11 12:58:46 -0800 | [diff] [blame] | 945 | probes++) |
| 946 | ; |
Craig Tiller | 04cc8be | 2015-02-10 16:11:22 -0800 | [diff] [blame] | 947 | if (probes > max_probes) max_probes = probes; |
| 948 | crm = &chand->registered_methods[(hash + probes) % slots]; |
| 949 | crm->server_registered_method = rm; |
| 950 | crm->host = host; |
| 951 | crm->method = method; |
| 952 | } |
Craig Tiller | f96dfc3 | 2015-09-10 14:43:18 -0700 | [diff] [blame^] | 953 | GPR_ASSERT(slots <= GPR_UINT32_MAX); |
| 954 | chand->registered_method_slots = (gpr_uint32)slots; |
Craig Tiller | 04cc8be | 2015-02-10 16:11:22 -0800 | [diff] [blame] | 955 | chand->registered_method_max_probes = max_probes; |
| 956 | } |
| 957 | |
Craig Tiller | 1064f8b | 2015-06-25 13:52:57 -0700 | [diff] [blame] | 958 | grpc_connected_channel_bind_transport(grpc_channel_get_channel_stack(channel), |
| 959 | transport); |
Craig Tiller | 7bd5ab1 | 2015-02-17 22:29:04 -0800 | [diff] [blame] | 960 | |
Vijay Pai | 8931cdd | 2015-06-17 12:42:17 -0700 | [diff] [blame] | 961 | gpr_mu_lock(&s->mu_global); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 962 | chand->next = &s->root_channel_data; |
| 963 | chand->prev = chand->next->prev; |
| 964 | chand->next->prev = chand->prev->next = chand; |
Vijay Pai | 8931cdd | 2015-06-17 12:42:17 -0700 | [diff] [blame] | 965 | gpr_mu_unlock(&s->mu_global); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 966 | |
| 967 | gpr_free(filters); |
Craig Tiller | 4b80410 | 2015-06-26 16:16:12 -0700 | [diff] [blame] | 968 | |
| 969 | GRPC_CHANNEL_INTERNAL_REF(channel, "connectivity"); |
| 970 | memset(&op, 0, sizeof(op)); |
| 971 | op.set_accept_stream = accept_stream; |
| 972 | op.set_accept_stream_user_data = chand; |
| 973 | op.on_connectivity_state_change = &chand->channel_connectivity_changed; |
| 974 | op.connectivity_state = &chand->connectivity_state; |
Craig Tiller | f96dfc3 | 2015-09-10 14:43:18 -0700 | [diff] [blame^] | 975 | op.disconnect = gpr_atm_acq_load(&s->shutdown_flag) != 0; |
Craig Tiller | 4b80410 | 2015-06-26 16:16:12 -0700 | [diff] [blame] | 976 | grpc_transport_perform_op(transport, &op); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 977 | } |
| 978 | |
murgatroid99 | 00a3dab | 2015-08-19 11:15:38 -0700 | [diff] [blame] | 979 | void done_published_shutdown(void *done_arg, grpc_cq_completion *storage) { |
| 980 | (void) done_arg; |
| 981 | gpr_free(storage); |
| 982 | } |
| 983 | |
Craig Tiller | bce999f | 2015-05-27 09:55:51 -0700 | [diff] [blame] | 984 | void grpc_server_shutdown_and_notify(grpc_server *server, |
| 985 | grpc_completion_queue *cq, void *tag) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 986 | listener *l; |
Craig Tiller | bce999f | 2015-05-27 09:55:51 -0700 | [diff] [blame] | 987 | shutdown_tag *sdt; |
Craig Tiller | ff3ae68 | 2015-06-29 17:44:04 -0700 | [diff] [blame] | 988 | channel_broadcaster broadcaster; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 989 | |
Craig Tiller | 7156fca | 2015-07-15 13:54:20 -0700 | [diff] [blame] | 990 | GRPC_SERVER_LOG_SHUTDOWN(GPR_INFO, server, cq, tag); |
| 991 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 992 | /* lock, and gather up some stuff to do */ |
Vijay Pai | 8931cdd | 2015-06-17 12:42:17 -0700 | [diff] [blame] | 993 | gpr_mu_lock(&server->mu_global); |
Craig Tiller | 97fc6a3 | 2015-07-08 15:31:35 -0700 | [diff] [blame] | 994 | grpc_cq_begin_op(cq); |
murgatroid99 | 00a3dab | 2015-08-19 11:15:38 -0700 | [diff] [blame] | 995 | if (server->shutdown_published) { |
| 996 | grpc_cq_end_op(cq, tag, 1, done_published_shutdown, NULL, |
| 997 | gpr_malloc(sizeof(grpc_cq_completion))); |
| 998 | gpr_mu_unlock(&server->mu_global); |
| 999 | return; |
| 1000 | } |
Craig Tiller | ee945e8 | 2015-05-26 16:15:34 -0700 | [diff] [blame] | 1001 | server->shutdown_tags = |
| 1002 | gpr_realloc(server->shutdown_tags, |
Craig Tiller | 208d212 | 2015-05-29 08:50:08 -0700 | [diff] [blame] | 1003 | sizeof(shutdown_tag) * (server->num_shutdown_tags + 1)); |
Craig Tiller | bce999f | 2015-05-27 09:55:51 -0700 | [diff] [blame] | 1004 | sdt = &server->shutdown_tags[server->num_shutdown_tags++]; |
| 1005 | sdt->tag = tag; |
| 1006 | sdt->cq = cq; |
Craig Tiller | 6a006ce | 2015-07-13 16:25:40 -0700 | [diff] [blame] | 1007 | if (gpr_atm_acq_load(&server->shutdown_flag)) { |
Vijay Pai | 8931cdd | 2015-06-17 12:42:17 -0700 | [diff] [blame] | 1008 | gpr_mu_unlock(&server->mu_global); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1009 | return; |
| 1010 | } |
| 1011 | |
Craig Tiller | 080d6c5 | 2015-07-10 10:23:10 -0700 | [diff] [blame] | 1012 | server->last_shutdown_message_time = gpr_now(GPR_CLOCK_REALTIME); |
Craig Tiller | ab54f79 | 2015-07-08 08:34:20 -0700 | [diff] [blame] | 1013 | |
Craig Tiller | ff3ae68 | 2015-06-29 17:44:04 -0700 | [diff] [blame] | 1014 | channel_broadcaster_init(server, &broadcaster); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 1015 | |
Craig Tiller | bd21757 | 2015-02-11 18:10:56 -0800 | [diff] [blame] | 1016 | /* collect all unregistered then registered calls */ |
Vijay Pai | 8931cdd | 2015-06-17 12:42:17 -0700 | [diff] [blame] | 1017 | gpr_mu_lock(&server->mu_call); |
Craig Tiller | 1191e21 | 2015-07-30 14:49:02 -0700 | [diff] [blame] | 1018 | kill_pending_work_locked(server); |
Vijay Pai | 8931cdd | 2015-06-17 12:42:17 -0700 | [diff] [blame] | 1019 | gpr_mu_unlock(&server->mu_call); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1020 | |
Craig Tiller | 6a006ce | 2015-07-13 16:25:40 -0700 | [diff] [blame] | 1021 | gpr_atm_rel_store(&server->shutdown_flag, 1); |
Craig Tiller | dc62772 | 2015-05-26 15:27:02 -0700 | [diff] [blame] | 1022 | maybe_finish_shutdown(server); |
Vijay Pai | 8931cdd | 2015-06-17 12:42:17 -0700 | [diff] [blame] | 1023 | gpr_mu_unlock(&server->mu_global); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1024 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1025 | /* Shutdown listeners */ |
| 1026 | for (l = server->listeners; l; l = l->next) { |
| 1027 | l->destroy(server, l->arg); |
| 1028 | } |
Craig Tiller | ff3ae68 | 2015-06-29 17:44:04 -0700 | [diff] [blame] | 1029 | |
| 1030 | channel_broadcaster_shutdown(&broadcaster, 1, 0); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1031 | } |
| 1032 | |
Craig Tiller | aec96aa | 2015-04-07 14:32:15 -0700 | [diff] [blame] | 1033 | void grpc_server_listener_destroy_done(void *s) { |
| 1034 | grpc_server *server = s; |
Vijay Pai | 8931cdd | 2015-06-17 12:42:17 -0700 | [diff] [blame] | 1035 | gpr_mu_lock(&server->mu_global); |
Craig Tiller | aec96aa | 2015-04-07 14:32:15 -0700 | [diff] [blame] | 1036 | server->listeners_destroyed++; |
Craig Tiller | ee945e8 | 2015-05-26 16:15:34 -0700 | [diff] [blame] | 1037 | maybe_finish_shutdown(server); |
Vijay Pai | 8931cdd | 2015-06-17 12:42:17 -0700 | [diff] [blame] | 1038 | gpr_mu_unlock(&server->mu_global); |
Craig Tiller | aec96aa | 2015-04-07 14:32:15 -0700 | [diff] [blame] | 1039 | } |
| 1040 | |
Craig Tiller | afa2d63 | 2015-05-26 16:39:13 -0700 | [diff] [blame] | 1041 | void grpc_server_cancel_all_calls(grpc_server *server) { |
Craig Tiller | 092d8d1 | 2015-07-04 22:35:00 -0700 | [diff] [blame] | 1042 | channel_broadcaster broadcaster; |
Craig Tiller | afa2d63 | 2015-05-26 16:39:13 -0700 | [diff] [blame] | 1043 | |
Craig Tiller | 092d8d1 | 2015-07-04 22:35:00 -0700 | [diff] [blame] | 1044 | gpr_mu_lock(&server->mu_global); |
| 1045 | channel_broadcaster_init(server, &broadcaster); |
| 1046 | gpr_mu_unlock(&server->mu_global); |
Craig Tiller | afa2d63 | 2015-05-26 16:39:13 -0700 | [diff] [blame] | 1047 | |
Craig Tiller | 092d8d1 | 2015-07-04 22:35:00 -0700 | [diff] [blame] | 1048 | channel_broadcaster_shutdown(&broadcaster, 0, 1); |
Craig Tiller | afa2d63 | 2015-05-26 16:39:13 -0700 | [diff] [blame] | 1049 | } |
| 1050 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1051 | void grpc_server_destroy(grpc_server *server) { |
Craig Tiller | aec96aa | 2015-04-07 14:32:15 -0700 | [diff] [blame] | 1052 | listener *l; |
Craig Tiller | 872af02 | 2015-04-24 15:57:52 -0700 | [diff] [blame] | 1053 | |
Vijay Pai | 8931cdd | 2015-06-17 12:42:17 -0700 | [diff] [blame] | 1054 | gpr_mu_lock(&server->mu_global); |
Craig Tiller | 6a006ce | 2015-07-13 16:25:40 -0700 | [diff] [blame] | 1055 | GPR_ASSERT(gpr_atm_acq_load(&server->shutdown_flag) || !server->listeners); |
Craig Tiller | ee945e8 | 2015-05-26 16:15:34 -0700 | [diff] [blame] | 1056 | GPR_ASSERT(server->listeners_destroyed == num_listeners(server)); |
Craig Tiller | aec96aa | 2015-04-07 14:32:15 -0700 | [diff] [blame] | 1057 | |
| 1058 | while (server->listeners) { |
| 1059 | l = server->listeners; |
| 1060 | server->listeners = l->next; |
| 1061 | gpr_free(l); |
| 1062 | } |
| 1063 | |
Vijay Pai | 8931cdd | 2015-06-17 12:42:17 -0700 | [diff] [blame] | 1064 | gpr_mu_unlock(&server->mu_global); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1065 | |
| 1066 | server_unref(server); |
| 1067 | } |
| 1068 | |
| 1069 | void grpc_server_add_listener(grpc_server *server, void *arg, |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 1070 | void (*start)(grpc_server *server, void *arg, |
Craig Tiller | ec3257c | 2015-02-12 15:59:43 -0800 | [diff] [blame] | 1071 | grpc_pollset **pollsets, |
| 1072 | size_t pollset_count), |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1073 | void (*destroy)(grpc_server *server, void *arg)) { |
| 1074 | listener *l = gpr_malloc(sizeof(listener)); |
| 1075 | l->arg = arg; |
| 1076 | l->start = start; |
| 1077 | l->destroy = destroy; |
| 1078 | l->next = server->listeners; |
| 1079 | server->listeners = l; |
| 1080 | } |
| 1081 | |
Craig Tiller | 9f28ac2 | 2015-01-27 17:01:29 -0800 | [diff] [blame] | 1082 | static grpc_call_error queue_call_request(grpc_server *server, |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 1083 | requested_call *rc) { |
Yang Gao | eb8e7cd | 2015-02-11 11:43:40 -0800 | [diff] [blame] | 1084 | call_data *calld = NULL; |
Craig Tiller | 729b35a | 2015-07-13 12:36:47 -0700 | [diff] [blame] | 1085 | request_matcher *request_matcher = NULL; |
Craig Tiller | 6a006ce | 2015-07-13 16:25:40 -0700 | [diff] [blame] | 1086 | int request_id; |
| 1087 | if (gpr_atm_acq_load(&server->shutdown_flag)) { |
| 1088 | fail_call(server, rc); |
| 1089 | return GRPC_CALL_OK; |
| 1090 | } |
| 1091 | request_id = gpr_stack_lockfree_pop(server->request_freelist); |
| 1092 | if (request_id == -1) { |
| 1093 | /* out of request ids: just fail this one */ |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 1094 | fail_call(server, rc); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1095 | return GRPC_CALL_OK; |
| 1096 | } |
Craig Tiller | 04cc8be | 2015-02-10 16:11:22 -0800 | [diff] [blame] | 1097 | switch (rc->type) { |
Craig Tiller | 04cc8be | 2015-02-10 16:11:22 -0800 | [diff] [blame] | 1098 | case BATCH_CALL: |
Craig Tiller | 729b35a | 2015-07-13 12:36:47 -0700 | [diff] [blame] | 1099 | request_matcher = &server->unregistered_request_matcher; |
Craig Tiller | 04cc8be | 2015-02-10 16:11:22 -0800 | [diff] [blame] | 1100 | break; |
| 1101 | case REGISTERED_CALL: |
Craig Tiller | 729b35a | 2015-07-13 12:36:47 -0700 | [diff] [blame] | 1102 | request_matcher = &rc->data.registered.registered_method->request_matcher; |
Craig Tiller | 04cc8be | 2015-02-10 16:11:22 -0800 | [diff] [blame] | 1103 | break; |
| 1104 | } |
Craig Tiller | 6a006ce | 2015-07-13 16:25:40 -0700 | [diff] [blame] | 1105 | server->requested_calls[request_id] = *rc; |
| 1106 | gpr_free(rc); |
| 1107 | if (gpr_stack_lockfree_push(request_matcher->requests, request_id)) { |
| 1108 | /* this was the first queued request: we need to lock and start |
| 1109 | matching calls */ |
| 1110 | gpr_mu_lock(&server->mu_call); |
| 1111 | while ((calld = request_matcher->pending_head) != NULL) { |
| 1112 | request_id = gpr_stack_lockfree_pop(request_matcher->requests); |
| 1113 | if (request_id == -1) break; |
| 1114 | request_matcher->pending_head = calld->pending_next; |
| 1115 | gpr_mu_unlock(&server->mu_call); |
| 1116 | gpr_mu_lock(&calld->mu_state); |
| 1117 | if (calld->state == ZOMBIED) { |
| 1118 | gpr_mu_unlock(&calld->mu_state); |
| 1119 | grpc_iomgr_closure_init( |
| 1120 | &calld->kill_zombie_closure, kill_zombie, |
| 1121 | grpc_call_stack_element(grpc_call_get_call_stack(calld->call), 0)); |
| 1122 | grpc_iomgr_add_callback(&calld->kill_zombie_closure); |
| 1123 | } else { |
| 1124 | GPR_ASSERT(calld->state == PENDING); |
| 1125 | calld->state = ACTIVATED; |
| 1126 | gpr_mu_unlock(&calld->mu_state); |
Craig Tiller | b645026 | 2015-07-14 07:12:19 -0700 | [diff] [blame] | 1127 | begin_call(server, calld, &server->requested_calls[request_id]); |
Craig Tiller | 6a006ce | 2015-07-13 16:25:40 -0700 | [diff] [blame] | 1128 | } |
| 1129 | gpr_mu_lock(&server->mu_call); |
Craig Tiller | 729b35a | 2015-07-13 12:36:47 -0700 | [diff] [blame] | 1130 | } |
Craig Tiller | 76d2c3b | 2015-07-07 11:46:01 -0700 | [diff] [blame] | 1131 | gpr_mu_unlock(&server->mu_call); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1132 | } |
Craig Tiller | 6a006ce | 2015-07-13 16:25:40 -0700 | [diff] [blame] | 1133 | return GRPC_CALL_OK; |
Craig Tiller | cce17ac | 2015-01-20 09:29:28 -0800 | [diff] [blame] | 1134 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1135 | |
Craig Tiller | f9e6adf | 2015-05-06 11:45:59 -0700 | [diff] [blame] | 1136 | grpc_call_error grpc_server_request_call( |
| 1137 | grpc_server *server, grpc_call **call, grpc_call_details *details, |
| 1138 | grpc_metadata_array *initial_metadata, |
| 1139 | grpc_completion_queue *cq_bound_to_call, |
| 1140 | grpc_completion_queue *cq_for_notification, void *tag) { |
Craig Tiller | 97fc6a3 | 2015-07-08 15:31:35 -0700 | [diff] [blame] | 1141 | requested_call *rc = gpr_malloc(sizeof(*rc)); |
murgatroid99 | ad7c20c | 2015-05-22 14:42:29 -0700 | [diff] [blame] | 1142 | GRPC_SERVER_LOG_REQUEST_CALL(GPR_INFO, server, call, details, |
| 1143 | initial_metadata, cq_bound_to_call, |
| 1144 | cq_for_notification, tag); |
Craig Tiller | b56975c | 2015-06-15 10:11:16 -0700 | [diff] [blame] | 1145 | if (!grpc_cq_is_server_cq(cq_for_notification)) { |
Craig Tiller | 97fc6a3 | 2015-07-08 15:31:35 -0700 | [diff] [blame] | 1146 | gpr_free(rc); |
Craig Tiller | b56975c | 2015-06-15 10:11:16 -0700 | [diff] [blame] | 1147 | return GRPC_CALL_ERROR_NOT_SERVER_COMPLETION_QUEUE; |
| 1148 | } |
Craig Tiller | 97fc6a3 | 2015-07-08 15:31:35 -0700 | [diff] [blame] | 1149 | grpc_cq_begin_op(cq_for_notification); |
Craig Tiller | 9928d39 | 2015-08-18 09:40:24 -0700 | [diff] [blame] | 1150 | details->reserved = NULL; |
Craig Tiller | 97fc6a3 | 2015-07-08 15:31:35 -0700 | [diff] [blame] | 1151 | rc->type = BATCH_CALL; |
Craig Tiller | 6a006ce | 2015-07-13 16:25:40 -0700 | [diff] [blame] | 1152 | rc->server = server; |
Craig Tiller | 97fc6a3 | 2015-07-08 15:31:35 -0700 | [diff] [blame] | 1153 | rc->tag = tag; |
| 1154 | rc->cq_bound_to_call = cq_bound_to_call; |
| 1155 | rc->cq_for_notification = cq_for_notification; |
| 1156 | rc->call = call; |
| 1157 | rc->data.batch.details = details; |
| 1158 | rc->data.batch.initial_metadata = initial_metadata; |
| 1159 | return queue_call_request(server, rc); |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 1160 | } |
| 1161 | |
| 1162 | grpc_call_error grpc_server_request_registered_call( |
Craig Tiller | ec3257c | 2015-02-12 15:59:43 -0800 | [diff] [blame] | 1163 | grpc_server *server, void *rm, grpc_call **call, gpr_timespec *deadline, |
| 1164 | grpc_metadata_array *initial_metadata, grpc_byte_buffer **optional_payload, |
Craig Tiller | f9e6adf | 2015-05-06 11:45:59 -0700 | [diff] [blame] | 1165 | grpc_completion_queue *cq_bound_to_call, |
| 1166 | grpc_completion_queue *cq_for_notification, void *tag) { |
Craig Tiller | 97fc6a3 | 2015-07-08 15:31:35 -0700 | [diff] [blame] | 1167 | requested_call *rc = gpr_malloc(sizeof(*rc)); |
Craig Tiller | 20bc56d | 2015-02-12 09:02:56 -0800 | [diff] [blame] | 1168 | registered_method *registered_method = rm; |
Craig Tiller | b56975c | 2015-06-15 10:11:16 -0700 | [diff] [blame] | 1169 | if (!grpc_cq_is_server_cq(cq_for_notification)) { |
Craig Tiller | 97fc6a3 | 2015-07-08 15:31:35 -0700 | [diff] [blame] | 1170 | gpr_free(rc); |
Craig Tiller | b56975c | 2015-06-15 10:11:16 -0700 | [diff] [blame] | 1171 | return GRPC_CALL_ERROR_NOT_SERVER_COMPLETION_QUEUE; |
| 1172 | } |
Craig Tiller | 97fc6a3 | 2015-07-08 15:31:35 -0700 | [diff] [blame] | 1173 | grpc_cq_begin_op(cq_for_notification); |
| 1174 | rc->type = REGISTERED_CALL; |
Craig Tiller | 6a006ce | 2015-07-13 16:25:40 -0700 | [diff] [blame] | 1175 | rc->server = server; |
Craig Tiller | 97fc6a3 | 2015-07-08 15:31:35 -0700 | [diff] [blame] | 1176 | rc->tag = tag; |
| 1177 | rc->cq_bound_to_call = cq_bound_to_call; |
| 1178 | rc->cq_for_notification = cq_for_notification; |
| 1179 | rc->call = call; |
| 1180 | rc->data.registered.registered_method = registered_method; |
| 1181 | rc->data.registered.deadline = deadline; |
| 1182 | rc->data.registered.initial_metadata = initial_metadata; |
| 1183 | rc->data.registered.optional_payload = optional_payload; |
| 1184 | return queue_call_request(server, rc); |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 1185 | } |
| 1186 | |
Craig Tiller | 64be9f7 | 2015-05-04 14:53:51 -0700 | [diff] [blame] | 1187 | static void publish_registered_or_batch(grpc_call *call, int success, |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 1188 | void *tag); |
Craig Tiller | 64be9f7 | 2015-05-04 14:53:51 -0700 | [diff] [blame] | 1189 | static void publish_was_not_set(grpc_call *call, int success, void *tag) { |
Yang Gao | eb8e7cd | 2015-02-11 11:43:40 -0800 | [diff] [blame] | 1190 | abort(); |
| 1191 | } |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 1192 | |
Craig Tiller | 166e250 | 2015-02-03 20:14:41 -0800 | [diff] [blame] | 1193 | static void cpstr(char **dest, size_t *capacity, grpc_mdstr *value) { |
| 1194 | gpr_slice slice = value->slice; |
| 1195 | size_t len = GPR_SLICE_LENGTH(slice); |
| 1196 | |
| 1197 | if (len + 1 > *capacity) { |
| 1198 | *capacity = GPR_MAX(len + 1, *capacity * 2); |
| 1199 | *dest = gpr_realloc(*dest, *capacity); |
| 1200 | } |
| 1201 | memcpy(*dest, grpc_mdstr_as_c_string(value), len + 1); |
| 1202 | } |
| 1203 | |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 1204 | static void begin_call(grpc_server *server, call_data *calld, |
| 1205 | requested_call *rc) { |
Yang Gao | eb8e7cd | 2015-02-11 11:43:40 -0800 | [diff] [blame] | 1206 | grpc_ioreq_completion_func publish = publish_was_not_set; |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 1207 | grpc_ioreq req[2]; |
| 1208 | grpc_ioreq *r = req; |
| 1209 | |
| 1210 | /* called once initial metadata has been read by the call, but BEFORE |
| 1211 | the ioreq to fetch it out of the call has been executed. |
| 1212 | This means metadata related fields can be relied on in calld, but to |
| 1213 | fill in the metadata array passed by the client, we need to perform |
| 1214 | an ioreq op, that should complete immediately. */ |
| 1215 | |
Craig Tiller | f9e6adf | 2015-05-06 11:45:59 -0700 | [diff] [blame] | 1216 | grpc_call_set_completion_queue(calld->call, rc->cq_bound_to_call); |
| 1217 | *rc->call = calld->call; |
| 1218 | calld->cq_new = rc->cq_for_notification; |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 1219 | switch (rc->type) { |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 1220 | case BATCH_CALL: |
Craig Tiller | 04c5d4b | 2015-06-26 17:21:41 -0700 | [diff] [blame] | 1221 | GPR_ASSERT(calld->host != NULL); |
| 1222 | GPR_ASSERT(calld->path != NULL); |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 1223 | cpstr(&rc->data.batch.details->host, |
| 1224 | &rc->data.batch.details->host_capacity, calld->host); |
| 1225 | cpstr(&rc->data.batch.details->method, |
| 1226 | &rc->data.batch.details->method_capacity, calld->path); |
Masood Malekghassemi | bf177c8 | 2015-04-27 12:14:38 -0700 | [diff] [blame] | 1227 | rc->data.batch.details->deadline = calld->deadline; |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 1228 | r->op = GRPC_IOREQ_RECV_INITIAL_METADATA; |
| 1229 | r->data.recv_metadata = rc->data.batch.initial_metadata; |
David Garcia Quintas | b8f5450 | 2015-06-15 16:19:10 -0700 | [diff] [blame] | 1230 | r->flags = 0; |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 1231 | r++; |
| 1232 | publish = publish_registered_or_batch; |
| 1233 | break; |
| 1234 | case REGISTERED_CALL: |
| 1235 | *rc->data.registered.deadline = calld->deadline; |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 1236 | r->op = GRPC_IOREQ_RECV_INITIAL_METADATA; |
| 1237 | r->data.recv_metadata = rc->data.registered.initial_metadata; |
David Garcia Quintas | b8f5450 | 2015-06-15 16:19:10 -0700 | [diff] [blame] | 1238 | r->flags = 0; |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 1239 | r++; |
| 1240 | if (rc->data.registered.optional_payload) { |
| 1241 | r->op = GRPC_IOREQ_RECV_MESSAGE; |
| 1242 | r->data.recv_message = rc->data.registered.optional_payload; |
David Garcia Quintas | b8f5450 | 2015-06-15 16:19:10 -0700 | [diff] [blame] | 1243 | r->flags = 0; |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 1244 | r++; |
| 1245 | } |
| 1246 | publish = publish_registered_or_batch; |
| 1247 | break; |
| 1248 | } |
| 1249 | |
Craig Tiller | 4df412b | 2015-04-28 07:57:54 -0700 | [diff] [blame] | 1250 | GRPC_CALL_INTERNAL_REF(calld->call, "server"); |
Craig Tiller | 32ca48c | 2015-09-10 11:47:15 -0700 | [diff] [blame] | 1251 | grpc_call_start_ioreq_and_call_back(calld->call, req, (size_t)(r - req), |
| 1252 | publish, rc); |
Craig Tiller | 97fc6a3 | 2015-07-08 15:31:35 -0700 | [diff] [blame] | 1253 | } |
| 1254 | |
| 1255 | static void done_request_event(void *req, grpc_cq_completion *c) { |
Craig Tiller | 6a006ce | 2015-07-13 16:25:40 -0700 | [diff] [blame] | 1256 | requested_call *rc = req; |
| 1257 | grpc_server *server = rc->server; |
| 1258 | |
| 1259 | if (rc >= server->requested_calls && |
| 1260 | rc < server->requested_calls + server->max_requested_calls) { |
Craig Tiller | f96dfc3 | 2015-09-10 14:43:18 -0700 | [diff] [blame^] | 1261 | GPR_ASSERT(rc - server->requested_calls <= INT_MAX); |
Craig Tiller | 6a006ce | 2015-07-13 16:25:40 -0700 | [diff] [blame] | 1262 | gpr_stack_lockfree_push(server->request_freelist, |
Craig Tiller | f96dfc3 | 2015-09-10 14:43:18 -0700 | [diff] [blame^] | 1263 | (int)(rc - server->requested_calls)); |
Craig Tiller | 6a006ce | 2015-07-13 16:25:40 -0700 | [diff] [blame] | 1264 | } else { |
| 1265 | gpr_free(req); |
| 1266 | } |
Craig Tiller | e2b3bfa | 2015-07-30 10:40:22 -0700 | [diff] [blame] | 1267 | |
| 1268 | server_unref(server); |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 1269 | } |
| 1270 | |
| 1271 | static void fail_call(grpc_server *server, requested_call *rc) { |
Craig Tiller | f9e6adf | 2015-05-06 11:45:59 -0700 | [diff] [blame] | 1272 | *rc->call = NULL; |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 1273 | switch (rc->type) { |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 1274 | case BATCH_CALL: |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 1275 | rc->data.batch.initial_metadata->count = 0; |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 1276 | break; |
| 1277 | case REGISTERED_CALL: |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 1278 | rc->data.registered.initial_metadata->count = 0; |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 1279 | break; |
| 1280 | } |
Craig Tiller | e2b3bfa | 2015-07-30 10:40:22 -0700 | [diff] [blame] | 1281 | server_ref(server); |
Craig Tiller | 12cf537 | 2015-07-09 13:48:11 -0700 | [diff] [blame] | 1282 | grpc_cq_end_op(rc->cq_for_notification, rc->tag, 0, done_request_event, rc, |
| 1283 | &rc->completion); |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 1284 | } |
| 1285 | |
Craig Tiller | 64be9f7 | 2015-05-04 14:53:51 -0700 | [diff] [blame] | 1286 | static void publish_registered_or_batch(grpc_call *call, int success, |
Craig Tiller | 97fc6a3 | 2015-07-08 15:31:35 -0700 | [diff] [blame] | 1287 | void *prc) { |
Craig Tiller | 8e8fd89 | 2015-02-10 17:02:08 -0800 | [diff] [blame] | 1288 | grpc_call_element *elem = |
| 1289 | grpc_call_stack_element(grpc_call_get_call_stack(call), 0); |
Craig Tiller | 97fc6a3 | 2015-07-08 15:31:35 -0700 | [diff] [blame] | 1290 | requested_call *rc = prc; |
Craig Tiller | 20bc56d | 2015-02-12 09:02:56 -0800 | [diff] [blame] | 1291 | call_data *calld = elem->call_data; |
Craig Tiller | e2b3bfa | 2015-07-30 10:40:22 -0700 | [diff] [blame] | 1292 | channel_data *chand = elem->channel_data; |
| 1293 | server_ref(chand->server); |
Craig Tiller | 12cf537 | 2015-07-09 13:48:11 -0700 | [diff] [blame] | 1294 | grpc_cq_end_op(calld->cq_new, rc->tag, success, done_request_event, rc, |
| 1295 | &rc->completion); |
Craig Tiller | c012258 | 2015-07-09 13:21:36 -0700 | [diff] [blame] | 1296 | GRPC_CALL_INTERNAL_UNREF(call, "server", 0); |
Craig Tiller | 24be0f7 | 2015-02-10 14:04:22 -0800 | [diff] [blame] | 1297 | } |
| 1298 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1299 | const grpc_channel_args *grpc_server_get_channel_args(grpc_server *server) { |
| 1300 | return server->channel_args; |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 1301 | } |
Craig Tiller | ba3c3cd | 2015-05-26 06:28:10 -0700 | [diff] [blame] | 1302 | |
| 1303 | int grpc_server_has_open_connections(grpc_server *server) { |
| 1304 | int r; |
Vijay Pai | 8931cdd | 2015-06-17 12:42:17 -0700 | [diff] [blame] | 1305 | gpr_mu_lock(&server->mu_global); |
Craig Tiller | ba3c3cd | 2015-05-26 06:28:10 -0700 | [diff] [blame] | 1306 | r = server->root_channel_data.next != &server->root_channel_data; |
Vijay Pai | 8931cdd | 2015-06-17 12:42:17 -0700 | [diff] [blame] | 1307 | gpr_mu_unlock(&server->mu_global); |
Craig Tiller | ba3c3cd | 2015-05-26 06:28:10 -0700 | [diff] [blame] | 1308 | return r; |
| 1309 | } |