blob: 9e11a8a9e07a41420694f406d15a88fc49f4d4b5 [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
Craig Tiller6169d5f2016-03-31 07:46:18 -07002 * Copyright 2015, Google Inc.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08003 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
14 * distribution.
15 * * Neither the name of Google Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 *
31 */
32
33#include <grpc++/server.h>
yang-g9e2f90c2015-08-21 15:35:03 -070034
Yuchen Zenga42ec212016-04-29 13:03:06 -070035#include <sstream>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080036#include <utility>
37
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080038#include <grpc++/completion_queue.h>
yang-g9e2f90c2015-08-21 15:35:03 -070039#include <grpc++/generic/async_generic_service.h>
yang-g50993b72016-12-29 10:00:27 -080040#include <grpc++/impl/codegen/async_unary_call.h>
David Garcia Quintase1300de2016-01-27 18:41:26 -080041#include <grpc++/impl/codegen/completion_queue_tag.h>
42#include <grpc++/impl/grpc_library.h>
Craig Tiller15f383c2016-01-07 12:45:32 -080043#include <grpc++/impl/method_handler_impl.h>
yangg1b151092015-01-09 15:31:05 -080044#include <grpc++/impl/rpc_service_method.h>
Yuchen Zenga42ec212016-04-29 13:03:06 -070045#include <grpc++/impl/server_initializer.h>
Craig Tiller8c8d0aa2015-02-12 11:38:36 -080046#include <grpc++/impl/service_type.h>
Julien Boeuf5be92a32015-08-28 16:28:18 -070047#include <grpc++/security/server_credentials.h>
David Garcia Quintase1300de2016-01-27 18:41:26 -080048#include <grpc++/server_context.h>
yang-g9e2f90c2015-08-21 15:35:03 -070049#include <grpc++/support/time.h>
David Garcia Quintase1300de2016-01-27 18:41:26 -080050#include <grpc/grpc.h>
51#include <grpc/support/alloc.h>
52#include <grpc/support/log.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080053
Craig Tiller9533d042016-03-25 17:11:06 -070054#include "src/core/lib/profiling/timers.h"
yang-gc3c475f2016-12-27 10:37:26 -080055#include "src/cpp/server/health/default_health_check_service.h"
Sree Kuchibhotlaf72ec6b2016-10-24 11:09:43 -070056#include "src/cpp/thread_manager/thread_manager.h"
Craig Tillerc4165772015-02-11 10:51:04 -080057
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080058namespace grpc {
59
Vijay Paic0b2acb2016-11-01 16:31:56 -070060class DefaultGlobalCallbacks final : public Server::GlobalCallbacks {
Craig Tiller7221d992015-11-24 06:59:33 -080061 public:
Vijay Paic0b2acb2016-11-01 16:31:56 -070062 ~DefaultGlobalCallbacks() override {}
63 void PreSynchronousRequest(ServerContext* context) override {}
64 void PostSynchronousRequest(ServerContext* context) override {}
Craig Tiller7221d992015-11-24 06:59:33 -080065};
66
Craig Tiller64616492016-01-26 14:16:20 -080067static std::shared_ptr<Server::GlobalCallbacks> g_callbacks = nullptr;
Craig Tiller8352b9e2015-12-02 11:43:40 -080068static gpr_once g_once_init_callbacks = GPR_ONCE_INIT;
69
Craig Tiller8352b9e2015-12-02 11:43:40 -080070static void InitGlobalCallbacks() {
Vijay Paib645a2d2016-06-09 18:39:06 -070071 if (!g_callbacks) {
Craig Tiller64616492016-01-26 14:16:20 -080072 g_callbacks.reset(new DefaultGlobalCallbacks());
Craig Tiller8352b9e2015-12-02 11:43:40 -080073 }
74}
Craig Tiller7221d992015-11-24 06:59:33 -080075
Craig Tiller8f7bff72015-08-17 13:23:14 -070076class Server::UnimplementedAsyncRequestContext {
77 protected:
78 UnimplementedAsyncRequestContext() : generic_stream_(&server_context_) {}
79
80 GenericServerContext server_context_;
81 GenericServerAsyncReaderWriter generic_stream_;
82};
83
Vijay Paic0b2acb2016-11-01 16:31:56 -070084class Server::UnimplementedAsyncRequest final
Craig Tiller8f7bff72015-08-17 13:23:14 -070085 : public UnimplementedAsyncRequestContext,
86 public GenericAsyncRequest {
87 public:
88 UnimplementedAsyncRequest(Server* server, ServerCompletionQueue* cq)
89 : GenericAsyncRequest(server, &server_context_, &generic_stream_, cq, cq,
90 NULL, false),
91 server_(server),
92 cq_(cq) {}
93
Vijay Paic0b2acb2016-11-01 16:31:56 -070094 bool FinalizeResult(void** tag, bool* status) override;
Craig Tiller8f7bff72015-08-17 13:23:14 -070095
96 ServerContext* context() { return &server_context_; }
97 GenericServerAsyncReaderWriter* stream() { return &generic_stream_; }
98
99 private:
100 Server* const server_;
101 ServerCompletionQueue* const cq_;
102};
103
104typedef SneakyCallOpSet<CallOpSendInitialMetadata, CallOpServerSendStatus>
105 UnimplementedAsyncResponseOp;
Vijay Paic0b2acb2016-11-01 16:31:56 -0700106class Server::UnimplementedAsyncResponse final
Craig Tiller8f7bff72015-08-17 13:23:14 -0700107 : public UnimplementedAsyncResponseOp {
108 public:
109 UnimplementedAsyncResponse(UnimplementedAsyncRequest* request);
110 ~UnimplementedAsyncResponse() { delete request_; }
111
Vijay Paic0b2acb2016-11-01 16:31:56 -0700112 bool FinalizeResult(void** tag, bool* status) override {
Craig Tiller8f7bff72015-08-17 13:23:14 -0700113 bool r = UnimplementedAsyncResponseOp::FinalizeResult(tag, status);
114 delete this;
115 return r;
116 }
117
118 private:
119 UnimplementedAsyncRequest* const request_;
120};
121
Sree Kuchibhotla4028d2c2016-09-21 10:45:33 -0700122class ShutdownTag : public CompletionQueueTag {
123 public:
Sree Kuchibhotla862acb92016-09-26 09:48:48 -0700124 bool FinalizeResult(void** tag, bool* status) { return false; }
Sree Kuchibhotla4028d2c2016-09-21 10:45:33 -0700125};
126
Vijay Paic0b2acb2016-11-01 16:31:56 -0700127class Server::SyncRequest final : public CompletionQueueTag {
Craig Tillerc4165772015-02-11 10:51:04 -0800128 public:
Craig Tiller1c9a2a92015-02-12 14:10:25 -0800129 SyncRequest(RpcServiceMethod* method, void* tag)
Craig Tillerc4165772015-02-11 10:51:04 -0800130 : method_(method),
131 tag_(tag),
Craig Tillercf133f42015-02-26 14:05:56 -0800132 in_flight_(false),
Craig Tillerc4165772015-02-11 10:51:04 -0800133 has_request_payload_(method->method_type() == RpcMethod::NORMAL_RPC ||
134 method->method_type() ==
135 RpcMethod::SERVER_STREAMING),
yang-g9b7757d2015-08-13 11:15:53 -0700136 call_details_(nullptr),
Vijay Paiced73bd2015-06-17 10:23:28 -0700137 cq_(nullptr) {
Craig Tillerc4165772015-02-11 10:51:04 -0800138 grpc_metadata_array_init(&request_metadata_);
139 }
140
yang-g9b7757d2015-08-13 11:15:53 -0700141 ~SyncRequest() {
142 if (call_details_) {
143 delete call_details_;
144 }
145 grpc_metadata_array_destroy(&request_metadata_);
146 }
Yang Gao7b49a772015-05-28 14:07:54 -0700147
Nicolas "Pixel" Nobleebb51402015-07-23 02:41:33 +0200148 void SetupRequest() { cq_ = grpc_completion_queue_create(nullptr); }
Vijay Paiced73bd2015-06-17 10:23:28 -0700149
150 void TeardownRequest() {
151 grpc_completion_queue_destroy(cq_);
152 cq_ = nullptr;
153 }
154
Craig Tillerf9e6adf2015-05-06 11:45:59 -0700155 void Request(grpc_server* server, grpc_completion_queue* notify_cq) {
Vijay Paiced73bd2015-06-17 10:23:28 -0700156 GPR_ASSERT(cq_ && !in_flight_);
Craig Tillerc4165772015-02-11 10:51:04 -0800157 in_flight_ = true;
yang-g9b7757d2015-08-13 11:15:53 -0700158 if (tag_) {
159 GPR_ASSERT(GRPC_CALL_OK ==
160 grpc_server_request_registered_call(
161 server, tag_, &call_, &deadline_, &request_metadata_,
162 has_request_payload_ ? &request_payload_ : nullptr, cq_,
163 notify_cq, this));
164 } else {
165 if (!call_details_) {
166 call_details_ = new grpc_call_details;
167 grpc_call_details_init(call_details_);
168 }
169 GPR_ASSERT(GRPC_CALL_OK == grpc_server_request_call(
170 server, &call_, call_details_,
171 &request_metadata_, cq_, notify_cq, this));
172 }
Craig Tillerc4165772015-02-11 10:51:04 -0800173 }
174
Vijay Paic0b2acb2016-11-01 16:31:56 -0700175 bool FinalizeResult(void** tag, bool* status) override {
Craig Tillerec3257c2015-02-12 15:59:43 -0800176 if (!*status) {
177 grpc_completion_queue_destroy(cq_);
178 }
yang-g9b7757d2015-08-13 11:15:53 -0700179 if (call_details_) {
180 deadline_ = call_details_->deadline;
181 grpc_call_details_destroy(call_details_);
182 grpc_call_details_init(call_details_);
183 }
Craig Tiller645466e2015-02-18 09:18:33 -0800184 return true;
Craig Tillerec3257c2015-02-12 15:59:43 -0800185 }
Craig Tillerc4165772015-02-11 10:51:04 -0800186
Vijay Paic0b2acb2016-11-01 16:31:56 -0700187 class CallData final {
Craig Tillerc4165772015-02-11 10:51:04 -0800188 public:
Craig Tiller1c9a2a92015-02-12 14:10:25 -0800189 explicit CallData(Server* server, SyncRequest* mrd)
Craig Tillerc4165772015-02-11 10:51:04 -0800190 : cq_(mrd->cq_),
yang-gf07ed452017-02-16 23:01:28 -0800191 call_(mrd->call_, server, &cq_, server->max_receive_message_size()),
yang-gc42d8442017-02-15 00:05:00 -0800192 ctx_(mrd->deadline_, &mrd->request_metadata_),
Craig Tillerc4165772015-02-11 10:51:04 -0800193 has_request_payload_(mrd->has_request_payload_),
Craig Tillerc4165772015-02-11 10:51:04 -0800194 request_payload_(mrd->request_payload_),
195 method_(mrd->method_) {
yang-g85c04f92015-07-07 17:47:31 -0700196 ctx_.set_call(mrd->call_);
Yang Gao1205f6f2015-03-22 15:18:14 -0700197 ctx_.cq_ = &cq_;
Craig Tillerc4165772015-02-11 10:51:04 -0800198 GPR_ASSERT(mrd->in_flight_);
199 mrd->in_flight_ = false;
200 mrd->request_metadata_.count = 0;
201 }
202
Craig Tillerec3257c2015-02-12 15:59:43 -0800203 ~CallData() {
204 if (has_request_payload_ && request_payload_) {
205 grpc_byte_buffer_destroy(request_payload_);
206 }
207 }
208
Craig Tiller64616492016-01-26 14:16:20 -0800209 void Run(std::shared_ptr<GlobalCallbacks> global_callbacks) {
Craig Tiller492968f2015-02-18 13:14:03 -0800210 ctx_.BeginCompletionOp(&call_);
Craig Tiller64616492016-01-26 14:16:20 -0800211 global_callbacks->PreSynchronousRequest(&ctx_);
Craig Tiller62f28bf2017-01-19 10:05:13 -0800212 method_->handler()->RunHandler(
213 MethodHandler::HandlerParameter(&call_, &ctx_, request_payload_));
Craig Tiller64616492016-01-26 14:16:20 -0800214 global_callbacks->PostSynchronousRequest(&ctx_);
Craig Tiller928ec8e2015-06-05 08:45:45 -0700215 request_payload_ = nullptr;
Craig Tiller492968f2015-02-18 13:14:03 -0800216 void* ignored_tag;
217 bool ignored_ok;
218 cq_.Shutdown();
219 GPR_ASSERT(cq_.Next(&ignored_tag, &ignored_ok) == false);
Craig Tillerc4165772015-02-11 10:51:04 -0800220 }
221
222 private:
223 CompletionQueue cq_;
224 Call call_;
225 ServerContext ctx_;
226 const bool has_request_payload_;
Craig Tiller8c8d0aa2015-02-12 11:38:36 -0800227 grpc_byte_buffer* request_payload_;
228 RpcServiceMethod* const method_;
Craig Tillerc4165772015-02-11 10:51:04 -0800229 };
230
231 private:
Craig Tiller8c8d0aa2015-02-12 11:38:36 -0800232 RpcServiceMethod* const method_;
233 void* const tag_;
Craig Tillercf133f42015-02-26 14:05:56 -0800234 bool in_flight_;
Craig Tillerc4165772015-02-11 10:51:04 -0800235 const bool has_request_payload_;
Craig Tiller8c8d0aa2015-02-12 11:38:36 -0800236 grpc_call* call_;
yang-g9b7757d2015-08-13 11:15:53 -0700237 grpc_call_details* call_details_;
Craig Tillerc4165772015-02-11 10:51:04 -0800238 gpr_timespec deadline_;
239 grpc_metadata_array request_metadata_;
Craig Tiller8c8d0aa2015-02-12 11:38:36 -0800240 grpc_byte_buffer* request_payload_;
241 grpc_completion_queue* cq_;
Craig Tillerc4165772015-02-11 10:51:04 -0800242};
243
Sree Kuchibhotla8f7739b2016-10-13 15:12:55 -0700244// Implementation of ThreadManager. Each instance of SyncRequestThreadManager
245// manages a pool of threads that poll for incoming Sync RPCs and call the
246// appropriate RPC handlers
247class Server::SyncRequestThreadManager : public ThreadManager {
Sree Kuchibhotlaaabada92016-08-24 10:01:13 -0700248 public:
Sree Kuchibhotla8f7739b2016-10-13 15:12:55 -0700249 SyncRequestThreadManager(Server* server, CompletionQueue* server_cq,
250 std::shared_ptr<GlobalCallbacks> global_callbacks,
251 int min_pollers, int max_pollers,
252 int cq_timeout_msec)
253 : ThreadManager(min_pollers, max_pollers),
Sree Kuchibhotlaaabada92016-08-24 10:01:13 -0700254 server_(server),
255 server_cq_(server_cq),
Sree Kuchibhotla892dbf42016-09-27 19:42:27 -0700256 cq_timeout_msec_(cq_timeout_msec),
Sree Kuchibhotlaaabada92016-08-24 10:01:13 -0700257 global_callbacks_(global_callbacks) {}
258
Vijay Paic0b2acb2016-11-01 16:31:56 -0700259 WorkStatus PollForWork(void** tag, bool* ok) override {
Sree Kuchibhotlaaabada92016-08-24 10:01:13 -0700260 *tag = nullptr;
261 gpr_timespec deadline =
Sree Kuchibhotla892dbf42016-09-27 19:42:27 -0700262 gpr_time_from_millis(cq_timeout_msec_, GPR_TIMESPAN);
Sree Kuchibhotlaaabada92016-08-24 10:01:13 -0700263
264 switch (server_cq_->AsyncNext(tag, ok, deadline)) {
265 case CompletionQueue::TIMEOUT:
266 return TIMEOUT;
267 case CompletionQueue::SHUTDOWN:
268 return SHUTDOWN;
269 case CompletionQueue::GOT_EVENT:
270 return WORK_FOUND;
271 }
272
273 GPR_UNREACHABLE_CODE(return TIMEOUT);
274 }
275
Vijay Paic0b2acb2016-11-01 16:31:56 -0700276 void DoWork(void* tag, bool ok) override {
Sree Kuchibhotlaaabada92016-08-24 10:01:13 -0700277 SyncRequest* sync_req = static_cast<SyncRequest*>(tag);
Sree Kuchibhotla4306eee2016-09-21 09:56:29 -0700278
279 if (!sync_req) {
Sree Kuchibhotla33382d02016-10-03 15:08:48 -0700280 // No tag. Nothing to work on. This is an unlikley scenario and possibly a
281 // bug in RPC Manager implementation.
282 gpr_log(GPR_ERROR, "Sync server. DoWork() was called with NULL tag");
Sree Kuchibhotla4306eee2016-09-21 09:56:29 -0700283 return;
284 }
285
286 if (ok) {
Sree Kuchibhotla33382d02016-10-03 15:08:48 -0700287 // Calldata takes ownership of the completion queue inside sync_req
Sree Kuchibhotlaaabada92016-08-24 10:01:13 -0700288 SyncRequest::CallData cd(server_, sync_req);
289 {
Sree Kuchibhotla33382d02016-10-03 15:08:48 -0700290 // Prepare for the next request
Sree Kuchibhotlaaabada92016-08-24 10:01:13 -0700291 if (!IsShutdown()) {
Sree Kuchibhotla33382d02016-10-03 15:08:48 -0700292 sync_req->SetupRequest(); // Create new completion queue for sync_req
Sree Kuchibhotlaaabada92016-08-24 10:01:13 -0700293 sync_req->Request(server_->c_server(), server_cq_->cq());
Sree Kuchibhotlaaabada92016-08-24 10:01:13 -0700294 }
295 }
Sree Kuchibhotla33382d02016-10-03 15:08:48 -0700296
Sree Kuchibhotlaaabada92016-08-24 10:01:13 -0700297 GPR_TIMER_SCOPE("cd.Run()", 0);
298 cd.Run(global_callbacks_);
299 }
Sree Kuchibhotla33382d02016-10-03 15:08:48 -0700300 // TODO (sreek) If ok is false here (which it isn't in case of
301 // grpc_request_registered_call), we should still re-queue the request
302 // object
Sree Kuchibhotlaaabada92016-08-24 10:01:13 -0700303 }
304
305 void AddSyncMethod(RpcServiceMethod* method, void* tag) {
Sree Kuchibhotlacb18d7a2016-10-20 12:39:30 -0700306 sync_requests_.emplace_back(new SyncRequest(method, tag));
Sree Kuchibhotlaaabada92016-08-24 10:01:13 -0700307 }
308
309 void AddUnknownSyncMethod() {
Sree Kuchibhotlada069a52016-10-19 11:22:29 -0700310 if (!sync_requests_.empty()) {
Sree Kuchibhotlaaabada92016-08-24 10:01:13 -0700311 unknown_method_.reset(new RpcServiceMethod(
312 "unknown", RpcMethod::BIDI_STREAMING, new UnknownMethodHandler));
Sree Kuchibhotlacb18d7a2016-10-20 12:39:30 -0700313 sync_requests_.emplace_back(
314 new SyncRequest(unknown_method_.get(), nullptr));
Sree Kuchibhotlaaabada92016-08-24 10:01:13 -0700315 }
316 }
317
Sree Kuchibhotla862acb92016-09-26 09:48:48 -0700318 void ShutdownAndDrainCompletionQueue() {
319 server_cq_->Shutdown();
320
321 // Drain any pending items from the queue
322 void* tag;
323 bool ok;
324 while (server_cq_->Next(&tag, &ok)) {
325 // Nothing to be done here
326 }
327 }
328
Sree Kuchibhotlaaabada92016-08-24 10:01:13 -0700329 void Start() {
Sree Kuchibhotlada069a52016-10-19 11:22:29 -0700330 if (!sync_requests_.empty()) {
331 for (auto m = sync_requests_.begin(); m != sync_requests_.end(); m++) {
Sree Kuchibhotlacb18d7a2016-10-20 12:39:30 -0700332 (*m)->SetupRequest();
333 (*m)->Request(server_->c_server(), server_cq_->cq());
Sree Kuchibhotlaaabada92016-08-24 10:01:13 -0700334 }
335
Sree Kuchibhotlacb18d7a2016-10-20 12:39:30 -0700336 Initialize(); // ThreadManager's Initialize()
Sree Kuchibhotlaaabada92016-08-24 10:01:13 -0700337 }
338 }
339
340 private:
341 Server* server_;
342 CompletionQueue* server_cq_;
Sree Kuchibhotla892dbf42016-09-27 19:42:27 -0700343 int cq_timeout_msec_;
Sree Kuchibhotlacb18d7a2016-10-20 12:39:30 -0700344 std::vector<std::unique_ptr<SyncRequest>> sync_requests_;
Sree Kuchibhotlaaabada92016-08-24 10:01:13 -0700345 std::unique_ptr<RpcServiceMethod> unknown_method_;
yang-g8d668d82016-12-01 15:09:28 -0800346 std::unique_ptr<RpcServiceMethod> health_check_;
Sree Kuchibhotlaaabada92016-08-24 10:01:13 -0700347 std::shared_ptr<Server::GlobalCallbacks> global_callbacks_;
348};
349
David Garcia Quintasd79ef3a2016-01-28 00:21:27 -0800350static internal::GrpcLibraryInitializer g_gli_initializer;
Sree Kuchibhotlaaabada92016-08-24 10:01:13 -0700351Server::Server(
Sree Kuchibhotlae4eb51f2016-10-18 11:51:28 -0700352 int max_receive_message_size, ChannelArguments* args,
Sree Kuchibhotla4028d2c2016-09-21 10:45:33 -0700353 std::shared_ptr<std::vector<std::unique_ptr<ServerCompletionQueue>>>
354 sync_server_cqs,
Sree Kuchibhotlae4eb51f2016-10-18 11:51:28 -0700355 int min_pollers, int max_pollers, int sync_cq_timeout_msec)
Mark D. Roth69803622016-09-06 08:15:36 -0700356 : max_receive_message_size_(max_receive_message_size),
Sree Kuchibhotlaaabada92016-08-24 10:01:13 -0700357 sync_server_cqs_(sync_server_cqs),
Yang Gao3921c562015-04-30 16:07:06 -0700358 started_(false),
vjpaicad5f0a2015-02-18 22:02:52 -0800359 shutdown_(false),
yang-g6ec11f22016-07-14 14:53:35 -0700360 shutdown_notified_(false),
yang-g9b7757d2015-08-13 11:15:53 -0700361 has_generic_service_(false),
yang-geb62c942016-02-17 15:37:25 -0800362 server_(nullptr),
yang-gad327642016-12-12 14:32:09 -0800363 server_initializer_(new ServerInitializer(this)),
364 health_check_service_disabled_(false) {
David Garcia Quintasd79ef3a2016-01-28 00:21:27 -0800365 g_gli_initializer.summon();
Craig Tiller8352b9e2015-12-02 11:43:40 -0800366 gpr_once_init(&g_once_init_callbacks, InitGlobalCallbacks);
Craig Tiller64616492016-01-26 14:16:20 -0800367 global_callbacks_ = g_callbacks;
yang-geb62c942016-02-17 15:37:25 -0800368 global_callbacks_->UpdateArguments(args);
Sree Kuchibhotlaaabada92016-08-24 10:01:13 -0700369
370 for (auto it = sync_server_cqs_->begin(); it != sync_server_cqs_->end();
371 it++) {
Sree Kuchibhotla8f7739b2016-10-13 15:12:55 -0700372 sync_req_mgrs_.emplace_back(new SyncRequestThreadManager(
373 this, (*it).get(), global_callbacks_, min_pollers, max_pollers,
374 sync_cq_timeout_msec));
Sree Kuchibhotlaaabada92016-08-24 10:01:13 -0700375 }
376
yang-geb62c942016-02-17 15:37:25 -0800377 grpc_channel_args channel_args;
378 args->SetChannelArgs(&channel_args);
Sree Kuchibhotla3ea9e242016-08-22 14:15:43 -0700379
yang-g8d668d82016-12-01 15:09:28 -0800380 for (size_t i = 0; i < channel_args.num_args; i++) {
yang-gb90631d2016-12-29 14:08:13 -0800381 if (0 ==
382 strcmp(channel_args.args[i].key, kHealthCheckServiceInterfaceArg)) {
yang-g6d0fbfa2016-12-06 16:49:04 -0800383 if (channel_args.args[i].value.pointer.p == nullptr) {
yang-gad327642016-12-12 14:32:09 -0800384 health_check_service_disabled_ = true;
yang-g8d668d82016-12-01 15:09:28 -0800385 } else {
yang-g6d0fbfa2016-12-06 16:49:04 -0800386 health_check_service_.reset(static_cast<HealthCheckServiceInterface*>(
387 channel_args.args[i].value.pointer.p));
yang-g8d668d82016-12-01 15:09:28 -0800388 }
389 break;
390 }
391 }
392
Sree Kuchibhotlaaabada92016-08-24 10:01:13 -0700393 server_ = grpc_server_create(&channel_args, nullptr);
Craig Tillerf9e6adf2015-05-06 11:45:59 -0700394}
vjpaicad5f0a2015-02-18 22:02:52 -0800395
396Server::~Server() {
Ruyi Wangb486ba62015-03-14 22:19:44 +0800397 {
Vijay Pai320ed132016-11-01 17:16:55 -0700398 std::unique_lock<std::mutex> lock(mu_);
Ruyi Wangb486ba62015-03-14 22:19:44 +0800399 if (started_ && !shutdown_) {
400 lock.unlock();
401 Shutdown();
yang-gb9711732016-01-15 16:53:08 -0800402 } else if (!started_) {
Sree Kuchibhotla862acb92016-09-26 09:48:48 -0700403 // Shutdown the completion queues
404 for (auto it = sync_req_mgrs_.begin(); it != sync_req_mgrs_.end(); it++) {
405 (*it)->ShutdownAndDrainCompletionQueue();
Sree Kuchibhotla4306eee2016-09-21 09:56:29 -0700406 }
Ruyi Wangb486ba62015-03-14 22:19:44 +0800407 }
vjpaicad5f0a2015-02-18 22:02:52 -0800408 }
Sree Kuchibhotlabb5519f2016-07-19 11:00:39 -0700409
vjpaicad5f0a2015-02-18 22:02:52 -0800410 grpc_server_destroy(server_);
vjpaicad5f0a2015-02-18 22:02:52 -0800411}
412
Craig Tiller7221d992015-11-24 06:59:33 -0800413void Server::SetGlobalCallbacks(GlobalCallbacks* callbacks) {
Vijay Paib645a2d2016-06-09 18:39:06 -0700414 GPR_ASSERT(!g_callbacks);
415 GPR_ASSERT(callbacks);
Craig Tiller64616492016-01-26 14:16:20 -0800416 g_callbacks.reset(callbacks);
Craig Tiller7221d992015-11-24 06:59:33 -0800417}
418
Adam Michalik4ad746e2016-06-07 15:02:59 -0700419grpc_server* Server::c_server() { return server_; }
Adam Michalikb97e2d12016-06-02 12:12:55 -0700420
Craig Tiller06cb1a92016-04-04 08:10:47 -0700421static grpc_server_register_method_payload_handling PayloadHandlingForMethod(
422 RpcServiceMethod* method) {
423 switch (method->method_type()) {
424 case RpcMethod::NORMAL_RPC:
425 case RpcMethod::SERVER_STREAMING:
426 return GRPC_SRM_PAYLOAD_READ_INITIAL_BYTE_BUFFER;
427 case RpcMethod::CLIENT_STREAMING:
428 case RpcMethod::BIDI_STREAMING:
429 return GRPC_SRM_PAYLOAD_NONE;
430 }
431 GPR_UNREACHABLE_CODE(return GRPC_SRM_PAYLOAD_NONE;);
432}
433
Craig Tiller15f383c2016-01-07 12:45:32 -0800434bool Server::RegisterService(const grpc::string* host, Service* service) {
435 bool has_async_methods = service->has_async_methods();
436 if (has_async_methods) {
437 GPR_ASSERT(service->server_ == nullptr &&
438 "Can only register an asynchronous service against one server.");
439 service->server_ = this;
440 }
Sree Kuchibhotlaaabada92016-08-24 10:01:13 -0700441
Yuchen Zenga42ec212016-04-29 13:03:06 -0700442 const char* method_name = nullptr;
Craig Tiller15f383c2016-01-07 12:45:32 -0800443 for (auto it = service->methods_.begin(); it != service->methods_.end();
444 ++it) {
yang-g269b8be2016-01-15 10:46:26 -0800445 if (it->get() == nullptr) { // Handled by generic service if any.
446 continue;
447 }
Sree Kuchibhotlada069a52016-10-19 11:22:29 -0700448
Craig Tiller15f383c2016-01-07 12:45:32 -0800449 RpcServiceMethod* method = it->get();
Craig Tiller06cb1a92016-04-04 08:10:47 -0700450 void* tag = grpc_server_register_method(
451 server_, method->name(), host ? host->c_str() : nullptr,
452 PayloadHandlingForMethod(method), 0);
Craig Tiller15f383c2016-01-07 12:45:32 -0800453 if (tag == nullptr) {
vjpaicad5f0a2015-02-18 22:02:52 -0800454 gpr_log(GPR_DEBUG, "Attempt to register %s multiple times",
455 method->name());
456 return false;
457 }
Sree Kuchibhotlada069a52016-10-19 11:22:29 -0700458
Sree Kuchibhotlacb18d7a2016-10-20 12:39:30 -0700459 if (method->handler() == nullptr) { // Async method
Craig Tiller15f383c2016-01-07 12:45:32 -0800460 method->set_server_tag(tag);
461 } else {
Sree Kuchibhotlaaabada92016-08-24 10:01:13 -0700462 for (auto it = sync_req_mgrs_.begin(); it != sync_req_mgrs_.end(); it++) {
463 (*it)->AddSyncMethod(method, tag);
464 }
vjpaicad5f0a2015-02-18 22:02:52 -0800465 }
Sree Kuchibhotlada069a52016-10-19 11:22:29 -0700466
Yuchen Zenga42ec212016-04-29 13:03:06 -0700467 method_name = method->name();
468 }
469
470 // Parse service name.
471 if (method_name != nullptr) {
472 std::stringstream ss(method_name);
473 grpc::string service_name;
474 if (std::getline(ss, service_name, '/') &&
475 std::getline(ss, service_name, '/')) {
476 services_.push_back(service_name);
477 }
vjpaicad5f0a2015-02-18 22:02:52 -0800478 }
479 return true;
480}
481
Yang Gao49996492015-03-12 16:40:19 -0700482void Server::RegisterAsyncGenericService(AsyncGenericService* service) {
Yang Gao1c402332015-03-05 16:39:25 -0800483 GPR_ASSERT(service->server_ == nullptr &&
Yang Gao1ad253d2015-03-16 13:11:29 -0700484 "Can only register an async generic service against one server.");
Yang Gao1c402332015-03-05 16:39:25 -0800485 service->server_ = this;
yang-g9b7757d2015-08-13 11:15:53 -0700486 has_generic_service_ = true;
Yang Gao1c402332015-03-05 16:39:25 -0800487}
488
Nicolas Noblecfd60732015-03-18 16:27:43 -0700489int Server::AddListeningPort(const grpc::string& addr,
490 ServerCredentials* creds) {
vjpaicad5f0a2015-02-18 22:02:52 -0800491 GPR_ASSERT(!started_);
Craig Tiller42bc87c2015-02-23 08:50:19 -0800492 return creds->AddPortToServer(addr, server_);
vjpaicad5f0a2015-02-18 22:02:52 -0800493}
494
Craig Tiller8f7bff72015-08-17 13:23:14 -0700495bool Server::Start(ServerCompletionQueue** cqs, size_t num_cqs) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800496 GPR_ASSERT(!started_);
yang-gf2fe4f72017-02-07 12:47:22 -0800497 global_callbacks_->PreServerStart(this);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800498 started_ = true;
yang-gad327642016-12-12 14:32:09 -0800499
500 // Only create default health check service when user did not provide an
501 // explicit one.
502 if (health_check_service_ == nullptr && !health_check_service_disabled_ &&
503 DefaultHealthCheckServiceEnabled()) {
yang-g076bac02017-02-07 13:50:36 -0800504 if (sync_server_cqs_->empty()) {
505 gpr_log(GPR_ERROR,
506 "Default health check service disabled at async-only server.");
507 } else {
508 auto* default_hc_service = new DefaultHealthCheckService;
509 health_check_service_.reset(default_hc_service);
510 RegisterService(nullptr, default_hc_service->GetHealthCheckService());
511 }
yang-gad327642016-12-12 14:32:09 -0800512 }
513
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800514 grpc_server_start(server_);
515
yang-g9b7757d2015-08-13 11:15:53 -0700516 if (!has_generic_service_) {
Sree Kuchibhotlaaabada92016-08-24 10:01:13 -0700517 for (auto it = sync_req_mgrs_.begin(); it != sync_req_mgrs_.end(); it++) {
518 (*it)->AddUnknownSyncMethod();
Craig Tiller86d31772015-08-19 12:52:50 -0700519 }
Sree Kuchibhotla3ea9e242016-08-22 14:15:43 -0700520
Craig Tiller8f7bff72015-08-17 13:23:14 -0700521 for (size_t i = 0; i < num_cqs; i++) {
Craig Tillere0049582016-05-20 10:31:09 -0700522 if (cqs[i]->IsFrequentlyPolled()) {
523 new UnimplementedAsyncRequest(this, cqs[i]);
524 }
Craig Tillercbd04852015-02-10 17:39:54 -0800525 }
yang-g9b7757d2015-08-13 11:15:53 -0700526 }
Sree Kuchibhotla3ea9e242016-08-22 14:15:43 -0700527
Sree Kuchibhotlaaabada92016-08-24 10:01:13 -0700528 for (auto it = sync_req_mgrs_.begin(); it != sync_req_mgrs_.end(); it++) {
529 (*it)->Start();
530 }
531
Craig Tiller0db1bef2015-02-09 13:47:39 -0800532 return true;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800533}
534
Craig Tillere50e5cb2015-08-18 12:44:57 -0700535void Server::ShutdownInternal(gpr_timespec deadline) {
Vijay Pai320ed132016-11-01 17:16:55 -0700536 std::unique_lock<std::mutex> lock(mu_);
Craig Tiller6e57b9e2015-02-24 15:46:22 -0800537 if (started_ && !shutdown_) {
538 shutdown_ = true;
Sree Kuchibhotlaaabada92016-08-24 10:01:13 -0700539
Sree Kuchibhotla862acb92016-09-26 09:48:48 -0700540 /// The completion queue to use for server shutdown completion notification
541 CompletionQueue shutdown_cq;
Sree Kuchibhotla4028d2c2016-09-21 10:45:33 -0700542 ShutdownTag shutdown_tag; // Dummy shutdown tag
Sree Kuchibhotla862acb92016-09-26 09:48:48 -0700543 grpc_server_shutdown_and_notify(server_, shutdown_cq.cq(), &shutdown_tag);
Sree Kuchibhotlaaabada92016-08-24 10:01:13 -0700544
Sree Kuchibhotla862acb92016-09-26 09:48:48 -0700545 shutdown_cq.Shutdown();
Sree Kuchibhotlaaabada92016-08-24 10:01:13 -0700546
547 void* tag;
548 bool ok;
549 CompletionQueue::NextStatus status =
Sree Kuchibhotla862acb92016-09-26 09:48:48 -0700550 shutdown_cq.AsyncNext(&tag, &ok, deadline);
Sree Kuchibhotlaaabada92016-08-24 10:01:13 -0700551
Sree Kuchibhotla862acb92016-09-26 09:48:48 -0700552 // If this timed out, it means we are done with the grace period for a clean
553 // shutdown. We should force a shutdown now by cancelling all inflight calls
Sree Kuchibhotlaaabada92016-08-24 10:01:13 -0700554 if (status == CompletionQueue::NextStatus::TIMEOUT) {
555 grpc_server_cancel_all_calls(server_);
556 }
557 // Else in case of SHUTDOWN or GOT_EVENT, it means that the server has
558 // successfully shutdown
559
Craig Tiller9c5318a2016-12-05 15:07:04 -0800560 // Shutdown all ThreadManagers. This will try to gracefully stop all the
561 // threads in the ThreadManagers (once they process any inflight requests)
562 for (auto it = sync_req_mgrs_.begin(); it != sync_req_mgrs_.end(); it++) {
563 (*it)->Shutdown(); // ThreadManager's Shutdown()
564 }
565
Sree Kuchibhotla8f7739b2016-10-13 15:12:55 -0700566 // Wait for threads in all ThreadManagers to terminate
Sree Kuchibhotlaaabada92016-08-24 10:01:13 -0700567 for (auto it = sync_req_mgrs_.begin(); it != sync_req_mgrs_.end(); it++) {
568 (*it)->Wait();
Sree Kuchibhotla862acb92016-09-26 09:48:48 -0700569 (*it)->ShutdownAndDrainCompletionQueue();
Sree Kuchibhotlaaabada92016-08-24 10:01:13 -0700570 }
571
Sree Kuchibhotla862acb92016-09-26 09:48:48 -0700572 // Drain the shutdown queue (if the previous call to AsyncNext() timed out
573 // and we didn't remove the tag from the queue yet)
Sree Kuchibhotla892dbf42016-09-27 19:42:27 -0700574 while (shutdown_cq.Next(&tag, &ok)) {
Sree Kuchibhotla33382d02016-10-03 15:08:48 -0700575 // Nothing to be done here. Just ignore ok and tag values
Sree Kuchibhotlaaabada92016-08-24 10:01:13 -0700576 }
577
yang-g6ec11f22016-07-14 14:53:35 -0700578 shutdown_notified_ = true;
yang-ge89dc6c2016-07-11 15:48:01 -0700579 shutdown_cv_.notify_all();
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800580 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800581}
582
Craig Tiller6e57b9e2015-02-24 15:46:22 -0800583void Server::Wait() {
Vijay Pai320ed132016-11-01 17:16:55 -0700584 std::unique_lock<std::mutex> lock(mu_);
yang-g6ec11f22016-07-14 14:53:35 -0700585 while (started_ && !shutdown_notified_) {
586 shutdown_cv_.wait(lock);
587 }
Craig Tiller6e57b9e2015-02-24 15:46:22 -0800588}
589
Craig Tiller50a7a682015-06-04 12:53:40 -0700590void Server::PerformOpsOnCall(CallOpSetInterface* ops, Call* call) {
Craig Tillerbb5227f2015-02-11 13:34:48 -0800591 static const size_t MAX_OPS = 8;
Craig Tiller50a7a682015-06-04 12:53:40 -0700592 size_t nops = 0;
593 grpc_op cops[MAX_OPS];
594 ops->FillOps(cops, &nops);
Nicolas "Pixel" Nobleebb51402015-07-23 02:41:33 +0200595 auto result = grpc_call_start_batch(call->call(), cops, nops, ops, nullptr);
596 GPR_ASSERT(GRPC_CALL_OK == result);
Craig Tillerbb5227f2015-02-11 13:34:48 -0800597}
598
David Garcia Quintas44f32492016-01-14 18:00:04 -0800599ServerInterface::BaseAsyncRequest::BaseAsyncRequest(
600 ServerInterface* server, ServerContext* context,
Craig Tiller8f7bff72015-08-17 13:23:14 -0700601 ServerAsyncStreamingInterface* stream, CompletionQueue* call_cq, void* tag,
602 bool delete_on_finalize)
Craig Tillerce40de52015-06-05 07:14:58 -0700603 : server_(server),
604 context_(context),
605 stream_(stream),
606 call_cq_(call_cq),
Craig Tiller50955812015-06-05 08:03:17 -0700607 tag_(tag),
Craig Tiller8f7bff72015-08-17 13:23:14 -0700608 delete_on_finalize_(delete_on_finalize),
Craig Tillerce40de52015-06-05 07:14:58 -0700609 call_(nullptr) {
Vijay Paibf24dd92016-12-05 13:59:09 -0800610 call_cq_->RegisterAvalanching(); // This op will trigger more ops
Craig Tiller7bc97bc2015-06-04 17:22:54 -0700611}
612
Vijay Paicbe15992016-12-05 13:56:29 -0800613ServerInterface::BaseAsyncRequest::~BaseAsyncRequest() {
614 call_cq_->CompleteAvalanching();
615}
616
David Garcia Quintasf3ddb7c2016-01-20 16:02:22 -0800617bool ServerInterface::BaseAsyncRequest::FinalizeResult(void** tag,
618 bool* status) {
Craig Tiller7bc97bc2015-06-04 17:22:54 -0700619 if (*status) {
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800620 context_->client_metadata_.FillMap();
Craig Tiller7bc97bc2015-06-04 17:22:54 -0700621 }
yang-g85c04f92015-07-07 17:47:31 -0700622 context_->set_call(call_);
Craig Tiller7bc97bc2015-06-04 17:22:54 -0700623 context_->cq_ = call_cq_;
yang-gf07ed452017-02-16 23:01:28 -0800624 Call call(call_, server_, call_cq_, server_->max_receive_message_size());
Craig Tiller7bc97bc2015-06-04 17:22:54 -0700625 if (*status && call_) {
626 context_->BeginCompletionOp(&call);
627 }
628 // just the pointers inside call are copied here
629 stream_->BindCall(&call);
Craig Tiller50955812015-06-05 08:03:17 -0700630 *tag = tag_;
Craig Tiller8f7bff72015-08-17 13:23:14 -0700631 if (delete_on_finalize_) {
632 delete this;
633 }
Craig Tiller7bc97bc2015-06-04 17:22:54 -0700634 return true;
635}
636
David Garcia Quintas44f32492016-01-14 18:00:04 -0800637ServerInterface::RegisteredAsyncRequest::RegisteredAsyncRequest(
638 ServerInterface* server, ServerContext* context,
yang-g076bac02017-02-07 13:50:36 -0800639 ServerAsyncStreamingInterface* stream, CompletionQueue* call_cq, void* tag)
640 : BaseAsyncRequest(server, context, stream, call_cq, tag, true) {}
Craig Tiller7bc97bc2015-06-04 17:22:54 -0700641
David Garcia Quintas44f32492016-01-14 18:00:04 -0800642void ServerInterface::RegisteredAsyncRequest::IssueRequest(
Craig Tillerce40de52015-06-05 07:14:58 -0700643 void* registered_method, grpc_byte_buffer** payload,
644 ServerCompletionQueue* notification_cq) {
Craig Tiller7bc97bc2015-06-04 17:22:54 -0700645 grpc_server_request_registered_call(
David Garcia Quintas44f32492016-01-14 18:00:04 -0800646 server_->server(), registered_method, &call_, &context_->deadline_,
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800647 context_->client_metadata_.arr(), payload, call_cq_->cq(),
648 notification_cq->cq(), this);
Craig Tiller7bc97bc2015-06-04 17:22:54 -0700649}
650
David Garcia Quintas44f32492016-01-14 18:00:04 -0800651ServerInterface::GenericAsyncRequest::GenericAsyncRequest(
652 ServerInterface* server, GenericServerContext* context,
Craig Tillerce40de52015-06-05 07:14:58 -0700653 ServerAsyncStreamingInterface* stream, CompletionQueue* call_cq,
Craig Tiller8f7bff72015-08-17 13:23:14 -0700654 ServerCompletionQueue* notification_cq, void* tag, bool delete_on_finalize)
655 : BaseAsyncRequest(server, context, stream, call_cq, tag,
656 delete_on_finalize) {
Craig Tiller7bc97bc2015-06-04 17:22:54 -0700657 grpc_call_details_init(&call_details_);
658 GPR_ASSERT(notification_cq);
659 GPR_ASSERT(call_cq);
David Garcia Quintas44f32492016-01-14 18:00:04 -0800660 grpc_server_request_call(server->server(), &call_, &call_details_,
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800661 context->client_metadata_.arr(), call_cq->cq(),
Craig Tillerce40de52015-06-05 07:14:58 -0700662 notification_cq->cq(), this);
Craig Tiller7bc97bc2015-06-04 17:22:54 -0700663}
664
David Garcia Quintasf3ddb7c2016-01-20 16:02:22 -0800665bool ServerInterface::GenericAsyncRequest::FinalizeResult(void** tag,
666 bool* status) {
Craig Tiller7bc97bc2015-06-04 17:22:54 -0700667 // TODO(yangg) remove the copy here.
yang-g3deb0062015-06-23 14:06:08 -0700668 if (*status) {
yang-ga58cab32015-06-23 14:37:15 -0700669 static_cast<GenericServerContext*>(context_)->method_ =
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800670 StringFromCopiedSlice(call_details_.method);
671 static_cast<GenericServerContext*>(context_)->host_ =
672 StringFromCopiedSlice(call_details_.host);
yang-g3deb0062015-06-23 14:06:08 -0700673 }
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800674 grpc_slice_unref(call_details_.method);
675 grpc_slice_unref(call_details_.host);
Craig Tiller7bc97bc2015-06-04 17:22:54 -0700676 return BaseAsyncRequest::FinalizeResult(tag, status);
677}
678
Craig Tiller8f7bff72015-08-17 13:23:14 -0700679bool Server::UnimplementedAsyncRequest::FinalizeResult(void** tag,
680 bool* status) {
681 if (GenericAsyncRequest::FinalizeResult(tag, status) && *status) {
682 new UnimplementedAsyncRequest(server_, cq_);
683 new UnimplementedAsyncResponse(this);
684 } else {
685 delete this;
686 }
687 return false;
688}
689
690Server::UnimplementedAsyncResponse::UnimplementedAsyncResponse(
691 UnimplementedAsyncRequest* request)
692 : request_(request) {
693 Status status(StatusCode::UNIMPLEMENTED, "");
694 UnknownMethodHandler::FillOps(request_->context(), this);
695 request_->stream()->call_.PerformOps(this);
696}
697
Yuchen Zenga42ec212016-04-29 13:03:06 -0700698ServerInitializer* Server::initializer() { return server_initializer_.get(); }
699
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800700} // namespace grpc