server.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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_H
  19. #define GRPCPP_SERVER_H
  20. #if defined(__GNUC__)
  21. #pragma GCC system_header
  22. #endif
  23. #include <grpc/support/port_platform.h>
  24. #include <list>
  25. #include <memory>
  26. #include <vector>
  27. #include <grpc/compression.h>
  28. #include <grpc/support/atm.h>
  29. #include <grpcpp/channel.h>
  30. #include <grpcpp/completion_queue.h>
  31. #include <grpcpp/health_check_service_interface.h>
  32. #include <grpcpp/impl/call.h>
  33. #include <grpcpp/impl/grpc_library.h>
  34. #include <grpcpp/impl/rpc_service_method.h>
  35. #include <grpcpp/security/server_credentials.h>
  36. #include <grpcpp/server_interface.h>
  37. #include <grpcpp/support/channel_arguments.h>
  38. #include <grpcpp/support/client_interceptor.h>
  39. #include <grpcpp/support/config.h>
  40. #include <grpcpp/support/status.h>
  41. struct grpc_server;
  42. namespace grpc {
  43. class AsyncGenericService;
  44. class ServerContext;
  45. class ServerInitializer;
  46. namespace internal {
  47. class ExternalConnectionAcceptorImpl;
  48. } // namespace internal
  49. /// Represents a gRPC server.
  50. ///
  51. /// Use a \a grpc::ServerBuilder to create, configure, and start
  52. /// \a Server instances.
  53. class Server : public ServerInterface, private internal::GrpcLibrary {
  54. public:
  55. ~Server() Y_ABSL_LOCKS_EXCLUDED(mu_) override;
  56. /// Block until the server shuts down.
  57. ///
  58. /// \warning The server must be either shutting down or some other thread must
  59. /// call \a Shutdown for this function to ever return.
  60. void Wait() Y_ABSL_LOCKS_EXCLUDED(mu_) override;
  61. /// Global callbacks are a set of hooks that are called when server
  62. /// events occur. \a SetGlobalCallbacks method is used to register
  63. /// the hooks with gRPC. Note that
  64. /// the \a GlobalCallbacks instance will be shared among all
  65. /// \a Server instances in an application and can be set exactly
  66. /// once per application.
  67. class GlobalCallbacks {
  68. public:
  69. virtual ~GlobalCallbacks() {}
  70. /// Called before server is created.
  71. virtual void UpdateArguments(ChannelArguments* /*args*/) {}
  72. /// Called before application callback for each synchronous server request
  73. virtual void PreSynchronousRequest(ServerContext* context) = 0;
  74. /// Called after application callback for each synchronous server request
  75. virtual void PostSynchronousRequest(ServerContext* context) = 0;
  76. /// Called before server is started.
  77. virtual void PreServerStart(Server* /*server*/) {}
  78. /// Called after a server port is added.
  79. virtual void AddPort(Server* /*server*/, const TString& /*addr*/,
  80. ServerCredentials* /*creds*/, int /*port*/) {}
  81. };
  82. /// Set the global callback object. Can only be called once per application.
  83. /// Does not take ownership of callbacks, and expects the pointed to object
  84. /// to be alive until all server objects in the process have been destroyed.
  85. /// The same \a GlobalCallbacks object will be used throughout the
  86. /// application and is shared among all \a Server objects.
  87. static void SetGlobalCallbacks(GlobalCallbacks* callbacks);
  88. /// Returns a \em raw pointer to the underlying \a grpc_server instance.
  89. /// EXPERIMENTAL: for internal/test use only
  90. grpc_server* c_server();
  91. /// Returns the health check service.
  92. HealthCheckServiceInterface* GetHealthCheckService() const {
  93. return health_check_service_.get();
  94. }
  95. /// Establish a channel for in-process communication
  96. std::shared_ptr<Channel> InProcessChannel(const ChannelArguments& args);
  97. /// NOTE: class experimental_type is not part of the public API of this class.
  98. /// TODO(yashykt): Integrate into public API when this is no longer
  99. /// experimental.
  100. class experimental_type {
  101. public:
  102. explicit experimental_type(Server* server) : server_(server) {}
  103. /// Establish a channel for in-process communication with client
  104. /// interceptors
  105. std::shared_ptr<Channel> InProcessChannelWithInterceptors(
  106. const ChannelArguments& args,
  107. std::vector<
  108. std::unique_ptr<experimental::ClientInterceptorFactoryInterface>>
  109. interceptor_creators);
  110. private:
  111. Server* server_;
  112. };
  113. /// NOTE: The function experimental() is not stable public API. It is a view
  114. /// to the experimental components of this class. It may be changed or removed
  115. /// at any time.
  116. experimental_type experimental() { return experimental_type(this); }
  117. protected:
  118. /// Register a service. This call does not take ownership of the service.
  119. /// The service must exist for the lifetime of the Server instance.
  120. bool RegisterService(const TString* addr, Service* service) override;
  121. /// Try binding the server to the given \a addr endpoint
  122. /// (port, and optionally including IP address to bind to).
  123. ///
  124. /// It can be invoked multiple times. Should be used before
  125. /// starting the server.
  126. ///
  127. /// \param addr The address to try to bind to the server (eg, localhost:1234,
  128. /// 192.168.1.1:31416, [::1]:27182, etc.).
  129. /// \param creds The credentials associated with the server.
  130. ///
  131. /// \return bound port number on success, 0 on failure.
  132. ///
  133. /// \warning It is an error to call this method on an already started server.
  134. int AddListeningPort(const TString& addr,
  135. ServerCredentials* creds) override;
  136. /// NOTE: This is *NOT* a public API. The server constructors are supposed to
  137. /// be used by \a ServerBuilder class only. The constructor will be made
  138. /// 'private' very soon.
  139. ///
  140. /// Server constructors. To be used by \a ServerBuilder only.
  141. ///
  142. /// \param args The channel args
  143. ///
  144. /// \param sync_server_cqs The completion queues to use if the server is a
  145. /// synchronous server (or a hybrid server). The server polls for new RPCs on
  146. /// these queues
  147. ///
  148. /// \param min_pollers The minimum number of polling threads per server
  149. /// completion queue (in param sync_server_cqs) to use for listening to
  150. /// incoming requests (used only in case of sync server)
  151. ///
  152. /// \param max_pollers The maximum number of polling threads per server
  153. /// completion queue (in param sync_server_cqs) to use for listening to
  154. /// incoming requests (used only in case of sync server)
  155. ///
  156. /// \param sync_cq_timeout_msec The timeout to use when calling AsyncNext() on
  157. /// server completion queues passed via sync_server_cqs param.
  158. Server(ChannelArguments* args,
  159. std::shared_ptr<std::vector<std::unique_ptr<ServerCompletionQueue>>>
  160. sync_server_cqs,
  161. int min_pollers, int max_pollers, int sync_cq_timeout_msec,
  162. std::vector<std::shared_ptr<internal::ExternalConnectionAcceptorImpl>>
  163. acceptors,
  164. grpc_server_config_fetcher* server_config_fetcher = nullptr,
  165. grpc_resource_quota* server_rq = nullptr,
  166. std::vector<
  167. std::unique_ptr<experimental::ServerInterceptorFactoryInterface>>
  168. interceptor_creators = std::vector<std::unique_ptr<
  169. experimental::ServerInterceptorFactoryInterface>>(),
  170. experimental::ServerMetricRecorder* server_metric_recorder = nullptr);
  171. /// Start the server.
  172. ///
  173. /// \param cqs Completion queues for handling asynchronous services. The
  174. /// caller is required to keep all completion queues live until the server is
  175. /// destroyed.
  176. /// \param num_cqs How many completion queues does \a cqs hold.
  177. void Start(ServerCompletionQueue** cqs, size_t num_cqs) override;
  178. grpc_server* server() override { return server_; }
  179. /// NOTE: This method is not part of the public API for this class.
  180. void set_health_check_service(
  181. std::unique_ptr<HealthCheckServiceInterface> service) {
  182. health_check_service_ = std::move(service);
  183. }
  184. ContextAllocator* context_allocator() { return context_allocator_.get(); }
  185. /// NOTE: This method is not part of the public API for this class.
  186. bool health_check_service_disabled() const {
  187. return health_check_service_disabled_;
  188. }
  189. private:
  190. std::vector<std::unique_ptr<experimental::ServerInterceptorFactoryInterface>>*
  191. interceptor_creators() override {
  192. return &interceptor_creators_;
  193. }
  194. friend class AsyncGenericService;
  195. friend class ServerBuilder;
  196. friend class ServerInitializer;
  197. class SyncRequest;
  198. class CallbackRequestBase;
  199. template <class ServerContextType>
  200. class CallbackRequest;
  201. class UnimplementedAsyncRequest;
  202. class UnimplementedAsyncResponse;
  203. /// SyncRequestThreadManager is an implementation of ThreadManager. This class
  204. /// is responsible for polling for incoming RPCs and calling the RPC handlers.
  205. /// This is only used in case of a Sync server (i.e a server exposing a sync
  206. /// interface)
  207. class SyncRequestThreadManager;
  208. /// Register a generic service. This call does not take ownership of the
  209. /// service. The service must exist for the lifetime of the Server instance.
  210. void RegisterAsyncGenericService(AsyncGenericService* service) override;
  211. /// Register a callback-based generic service. This call does not take
  212. /// ownership of theservice. The service must exist for the lifetime of the
  213. /// Server instance.
  214. void RegisterCallbackGenericService(CallbackGenericService* service) override;
  215. void RegisterContextAllocator(
  216. std::unique_ptr<ContextAllocator> context_allocator) {
  217. context_allocator_ = std::move(context_allocator);
  218. }
  219. void PerformOpsOnCall(internal::CallOpSetInterface* ops,
  220. internal::Call* call) override;
  221. void ShutdownInternal(gpr_timespec deadline)
  222. Y_ABSL_LOCKS_EXCLUDED(mu_) override;
  223. int max_receive_message_size() const override {
  224. return max_receive_message_size_;
  225. }
  226. bool call_metric_recording_enabled() const override {
  227. return call_metric_recording_enabled_;
  228. }
  229. experimental::ServerMetricRecorder* server_metric_recorder() const override {
  230. return server_metric_recorder_;
  231. }
  232. CompletionQueue* CallbackCQ() Y_ABSL_LOCKS_EXCLUDED(mu_) override;
  233. ServerInitializer* initializer();
  234. // Functions to manage the server shutdown ref count. Things that increase
  235. // the ref count are the running state of the server (take a ref at start and
  236. // drop it at shutdown) and each running callback RPC.
  237. void Ref();
  238. void UnrefWithPossibleNotify() Y_ABSL_LOCKS_EXCLUDED(mu_);
  239. void UnrefAndWaitLocked() Y_ABSL_EXCLUSIVE_LOCKS_REQUIRED(mu_);
  240. std::vector<std::shared_ptr<internal::ExternalConnectionAcceptorImpl>>
  241. acceptors_;
  242. // A vector of interceptor factory objects.
  243. // This should be destroyed after health_check_service_ and this requirement
  244. // is satisfied by declaring interceptor_creators_ before
  245. // health_check_service_. (C++ mandates that member objects be destroyed in
  246. // the reverse order of initialization.)
  247. std::vector<std::unique_ptr<experimental::ServerInterceptorFactoryInterface>>
  248. interceptor_creators_;
  249. int max_receive_message_size_;
  250. /// The following completion queues are ONLY used in case of Sync API
  251. /// i.e. if the server has any services with sync methods. The server uses
  252. /// these completion queues to poll for new RPCs
  253. std::shared_ptr<std::vector<std::unique_ptr<ServerCompletionQueue>>>
  254. sync_server_cqs_;
  255. /// List of \a ThreadManager instances (one for each cq in
  256. /// the \a sync_server_cqs)
  257. std::vector<std::unique_ptr<SyncRequestThreadManager>> sync_req_mgrs_;
  258. // Server status
  259. internal::Mutex mu_;
  260. bool started_;
  261. bool shutdown_ Y_ABSL_GUARDED_BY(mu_);
  262. bool shutdown_notified_
  263. Y_ABSL_GUARDED_BY(mu_); // Was notify called on the shutdown_cv_
  264. internal::CondVar shutdown_done_cv_;
  265. bool shutdown_done_ Y_ABSL_GUARDED_BY(mu_) = false;
  266. std::atomic_int shutdown_refs_outstanding_{1};
  267. internal::CondVar shutdown_cv_;
  268. std::shared_ptr<GlobalCallbacks> global_callbacks_;
  269. std::vector<TString> services_;
  270. bool has_async_generic_service_ = false;
  271. bool has_callback_generic_service_ = false;
  272. bool has_callback_methods_ = false;
  273. // Pointer to the wrapped grpc_server.
  274. grpc_server* server_;
  275. std::unique_ptr<ServerInitializer> server_initializer_;
  276. std::unique_ptr<ContextAllocator> context_allocator_;
  277. std::unique_ptr<HealthCheckServiceInterface> health_check_service_;
  278. bool health_check_service_disabled_;
  279. // When appropriate, use a default callback generic service to handle
  280. // unimplemented methods
  281. std::unique_ptr<CallbackGenericService> unimplemented_service_;
  282. // A special handler for resource exhausted in sync case
  283. std::unique_ptr<internal::MethodHandler> resource_exhausted_handler_;
  284. // Handler for callback generic service, if any
  285. std::unique_ptr<internal::MethodHandler> generic_handler_;
  286. // callback_cq_ references the callbackable completion queue associated
  287. // with this server (if any). It is set on the first call to CallbackCQ().
  288. // It is _not owned_ by the server; ownership belongs with its internal
  289. // shutdown callback tag (invoked when the CQ is fully shutdown).
  290. std::atomic<CompletionQueue*> callback_cq_{nullptr};
  291. // List of CQs passed in by user that must be Shutdown only after Server is
  292. // Shutdown. Even though this is only used with NDEBUG, instantiate it in all
  293. // cases since otherwise the size will be inconsistent.
  294. std::vector<CompletionQueue*> cq_list_;
  295. // Whetner per-call load reporting is enabled.
  296. bool call_metric_recording_enabled_ = false;
  297. // Interface to read or update server-wide metrics. Optional.
  298. experimental::ServerMetricRecorder* server_metric_recorder_ = nullptr;
  299. };
  300. } // namespace grpc
  301. #endif // GRPCPP_SERVER_H