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