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