blob: 8ca29ee58c31281fbdeff4fb576cd92e36d7782a [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;
Sree Kuchibhotla96766192016-10-13 12:42:54 -0700119 case MIN_POLLERS:
120 sync_server_settings_.min_pollers = val;
121 break;
122 case MAX_POLLERS:
123 sync_server_settings_.max_pollers = val;
124 break;
125 case CQ_TIMEOUT_MSEC:
126 sync_server_settings_.cq_timeout_msec = val;
127 break;
128 }
129 return *this;
130}
131
David Garcia Quintas2d024562016-05-17 23:24:39 -0700132ServerBuilder& ServerBuilder::SetCompressionAlgorithmSupportStatus(
133 grpc_compression_algorithm algorithm, bool enabled) {
134 if (enabled) {
135 GPR_BITSET(&enabled_compression_algorithms_bitset_, algorithm);
136 } else {
137 GPR_BITCLEAR(&enabled_compression_algorithms_bitset_, algorithm);
138 }
139 return *this;
140}
141
142ServerBuilder& ServerBuilder::SetDefaultCompressionLevel(
143 grpc_compression_level level) {
144 maybe_default_compression_level_.level = level;
145 return *this;
146}
147
148ServerBuilder& ServerBuilder::SetDefaultCompressionAlgorithm(
149 grpc_compression_algorithm algorithm) {
150 maybe_default_compression_algorithm_.is_set = true;
151 maybe_default_compression_algorithm_.algorithm = algorithm;
152 return *this;
153}
154
155ServerBuilder& ServerBuilder::AddListeningPort(
156 const grpc::string& addr, std::shared_ptr<ServerCredentials> creds,
157 int* selected_port) {
Nicolas Noble30862032015-04-24 18:17:45 -0700158 Port port = {addr, creds, selected_port};
159 ports_.push_back(port);
David Garcia Quintas2d024562016-05-17 23:24:39 -0700160 return *this;
yangg9e21f722014-12-08 15:49:52 -0800161}
162
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800163std::unique_ptr<Server> ServerBuilder::BuildAndStart() {
Sree Kuchibhotla2d08f5b2016-09-26 16:11:02 -0700164 ChannelArguments args;
165 for (auto option = options_.begin(); option != options_.end(); ++option) {
166 (*option)->UpdateArguments(&args);
167 (*option)->UpdatePlugins(&plugins_);
168 }
169
170 for (auto plugin = plugins_.begin(); plugin != plugins_.end(); plugin++) {
171 (*plugin)->UpdateChannelArguments(&args);
172 }
173
174 if (max_receive_message_size_ >= 0) {
175 args.SetInt(GRPC_ARG_MAX_RECEIVE_MESSAGE_LENGTH, max_receive_message_size_);
176 }
177
178 if (max_send_message_size_ >= 0) {
179 args.SetInt(GRPC_ARG_MAX_SEND_MESSAGE_LENGTH, max_send_message_size_);
180 }
181
182 args.SetInt(GRPC_COMPRESSION_CHANNEL_ENABLED_ALGORITHMS_BITSET,
183 enabled_compression_algorithms_bitset_);
184 if (maybe_default_compression_level_.is_set) {
185 args.SetInt(GRPC_COMPRESSION_CHANNEL_DEFAULT_LEVEL,
186 maybe_default_compression_level_.level);
187 }
188 if (maybe_default_compression_algorithm_.is_set) {
189 args.SetInt(GRPC_COMPRESSION_CHANNEL_DEFAULT_ALGORITHM,
190 maybe_default_compression_algorithm_.algorithm);
191 }
192
Sree Kuchibhotla3ea9e242016-08-22 14:15:43 -0700193 // == Determine if the server has any syncrhonous methods ==
Craig Tiller8a7fe1a2016-05-20 11:17:20 -0700194 bool has_sync_methods = false;
Craig Tiller15f383c2016-01-07 12:45:32 -0800195 for (auto it = services_.begin(); it != services_.end(); ++it) {
196 if ((*it)->service->has_synchronous_methods()) {
Sree Kuchibhotla3ea9e242016-08-22 14:15:43 -0700197 has_sync_methods = true;
198 break;
199 }
200 }
201
202 if (!has_sync_methods) {
203 for (auto plugin = plugins_.begin(); plugin != plugins_.end(); plugin++) {
204 if ((*plugin)->has_sync_methods()) {
Craig Tiller8a7fe1a2016-05-20 11:17:20 -0700205 has_sync_methods = true;
yang-gbef0d872016-01-13 15:27:33 -0800206 break;
Craig Tiller15f383c2016-01-07 12:45:32 -0800207 }
208 }
Craig Tiller0db1bef2015-02-09 13:47:39 -0800209 }
Sree Kuchibhotla3ea9e242016-08-22 14:15:43 -0700210
Sree Kuchibhotlaaabada92016-08-24 10:01:13 -0700211 // If this is a Sync server, i.e a server expositing sync API, then the server
212 // needs to create some completion queues to listen for incoming requests.
213 // 'sync_server_cqs' are those internal completion queues.
214 //
215 // This is different from the completion queues added to the server via
216 // ServerBuilder's AddCompletionQueue() method (those completion queues
217 // are in 'cqs_' member variable of ServerBuilder object)
Sree Kuchibhotla4028d2c2016-09-21 10:45:33 -0700218 std::shared_ptr<std::vector<std::unique_ptr<ServerCompletionQueue>>>
Sree Kuchibhotla61355352016-10-20 11:17:22 -0700219 sync_server_cqs(std::make_shared<
220 std::vector<std::unique_ptr<ServerCompletionQueue>>>());
Sree Kuchibhotlaaabada92016-08-24 10:01:13 -0700221
222 if (has_sync_methods) {
Sree Kuchibhotlaa7a21d22016-09-28 12:38:01 -0700223 // This is a Sync server
224 gpr_log(GPR_INFO,
225 "Synchronous server. Num CQs: %d, Min pollers: %d, Max Pollers: "
226 "%d, CQ timeout (msec): %d",
227 sync_server_settings_.num_cqs, sync_server_settings_.min_pollers,
228 sync_server_settings_.max_pollers,
229 sync_server_settings_.cq_timeout_msec);
230
231 // Create completion queues to listen to incoming rpc requests
Sree Kuchibhotla892dbf42016-09-27 19:42:27 -0700232 for (int i = 0; i < sync_server_settings_.num_cqs; i++) {
Sree Kuchibhotla4028d2c2016-09-21 10:45:33 -0700233 sync_server_cqs->emplace_back(new ServerCompletionQueue());
Sree Kuchibhotlaaabada92016-08-24 10:01:13 -0700234 }
235 }
236
Sree Kuchibhotla892dbf42016-09-27 19:42:27 -0700237 std::unique_ptr<Server> server(new Server(
Sree Kuchibhotlae4eb51f2016-10-18 11:51:28 -0700238 max_receive_message_size_, &args, sync_server_cqs,
Sree Kuchibhotla892dbf42016-09-27 19:42:27 -0700239 sync_server_settings_.min_pollers, sync_server_settings_.max_pollers,
240 sync_server_settings_.cq_timeout_msec));
Sree Kuchibhotla3ea9e242016-08-22 14:15:43 -0700241
Yuchen Zenga42ec212016-04-29 13:03:06 -0700242 ServerInitializer* initializer = server->initializer();
Sree Kuchibhotla7def0362016-04-21 14:54:32 -0700243
Sree Kuchibhotlaaabada92016-08-24 10:01:13 -0700244 // Register all the completion queues with the server. i.e
245 // 1. sync_server_cqs: internal completion queues created IF this is a sync
246 // server
247 // 2. cqs_: Completion queues added via AddCompletionQueue() call
Sree Kuchibhotla7def0362016-04-21 14:54:32 -0700248
Sree Kuchibhotla8f7739b2016-10-13 15:12:55 -0700249 // All sync cqs (if any) are frequently polled by ThreadManager
Sree Kuchibhotlaaabada92016-08-24 10:01:13 -0700250 int num_frequently_polled_cqs = sync_server_cqs->size();
251
252 for (auto it = sync_server_cqs->begin(); it != sync_server_cqs->end(); ++it) {
Sree Kuchibhotla4028d2c2016-09-21 10:45:33 -0700253 grpc_server_register_completion_queue(server->server_, (*it)->cq(),
254 nullptr);
Sree Kuchibhotlaaabada92016-08-24 10:01:13 -0700255 }
256
257 // cqs_ contains the completion queue added by calling the ServerBuilder's
258 // AddCompletionQueue() API. Some of them may not be frequently polled (i.e by
259 // calling Next() or AsyncNext()) and hence are not safe to be used for
260 // listening to incoming channels. Such completion queues must be registered
261 // as non-listening queues
262 for (auto it = cqs_.begin(); it != cqs_.end(); ++it) {
263 if ((*it)->IsFrequentlyPolled()) {
264 grpc_server_register_completion_queue(server->server_, (*it)->cq(),
Sree Kuchibhotla1f5e2622016-04-21 12:28:09 -0700265 nullptr);
Craig Tiller20431a82016-05-20 10:02:07 -0700266 num_frequently_polled_cqs++;
Sree Kuchibhotla1f5e2622016-04-21 12:28:09 -0700267 } else {
268 grpc_server_register_non_listening_completion_queue(server->server_,
Sree Kuchibhotlaaabada92016-08-24 10:01:13 -0700269 (*it)->cq(), nullptr);
Sree Kuchibhotla1f5e2622016-04-21 12:28:09 -0700270 }
Craig Tillerf9e6adf2015-05-06 11:45:59 -0700271 }
Sree Kuchibhotla7def0362016-04-21 14:54:32 -0700272
273 if (num_frequently_polled_cqs == 0) {
274 gpr_log(GPR_ERROR,
Craig Tilleraae6c2c2016-05-20 08:58:30 -0700275 "At least one of the completion queues must be frequently polled");
Sree Kuchibhotla7def0362016-04-21 14:54:32 -0700276 return nullptr;
277 }
278
Vijay Pai82dd80a2015-03-24 10:36:08 -0700279 for (auto service = services_.begin(); service != services_.end();
280 service++) {
Craig Tillerd9b6fcf2015-07-07 16:28:20 -0700281 if (!server->RegisterService((*service)->host.get(), (*service)->service)) {
Craig Tiller0db1bef2015-02-09 13:47:39 -0800282 return nullptr;
283 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800284 }
Sree Kuchibhotla3ea9e242016-08-22 14:15:43 -0700285
Yuchen Zenga42ec212016-04-29 13:03:06 -0700286 for (auto plugin = plugins_.begin(); plugin != plugins_.end(); plugin++) {
Vijay Pai15855f32016-06-10 12:25:32 -0700287 (*plugin)->InitServer(initializer);
Yuchen Zenga42ec212016-04-29 13:03:06 -0700288 }
Sree Kuchibhotla3ea9e242016-08-22 14:15:43 -0700289
Yang Gao005eb882015-03-11 22:17:13 -0700290 if (generic_service_) {
Yang Gao49996492015-03-12 16:40:19 -0700291 server->RegisterAsyncGenericService(generic_service_);
yang-g1ac6f452016-01-15 11:46:40 -0800292 } else {
293 for (auto it = services_.begin(); it != services_.end(); ++it) {
294 if ((*it)->service->has_generic_methods()) {
295 gpr_log(GPR_ERROR,
296 "Some methods were marked generic but there is no "
297 "generic service registered.");
298 return nullptr;
299 }
300 }
Yang Gao1c402332015-03-05 16:39:25 -0800301 }
Sree Kuchibhotla3ea9e242016-08-22 14:15:43 -0700302
Vijay Pai82dd80a2015-03-24 10:36:08 -0700303 for (auto port = ports_.begin(); port != ports_.end(); port++) {
304 int r = server->AddListeningPort(port->addr, port->creds.get());
Craig Tiller42bc87c2015-02-23 08:50:19 -0800305 if (!r) return nullptr;
Vijay Pai82dd80a2015-03-24 10:36:08 -0700306 if (port->selected_port != nullptr) {
307 *port->selected_port = r;
Craig Tiller0db1bef2015-02-09 13:47:39 -0800308 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800309 }
Sree Kuchibhotla3ea9e242016-08-22 14:15:43 -0700310
yang-g61e461e2015-09-03 13:08:59 -0700311 auto cqs_data = cqs_.empty() ? nullptr : &cqs_[0];
312 if (!server->Start(cqs_data, cqs_.size())) {
Craig Tiller0db1bef2015-02-09 13:47:39 -0800313 return nullptr;
314 }
Sree Kuchibhotla3ea9e242016-08-22 14:15:43 -0700315
Yuchen Zenga42ec212016-04-29 13:03:06 -0700316 for (auto plugin = plugins_.begin(); plugin != plugins_.end(); plugin++) {
Vijay Pai15855f32016-06-10 12:25:32 -0700317 (*plugin)->Finish(initializer);
Yuchen Zenga42ec212016-04-29 13:03:06 -0700318 }
Sree Kuchibhotla3ea9e242016-08-22 14:15:43 -0700319
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800320 return server;
321}
322
Yuchen Zeng3b8f3352016-05-03 12:18:13 -0700323void ServerBuilder::InternalAddPluginFactory(
324 std::unique_ptr<ServerBuilderPlugin> (*CreatePlugin)()) {
325 gpr_once_init(&once_init_plugin_list, do_plugin_list_init);
Yuchen Zeng7d099a52016-05-06 13:21:36 -0700326 (*g_plugin_factory_list).push_back(CreatePlugin);
Yuchen Zeng3b8f3352016-05-03 12:18:13 -0700327}
328
Craig Tiller190d3602015-02-18 09:23:38 -0800329} // namespace grpc