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