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