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