server_interface.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. //
  2. //
  3. // Copyright 2015 gRPC authors.
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License");
  6. // you may not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. //
  17. //
  18. #ifndef GRPCPP_SERVER_INTERFACE_H
  19. #define GRPCPP_SERVER_INTERFACE_H
  20. #include <grpc/support/port_platform.h>
  21. #include <grpc/grpc.h>
  22. #include <grpc/impl/grpc_types.h>
  23. #include <grpc/support/log.h>
  24. #include <grpc/support/time.h>
  25. #include <grpcpp/impl/call.h>
  26. #include <grpcpp/impl/call_hook.h>
  27. #include <grpcpp/impl/codegen/interceptor_common.h>
  28. #include <grpcpp/impl/completion_queue_tag.h>
  29. #include <grpcpp/impl/rpc_service_method.h>
  30. #include <grpcpp/server_context.h>
  31. #include <grpcpp/support/byte_buffer.h>
  32. namespace grpc {
  33. class AsyncGenericService;
  34. class Channel;
  35. class CompletionQueue;
  36. class GenericServerContext;
  37. class ServerCompletionQueue;
  38. class ServerCredentials;
  39. class Service;
  40. /// Models a gRPC server.
  41. ///
  42. /// Servers are configured and started via \a grpc::ServerBuilder.
  43. namespace internal {
  44. class ServerAsyncStreamingInterface;
  45. } // namespace internal
  46. class CallbackGenericService;
  47. namespace experimental {
  48. class ServerInterceptorFactoryInterface;
  49. class ServerMetricRecorder;
  50. } // namespace experimental
  51. class ServerInterface : public internal::CallHook {
  52. public:
  53. ~ServerInterface() override {}
  54. /// \a Shutdown does the following things:
  55. ///
  56. /// 1. Shutdown the server: deactivate all listening ports, mark it in
  57. /// "shutdown mode" so that further call Request's or incoming RPC matches
  58. /// are no longer allowed. Also return all Request'ed-but-not-yet-active
  59. /// calls as failed (!ok). This refers to calls that have been requested
  60. /// at the server by the server-side library or application code but that
  61. /// have not yet been matched to incoming RPCs from the client. Note that
  62. /// this would even include default calls added automatically by the gRPC
  63. /// C++ API without the user's input (e.g., "Unimplemented RPC method")
  64. ///
  65. /// 2. Block until all rpc method handlers invoked automatically by the sync
  66. /// API finish.
  67. ///
  68. /// 3. If all pending calls complete (and all their operations are
  69. /// retrieved by Next) before \a deadline expires, this finishes
  70. /// gracefully. Otherwise, forcefully cancel all pending calls associated
  71. /// with the server after \a deadline expires. In the case of the sync API,
  72. /// if the RPC function for a streaming call has already been started and
  73. /// takes a week to complete, the RPC function won't be forcefully
  74. /// terminated (since that would leave state corrupt and incomplete) and
  75. /// the method handler will just keep running (which will prevent the
  76. /// server from completing the "join" operation that it needs to do at
  77. /// shutdown time).
  78. ///
  79. /// All completion queue associated with the server (for example, for async
  80. /// serving) must be shutdown *after* this method has returned:
  81. /// See \a ServerBuilder::AddCompletionQueue for details.
  82. /// They must also be drained (by repeated Next) after being shutdown.
  83. ///
  84. /// \param deadline How long to wait until pending rpcs are forcefully
  85. /// terminated.
  86. template <class T>
  87. void Shutdown(const T& deadline) {
  88. ShutdownInternal(TimePoint<T>(deadline).raw_time());
  89. }
  90. /// Shutdown the server without a deadline and forced cancellation.
  91. ///
  92. /// All completion queue associated with the server (for example, for async
  93. /// serving) must be shutdown *after* this method has returned:
  94. /// See \a ServerBuilder::AddCompletionQueue for details.
  95. void Shutdown() { ShutdownInternal(gpr_inf_future(GPR_CLOCK_MONOTONIC)); }
  96. /// Block waiting for all work to complete.
  97. ///
  98. /// \warning The server must be either shutting down or some other thread must
  99. /// call \a Shutdown for this function to ever return.
  100. virtual void Wait() = 0;
  101. protected:
  102. friend class grpc::Service;
  103. /// Register a service. This call does not take ownership of the service.
  104. /// The service must exist for the lifetime of the Server instance.
  105. virtual bool RegisterService(const TString* host, Service* service) = 0;
  106. /// Register a generic service. This call does not take ownership of the
  107. /// service. The service must exist for the lifetime of the Server instance.
  108. virtual void RegisterAsyncGenericService(AsyncGenericService* service) = 0;
  109. /// Register a callback generic service. This call does not take ownership of
  110. /// the service. The service must exist for the lifetime of the Server
  111. /// instance. May not be abstract since this is a post-1.0 API addition.
  112. virtual void RegisterCallbackGenericService(CallbackGenericService*
  113. /*service*/) {}
  114. /// Tries to bind \a server to the given \a addr.
  115. ///
  116. /// It can be invoked multiple times.
  117. ///
  118. /// \param addr The address to try to bind to the server (eg, localhost:1234,
  119. /// 192.168.1.1:31416, [::1]:27182, etc.).
  120. /// \params creds The credentials associated with the server.
  121. ///
  122. /// \return bound port number on success, 0 on failure.
  123. ///
  124. /// \warning It's an error to call this method on an already started server.
  125. virtual int AddListeningPort(const TString& addr,
  126. ServerCredentials* creds) = 0;
  127. /// Start the server.
  128. ///
  129. /// \param cqs Completion queues for handling asynchronous services. The
  130. /// caller is required to keep all completion queues live until the server is
  131. /// destroyed.
  132. /// \param num_cqs How many completion queues does \a cqs hold.
  133. virtual void Start(grpc::ServerCompletionQueue** cqs, size_t num_cqs) = 0;
  134. virtual void ShutdownInternal(gpr_timespec deadline) = 0;
  135. virtual int max_receive_message_size() const = 0;
  136. virtual grpc_server* server() = 0;
  137. void PerformOpsOnCall(internal::CallOpSetInterface* ops,
  138. internal::Call* call) override = 0;
  139. class BaseAsyncRequest : public internal::CompletionQueueTag {
  140. public:
  141. BaseAsyncRequest(ServerInterface* server, grpc::ServerContext* context,
  142. internal::ServerAsyncStreamingInterface* stream,
  143. grpc::CompletionQueue* call_cq,
  144. grpc::ServerCompletionQueue* notification_cq, void* tag,
  145. bool delete_on_finalize);
  146. ~BaseAsyncRequest() override;
  147. bool FinalizeResult(void** tag, bool* status) override;
  148. private:
  149. void ContinueFinalizeResultAfterInterception();
  150. protected:
  151. ServerInterface* const server_;
  152. grpc::ServerContext* const context_;
  153. internal::ServerAsyncStreamingInterface* const stream_;
  154. grpc::CompletionQueue* const call_cq_;
  155. grpc::ServerCompletionQueue* const notification_cq_;
  156. void* const tag_;
  157. const bool delete_on_finalize_;
  158. grpc_call* call_;
  159. internal::Call call_wrapper_;
  160. internal::InterceptorBatchMethodsImpl interceptor_methods_;
  161. bool done_intercepting_;
  162. bool call_metric_recording_enabled_;
  163. experimental::ServerMetricRecorder* server_metric_recorder_;
  164. };
  165. /// RegisteredAsyncRequest is not part of the C++ API
  166. class RegisteredAsyncRequest : public BaseAsyncRequest {
  167. public:
  168. RegisteredAsyncRequest(ServerInterface* server,
  169. grpc::ServerContext* context,
  170. internal::ServerAsyncStreamingInterface* stream,
  171. grpc::CompletionQueue* call_cq,
  172. grpc::ServerCompletionQueue* notification_cq,
  173. void* tag, const char* name,
  174. internal::RpcMethod::RpcType type);
  175. bool FinalizeResult(void** tag, bool* status) override {
  176. // If we are done intercepting, then there is nothing more for us to do
  177. if (done_intercepting_) {
  178. return BaseAsyncRequest::FinalizeResult(tag, status);
  179. }
  180. call_wrapper_ = grpc::internal::Call(
  181. call_, server_, call_cq_, server_->max_receive_message_size(),
  182. context_->set_server_rpc_info(name_, type_,
  183. *server_->interceptor_creators()));
  184. return BaseAsyncRequest::FinalizeResult(tag, status);
  185. }
  186. protected:
  187. void IssueRequest(void* registered_method, grpc_byte_buffer** payload,
  188. grpc::ServerCompletionQueue* notification_cq);
  189. const char* name_;
  190. const internal::RpcMethod::RpcType type_;
  191. };
  192. class NoPayloadAsyncRequest final : public RegisteredAsyncRequest {
  193. public:
  194. NoPayloadAsyncRequest(internal::RpcServiceMethod* registered_method,
  195. ServerInterface* server, grpc::ServerContext* context,
  196. internal::ServerAsyncStreamingInterface* stream,
  197. grpc::CompletionQueue* call_cq,
  198. grpc::ServerCompletionQueue* notification_cq,
  199. void* tag)
  200. : RegisteredAsyncRequest(
  201. server, context, stream, call_cq, notification_cq, tag,
  202. registered_method->name(), registered_method->method_type()) {
  203. IssueRequest(registered_method->server_tag(), nullptr, notification_cq);
  204. }
  205. // uses RegisteredAsyncRequest::FinalizeResult
  206. };
  207. template <class Message>
  208. class PayloadAsyncRequest final : public RegisteredAsyncRequest {
  209. public:
  210. PayloadAsyncRequest(internal::RpcServiceMethod* registered_method,
  211. ServerInterface* server, grpc::ServerContext* context,
  212. internal::ServerAsyncStreamingInterface* stream,
  213. grpc::CompletionQueue* call_cq,
  214. grpc::ServerCompletionQueue* notification_cq, void* tag,
  215. Message* request)
  216. : RegisteredAsyncRequest(
  217. server, context, stream, call_cq, notification_cq, tag,
  218. registered_method->name(), registered_method->method_type()),
  219. registered_method_(registered_method),
  220. request_(request) {
  221. IssueRequest(registered_method->server_tag(), payload_.bbuf_ptr(),
  222. notification_cq);
  223. }
  224. ~PayloadAsyncRequest() override {
  225. payload_.Release(); // We do not own the payload_
  226. }
  227. bool FinalizeResult(void** tag, bool* status) override {
  228. // If we are done intercepting, then there is nothing more for us to do
  229. if (done_intercepting_) {
  230. return RegisteredAsyncRequest::FinalizeResult(tag, status);
  231. }
  232. if (*status) {
  233. if (!payload_.Valid() || !SerializationTraits<Message>::Deserialize(
  234. payload_.bbuf_ptr(), request_)
  235. .ok()) {
  236. // If deserialization fails, we cancel the call and instantiate
  237. // a new instance of ourselves to request another call. We then
  238. // return false, which prevents the call from being returned to
  239. // the application.
  240. grpc_call_cancel_with_status(call_, GRPC_STATUS_INTERNAL,
  241. "Unable to parse request", nullptr);
  242. grpc_call_unref(call_);
  243. new PayloadAsyncRequest(registered_method_, server_, context_,
  244. stream_, call_cq_, notification_cq_, tag_,
  245. request_);
  246. delete this;
  247. return false;
  248. }
  249. }
  250. // Set interception point for recv message
  251. interceptor_methods_.AddInterceptionHookPoint(
  252. experimental::InterceptionHookPoints::POST_RECV_MESSAGE);
  253. interceptor_methods_.SetRecvMessage(request_, nullptr);
  254. return RegisteredAsyncRequest::FinalizeResult(tag, status);
  255. }
  256. private:
  257. internal::RpcServiceMethod* const registered_method_;
  258. Message* const request_;
  259. ByteBuffer payload_;
  260. };
  261. class GenericAsyncRequest : public BaseAsyncRequest {
  262. public:
  263. GenericAsyncRequest(ServerInterface* server, GenericServerContext* context,
  264. internal::ServerAsyncStreamingInterface* stream,
  265. grpc::CompletionQueue* call_cq,
  266. grpc::ServerCompletionQueue* notification_cq, void* tag,
  267. bool delete_on_finalize, bool issue_request = true);
  268. bool FinalizeResult(void** tag, bool* status) override;
  269. protected:
  270. void IssueRequest();
  271. private:
  272. grpc_call_details call_details_;
  273. };
  274. template <class Message>
  275. void RequestAsyncCall(internal::RpcServiceMethod* method,
  276. grpc::ServerContext* context,
  277. internal::ServerAsyncStreamingInterface* stream,
  278. grpc::CompletionQueue* call_cq,
  279. grpc::ServerCompletionQueue* notification_cq, void* tag,
  280. Message* message) {
  281. GPR_ASSERT(method);
  282. new PayloadAsyncRequest<Message>(method, this, context, stream, call_cq,
  283. notification_cq, tag, message);
  284. }
  285. void RequestAsyncCall(internal::RpcServiceMethod* method,
  286. grpc::ServerContext* context,
  287. internal::ServerAsyncStreamingInterface* stream,
  288. grpc::CompletionQueue* call_cq,
  289. grpc::ServerCompletionQueue* notification_cq,
  290. void* tag) {
  291. GPR_ASSERT(method);
  292. new NoPayloadAsyncRequest(method, this, context, stream, call_cq,
  293. notification_cq, tag);
  294. }
  295. void RequestAsyncGenericCall(GenericServerContext* context,
  296. internal::ServerAsyncStreamingInterface* stream,
  297. grpc::CompletionQueue* call_cq,
  298. grpc::ServerCompletionQueue* notification_cq,
  299. void* tag) {
  300. new GenericAsyncRequest(this, context, stream, call_cq, notification_cq,
  301. tag, true);
  302. }
  303. private:
  304. // EXPERIMENTAL
  305. // Getter method for the vector of interceptor factory objects.
  306. // Returns a nullptr (rather than being pure) since this is a post-1.0 method
  307. // and adding a new pure method to an interface would be a breaking change
  308. // (even though this is private and non-API)
  309. virtual std::vector<
  310. std::unique_ptr<experimental::ServerInterceptorFactoryInterface>>*
  311. interceptor_creators() {
  312. return nullptr;
  313. }
  314. // Whether per-call load reporting is enabled.
  315. virtual bool call_metric_recording_enabled() const = 0;
  316. // Interface to read or update server-wide metrics. Returns null when not set.
  317. virtual experimental::ServerMetricRecorder* server_metric_recorder()
  318. const = 0;
  319. // A method to get the callbackable completion queue associated with this
  320. // server. If the return value is nullptr, this server doesn't support
  321. // callback operations.
  322. // TODO(vjpai): Consider a better default like using a global CQ
  323. // Returns nullptr (rather than being pure) since this is a post-1.0 method
  324. // and adding a new pure method to an interface would be a breaking change
  325. // (even though this is private and non-API)
  326. virtual grpc::CompletionQueue* CallbackCQ() { return nullptr; }
  327. };
  328. } // namespace grpc
  329. #endif // GRPCPP_SERVER_INTERFACE_H