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