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