blob: f4e7733312dd5ccbc115461e1d3c6c92f9c3564d [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>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080037#include <grpc++/server.h>
Craig Tillerf40df232016-03-25 13:38:14 -070038#include <grpc/support/cpu.h>
39#include <grpc/support/log.h>
David Garcia Quintas468e16d2016-08-31 13:29:40 +020040#include <grpc/support/useful.h>
David Garcia Quintas2d024562016-05-17 23:24:39 -070041
Vijay Paie8a7e302015-08-24 10:52:33 -070042#include "src/cpp/server/thread_pool_interface.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080043
44namespace grpc {
45
Yuchen Zeng7d099a52016-05-06 13:21:36 -070046static std::vector<std::unique_ptr<ServerBuilderPlugin> (*)()>*
47 g_plugin_factory_list;
Yuchen Zeng3b8f3352016-05-03 12:18:13 -070048static gpr_once once_init_plugin_list = GPR_ONCE_INIT;
49
50static void do_plugin_list_init(void) {
Yuchen Zeng7d099a52016-05-06 13:21:36 -070051 g_plugin_factory_list =
52 new std::vector<std::unique_ptr<ServerBuilderPlugin> (*)()>();
Yuchen Zeng3b8f3352016-05-03 12:18:13 -070053}
54
Yang Gao5f4539f2015-03-06 16:11:16 -080055ServerBuilder::ServerBuilder()
Mark D. Roth69803622016-09-06 08:15:36 -070056 : max_receive_message_size_(-1),
57 max_send_message_size_(-1),
Sree Kuchibhotla96766192016-10-13 12:42:54 -070058 sync_server_settings_(SyncServerSettings()),
Mark D. Roth69803622016-09-06 08:15:36 -070059 generic_service_(nullptr) {
Yuchen Zeng3b8f3352016-05-03 12:18:13 -070060 gpr_once_init(&once_init_plugin_list, do_plugin_list_init);
Vijay Paieac07c32016-06-10 01:36:53 -070061 for (auto it = g_plugin_factory_list->begin();
62 it != g_plugin_factory_list->end(); it++) {
Vijay Paib645a2d2016-06-09 18:39:06 -070063 auto& factory = *it;
Vijay Pai15855f32016-06-10 12:25:32 -070064 plugins_.emplace_back(factory());
Yuchen Zeng3b8f3352016-05-03 12:18:13 -070065 }
Sree Kuchibhotla892dbf42016-09-27 19:42:27 -070066
David Garcia Quintas2d024562016-05-17 23:24:39 -070067 // all compression algorithms enabled by default.
68 enabled_compression_algorithms_bitset_ =
69 (1u << GRPC_COMPRESS_ALGORITHMS_COUNT) - 1;
70 memset(&maybe_default_compression_level_, 0,
71 sizeof(maybe_default_compression_level_));
72 memset(&maybe_default_compression_algorithm_, 0,
73 sizeof(maybe_default_compression_algorithm_));
David Garcia Quintasbeac88c2015-08-10 13:39:52 -070074}
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080075
Sree Kuchibhotla1f5e2622016-04-21 12:28:09 -070076std::unique_ptr<ServerCompletionQueue> ServerBuilder::AddCompletionQueue(
77 bool is_frequently_polled) {
78 ServerCompletionQueue* cq = new ServerCompletionQueue(is_frequently_polled);
Craig Tillerf9e6adf2015-05-06 11:45:59 -070079 cqs_.push_back(cq);
80 return std::unique_ptr<ServerCompletionQueue>(cq);
81}
82
David Garcia Quintas2d024562016-05-17 23:24:39 -070083ServerBuilder& ServerBuilder::RegisterService(Service* service) {
Craig Tiller15f383c2016-01-07 12:45:32 -080084 services_.emplace_back(new NamedService(service));
David Garcia Quintas2d024562016-05-17 23:24:39 -070085 return *this;
Craig Tiller822d2c72015-07-07 16:08:00 -070086}
87
David Garcia Quintas2d024562016-05-17 23:24:39 -070088ServerBuilder& ServerBuilder::RegisterService(const grpc::string& addr,
89 Service* service) {
Craig Tiller15f383c2016-01-07 12:45:32 -080090 services_.emplace_back(new NamedService(addr, service));
David Garcia Quintas2d024562016-05-17 23:24:39 -070091 return *this;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080092}
93
David Garcia Quintas2d024562016-05-17 23:24:39 -070094ServerBuilder& ServerBuilder::RegisterAsyncGenericService(
95 AsyncGenericService* service) {
Yang Gao005eb882015-03-11 22:17:13 -070096 if (generic_service_) {
Yang Gao1c402332015-03-05 16:39:25 -080097 gpr_log(GPR_ERROR,
Yang Gao49996492015-03-12 16:40:19 -070098 "Adding multiple AsyncGenericService is unsupported for now. "
Yang Gao6baa9b62015-03-17 10:49:39 -070099 "Dropping the service %p",
Sree Kuchibhotla4028d2c2016-09-21 10:45:33 -0700100 (void*)service);
David Garcia Quintas2d024562016-05-17 23:24:39 -0700101 } else {
102 generic_service_ = service;
Yang Gao1c402332015-03-05 16:39:25 -0800103 }
David Garcia Quintas2d024562016-05-17 23:24:39 -0700104 return *this;
Yang Gao1c402332015-03-05 16:39:25 -0800105}
106
David Garcia Quintas2d024562016-05-17 23:24:39 -0700107ServerBuilder& ServerBuilder::SetOption(
108 std::unique_ptr<ServerBuilderOption> option) {
yang-ga23f17b2015-11-25 10:21:05 -0800109 options_.push_back(std::move(option));
David Garcia Quintas2d024562016-05-17 23:24:39 -0700110 return *this;
yang-ga23f17b2015-11-25 10:21:05 -0800111}
112
Sree Kuchibhotla96766192016-10-13 12:42:54 -0700113ServerBuilder& ServerBuilder::SetSyncServerOption(
114 ServerBuilder::SyncServerOption option, int val) {
115 switch (option) {
116 case NUM_CQS:
117 sync_server_settings_.num_cqs = val;
118 break;
119
120 case MIN_POLLERS:
121 sync_server_settings_.min_pollers = val;
122 break;
123 case MAX_POLLERS:
124 sync_server_settings_.max_pollers = val;
125 break;
126 case CQ_TIMEOUT_MSEC:
127 sync_server_settings_.cq_timeout_msec = val;
128 break;
129 }
130 return *this;
131}
132
David Garcia Quintas2d024562016-05-17 23:24:39 -0700133ServerBuilder& ServerBuilder::SetCompressionAlgorithmSupportStatus(
134 grpc_compression_algorithm algorithm, bool enabled) {
135 if (enabled) {
136 GPR_BITSET(&enabled_compression_algorithms_bitset_, algorithm);
137 } else {
138 GPR_BITCLEAR(&enabled_compression_algorithms_bitset_, algorithm);
139 }
140 return *this;
141}
142
143ServerBuilder& ServerBuilder::SetDefaultCompressionLevel(
144 grpc_compression_level level) {
145 maybe_default_compression_level_.level = level;
146 return *this;
147}
148
149ServerBuilder& ServerBuilder::SetDefaultCompressionAlgorithm(
150 grpc_compression_algorithm algorithm) {
151 maybe_default_compression_algorithm_.is_set = true;
152 maybe_default_compression_algorithm_.algorithm = algorithm;
153 return *this;
154}
155
156ServerBuilder& ServerBuilder::AddListeningPort(
157 const grpc::string& addr, std::shared_ptr<ServerCredentials> creds,
158 int* selected_port) {
Nicolas Noble30862032015-04-24 18:17:45 -0700159 Port port = {addr, creds, selected_port};
160 ports_.push_back(port);
David Garcia Quintas2d024562016-05-17 23:24:39 -0700161 return *this;
yangg9e21f722014-12-08 15:49:52 -0800162}
163
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800164std::unique_ptr<Server> ServerBuilder::BuildAndStart() {
Sree Kuchibhotla2d08f5b2016-09-26 16:11:02 -0700165 ChannelArguments args;
166 for (auto option = options_.begin(); option != options_.end(); ++option) {
167 (*option)->UpdateArguments(&args);
168 (*option)->UpdatePlugins(&plugins_);
169 }
170
171 for (auto plugin = plugins_.begin(); plugin != plugins_.end(); plugin++) {
172 (*plugin)->UpdateChannelArguments(&args);
173 }
174
175 if (max_receive_message_size_ >= 0) {
176 args.SetInt(GRPC_ARG_MAX_RECEIVE_MESSAGE_LENGTH, max_receive_message_size_);
177 }
178
179 if (max_send_message_size_ >= 0) {
180 args.SetInt(GRPC_ARG_MAX_SEND_MESSAGE_LENGTH, max_send_message_size_);
181 }
182
183 args.SetInt(GRPC_COMPRESSION_CHANNEL_ENABLED_ALGORITHMS_BITSET,
184 enabled_compression_algorithms_bitset_);
185 if (maybe_default_compression_level_.is_set) {
186 args.SetInt(GRPC_COMPRESSION_CHANNEL_DEFAULT_LEVEL,
187 maybe_default_compression_level_.level);
188 }
189 if (maybe_default_compression_algorithm_.is_set) {
190 args.SetInt(GRPC_COMPRESSION_CHANNEL_DEFAULT_ALGORITHM,
191 maybe_default_compression_algorithm_.algorithm);
192 }
193
Sree Kuchibhotla3ea9e242016-08-22 14:15:43 -0700194 // == Determine if the server has any syncrhonous methods ==
Craig Tiller8a7fe1a2016-05-20 11:17:20 -0700195 bool has_sync_methods = false;
Craig Tiller15f383c2016-01-07 12:45:32 -0800196 for (auto it = services_.begin(); it != services_.end(); ++it) {
197 if ((*it)->service->has_synchronous_methods()) {
Sree Kuchibhotla3ea9e242016-08-22 14:15:43 -0700198 has_sync_methods = true;
199 break;
200 }
201 }
202
203 if (!has_sync_methods) {
204 for (auto plugin = plugins_.begin(); plugin != plugins_.end(); plugin++) {
205 if ((*plugin)->has_sync_methods()) {
Craig Tiller8a7fe1a2016-05-20 11:17:20 -0700206 has_sync_methods = true;
yang-gbef0d872016-01-13 15:27:33 -0800207 break;
Craig Tiller15f383c2016-01-07 12:45:32 -0800208 }
209 }
Craig Tiller0db1bef2015-02-09 13:47:39 -0800210 }
Sree Kuchibhotla3ea9e242016-08-22 14:15:43 -0700211
Sree Kuchibhotlaaabada92016-08-24 10:01:13 -0700212 // If this is a Sync server, i.e a server expositing sync API, then the server
213 // needs to create some completion queues to listen for incoming requests.
214 // 'sync_server_cqs' are those internal completion queues.
215 //
216 // This is different from the completion queues added to the server via
217 // ServerBuilder's AddCompletionQueue() method (those completion queues
218 // are in 'cqs_' member variable of ServerBuilder object)
Sree Kuchibhotla4028d2c2016-09-21 10:45:33 -0700219 std::shared_ptr<std::vector<std::unique_ptr<ServerCompletionQueue>>>
220 sync_server_cqs(
221 new std::vector<std::unique_ptr<ServerCompletionQueue>>());
Sree Kuchibhotlaaabada92016-08-24 10:01:13 -0700222
223 if (has_sync_methods) {
Sree Kuchibhotlaa7a21d22016-09-28 12:38:01 -0700224 // This is a Sync server
225 gpr_log(GPR_INFO,
226 "Synchronous server. Num CQs: %d, Min pollers: %d, Max Pollers: "
227 "%d, CQ timeout (msec): %d",
228 sync_server_settings_.num_cqs, sync_server_settings_.min_pollers,
229 sync_server_settings_.max_pollers,
230 sync_server_settings_.cq_timeout_msec);
231
232 // Create completion queues to listen to incoming rpc requests
Sree Kuchibhotla892dbf42016-09-27 19:42:27 -0700233 for (int i = 0; i < sync_server_settings_.num_cqs; i++) {
Sree Kuchibhotla4028d2c2016-09-21 10:45:33 -0700234 sync_server_cqs->emplace_back(new ServerCompletionQueue());
Sree Kuchibhotlaaabada92016-08-24 10:01:13 -0700235 }
236 }
237
Sree Kuchibhotla892dbf42016-09-27 19:42:27 -0700238 std::unique_ptr<Server> server(new Server(
Sree Kuchibhotlae4eb51f2016-10-18 11:51:28 -0700239 max_receive_message_size_, &args, sync_server_cqs,
Sree Kuchibhotla892dbf42016-09-27 19:42:27 -0700240 sync_server_settings_.min_pollers, sync_server_settings_.max_pollers,
241 sync_server_settings_.cq_timeout_msec));
Sree Kuchibhotla3ea9e242016-08-22 14:15:43 -0700242
Yuchen Zenga42ec212016-04-29 13:03:06 -0700243 ServerInitializer* initializer = server->initializer();
Sree Kuchibhotla7def0362016-04-21 14:54:32 -0700244
Sree Kuchibhotlaaabada92016-08-24 10:01:13 -0700245 // Register all the completion queues with the server. i.e
246 // 1. sync_server_cqs: internal completion queues created IF this is a sync
247 // server
248 // 2. cqs_: Completion queues added via AddCompletionQueue() call
Sree Kuchibhotla7def0362016-04-21 14:54:32 -0700249
Sree Kuchibhotla8f7739b2016-10-13 15:12:55 -0700250 // All sync cqs (if any) are frequently polled by ThreadManager
Sree Kuchibhotlaaabada92016-08-24 10:01:13 -0700251 int num_frequently_polled_cqs = sync_server_cqs->size();
252
253 for (auto it = sync_server_cqs->begin(); it != sync_server_cqs->end(); ++it) {
Sree Kuchibhotla4028d2c2016-09-21 10:45:33 -0700254 grpc_server_register_completion_queue(server->server_, (*it)->cq(),
255 nullptr);
Sree Kuchibhotlaaabada92016-08-24 10:01:13 -0700256 }
257
258 // cqs_ contains the completion queue added by calling the ServerBuilder's
259 // AddCompletionQueue() API. Some of them may not be frequently polled (i.e by
260 // calling Next() or AsyncNext()) and hence are not safe to be used for
261 // listening to incoming channels. Such completion queues must be registered
262 // as non-listening queues
263 for (auto it = cqs_.begin(); it != cqs_.end(); ++it) {
264 if ((*it)->IsFrequentlyPolled()) {
265 grpc_server_register_completion_queue(server->server_, (*it)->cq(),
Sree Kuchibhotla1f5e2622016-04-21 12:28:09 -0700266 nullptr);
Craig Tiller20431a82016-05-20 10:02:07 -0700267 num_frequently_polled_cqs++;
Sree Kuchibhotla1f5e2622016-04-21 12:28:09 -0700268 } else {
269 grpc_server_register_non_listening_completion_queue(server->server_,
Sree Kuchibhotlaaabada92016-08-24 10:01:13 -0700270 (*it)->cq(), nullptr);
Sree Kuchibhotla1f5e2622016-04-21 12:28:09 -0700271 }
Craig Tillerf9e6adf2015-05-06 11:45:59 -0700272 }
Sree Kuchibhotla7def0362016-04-21 14:54:32 -0700273
274 if (num_frequently_polled_cqs == 0) {
275 gpr_log(GPR_ERROR,
Craig Tilleraae6c2c2016-05-20 08:58:30 -0700276 "At least one of the completion queues must be frequently polled");
Sree Kuchibhotla7def0362016-04-21 14:54:32 -0700277 return nullptr;
278 }
279
Vijay Pai82dd80a2015-03-24 10:36:08 -0700280 for (auto service = services_.begin(); service != services_.end();
281 service++) {
Craig Tillerd9b6fcf2015-07-07 16:28:20 -0700282 if (!server->RegisterService((*service)->host.get(), (*service)->service)) {
Craig Tiller0db1bef2015-02-09 13:47:39 -0800283 return nullptr;
284 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800285 }
Sree Kuchibhotla3ea9e242016-08-22 14:15:43 -0700286
Yuchen Zenga42ec212016-04-29 13:03:06 -0700287 for (auto plugin = plugins_.begin(); plugin != plugins_.end(); plugin++) {
Vijay Pai15855f32016-06-10 12:25:32 -0700288 (*plugin)->InitServer(initializer);
Yuchen Zenga42ec212016-04-29 13:03:06 -0700289 }
Sree Kuchibhotla3ea9e242016-08-22 14:15:43 -0700290
Yang Gao005eb882015-03-11 22:17:13 -0700291 if (generic_service_) {
Yang Gao49996492015-03-12 16:40:19 -0700292 server->RegisterAsyncGenericService(generic_service_);
yang-g1ac6f452016-01-15 11:46:40 -0800293 } else {
294 for (auto it = services_.begin(); it != services_.end(); ++it) {
295 if ((*it)->service->has_generic_methods()) {
296 gpr_log(GPR_ERROR,
297 "Some methods were marked generic but there is no "
298 "generic service registered.");
299 return nullptr;
300 }
301 }
Yang Gao1c402332015-03-05 16:39:25 -0800302 }
Sree Kuchibhotla3ea9e242016-08-22 14:15:43 -0700303
Vijay Pai82dd80a2015-03-24 10:36:08 -0700304 for (auto port = ports_.begin(); port != ports_.end(); port++) {
305 int r = server->AddListeningPort(port->addr, port->creds.get());
Craig Tiller42bc87c2015-02-23 08:50:19 -0800306 if (!r) return nullptr;
Vijay Pai82dd80a2015-03-24 10:36:08 -0700307 if (port->selected_port != nullptr) {
308 *port->selected_port = r;
Craig Tiller0db1bef2015-02-09 13:47:39 -0800309 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800310 }
Sree Kuchibhotla3ea9e242016-08-22 14:15:43 -0700311
yang-g61e461e2015-09-03 13:08:59 -0700312 auto cqs_data = cqs_.empty() ? nullptr : &cqs_[0];
313 if (!server->Start(cqs_data, cqs_.size())) {
Craig Tiller0db1bef2015-02-09 13:47:39 -0800314 return nullptr;
315 }
Sree Kuchibhotla3ea9e242016-08-22 14:15:43 -0700316
Yuchen Zenga42ec212016-04-29 13:03:06 -0700317 for (auto plugin = plugins_.begin(); plugin != plugins_.end(); plugin++) {
Vijay Pai15855f32016-06-10 12:25:32 -0700318 (*plugin)->Finish(initializer);
Yuchen Zenga42ec212016-04-29 13:03:06 -0700319 }
Sree Kuchibhotla3ea9e242016-08-22 14:15:43 -0700320
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800321 return server;
322}
323
Yuchen Zeng3b8f3352016-05-03 12:18:13 -0700324void ServerBuilder::InternalAddPluginFactory(
325 std::unique_ptr<ServerBuilderPlugin> (*CreatePlugin)()) {
326 gpr_once_init(&once_init_plugin_list, do_plugin_list_init);
Yuchen Zeng7d099a52016-05-06 13:21:36 -0700327 (*g_plugin_factory_list).push_back(CreatePlugin);
Yuchen Zeng3b8f3352016-05-03 12:18:13 -0700328}
329
Craig Tiller190d3602015-02-18 09:23:38 -0800330} // namespace grpc