blob: 00a90bb184c6d6233d2871e4f1fe3478b69ca39c [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Sree Kuchibhotla01907122016-04-21 15:09:13 -07003 * Copyright 2015-2016, Google Inc.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08004 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
34#include <grpc++/server_builder.h>
35
Craig Tiller14a65f92015-02-09 13:13:14 -080036#include <grpc++/impl/service_type.h>
Craig Tillerafcc8752016-10-18 16:10:06 -070037#include <grpc++/resource_quota.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080038#include <grpc++/server.h>
Craig Tillerf40df232016-03-25 13:38:14 -070039#include <grpc/support/cpu.h>
40#include <grpc/support/log.h>
David Garcia Quintas468e16d2016-08-31 13:29:40 +020041#include <grpc/support/useful.h>
David Garcia Quintas2d024562016-05-17 23:24:39 -070042
Vijay Paie8a7e302015-08-24 10:52:33 -070043#include "src/cpp/server/thread_pool_interface.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080044
45namespace grpc {
46
Yuchen Zeng7d099a52016-05-06 13:21:36 -070047static std::vector<std::unique_ptr<ServerBuilderPlugin> (*)()>*
48 g_plugin_factory_list;
Yuchen Zeng3b8f3352016-05-03 12:18:13 -070049static gpr_once once_init_plugin_list = GPR_ONCE_INIT;
50
51static void do_plugin_list_init(void) {
Yuchen Zeng7d099a52016-05-06 13:21:36 -070052 g_plugin_factory_list =
53 new std::vector<std::unique_ptr<ServerBuilderPlugin> (*)()>();
Yuchen Zeng3b8f3352016-05-03 12:18:13 -070054}
55
Yang Gao5f4539f2015-03-06 16:11:16 -080056ServerBuilder::ServerBuilder()
Mark D. Roth69803622016-09-06 08:15:36 -070057 : max_receive_message_size_(-1),
58 max_send_message_size_(-1),
Sree Kuchibhotla96766192016-10-13 12:42:54 -070059 sync_server_settings_(SyncServerSettings()),
Craig Tiller20afa3d2016-10-17 14:52:14 -070060 resource_quota_(nullptr),
Mark D. Roth69803622016-09-06 08:15:36 -070061 generic_service_(nullptr) {
Yuchen Zeng3b8f3352016-05-03 12:18:13 -070062 gpr_once_init(&once_init_plugin_list, do_plugin_list_init);
Vijay Paieac07c32016-06-10 01:36:53 -070063 for (auto it = g_plugin_factory_list->begin();
64 it != g_plugin_factory_list->end(); it++) {
Vijay Paib645a2d2016-06-09 18:39:06 -070065 auto& factory = *it;
Vijay Pai15855f32016-06-10 12:25:32 -070066 plugins_.emplace_back(factory());
Yuchen Zeng3b8f3352016-05-03 12:18:13 -070067 }
Sree Kuchibhotla892dbf42016-09-27 19:42:27 -070068
David Garcia Quintas2d024562016-05-17 23:24:39 -070069 // all compression algorithms enabled by default.
70 enabled_compression_algorithms_bitset_ =
71 (1u << GRPC_COMPRESS_ALGORITHMS_COUNT) - 1;
72 memset(&maybe_default_compression_level_, 0,
73 sizeof(maybe_default_compression_level_));
74 memset(&maybe_default_compression_algorithm_, 0,
75 sizeof(maybe_default_compression_algorithm_));
David Garcia Quintasbeac88c2015-08-10 13:39:52 -070076}
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080077
Craig Tillerdb1a5cc2016-09-28 14:22:12 -070078ServerBuilder::~ServerBuilder() {
Craig Tiller20afa3d2016-10-17 14:52:14 -070079 if (resource_quota_ != nullptr) {
80 grpc_resource_quota_unref(resource_quota_);
Craig Tillerdb1a5cc2016-09-28 14:22:12 -070081 }
82}
83
Sree Kuchibhotla1f5e2622016-04-21 12:28:09 -070084std::unique_ptr<ServerCompletionQueue> ServerBuilder::AddCompletionQueue(
85 bool is_frequently_polled) {
86 ServerCompletionQueue* cq = new ServerCompletionQueue(is_frequently_polled);
Craig Tillerf9e6adf2015-05-06 11:45:59 -070087 cqs_.push_back(cq);
88 return std::unique_ptr<ServerCompletionQueue>(cq);
89}
90
David Garcia Quintas2d024562016-05-17 23:24:39 -070091ServerBuilder& ServerBuilder::RegisterService(Service* service) {
Craig Tiller15f383c2016-01-07 12:45:32 -080092 services_.emplace_back(new NamedService(service));
David Garcia Quintas2d024562016-05-17 23:24:39 -070093 return *this;
Craig Tiller822d2c72015-07-07 16:08:00 -070094}
95
David Garcia Quintas2d024562016-05-17 23:24:39 -070096ServerBuilder& ServerBuilder::RegisterService(const grpc::string& addr,
97 Service* service) {
Craig Tiller15f383c2016-01-07 12:45:32 -080098 services_.emplace_back(new NamedService(addr, service));
David Garcia Quintas2d024562016-05-17 23:24:39 -070099 return *this;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800100}
101
David Garcia Quintas2d024562016-05-17 23:24:39 -0700102ServerBuilder& ServerBuilder::RegisterAsyncGenericService(
103 AsyncGenericService* service) {
Yang Gao005eb882015-03-11 22:17:13 -0700104 if (generic_service_) {
Yang Gao1c402332015-03-05 16:39:25 -0800105 gpr_log(GPR_ERROR,
Yang Gao49996492015-03-12 16:40:19 -0700106 "Adding multiple AsyncGenericService is unsupported for now. "
Yang Gao6baa9b62015-03-17 10:49:39 -0700107 "Dropping the service %p",
Sree Kuchibhotla4028d2c2016-09-21 10:45:33 -0700108 (void*)service);
David Garcia Quintas2d024562016-05-17 23:24:39 -0700109 } else {
110 generic_service_ = service;
Yang Gao1c402332015-03-05 16:39:25 -0800111 }
David Garcia Quintas2d024562016-05-17 23:24:39 -0700112 return *this;
Yang Gao1c402332015-03-05 16:39:25 -0800113}
114
David Garcia Quintas2d024562016-05-17 23:24:39 -0700115ServerBuilder& ServerBuilder::SetOption(
116 std::unique_ptr<ServerBuilderOption> option) {
yang-ga23f17b2015-11-25 10:21:05 -0800117 options_.push_back(std::move(option));
David Garcia Quintas2d024562016-05-17 23:24:39 -0700118 return *this;
yang-ga23f17b2015-11-25 10:21:05 -0800119}
120
Sree Kuchibhotla96766192016-10-13 12:42:54 -0700121ServerBuilder& ServerBuilder::SetSyncServerOption(
122 ServerBuilder::SyncServerOption option, int val) {
123 switch (option) {
124 case NUM_CQS:
125 sync_server_settings_.num_cqs = val;
126 break;
Sree Kuchibhotla96766192016-10-13 12:42:54 -0700127 case MIN_POLLERS:
128 sync_server_settings_.min_pollers = val;
129 break;
130 case MAX_POLLERS:
131 sync_server_settings_.max_pollers = val;
132 break;
133 case CQ_TIMEOUT_MSEC:
134 sync_server_settings_.cq_timeout_msec = val;
135 break;
136 }
137 return *this;
138}
139
David Garcia Quintas2d024562016-05-17 23:24:39 -0700140ServerBuilder& ServerBuilder::SetCompressionAlgorithmSupportStatus(
141 grpc_compression_algorithm algorithm, bool enabled) {
142 if (enabled) {
143 GPR_BITSET(&enabled_compression_algorithms_bitset_, algorithm);
144 } else {
145 GPR_BITCLEAR(&enabled_compression_algorithms_bitset_, algorithm);
146 }
147 return *this;
148}
149
150ServerBuilder& ServerBuilder::SetDefaultCompressionLevel(
151 grpc_compression_level level) {
152 maybe_default_compression_level_.level = level;
153 return *this;
154}
155
156ServerBuilder& ServerBuilder::SetDefaultCompressionAlgorithm(
157 grpc_compression_algorithm algorithm) {
158 maybe_default_compression_algorithm_.is_set = true;
159 maybe_default_compression_algorithm_.algorithm = algorithm;
160 return *this;
161}
162
Craig Tiller20afa3d2016-10-17 14:52:14 -0700163ServerBuilder& ServerBuilder::SetResourceQuota(
164 const grpc::ResourceQuota& resource_quota) {
165 if (resource_quota_ != nullptr) {
166 grpc_resource_quota_unref(resource_quota_);
Craig Tillerdb1a5cc2016-09-28 14:22:12 -0700167 }
Craig Tiller20afa3d2016-10-17 14:52:14 -0700168 resource_quota_ = resource_quota.c_resource_quota();
169 grpc_resource_quota_ref(resource_quota_);
Craig Tillerdb1a5cc2016-09-28 14:22:12 -0700170 return *this;
171}
172
David Garcia Quintas2d024562016-05-17 23:24:39 -0700173ServerBuilder& ServerBuilder::AddListeningPort(
174 const grpc::string& addr, std::shared_ptr<ServerCredentials> creds,
175 int* selected_port) {
Nicolas Noble30862032015-04-24 18:17:45 -0700176 Port port = {addr, creds, selected_port};
177 ports_.push_back(port);
David Garcia Quintas2d024562016-05-17 23:24:39 -0700178 return *this;
yangg9e21f722014-12-08 15:49:52 -0800179}
180
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800181std::unique_ptr<Server> ServerBuilder::BuildAndStart() {
Sree Kuchibhotla2d08f5b2016-09-26 16:11:02 -0700182 ChannelArguments args;
183 for (auto option = options_.begin(); option != options_.end(); ++option) {
184 (*option)->UpdateArguments(&args);
185 (*option)->UpdatePlugins(&plugins_);
186 }
187
188 for (auto plugin = plugins_.begin(); plugin != plugins_.end(); plugin++) {
189 (*plugin)->UpdateChannelArguments(&args);
190 }
191
192 if (max_receive_message_size_ >= 0) {
193 args.SetInt(GRPC_ARG_MAX_RECEIVE_MESSAGE_LENGTH, max_receive_message_size_);
194 }
195
196 if (max_send_message_size_ >= 0) {
197 args.SetInt(GRPC_ARG_MAX_SEND_MESSAGE_LENGTH, max_send_message_size_);
198 }
199
200 args.SetInt(GRPC_COMPRESSION_CHANNEL_ENABLED_ALGORITHMS_BITSET,
201 enabled_compression_algorithms_bitset_);
202 if (maybe_default_compression_level_.is_set) {
203 args.SetInt(GRPC_COMPRESSION_CHANNEL_DEFAULT_LEVEL,
204 maybe_default_compression_level_.level);
205 }
206 if (maybe_default_compression_algorithm_.is_set) {
207 args.SetInt(GRPC_COMPRESSION_CHANNEL_DEFAULT_ALGORITHM,
208 maybe_default_compression_algorithm_.algorithm);
209 }
210
Craig Tiller20afa3d2016-10-17 14:52:14 -0700211 if (resource_quota_ != nullptr) {
Craig Tiller153eaa72016-10-21 13:52:36 -0700212 args.SetPointerWithVtable(GRPC_ARG_RESOURCE_QUOTA, resource_quota_,
Craig Tiller20afa3d2016-10-17 14:52:14 -0700213 grpc_resource_quota_arg_vtable());
Craig Tillerdb1a5cc2016-09-28 14:22:12 -0700214 }
Sree Kuchibhotla1e088b42016-10-28 17:34:55 -0700215
Sree Kuchibhotla3ea9e242016-08-22 14:15:43 -0700216 // == Determine if the server has any syncrhonous methods ==
Craig Tiller8a7fe1a2016-05-20 11:17:20 -0700217 bool has_sync_methods = false;
Craig Tiller15f383c2016-01-07 12:45:32 -0800218 for (auto it = services_.begin(); it != services_.end(); ++it) {
219 if ((*it)->service->has_synchronous_methods()) {
Sree Kuchibhotla3ea9e242016-08-22 14:15:43 -0700220 has_sync_methods = true;
221 break;
222 }
223 }
224
225 if (!has_sync_methods) {
226 for (auto plugin = plugins_.begin(); plugin != plugins_.end(); plugin++) {
227 if ((*plugin)->has_sync_methods()) {
Craig Tiller8a7fe1a2016-05-20 11:17:20 -0700228 has_sync_methods = true;
yang-gbef0d872016-01-13 15:27:33 -0800229 break;
Craig Tiller15f383c2016-01-07 12:45:32 -0800230 }
231 }
Craig Tiller0db1bef2015-02-09 13:47:39 -0800232 }
Sree Kuchibhotla3ea9e242016-08-22 14:15:43 -0700233
Sree Kuchibhotlaaabada92016-08-24 10:01:13 -0700234 // If this is a Sync server, i.e a server expositing sync API, then the server
235 // needs to create some completion queues to listen for incoming requests.
236 // 'sync_server_cqs' are those internal completion queues.
237 //
238 // This is different from the completion queues added to the server via
239 // ServerBuilder's AddCompletionQueue() method (those completion queues
240 // are in 'cqs_' member variable of ServerBuilder object)
Sree Kuchibhotla4028d2c2016-09-21 10:45:33 -0700241 std::shared_ptr<std::vector<std::unique_ptr<ServerCompletionQueue>>>
Sree Kuchibhotla61355352016-10-20 11:17:22 -0700242 sync_server_cqs(std::make_shared<
243 std::vector<std::unique_ptr<ServerCompletionQueue>>>());
Sree Kuchibhotlaaabada92016-08-24 10:01:13 -0700244
245 if (has_sync_methods) {
Sree Kuchibhotlaa7a21d22016-09-28 12:38:01 -0700246 // This is a Sync server
247 gpr_log(GPR_INFO,
248 "Synchronous server. Num CQs: %d, Min pollers: %d, Max Pollers: "
249 "%d, CQ timeout (msec): %d",
250 sync_server_settings_.num_cqs, sync_server_settings_.min_pollers,
251 sync_server_settings_.max_pollers,
252 sync_server_settings_.cq_timeout_msec);
253
254 // Create completion queues to listen to incoming rpc requests
Sree Kuchibhotla892dbf42016-09-27 19:42:27 -0700255 for (int i = 0; i < sync_server_settings_.num_cqs; i++) {
Sree Kuchibhotla4028d2c2016-09-21 10:45:33 -0700256 sync_server_cqs->emplace_back(new ServerCompletionQueue());
Sree Kuchibhotlaaabada92016-08-24 10:01:13 -0700257 }
258 }
259
Sree Kuchibhotla892dbf42016-09-27 19:42:27 -0700260 std::unique_ptr<Server> server(new Server(
Sree Kuchibhotlae4eb51f2016-10-18 11:51:28 -0700261 max_receive_message_size_, &args, sync_server_cqs,
Sree Kuchibhotla892dbf42016-09-27 19:42:27 -0700262 sync_server_settings_.min_pollers, sync_server_settings_.max_pollers,
263 sync_server_settings_.cq_timeout_msec));
Sree Kuchibhotla3ea9e242016-08-22 14:15:43 -0700264
Yuchen Zenga42ec212016-04-29 13:03:06 -0700265 ServerInitializer* initializer = server->initializer();
Sree Kuchibhotla7def0362016-04-21 14:54:32 -0700266
Sree Kuchibhotlaaabada92016-08-24 10:01:13 -0700267 // Register all the completion queues with the server. i.e
268 // 1. sync_server_cqs: internal completion queues created IF this is a sync
269 // server
270 // 2. cqs_: Completion queues added via AddCompletionQueue() call
Sree Kuchibhotla7def0362016-04-21 14:54:32 -0700271
Sree Kuchibhotla8f7739b2016-10-13 15:12:55 -0700272 // All sync cqs (if any) are frequently polled by ThreadManager
Sree Kuchibhotlaaabada92016-08-24 10:01:13 -0700273 int num_frequently_polled_cqs = sync_server_cqs->size();
274
275 for (auto it = sync_server_cqs->begin(); it != sync_server_cqs->end(); ++it) {
Sree Kuchibhotla4028d2c2016-09-21 10:45:33 -0700276 grpc_server_register_completion_queue(server->server_, (*it)->cq(),
277 nullptr);
Sree Kuchibhotlaaabada92016-08-24 10:01:13 -0700278 }
279
280 // cqs_ contains the completion queue added by calling the ServerBuilder's
281 // AddCompletionQueue() API. Some of them may not be frequently polled (i.e by
282 // calling Next() or AsyncNext()) and hence are not safe to be used for
283 // listening to incoming channels. Such completion queues must be registered
284 // as non-listening queues
285 for (auto it = cqs_.begin(); it != cqs_.end(); ++it) {
286 if ((*it)->IsFrequentlyPolled()) {
287 grpc_server_register_completion_queue(server->server_, (*it)->cq(),
Sree Kuchibhotla1f5e2622016-04-21 12:28:09 -0700288 nullptr);
Craig Tiller20431a82016-05-20 10:02:07 -0700289 num_frequently_polled_cqs++;
Sree Kuchibhotla1f5e2622016-04-21 12:28:09 -0700290 } else {
291 grpc_server_register_non_listening_completion_queue(server->server_,
Sree Kuchibhotlaaabada92016-08-24 10:01:13 -0700292 (*it)->cq(), nullptr);
Sree Kuchibhotla1f5e2622016-04-21 12:28:09 -0700293 }
Craig Tillerf9e6adf2015-05-06 11:45:59 -0700294 }
Sree Kuchibhotla7def0362016-04-21 14:54:32 -0700295
296 if (num_frequently_polled_cqs == 0) {
297 gpr_log(GPR_ERROR,
Craig Tilleraae6c2c2016-05-20 08:58:30 -0700298 "At least one of the completion queues must be frequently polled");
Sree Kuchibhotla7def0362016-04-21 14:54:32 -0700299 return nullptr;
300 }
301
Vijay Pai82dd80a2015-03-24 10:36:08 -0700302 for (auto service = services_.begin(); service != services_.end();
303 service++) {
Craig Tillerd9b6fcf2015-07-07 16:28:20 -0700304 if (!server->RegisterService((*service)->host.get(), (*service)->service)) {
Craig Tiller0db1bef2015-02-09 13:47:39 -0800305 return nullptr;
306 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800307 }
Sree Kuchibhotla3ea9e242016-08-22 14:15:43 -0700308
Yuchen Zenga42ec212016-04-29 13:03:06 -0700309 for (auto plugin = plugins_.begin(); plugin != plugins_.end(); plugin++) {
Vijay Pai15855f32016-06-10 12:25:32 -0700310 (*plugin)->InitServer(initializer);
Yuchen Zenga42ec212016-04-29 13:03:06 -0700311 }
Sree Kuchibhotla3ea9e242016-08-22 14:15:43 -0700312
Yang Gao005eb882015-03-11 22:17:13 -0700313 if (generic_service_) {
Yang Gao49996492015-03-12 16:40:19 -0700314 server->RegisterAsyncGenericService(generic_service_);
yang-g1ac6f452016-01-15 11:46:40 -0800315 } else {
316 for (auto it = services_.begin(); it != services_.end(); ++it) {
317 if ((*it)->service->has_generic_methods()) {
318 gpr_log(GPR_ERROR,
319 "Some methods were marked generic but there is no "
320 "generic service registered.");
321 return nullptr;
322 }
323 }
Yang Gao1c402332015-03-05 16:39:25 -0800324 }
Sree Kuchibhotla3ea9e242016-08-22 14:15:43 -0700325
Vijay Pai82dd80a2015-03-24 10:36:08 -0700326 for (auto port = ports_.begin(); port != ports_.end(); port++) {
327 int r = server->AddListeningPort(port->addr, port->creds.get());
Craig Tiller42bc87c2015-02-23 08:50:19 -0800328 if (!r) return nullptr;
Vijay Pai82dd80a2015-03-24 10:36:08 -0700329 if (port->selected_port != nullptr) {
330 *port->selected_port = r;
Craig Tiller0db1bef2015-02-09 13:47:39 -0800331 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800332 }
Sree Kuchibhotla3ea9e242016-08-22 14:15:43 -0700333
yang-g61e461e2015-09-03 13:08:59 -0700334 auto cqs_data = cqs_.empty() ? nullptr : &cqs_[0];
335 if (!server->Start(cqs_data, cqs_.size())) {
Craig Tiller0db1bef2015-02-09 13:47:39 -0800336 return nullptr;
337 }
Sree Kuchibhotla3ea9e242016-08-22 14:15:43 -0700338
Yuchen Zenga42ec212016-04-29 13:03:06 -0700339 for (auto plugin = plugins_.begin(); plugin != plugins_.end(); plugin++) {
Vijay Pai15855f32016-06-10 12:25:32 -0700340 (*plugin)->Finish(initializer);
Yuchen Zenga42ec212016-04-29 13:03:06 -0700341 }
Sree Kuchibhotla3ea9e242016-08-22 14:15:43 -0700342
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800343 return server;
344}
345
Yuchen Zeng3b8f3352016-05-03 12:18:13 -0700346void ServerBuilder::InternalAddPluginFactory(
347 std::unique_ptr<ServerBuilderPlugin> (*CreatePlugin)()) {
348 gpr_once_init(&once_init_plugin_list, do_plugin_list_init);
Yuchen Zeng7d099a52016-05-06 13:21:36 -0700349 (*g_plugin_factory_list).push_back(CreatePlugin);
Yuchen Zeng3b8f3352016-05-03 12:18:13 -0700350}
351
Craig Tiller190d3602015-02-18 09:23:38 -0800352} // namespace grpc