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