server.h 13 KB

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