Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
Craig Tiller | 6169d5f | 2016-03-31 07:46:18 -0700 | [diff] [blame] | 2 | * Copyright 2015, Google Inc. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions are |
| 7 | * met: |
| 8 | * |
| 9 | * * Redistributions of source code must retain the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer. |
| 11 | * * Redistributions in binary form must reproduce the above |
| 12 | * copyright notice, this list of conditions and the following disclaimer |
| 13 | * in the documentation and/or other materials provided with the |
| 14 | * distribution. |
| 15 | * * Neither the name of Google Inc. nor the names of its |
| 16 | * contributors may be used to endorse or promote products derived from |
| 17 | * this software without specific prior written permission. |
| 18 | * |
| 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | * |
| 31 | */ |
| 32 | |
| 33 | #include <grpc++/server.h> |
yang-g | 9e2f90c | 2015-08-21 15:35:03 -0700 | [diff] [blame] | 34 | |
Yuchen Zeng | a42ec21 | 2016-04-29 13:03:06 -0700 | [diff] [blame] | 35 | #include <sstream> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 36 | #include <utility> |
| 37 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 38 | #include <grpc++/completion_queue.h> |
yang-g | 9e2f90c | 2015-08-21 15:35:03 -0700 | [diff] [blame] | 39 | #include <grpc++/generic/async_generic_service.h> |
yang-g | 50993b7 | 2016-12-29 10:00:27 -0800 | [diff] [blame] | 40 | #include <grpc++/impl/codegen/async_unary_call.h> |
David Garcia Quintas | e1300de | 2016-01-27 18:41:26 -0800 | [diff] [blame] | 41 | #include <grpc++/impl/codegen/completion_queue_tag.h> |
| 42 | #include <grpc++/impl/grpc_library.h> |
Craig Tiller | 15f383c | 2016-01-07 12:45:32 -0800 | [diff] [blame] | 43 | #include <grpc++/impl/method_handler_impl.h> |
yangg | 1b15109 | 2015-01-09 15:31:05 -0800 | [diff] [blame] | 44 | #include <grpc++/impl/rpc_service_method.h> |
Yuchen Zeng | a42ec21 | 2016-04-29 13:03:06 -0700 | [diff] [blame] | 45 | #include <grpc++/impl/server_initializer.h> |
Craig Tiller | 8c8d0aa | 2015-02-12 11:38:36 -0800 | [diff] [blame] | 46 | #include <grpc++/impl/service_type.h> |
Julien Boeuf | 5be92a3 | 2015-08-28 16:28:18 -0700 | [diff] [blame] | 47 | #include <grpc++/security/server_credentials.h> |
David Garcia Quintas | e1300de | 2016-01-27 18:41:26 -0800 | [diff] [blame] | 48 | #include <grpc++/server_context.h> |
yang-g | 9e2f90c | 2015-08-21 15:35:03 -0700 | [diff] [blame] | 49 | #include <grpc++/support/time.h> |
David Garcia Quintas | e1300de | 2016-01-27 18:41:26 -0800 | [diff] [blame] | 50 | #include <grpc/grpc.h> |
| 51 | #include <grpc/support/alloc.h> |
| 52 | #include <grpc/support/log.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 53 | |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 54 | #include "src/core/lib/profiling/timers.h" |
yang-g | c3c475f | 2016-12-27 10:37:26 -0800 | [diff] [blame] | 55 | #include "src/cpp/server/health/default_health_check_service.h" |
Sree Kuchibhotla | f72ec6b | 2016-10-24 11:09:43 -0700 | [diff] [blame] | 56 | #include "src/cpp/thread_manager/thread_manager.h" |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 57 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 58 | namespace grpc { |
| 59 | |
Vijay Pai | c0b2acb | 2016-11-01 16:31:56 -0700 | [diff] [blame] | 60 | class DefaultGlobalCallbacks final : public Server::GlobalCallbacks { |
Craig Tiller | 7221d99 | 2015-11-24 06:59:33 -0800 | [diff] [blame] | 61 | public: |
Vijay Pai | c0b2acb | 2016-11-01 16:31:56 -0700 | [diff] [blame] | 62 | ~DefaultGlobalCallbacks() override {} |
| 63 | void PreSynchronousRequest(ServerContext* context) override {} |
| 64 | void PostSynchronousRequest(ServerContext* context) override {} |
Craig Tiller | 7221d99 | 2015-11-24 06:59:33 -0800 | [diff] [blame] | 65 | }; |
| 66 | |
Craig Tiller | 6461649 | 2016-01-26 14:16:20 -0800 | [diff] [blame] | 67 | static std::shared_ptr<Server::GlobalCallbacks> g_callbacks = nullptr; |
Craig Tiller | 8352b9e | 2015-12-02 11:43:40 -0800 | [diff] [blame] | 68 | static gpr_once g_once_init_callbacks = GPR_ONCE_INIT; |
| 69 | |
Craig Tiller | 8352b9e | 2015-12-02 11:43:40 -0800 | [diff] [blame] | 70 | static void InitGlobalCallbacks() { |
Vijay Pai | b645a2d | 2016-06-09 18:39:06 -0700 | [diff] [blame] | 71 | if (!g_callbacks) { |
Craig Tiller | 6461649 | 2016-01-26 14:16:20 -0800 | [diff] [blame] | 72 | g_callbacks.reset(new DefaultGlobalCallbacks()); |
Craig Tiller | 8352b9e | 2015-12-02 11:43:40 -0800 | [diff] [blame] | 73 | } |
| 74 | } |
Craig Tiller | 7221d99 | 2015-11-24 06:59:33 -0800 | [diff] [blame] | 75 | |
Craig Tiller | 8f7bff7 | 2015-08-17 13:23:14 -0700 | [diff] [blame] | 76 | class Server::UnimplementedAsyncRequestContext { |
| 77 | protected: |
| 78 | UnimplementedAsyncRequestContext() : generic_stream_(&server_context_) {} |
| 79 | |
| 80 | GenericServerContext server_context_; |
| 81 | GenericServerAsyncReaderWriter generic_stream_; |
| 82 | }; |
| 83 | |
Vijay Pai | c0b2acb | 2016-11-01 16:31:56 -0700 | [diff] [blame] | 84 | class Server::UnimplementedAsyncRequest final |
Craig Tiller | 8f7bff7 | 2015-08-17 13:23:14 -0700 | [diff] [blame] | 85 | : public UnimplementedAsyncRequestContext, |
| 86 | public GenericAsyncRequest { |
| 87 | public: |
| 88 | UnimplementedAsyncRequest(Server* server, ServerCompletionQueue* cq) |
| 89 | : GenericAsyncRequest(server, &server_context_, &generic_stream_, cq, cq, |
| 90 | NULL, false), |
| 91 | server_(server), |
| 92 | cq_(cq) {} |
| 93 | |
Vijay Pai | c0b2acb | 2016-11-01 16:31:56 -0700 | [diff] [blame] | 94 | bool FinalizeResult(void** tag, bool* status) override; |
Craig Tiller | 8f7bff7 | 2015-08-17 13:23:14 -0700 | [diff] [blame] | 95 | |
| 96 | ServerContext* context() { return &server_context_; } |
| 97 | GenericServerAsyncReaderWriter* stream() { return &generic_stream_; } |
| 98 | |
| 99 | private: |
| 100 | Server* const server_; |
| 101 | ServerCompletionQueue* const cq_; |
| 102 | }; |
| 103 | |
| 104 | typedef SneakyCallOpSet<CallOpSendInitialMetadata, CallOpServerSendStatus> |
| 105 | UnimplementedAsyncResponseOp; |
Vijay Pai | c0b2acb | 2016-11-01 16:31:56 -0700 | [diff] [blame] | 106 | class Server::UnimplementedAsyncResponse final |
Craig Tiller | 8f7bff7 | 2015-08-17 13:23:14 -0700 | [diff] [blame] | 107 | : public UnimplementedAsyncResponseOp { |
| 108 | public: |
| 109 | UnimplementedAsyncResponse(UnimplementedAsyncRequest* request); |
| 110 | ~UnimplementedAsyncResponse() { delete request_; } |
| 111 | |
Vijay Pai | c0b2acb | 2016-11-01 16:31:56 -0700 | [diff] [blame] | 112 | bool FinalizeResult(void** tag, bool* status) override { |
Craig Tiller | 8f7bff7 | 2015-08-17 13:23:14 -0700 | [diff] [blame] | 113 | bool r = UnimplementedAsyncResponseOp::FinalizeResult(tag, status); |
| 114 | delete this; |
| 115 | return r; |
| 116 | } |
| 117 | |
| 118 | private: |
| 119 | UnimplementedAsyncRequest* const request_; |
| 120 | }; |
| 121 | |
Sree Kuchibhotla | 4028d2c | 2016-09-21 10:45:33 -0700 | [diff] [blame] | 122 | class ShutdownTag : public CompletionQueueTag { |
| 123 | public: |
Sree Kuchibhotla | 862acb9 | 2016-09-26 09:48:48 -0700 | [diff] [blame] | 124 | bool FinalizeResult(void** tag, bool* status) { return false; } |
Sree Kuchibhotla | 4028d2c | 2016-09-21 10:45:33 -0700 | [diff] [blame] | 125 | }; |
| 126 | |
Vijay Pai | c0b2acb | 2016-11-01 16:31:56 -0700 | [diff] [blame] | 127 | class Server::SyncRequest final : public CompletionQueueTag { |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 128 | public: |
Craig Tiller | 1c9a2a9 | 2015-02-12 14:10:25 -0800 | [diff] [blame] | 129 | SyncRequest(RpcServiceMethod* method, void* tag) |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 130 | : method_(method), |
| 131 | tag_(tag), |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 132 | in_flight_(false), |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 133 | has_request_payload_(method->method_type() == RpcMethod::NORMAL_RPC || |
| 134 | method->method_type() == |
| 135 | RpcMethod::SERVER_STREAMING), |
yang-g | 9b7757d | 2015-08-13 11:15:53 -0700 | [diff] [blame] | 136 | call_details_(nullptr), |
Vijay Pai | ced73bd | 2015-06-17 10:23:28 -0700 | [diff] [blame] | 137 | cq_(nullptr) { |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 138 | grpc_metadata_array_init(&request_metadata_); |
| 139 | } |
| 140 | |
yang-g | 9b7757d | 2015-08-13 11:15:53 -0700 | [diff] [blame] | 141 | ~SyncRequest() { |
| 142 | if (call_details_) { |
| 143 | delete call_details_; |
| 144 | } |
| 145 | grpc_metadata_array_destroy(&request_metadata_); |
| 146 | } |
Yang Gao | 7b49a77 | 2015-05-28 14:07:54 -0700 | [diff] [blame] | 147 | |
Nicolas "Pixel" Noble | ebb5140 | 2015-07-23 02:41:33 +0200 | [diff] [blame] | 148 | void SetupRequest() { cq_ = grpc_completion_queue_create(nullptr); } |
Vijay Pai | ced73bd | 2015-06-17 10:23:28 -0700 | [diff] [blame] | 149 | |
| 150 | void TeardownRequest() { |
| 151 | grpc_completion_queue_destroy(cq_); |
| 152 | cq_ = nullptr; |
| 153 | } |
| 154 | |
Craig Tiller | f9e6adf | 2015-05-06 11:45:59 -0700 | [diff] [blame] | 155 | void Request(grpc_server* server, grpc_completion_queue* notify_cq) { |
Vijay Pai | ced73bd | 2015-06-17 10:23:28 -0700 | [diff] [blame] | 156 | GPR_ASSERT(cq_ && !in_flight_); |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 157 | in_flight_ = true; |
yang-g | 9b7757d | 2015-08-13 11:15:53 -0700 | [diff] [blame] | 158 | if (tag_) { |
| 159 | GPR_ASSERT(GRPC_CALL_OK == |
| 160 | grpc_server_request_registered_call( |
| 161 | server, tag_, &call_, &deadline_, &request_metadata_, |
| 162 | has_request_payload_ ? &request_payload_ : nullptr, cq_, |
| 163 | notify_cq, this)); |
| 164 | } else { |
| 165 | if (!call_details_) { |
| 166 | call_details_ = new grpc_call_details; |
| 167 | grpc_call_details_init(call_details_); |
| 168 | } |
| 169 | GPR_ASSERT(GRPC_CALL_OK == grpc_server_request_call( |
| 170 | server, &call_, call_details_, |
| 171 | &request_metadata_, cq_, notify_cq, this)); |
| 172 | } |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 173 | } |
| 174 | |
Vijay Pai | c0b2acb | 2016-11-01 16:31:56 -0700 | [diff] [blame] | 175 | bool FinalizeResult(void** tag, bool* status) override { |
Craig Tiller | ec3257c | 2015-02-12 15:59:43 -0800 | [diff] [blame] | 176 | if (!*status) { |
| 177 | grpc_completion_queue_destroy(cq_); |
| 178 | } |
yang-g | 9b7757d | 2015-08-13 11:15:53 -0700 | [diff] [blame] | 179 | if (call_details_) { |
| 180 | deadline_ = call_details_->deadline; |
| 181 | grpc_call_details_destroy(call_details_); |
| 182 | grpc_call_details_init(call_details_); |
| 183 | } |
Craig Tiller | 645466e | 2015-02-18 09:18:33 -0800 | [diff] [blame] | 184 | return true; |
Craig Tiller | ec3257c | 2015-02-12 15:59:43 -0800 | [diff] [blame] | 185 | } |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 186 | |
Vijay Pai | c0b2acb | 2016-11-01 16:31:56 -0700 | [diff] [blame] | 187 | class CallData final { |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 188 | public: |
Craig Tiller | 1c9a2a9 | 2015-02-12 14:10:25 -0800 | [diff] [blame] | 189 | explicit CallData(Server* server, SyncRequest* mrd) |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 190 | : cq_(mrd->cq_), |
yang-g | f07ed45 | 2017-02-16 23:01:28 -0800 | [diff] [blame] | 191 | call_(mrd->call_, server, &cq_, server->max_receive_message_size()), |
yang-g | c42d844 | 2017-02-15 00:05:00 -0800 | [diff] [blame] | 192 | ctx_(mrd->deadline_, &mrd->request_metadata_), |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 193 | has_request_payload_(mrd->has_request_payload_), |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 194 | request_payload_(mrd->request_payload_), |
| 195 | method_(mrd->method_) { |
yang-g | 85c04f9 | 2015-07-07 17:47:31 -0700 | [diff] [blame] | 196 | ctx_.set_call(mrd->call_); |
Yang Gao | 1205f6f | 2015-03-22 15:18:14 -0700 | [diff] [blame] | 197 | ctx_.cq_ = &cq_; |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 198 | GPR_ASSERT(mrd->in_flight_); |
| 199 | mrd->in_flight_ = false; |
| 200 | mrd->request_metadata_.count = 0; |
| 201 | } |
| 202 | |
Craig Tiller | ec3257c | 2015-02-12 15:59:43 -0800 | [diff] [blame] | 203 | ~CallData() { |
| 204 | if (has_request_payload_ && request_payload_) { |
| 205 | grpc_byte_buffer_destroy(request_payload_); |
| 206 | } |
| 207 | } |
| 208 | |
Craig Tiller | 6461649 | 2016-01-26 14:16:20 -0800 | [diff] [blame] | 209 | void Run(std::shared_ptr<GlobalCallbacks> global_callbacks) { |
Craig Tiller | 492968f | 2015-02-18 13:14:03 -0800 | [diff] [blame] | 210 | ctx_.BeginCompletionOp(&call_); |
Craig Tiller | 6461649 | 2016-01-26 14:16:20 -0800 | [diff] [blame] | 211 | global_callbacks->PreSynchronousRequest(&ctx_); |
Craig Tiller | 62f28bf | 2017-01-19 10:05:13 -0800 | [diff] [blame] | 212 | method_->handler()->RunHandler( |
| 213 | MethodHandler::HandlerParameter(&call_, &ctx_, request_payload_)); |
Craig Tiller | 6461649 | 2016-01-26 14:16:20 -0800 | [diff] [blame] | 214 | global_callbacks->PostSynchronousRequest(&ctx_); |
Craig Tiller | 928ec8e | 2015-06-05 08:45:45 -0700 | [diff] [blame] | 215 | request_payload_ = nullptr; |
Craig Tiller | 492968f | 2015-02-18 13:14:03 -0800 | [diff] [blame] | 216 | void* ignored_tag; |
| 217 | bool ignored_ok; |
| 218 | cq_.Shutdown(); |
| 219 | GPR_ASSERT(cq_.Next(&ignored_tag, &ignored_ok) == false); |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | private: |
| 223 | CompletionQueue cq_; |
| 224 | Call call_; |
| 225 | ServerContext ctx_; |
| 226 | const bool has_request_payload_; |
Craig Tiller | 8c8d0aa | 2015-02-12 11:38:36 -0800 | [diff] [blame] | 227 | grpc_byte_buffer* request_payload_; |
| 228 | RpcServiceMethod* const method_; |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 229 | }; |
| 230 | |
| 231 | private: |
Craig Tiller | 8c8d0aa | 2015-02-12 11:38:36 -0800 | [diff] [blame] | 232 | RpcServiceMethod* const method_; |
| 233 | void* const tag_; |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 234 | bool in_flight_; |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 235 | const bool has_request_payload_; |
Craig Tiller | 8c8d0aa | 2015-02-12 11:38:36 -0800 | [diff] [blame] | 236 | grpc_call* call_; |
yang-g | 9b7757d | 2015-08-13 11:15:53 -0700 | [diff] [blame] | 237 | grpc_call_details* call_details_; |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 238 | gpr_timespec deadline_; |
| 239 | grpc_metadata_array request_metadata_; |
Craig Tiller | 8c8d0aa | 2015-02-12 11:38:36 -0800 | [diff] [blame] | 240 | grpc_byte_buffer* request_payload_; |
| 241 | grpc_completion_queue* cq_; |
Craig Tiller | c416577 | 2015-02-11 10:51:04 -0800 | [diff] [blame] | 242 | }; |
| 243 | |
Sree Kuchibhotla | 8f7739b | 2016-10-13 15:12:55 -0700 | [diff] [blame] | 244 | // Implementation of ThreadManager. Each instance of SyncRequestThreadManager |
| 245 | // manages a pool of threads that poll for incoming Sync RPCs and call the |
| 246 | // appropriate RPC handlers |
| 247 | class Server::SyncRequestThreadManager : public ThreadManager { |
Sree Kuchibhotla | aabada9 | 2016-08-24 10:01:13 -0700 | [diff] [blame] | 248 | public: |
Sree Kuchibhotla | 8f7739b | 2016-10-13 15:12:55 -0700 | [diff] [blame] | 249 | SyncRequestThreadManager(Server* server, CompletionQueue* server_cq, |
| 250 | std::shared_ptr<GlobalCallbacks> global_callbacks, |
| 251 | int min_pollers, int max_pollers, |
| 252 | int cq_timeout_msec) |
| 253 | : ThreadManager(min_pollers, max_pollers), |
Sree Kuchibhotla | aabada9 | 2016-08-24 10:01:13 -0700 | [diff] [blame] | 254 | server_(server), |
| 255 | server_cq_(server_cq), |
Sree Kuchibhotla | 892dbf4 | 2016-09-27 19:42:27 -0700 | [diff] [blame] | 256 | cq_timeout_msec_(cq_timeout_msec), |
Sree Kuchibhotla | aabada9 | 2016-08-24 10:01:13 -0700 | [diff] [blame] | 257 | global_callbacks_(global_callbacks) {} |
| 258 | |
Vijay Pai | c0b2acb | 2016-11-01 16:31:56 -0700 | [diff] [blame] | 259 | WorkStatus PollForWork(void** tag, bool* ok) override { |
Sree Kuchibhotla | aabada9 | 2016-08-24 10:01:13 -0700 | [diff] [blame] | 260 | *tag = nullptr; |
| 261 | gpr_timespec deadline = |
Sree Kuchibhotla | 892dbf4 | 2016-09-27 19:42:27 -0700 | [diff] [blame] | 262 | gpr_time_from_millis(cq_timeout_msec_, GPR_TIMESPAN); |
Sree Kuchibhotla | aabada9 | 2016-08-24 10:01:13 -0700 | [diff] [blame] | 263 | |
| 264 | switch (server_cq_->AsyncNext(tag, ok, deadline)) { |
| 265 | case CompletionQueue::TIMEOUT: |
| 266 | return TIMEOUT; |
| 267 | case CompletionQueue::SHUTDOWN: |
| 268 | return SHUTDOWN; |
| 269 | case CompletionQueue::GOT_EVENT: |
| 270 | return WORK_FOUND; |
| 271 | } |
| 272 | |
| 273 | GPR_UNREACHABLE_CODE(return TIMEOUT); |
| 274 | } |
| 275 | |
Vijay Pai | c0b2acb | 2016-11-01 16:31:56 -0700 | [diff] [blame] | 276 | void DoWork(void* tag, bool ok) override { |
Sree Kuchibhotla | aabada9 | 2016-08-24 10:01:13 -0700 | [diff] [blame] | 277 | SyncRequest* sync_req = static_cast<SyncRequest*>(tag); |
Sree Kuchibhotla | 4306eee | 2016-09-21 09:56:29 -0700 | [diff] [blame] | 278 | |
| 279 | if (!sync_req) { |
Sree Kuchibhotla | 33382d0 | 2016-10-03 15:08:48 -0700 | [diff] [blame] | 280 | // No tag. Nothing to work on. This is an unlikley scenario and possibly a |
| 281 | // bug in RPC Manager implementation. |
| 282 | gpr_log(GPR_ERROR, "Sync server. DoWork() was called with NULL tag"); |
Sree Kuchibhotla | 4306eee | 2016-09-21 09:56:29 -0700 | [diff] [blame] | 283 | return; |
| 284 | } |
| 285 | |
| 286 | if (ok) { |
Sree Kuchibhotla | 33382d0 | 2016-10-03 15:08:48 -0700 | [diff] [blame] | 287 | // Calldata takes ownership of the completion queue inside sync_req |
Sree Kuchibhotla | aabada9 | 2016-08-24 10:01:13 -0700 | [diff] [blame] | 288 | SyncRequest::CallData cd(server_, sync_req); |
| 289 | { |
Sree Kuchibhotla | 33382d0 | 2016-10-03 15:08:48 -0700 | [diff] [blame] | 290 | // Prepare for the next request |
Sree Kuchibhotla | aabada9 | 2016-08-24 10:01:13 -0700 | [diff] [blame] | 291 | if (!IsShutdown()) { |
Sree Kuchibhotla | 33382d0 | 2016-10-03 15:08:48 -0700 | [diff] [blame] | 292 | sync_req->SetupRequest(); // Create new completion queue for sync_req |
Sree Kuchibhotla | aabada9 | 2016-08-24 10:01:13 -0700 | [diff] [blame] | 293 | sync_req->Request(server_->c_server(), server_cq_->cq()); |
Sree Kuchibhotla | aabada9 | 2016-08-24 10:01:13 -0700 | [diff] [blame] | 294 | } |
| 295 | } |
Sree Kuchibhotla | 33382d0 | 2016-10-03 15:08:48 -0700 | [diff] [blame] | 296 | |
Sree Kuchibhotla | aabada9 | 2016-08-24 10:01:13 -0700 | [diff] [blame] | 297 | GPR_TIMER_SCOPE("cd.Run()", 0); |
| 298 | cd.Run(global_callbacks_); |
| 299 | } |
Sree Kuchibhotla | 33382d0 | 2016-10-03 15:08:48 -0700 | [diff] [blame] | 300 | // TODO (sreek) If ok is false here (which it isn't in case of |
| 301 | // grpc_request_registered_call), we should still re-queue the request |
| 302 | // object |
Sree Kuchibhotla | aabada9 | 2016-08-24 10:01:13 -0700 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | void AddSyncMethod(RpcServiceMethod* method, void* tag) { |
Sree Kuchibhotla | cb18d7a | 2016-10-20 12:39:30 -0700 | [diff] [blame] | 306 | sync_requests_.emplace_back(new SyncRequest(method, tag)); |
Sree Kuchibhotla | aabada9 | 2016-08-24 10:01:13 -0700 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | void AddUnknownSyncMethod() { |
Sree Kuchibhotla | da069a5 | 2016-10-19 11:22:29 -0700 | [diff] [blame] | 310 | if (!sync_requests_.empty()) { |
Sree Kuchibhotla | aabada9 | 2016-08-24 10:01:13 -0700 | [diff] [blame] | 311 | unknown_method_.reset(new RpcServiceMethod( |
| 312 | "unknown", RpcMethod::BIDI_STREAMING, new UnknownMethodHandler)); |
Sree Kuchibhotla | cb18d7a | 2016-10-20 12:39:30 -0700 | [diff] [blame] | 313 | sync_requests_.emplace_back( |
| 314 | new SyncRequest(unknown_method_.get(), nullptr)); |
Sree Kuchibhotla | aabada9 | 2016-08-24 10:01:13 -0700 | [diff] [blame] | 315 | } |
| 316 | } |
| 317 | |
Sree Kuchibhotla | 862acb9 | 2016-09-26 09:48:48 -0700 | [diff] [blame] | 318 | void ShutdownAndDrainCompletionQueue() { |
| 319 | server_cq_->Shutdown(); |
| 320 | |
| 321 | // Drain any pending items from the queue |
| 322 | void* tag; |
| 323 | bool ok; |
| 324 | while (server_cq_->Next(&tag, &ok)) { |
| 325 | // Nothing to be done here |
| 326 | } |
| 327 | } |
| 328 | |
Sree Kuchibhotla | aabada9 | 2016-08-24 10:01:13 -0700 | [diff] [blame] | 329 | void Start() { |
Sree Kuchibhotla | da069a5 | 2016-10-19 11:22:29 -0700 | [diff] [blame] | 330 | if (!sync_requests_.empty()) { |
| 331 | for (auto m = sync_requests_.begin(); m != sync_requests_.end(); m++) { |
Sree Kuchibhotla | cb18d7a | 2016-10-20 12:39:30 -0700 | [diff] [blame] | 332 | (*m)->SetupRequest(); |
| 333 | (*m)->Request(server_->c_server(), server_cq_->cq()); |
Sree Kuchibhotla | aabada9 | 2016-08-24 10:01:13 -0700 | [diff] [blame] | 334 | } |
| 335 | |
Sree Kuchibhotla | cb18d7a | 2016-10-20 12:39:30 -0700 | [diff] [blame] | 336 | Initialize(); // ThreadManager's Initialize() |
Sree Kuchibhotla | aabada9 | 2016-08-24 10:01:13 -0700 | [diff] [blame] | 337 | } |
| 338 | } |
| 339 | |
| 340 | private: |
| 341 | Server* server_; |
| 342 | CompletionQueue* server_cq_; |
Sree Kuchibhotla | 892dbf4 | 2016-09-27 19:42:27 -0700 | [diff] [blame] | 343 | int cq_timeout_msec_; |
Sree Kuchibhotla | cb18d7a | 2016-10-20 12:39:30 -0700 | [diff] [blame] | 344 | std::vector<std::unique_ptr<SyncRequest>> sync_requests_; |
Sree Kuchibhotla | aabada9 | 2016-08-24 10:01:13 -0700 | [diff] [blame] | 345 | std::unique_ptr<RpcServiceMethod> unknown_method_; |
yang-g | 8d668d8 | 2016-12-01 15:09:28 -0800 | [diff] [blame] | 346 | std::unique_ptr<RpcServiceMethod> health_check_; |
Sree Kuchibhotla | aabada9 | 2016-08-24 10:01:13 -0700 | [diff] [blame] | 347 | std::shared_ptr<Server::GlobalCallbacks> global_callbacks_; |
| 348 | }; |
| 349 | |
David Garcia Quintas | d79ef3a | 2016-01-28 00:21:27 -0800 | [diff] [blame] | 350 | static internal::GrpcLibraryInitializer g_gli_initializer; |
Sree Kuchibhotla | aabada9 | 2016-08-24 10:01:13 -0700 | [diff] [blame] | 351 | Server::Server( |
Sree Kuchibhotla | e4eb51f | 2016-10-18 11:51:28 -0700 | [diff] [blame] | 352 | int max_receive_message_size, ChannelArguments* args, |
Sree Kuchibhotla | 4028d2c | 2016-09-21 10:45:33 -0700 | [diff] [blame] | 353 | std::shared_ptr<std::vector<std::unique_ptr<ServerCompletionQueue>>> |
| 354 | sync_server_cqs, |
Sree Kuchibhotla | e4eb51f | 2016-10-18 11:51:28 -0700 | [diff] [blame] | 355 | int min_pollers, int max_pollers, int sync_cq_timeout_msec) |
Mark D. Roth | 6980362 | 2016-09-06 08:15:36 -0700 | [diff] [blame] | 356 | : max_receive_message_size_(max_receive_message_size), |
Sree Kuchibhotla | aabada9 | 2016-08-24 10:01:13 -0700 | [diff] [blame] | 357 | sync_server_cqs_(sync_server_cqs), |
Yang Gao | 3921c56 | 2015-04-30 16:07:06 -0700 | [diff] [blame] | 358 | started_(false), |
vjpai | cad5f0a | 2015-02-18 22:02:52 -0800 | [diff] [blame] | 359 | shutdown_(false), |
yang-g | 6ec11f2 | 2016-07-14 14:53:35 -0700 | [diff] [blame] | 360 | shutdown_notified_(false), |
yang-g | 9b7757d | 2015-08-13 11:15:53 -0700 | [diff] [blame] | 361 | has_generic_service_(false), |
yang-g | eb62c94 | 2016-02-17 15:37:25 -0800 | [diff] [blame] | 362 | server_(nullptr), |
yang-g | ad32764 | 2016-12-12 14:32:09 -0800 | [diff] [blame] | 363 | server_initializer_(new ServerInitializer(this)), |
| 364 | health_check_service_disabled_(false) { |
David Garcia Quintas | d79ef3a | 2016-01-28 00:21:27 -0800 | [diff] [blame] | 365 | g_gli_initializer.summon(); |
Craig Tiller | 8352b9e | 2015-12-02 11:43:40 -0800 | [diff] [blame] | 366 | gpr_once_init(&g_once_init_callbacks, InitGlobalCallbacks); |
Craig Tiller | 6461649 | 2016-01-26 14:16:20 -0800 | [diff] [blame] | 367 | global_callbacks_ = g_callbacks; |
yang-g | eb62c94 | 2016-02-17 15:37:25 -0800 | [diff] [blame] | 368 | global_callbacks_->UpdateArguments(args); |
Sree Kuchibhotla | aabada9 | 2016-08-24 10:01:13 -0700 | [diff] [blame] | 369 | |
| 370 | for (auto it = sync_server_cqs_->begin(); it != sync_server_cqs_->end(); |
| 371 | it++) { |
Sree Kuchibhotla | 8f7739b | 2016-10-13 15:12:55 -0700 | [diff] [blame] | 372 | sync_req_mgrs_.emplace_back(new SyncRequestThreadManager( |
| 373 | this, (*it).get(), global_callbacks_, min_pollers, max_pollers, |
| 374 | sync_cq_timeout_msec)); |
Sree Kuchibhotla | aabada9 | 2016-08-24 10:01:13 -0700 | [diff] [blame] | 375 | } |
| 376 | |
yang-g | eb62c94 | 2016-02-17 15:37:25 -0800 | [diff] [blame] | 377 | grpc_channel_args channel_args; |
| 378 | args->SetChannelArgs(&channel_args); |
Sree Kuchibhotla | 3ea9e24 | 2016-08-22 14:15:43 -0700 | [diff] [blame] | 379 | |
yang-g | 8d668d8 | 2016-12-01 15:09:28 -0800 | [diff] [blame] | 380 | for (size_t i = 0; i < channel_args.num_args; i++) { |
yang-g | b90631d | 2016-12-29 14:08:13 -0800 | [diff] [blame] | 381 | if (0 == |
| 382 | strcmp(channel_args.args[i].key, kHealthCheckServiceInterfaceArg)) { |
yang-g | 6d0fbfa | 2016-12-06 16:49:04 -0800 | [diff] [blame] | 383 | if (channel_args.args[i].value.pointer.p == nullptr) { |
yang-g | ad32764 | 2016-12-12 14:32:09 -0800 | [diff] [blame] | 384 | health_check_service_disabled_ = true; |
yang-g | 8d668d8 | 2016-12-01 15:09:28 -0800 | [diff] [blame] | 385 | } else { |
yang-g | 6d0fbfa | 2016-12-06 16:49:04 -0800 | [diff] [blame] | 386 | health_check_service_.reset(static_cast<HealthCheckServiceInterface*>( |
| 387 | channel_args.args[i].value.pointer.p)); |
yang-g | 8d668d8 | 2016-12-01 15:09:28 -0800 | [diff] [blame] | 388 | } |
| 389 | break; |
| 390 | } |
| 391 | } |
| 392 | |
Sree Kuchibhotla | aabada9 | 2016-08-24 10:01:13 -0700 | [diff] [blame] | 393 | server_ = grpc_server_create(&channel_args, nullptr); |
Craig Tiller | f9e6adf | 2015-05-06 11:45:59 -0700 | [diff] [blame] | 394 | } |
vjpai | cad5f0a | 2015-02-18 22:02:52 -0800 | [diff] [blame] | 395 | |
| 396 | Server::~Server() { |
Ruyi Wang | b486ba6 | 2015-03-14 22:19:44 +0800 | [diff] [blame] | 397 | { |
Vijay Pai | 320ed13 | 2016-11-01 17:16:55 -0700 | [diff] [blame] | 398 | std::unique_lock<std::mutex> lock(mu_); |
Ruyi Wang | b486ba6 | 2015-03-14 22:19:44 +0800 | [diff] [blame] | 399 | if (started_ && !shutdown_) { |
| 400 | lock.unlock(); |
| 401 | Shutdown(); |
yang-g | b971173 | 2016-01-15 16:53:08 -0800 | [diff] [blame] | 402 | } else if (!started_) { |
Sree Kuchibhotla | 862acb9 | 2016-09-26 09:48:48 -0700 | [diff] [blame] | 403 | // Shutdown the completion queues |
| 404 | for (auto it = sync_req_mgrs_.begin(); it != sync_req_mgrs_.end(); it++) { |
| 405 | (*it)->ShutdownAndDrainCompletionQueue(); |
Sree Kuchibhotla | 4306eee | 2016-09-21 09:56:29 -0700 | [diff] [blame] | 406 | } |
Ruyi Wang | b486ba6 | 2015-03-14 22:19:44 +0800 | [diff] [blame] | 407 | } |
vjpai | cad5f0a | 2015-02-18 22:02:52 -0800 | [diff] [blame] | 408 | } |
Sree Kuchibhotla | bb5519f | 2016-07-19 11:00:39 -0700 | [diff] [blame] | 409 | |
vjpai | cad5f0a | 2015-02-18 22:02:52 -0800 | [diff] [blame] | 410 | grpc_server_destroy(server_); |
vjpai | cad5f0a | 2015-02-18 22:02:52 -0800 | [diff] [blame] | 411 | } |
| 412 | |
Craig Tiller | 7221d99 | 2015-11-24 06:59:33 -0800 | [diff] [blame] | 413 | void Server::SetGlobalCallbacks(GlobalCallbacks* callbacks) { |
Vijay Pai | b645a2d | 2016-06-09 18:39:06 -0700 | [diff] [blame] | 414 | GPR_ASSERT(!g_callbacks); |
| 415 | GPR_ASSERT(callbacks); |
Craig Tiller | 6461649 | 2016-01-26 14:16:20 -0800 | [diff] [blame] | 416 | g_callbacks.reset(callbacks); |
Craig Tiller | 7221d99 | 2015-11-24 06:59:33 -0800 | [diff] [blame] | 417 | } |
| 418 | |
Adam Michalik | 4ad746e | 2016-06-07 15:02:59 -0700 | [diff] [blame] | 419 | grpc_server* Server::c_server() { return server_; } |
Adam Michalik | b97e2d1 | 2016-06-02 12:12:55 -0700 | [diff] [blame] | 420 | |
Craig Tiller | 06cb1a9 | 2016-04-04 08:10:47 -0700 | [diff] [blame] | 421 | static grpc_server_register_method_payload_handling PayloadHandlingForMethod( |
| 422 | RpcServiceMethod* method) { |
| 423 | switch (method->method_type()) { |
| 424 | case RpcMethod::NORMAL_RPC: |
| 425 | case RpcMethod::SERVER_STREAMING: |
| 426 | return GRPC_SRM_PAYLOAD_READ_INITIAL_BYTE_BUFFER; |
| 427 | case RpcMethod::CLIENT_STREAMING: |
| 428 | case RpcMethod::BIDI_STREAMING: |
| 429 | return GRPC_SRM_PAYLOAD_NONE; |
| 430 | } |
| 431 | GPR_UNREACHABLE_CODE(return GRPC_SRM_PAYLOAD_NONE;); |
| 432 | } |
| 433 | |
Craig Tiller | 15f383c | 2016-01-07 12:45:32 -0800 | [diff] [blame] | 434 | bool Server::RegisterService(const grpc::string* host, Service* service) { |
| 435 | bool has_async_methods = service->has_async_methods(); |
| 436 | if (has_async_methods) { |
| 437 | GPR_ASSERT(service->server_ == nullptr && |
| 438 | "Can only register an asynchronous service against one server."); |
| 439 | service->server_ = this; |
| 440 | } |
Sree Kuchibhotla | aabada9 | 2016-08-24 10:01:13 -0700 | [diff] [blame] | 441 | |
Yuchen Zeng | a42ec21 | 2016-04-29 13:03:06 -0700 | [diff] [blame] | 442 | const char* method_name = nullptr; |
Craig Tiller | 15f383c | 2016-01-07 12:45:32 -0800 | [diff] [blame] | 443 | for (auto it = service->methods_.begin(); it != service->methods_.end(); |
| 444 | ++it) { |
yang-g | 269b8be | 2016-01-15 10:46:26 -0800 | [diff] [blame] | 445 | if (it->get() == nullptr) { // Handled by generic service if any. |
| 446 | continue; |
| 447 | } |
Sree Kuchibhotla | da069a5 | 2016-10-19 11:22:29 -0700 | [diff] [blame] | 448 | |
Craig Tiller | 15f383c | 2016-01-07 12:45:32 -0800 | [diff] [blame] | 449 | RpcServiceMethod* method = it->get(); |
Craig Tiller | 06cb1a9 | 2016-04-04 08:10:47 -0700 | [diff] [blame] | 450 | void* tag = grpc_server_register_method( |
| 451 | server_, method->name(), host ? host->c_str() : nullptr, |
| 452 | PayloadHandlingForMethod(method), 0); |
Craig Tiller | 15f383c | 2016-01-07 12:45:32 -0800 | [diff] [blame] | 453 | if (tag == nullptr) { |
vjpai | cad5f0a | 2015-02-18 22:02:52 -0800 | [diff] [blame] | 454 | gpr_log(GPR_DEBUG, "Attempt to register %s multiple times", |
| 455 | method->name()); |
| 456 | return false; |
| 457 | } |
Sree Kuchibhotla | da069a5 | 2016-10-19 11:22:29 -0700 | [diff] [blame] | 458 | |
Sree Kuchibhotla | cb18d7a | 2016-10-20 12:39:30 -0700 | [diff] [blame] | 459 | if (method->handler() == nullptr) { // Async method |
Craig Tiller | 15f383c | 2016-01-07 12:45:32 -0800 | [diff] [blame] | 460 | method->set_server_tag(tag); |
| 461 | } else { |
Sree Kuchibhotla | aabada9 | 2016-08-24 10:01:13 -0700 | [diff] [blame] | 462 | for (auto it = sync_req_mgrs_.begin(); it != sync_req_mgrs_.end(); it++) { |
| 463 | (*it)->AddSyncMethod(method, tag); |
| 464 | } |
vjpai | cad5f0a | 2015-02-18 22:02:52 -0800 | [diff] [blame] | 465 | } |
Sree Kuchibhotla | da069a5 | 2016-10-19 11:22:29 -0700 | [diff] [blame] | 466 | |
Yuchen Zeng | a42ec21 | 2016-04-29 13:03:06 -0700 | [diff] [blame] | 467 | method_name = method->name(); |
| 468 | } |
| 469 | |
| 470 | // Parse service name. |
| 471 | if (method_name != nullptr) { |
| 472 | std::stringstream ss(method_name); |
| 473 | grpc::string service_name; |
| 474 | if (std::getline(ss, service_name, '/') && |
| 475 | std::getline(ss, service_name, '/')) { |
| 476 | services_.push_back(service_name); |
| 477 | } |
vjpai | cad5f0a | 2015-02-18 22:02:52 -0800 | [diff] [blame] | 478 | } |
| 479 | return true; |
| 480 | } |
| 481 | |
Yang Gao | 4999649 | 2015-03-12 16:40:19 -0700 | [diff] [blame] | 482 | void Server::RegisterAsyncGenericService(AsyncGenericService* service) { |
Yang Gao | 1c40233 | 2015-03-05 16:39:25 -0800 | [diff] [blame] | 483 | GPR_ASSERT(service->server_ == nullptr && |
Yang Gao | 1ad253d | 2015-03-16 13:11:29 -0700 | [diff] [blame] | 484 | "Can only register an async generic service against one server."); |
Yang Gao | 1c40233 | 2015-03-05 16:39:25 -0800 | [diff] [blame] | 485 | service->server_ = this; |
yang-g | 9b7757d | 2015-08-13 11:15:53 -0700 | [diff] [blame] | 486 | has_generic_service_ = true; |
Yang Gao | 1c40233 | 2015-03-05 16:39:25 -0800 | [diff] [blame] | 487 | } |
| 488 | |
Nicolas Noble | cfd6073 | 2015-03-18 16:27:43 -0700 | [diff] [blame] | 489 | int Server::AddListeningPort(const grpc::string& addr, |
| 490 | ServerCredentials* creds) { |
vjpai | cad5f0a | 2015-02-18 22:02:52 -0800 | [diff] [blame] | 491 | GPR_ASSERT(!started_); |
yang-g | d510fcf | 2017-03-31 11:00:02 -0700 | [diff] [blame] | 492 | int port = creds->AddPortToServer(addr, server_); |
| 493 | global_callbacks_->AddPort(this, port); |
| 494 | return port; |
vjpai | cad5f0a | 2015-02-18 22:02:52 -0800 | [diff] [blame] | 495 | } |
| 496 | |
Craig Tiller | 8f7bff7 | 2015-08-17 13:23:14 -0700 | [diff] [blame] | 497 | bool Server::Start(ServerCompletionQueue** cqs, size_t num_cqs) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 498 | GPR_ASSERT(!started_); |
yang-g | f2fe4f7 | 2017-02-07 12:47:22 -0800 | [diff] [blame] | 499 | global_callbacks_->PreServerStart(this); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 500 | started_ = true; |
yang-g | ad32764 | 2016-12-12 14:32:09 -0800 | [diff] [blame] | 501 | |
| 502 | // Only create default health check service when user did not provide an |
| 503 | // explicit one. |
| 504 | if (health_check_service_ == nullptr && !health_check_service_disabled_ && |
| 505 | DefaultHealthCheckServiceEnabled()) { |
yang-g | 076bac0 | 2017-02-07 13:50:36 -0800 | [diff] [blame] | 506 | if (sync_server_cqs_->empty()) { |
| 507 | gpr_log(GPR_ERROR, |
| 508 | "Default health check service disabled at async-only server."); |
| 509 | } else { |
| 510 | auto* default_hc_service = new DefaultHealthCheckService; |
| 511 | health_check_service_.reset(default_hc_service); |
| 512 | RegisterService(nullptr, default_hc_service->GetHealthCheckService()); |
| 513 | } |
yang-g | ad32764 | 2016-12-12 14:32:09 -0800 | [diff] [blame] | 514 | } |
| 515 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 516 | grpc_server_start(server_); |
| 517 | |
yang-g | 9b7757d | 2015-08-13 11:15:53 -0700 | [diff] [blame] | 518 | if (!has_generic_service_) { |
Sree Kuchibhotla | aabada9 | 2016-08-24 10:01:13 -0700 | [diff] [blame] | 519 | for (auto it = sync_req_mgrs_.begin(); it != sync_req_mgrs_.end(); it++) { |
| 520 | (*it)->AddUnknownSyncMethod(); |
Craig Tiller | 86d3177 | 2015-08-19 12:52:50 -0700 | [diff] [blame] | 521 | } |
Sree Kuchibhotla | 3ea9e24 | 2016-08-22 14:15:43 -0700 | [diff] [blame] | 522 | |
Craig Tiller | 8f7bff7 | 2015-08-17 13:23:14 -0700 | [diff] [blame] | 523 | for (size_t i = 0; i < num_cqs; i++) { |
Craig Tiller | e004958 | 2016-05-20 10:31:09 -0700 | [diff] [blame] | 524 | if (cqs[i]->IsFrequentlyPolled()) { |
| 525 | new UnimplementedAsyncRequest(this, cqs[i]); |
| 526 | } |
Craig Tiller | cbd0485 | 2015-02-10 17:39:54 -0800 | [diff] [blame] | 527 | } |
yang-g | 9b7757d | 2015-08-13 11:15:53 -0700 | [diff] [blame] | 528 | } |
Sree Kuchibhotla | 3ea9e24 | 2016-08-22 14:15:43 -0700 | [diff] [blame] | 529 | |
Sree Kuchibhotla | aabada9 | 2016-08-24 10:01:13 -0700 | [diff] [blame] | 530 | for (auto it = sync_req_mgrs_.begin(); it != sync_req_mgrs_.end(); it++) { |
| 531 | (*it)->Start(); |
| 532 | } |
| 533 | |
Craig Tiller | 0db1bef | 2015-02-09 13:47:39 -0800 | [diff] [blame] | 534 | return true; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 535 | } |
| 536 | |
Craig Tiller | e50e5cb | 2015-08-18 12:44:57 -0700 | [diff] [blame] | 537 | void Server::ShutdownInternal(gpr_timespec deadline) { |
Vijay Pai | 320ed13 | 2016-11-01 17:16:55 -0700 | [diff] [blame] | 538 | std::unique_lock<std::mutex> lock(mu_); |
Craig Tiller | 12352b2 | 2017-01-10 15:16:14 -0800 | [diff] [blame] | 539 | if (!shutdown_) { |
Craig Tiller | 6e57b9e | 2015-02-24 15:46:22 -0800 | [diff] [blame] | 540 | shutdown_ = true; |
Sree Kuchibhotla | aabada9 | 2016-08-24 10:01:13 -0700 | [diff] [blame] | 541 | |
Sree Kuchibhotla | 862acb9 | 2016-09-26 09:48:48 -0700 | [diff] [blame] | 542 | /// The completion queue to use for server shutdown completion notification |
| 543 | CompletionQueue shutdown_cq; |
Sree Kuchibhotla | 4028d2c | 2016-09-21 10:45:33 -0700 | [diff] [blame] | 544 | ShutdownTag shutdown_tag; // Dummy shutdown tag |
Sree Kuchibhotla | 862acb9 | 2016-09-26 09:48:48 -0700 | [diff] [blame] | 545 | grpc_server_shutdown_and_notify(server_, shutdown_cq.cq(), &shutdown_tag); |
Sree Kuchibhotla | aabada9 | 2016-08-24 10:01:13 -0700 | [diff] [blame] | 546 | |
Sree Kuchibhotla | 862acb9 | 2016-09-26 09:48:48 -0700 | [diff] [blame] | 547 | shutdown_cq.Shutdown(); |
Sree Kuchibhotla | aabada9 | 2016-08-24 10:01:13 -0700 | [diff] [blame] | 548 | |
| 549 | void* tag; |
| 550 | bool ok; |
| 551 | CompletionQueue::NextStatus status = |
Sree Kuchibhotla | 862acb9 | 2016-09-26 09:48:48 -0700 | [diff] [blame] | 552 | shutdown_cq.AsyncNext(&tag, &ok, deadline); |
Sree Kuchibhotla | aabada9 | 2016-08-24 10:01:13 -0700 | [diff] [blame] | 553 | |
Sree Kuchibhotla | 862acb9 | 2016-09-26 09:48:48 -0700 | [diff] [blame] | 554 | // If this timed out, it means we are done with the grace period for a clean |
| 555 | // shutdown. We should force a shutdown now by cancelling all inflight calls |
Sree Kuchibhotla | aabada9 | 2016-08-24 10:01:13 -0700 | [diff] [blame] | 556 | if (status == CompletionQueue::NextStatus::TIMEOUT) { |
| 557 | grpc_server_cancel_all_calls(server_); |
| 558 | } |
| 559 | // Else in case of SHUTDOWN or GOT_EVENT, it means that the server has |
| 560 | // successfully shutdown |
| 561 | |
Craig Tiller | 9c5318a | 2016-12-05 15:07:04 -0800 | [diff] [blame] | 562 | // Shutdown all ThreadManagers. This will try to gracefully stop all the |
| 563 | // threads in the ThreadManagers (once they process any inflight requests) |
| 564 | for (auto it = sync_req_mgrs_.begin(); it != sync_req_mgrs_.end(); it++) { |
| 565 | (*it)->Shutdown(); // ThreadManager's Shutdown() |
| 566 | } |
| 567 | |
Sree Kuchibhotla | 8f7739b | 2016-10-13 15:12:55 -0700 | [diff] [blame] | 568 | // Wait for threads in all ThreadManagers to terminate |
Sree Kuchibhotla | aabada9 | 2016-08-24 10:01:13 -0700 | [diff] [blame] | 569 | for (auto it = sync_req_mgrs_.begin(); it != sync_req_mgrs_.end(); it++) { |
| 570 | (*it)->Wait(); |
Sree Kuchibhotla | 862acb9 | 2016-09-26 09:48:48 -0700 | [diff] [blame] | 571 | (*it)->ShutdownAndDrainCompletionQueue(); |
Sree Kuchibhotla | aabada9 | 2016-08-24 10:01:13 -0700 | [diff] [blame] | 572 | } |
| 573 | |
Sree Kuchibhotla | 862acb9 | 2016-09-26 09:48:48 -0700 | [diff] [blame] | 574 | // Drain the shutdown queue (if the previous call to AsyncNext() timed out |
| 575 | // and we didn't remove the tag from the queue yet) |
Sree Kuchibhotla | 892dbf4 | 2016-09-27 19:42:27 -0700 | [diff] [blame] | 576 | while (shutdown_cq.Next(&tag, &ok)) { |
Sree Kuchibhotla | 33382d0 | 2016-10-03 15:08:48 -0700 | [diff] [blame] | 577 | // Nothing to be done here. Just ignore ok and tag values |
Sree Kuchibhotla | aabada9 | 2016-08-24 10:01:13 -0700 | [diff] [blame] | 578 | } |
| 579 | |
yang-g | 6ec11f2 | 2016-07-14 14:53:35 -0700 | [diff] [blame] | 580 | shutdown_notified_ = true; |
yang-g | e89dc6c | 2016-07-11 15:48:01 -0700 | [diff] [blame] | 581 | shutdown_cv_.notify_all(); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 582 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 583 | } |
| 584 | |
Craig Tiller | 6e57b9e | 2015-02-24 15:46:22 -0800 | [diff] [blame] | 585 | void Server::Wait() { |
Vijay Pai | 320ed13 | 2016-11-01 17:16:55 -0700 | [diff] [blame] | 586 | std::unique_lock<std::mutex> lock(mu_); |
yang-g | 6ec11f2 | 2016-07-14 14:53:35 -0700 | [diff] [blame] | 587 | while (started_ && !shutdown_notified_) { |
| 588 | shutdown_cv_.wait(lock); |
| 589 | } |
Craig Tiller | 6e57b9e | 2015-02-24 15:46:22 -0800 | [diff] [blame] | 590 | } |
| 591 | |
Craig Tiller | 50a7a68 | 2015-06-04 12:53:40 -0700 | [diff] [blame] | 592 | void Server::PerformOpsOnCall(CallOpSetInterface* ops, Call* call) { |
Craig Tiller | bb5227f | 2015-02-11 13:34:48 -0800 | [diff] [blame] | 593 | static const size_t MAX_OPS = 8; |
Craig Tiller | 50a7a68 | 2015-06-04 12:53:40 -0700 | [diff] [blame] | 594 | size_t nops = 0; |
| 595 | grpc_op cops[MAX_OPS]; |
Craig Tiller | 66051c6 | 2017-03-31 09:16:35 -0700 | [diff] [blame] | 596 | ops->FillOps(call->call(), cops, &nops); |
Nicolas "Pixel" Noble | ebb5140 | 2015-07-23 02:41:33 +0200 | [diff] [blame] | 597 | auto result = grpc_call_start_batch(call->call(), cops, nops, ops, nullptr); |
| 598 | GPR_ASSERT(GRPC_CALL_OK == result); |
Craig Tiller | bb5227f | 2015-02-11 13:34:48 -0800 | [diff] [blame] | 599 | } |
| 600 | |
David Garcia Quintas | 44f3249 | 2016-01-14 18:00:04 -0800 | [diff] [blame] | 601 | ServerInterface::BaseAsyncRequest::BaseAsyncRequest( |
| 602 | ServerInterface* server, ServerContext* context, |
Craig Tiller | 8f7bff7 | 2015-08-17 13:23:14 -0700 | [diff] [blame] | 603 | ServerAsyncStreamingInterface* stream, CompletionQueue* call_cq, void* tag, |
| 604 | bool delete_on_finalize) |
Craig Tiller | ce40de5 | 2015-06-05 07:14:58 -0700 | [diff] [blame] | 605 | : server_(server), |
| 606 | context_(context), |
| 607 | stream_(stream), |
| 608 | call_cq_(call_cq), |
Craig Tiller | 5095581 | 2015-06-05 08:03:17 -0700 | [diff] [blame] | 609 | tag_(tag), |
Craig Tiller | 8f7bff7 | 2015-08-17 13:23:14 -0700 | [diff] [blame] | 610 | delete_on_finalize_(delete_on_finalize), |
Craig Tiller | ce40de5 | 2015-06-05 07:14:58 -0700 | [diff] [blame] | 611 | call_(nullptr) { |
Vijay Pai | bf24dd9 | 2016-12-05 13:59:09 -0800 | [diff] [blame] | 612 | call_cq_->RegisterAvalanching(); // This op will trigger more ops |
Craig Tiller | 7bc97bc | 2015-06-04 17:22:54 -0700 | [diff] [blame] | 613 | } |
| 614 | |
Vijay Pai | cbe1599 | 2016-12-05 13:56:29 -0800 | [diff] [blame] | 615 | ServerInterface::BaseAsyncRequest::~BaseAsyncRequest() { |
| 616 | call_cq_->CompleteAvalanching(); |
| 617 | } |
| 618 | |
David Garcia Quintas | f3ddb7c | 2016-01-20 16:02:22 -0800 | [diff] [blame] | 619 | bool ServerInterface::BaseAsyncRequest::FinalizeResult(void** tag, |
| 620 | bool* status) { |
Craig Tiller | 7bc97bc | 2015-06-04 17:22:54 -0700 | [diff] [blame] | 621 | if (*status) { |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 622 | context_->client_metadata_.FillMap(); |
Craig Tiller | 7bc97bc | 2015-06-04 17:22:54 -0700 | [diff] [blame] | 623 | } |
yang-g | 85c04f9 | 2015-07-07 17:47:31 -0700 | [diff] [blame] | 624 | context_->set_call(call_); |
Craig Tiller | 7bc97bc | 2015-06-04 17:22:54 -0700 | [diff] [blame] | 625 | context_->cq_ = call_cq_; |
yang-g | f07ed45 | 2017-02-16 23:01:28 -0800 | [diff] [blame] | 626 | Call call(call_, server_, call_cq_, server_->max_receive_message_size()); |
Craig Tiller | 7bc97bc | 2015-06-04 17:22:54 -0700 | [diff] [blame] | 627 | if (*status && call_) { |
| 628 | context_->BeginCompletionOp(&call); |
| 629 | } |
| 630 | // just the pointers inside call are copied here |
| 631 | stream_->BindCall(&call); |
Craig Tiller | 5095581 | 2015-06-05 08:03:17 -0700 | [diff] [blame] | 632 | *tag = tag_; |
Craig Tiller | 8f7bff7 | 2015-08-17 13:23:14 -0700 | [diff] [blame] | 633 | if (delete_on_finalize_) { |
| 634 | delete this; |
| 635 | } |
Craig Tiller | 7bc97bc | 2015-06-04 17:22:54 -0700 | [diff] [blame] | 636 | return true; |
| 637 | } |
| 638 | |
David Garcia Quintas | 44f3249 | 2016-01-14 18:00:04 -0800 | [diff] [blame] | 639 | ServerInterface::RegisteredAsyncRequest::RegisteredAsyncRequest( |
| 640 | ServerInterface* server, ServerContext* context, |
yang-g | 076bac0 | 2017-02-07 13:50:36 -0800 | [diff] [blame] | 641 | ServerAsyncStreamingInterface* stream, CompletionQueue* call_cq, void* tag) |
| 642 | : BaseAsyncRequest(server, context, stream, call_cq, tag, true) {} |
Craig Tiller | 7bc97bc | 2015-06-04 17:22:54 -0700 | [diff] [blame] | 643 | |
David Garcia Quintas | 44f3249 | 2016-01-14 18:00:04 -0800 | [diff] [blame] | 644 | void ServerInterface::RegisteredAsyncRequest::IssueRequest( |
Craig Tiller | ce40de5 | 2015-06-05 07:14:58 -0700 | [diff] [blame] | 645 | void* registered_method, grpc_byte_buffer** payload, |
| 646 | ServerCompletionQueue* notification_cq) { |
Craig Tiller | 7bc97bc | 2015-06-04 17:22:54 -0700 | [diff] [blame] | 647 | grpc_server_request_registered_call( |
David Garcia Quintas | 44f3249 | 2016-01-14 18:00:04 -0800 | [diff] [blame] | 648 | server_->server(), registered_method, &call_, &context_->deadline_, |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 649 | context_->client_metadata_.arr(), payload, call_cq_->cq(), |
| 650 | notification_cq->cq(), this); |
Craig Tiller | 7bc97bc | 2015-06-04 17:22:54 -0700 | [diff] [blame] | 651 | } |
| 652 | |
David Garcia Quintas | 44f3249 | 2016-01-14 18:00:04 -0800 | [diff] [blame] | 653 | ServerInterface::GenericAsyncRequest::GenericAsyncRequest( |
| 654 | ServerInterface* server, GenericServerContext* context, |
Craig Tiller | ce40de5 | 2015-06-05 07:14:58 -0700 | [diff] [blame] | 655 | ServerAsyncStreamingInterface* stream, CompletionQueue* call_cq, |
Craig Tiller | 8f7bff7 | 2015-08-17 13:23:14 -0700 | [diff] [blame] | 656 | ServerCompletionQueue* notification_cq, void* tag, bool delete_on_finalize) |
| 657 | : BaseAsyncRequest(server, context, stream, call_cq, tag, |
| 658 | delete_on_finalize) { |
Craig Tiller | 7bc97bc | 2015-06-04 17:22:54 -0700 | [diff] [blame] | 659 | grpc_call_details_init(&call_details_); |
| 660 | GPR_ASSERT(notification_cq); |
| 661 | GPR_ASSERT(call_cq); |
David Garcia Quintas | 44f3249 | 2016-01-14 18:00:04 -0800 | [diff] [blame] | 662 | grpc_server_request_call(server->server(), &call_, &call_details_, |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 663 | context->client_metadata_.arr(), call_cq->cq(), |
Craig Tiller | ce40de5 | 2015-06-05 07:14:58 -0700 | [diff] [blame] | 664 | notification_cq->cq(), this); |
Craig Tiller | 7bc97bc | 2015-06-04 17:22:54 -0700 | [diff] [blame] | 665 | } |
| 666 | |
David Garcia Quintas | f3ddb7c | 2016-01-20 16:02:22 -0800 | [diff] [blame] | 667 | bool ServerInterface::GenericAsyncRequest::FinalizeResult(void** tag, |
| 668 | bool* status) { |
Craig Tiller | 7bc97bc | 2015-06-04 17:22:54 -0700 | [diff] [blame] | 669 | // TODO(yangg) remove the copy here. |
yang-g | 3deb006 | 2015-06-23 14:06:08 -0700 | [diff] [blame] | 670 | if (*status) { |
yang-g | a58cab3 | 2015-06-23 14:37:15 -0700 | [diff] [blame] | 671 | static_cast<GenericServerContext*>(context_)->method_ = |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 672 | StringFromCopiedSlice(call_details_.method); |
| 673 | static_cast<GenericServerContext*>(context_)->host_ = |
| 674 | StringFromCopiedSlice(call_details_.host); |
yang-g | 3deb006 | 2015-06-23 14:06:08 -0700 | [diff] [blame] | 675 | } |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 676 | grpc_slice_unref(call_details_.method); |
| 677 | grpc_slice_unref(call_details_.host); |
Craig Tiller | 7bc97bc | 2015-06-04 17:22:54 -0700 | [diff] [blame] | 678 | return BaseAsyncRequest::FinalizeResult(tag, status); |
| 679 | } |
| 680 | |
Craig Tiller | 8f7bff7 | 2015-08-17 13:23:14 -0700 | [diff] [blame] | 681 | bool Server::UnimplementedAsyncRequest::FinalizeResult(void** tag, |
| 682 | bool* status) { |
| 683 | if (GenericAsyncRequest::FinalizeResult(tag, status) && *status) { |
| 684 | new UnimplementedAsyncRequest(server_, cq_); |
| 685 | new UnimplementedAsyncResponse(this); |
| 686 | } else { |
| 687 | delete this; |
| 688 | } |
| 689 | return false; |
| 690 | } |
| 691 | |
| 692 | Server::UnimplementedAsyncResponse::UnimplementedAsyncResponse( |
| 693 | UnimplementedAsyncRequest* request) |
| 694 | : request_(request) { |
| 695 | Status status(StatusCode::UNIMPLEMENTED, ""); |
| 696 | UnknownMethodHandler::FillOps(request_->context(), this); |
| 697 | request_->stream()->call_.PerformOps(this); |
| 698 | } |
| 699 | |
Yuchen Zeng | a42ec21 | 2016-04-29 13:03:06 -0700 | [diff] [blame] | 700 | ServerInitializer* Server::initializer() { return server_initializer_.get(); } |
| 701 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 702 | } // namespace grpc |