| 1 |
/* |
| 2 |
* |
| 3 |
* Copyright 2015-2016 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 |
|
| 19 |
#include <limits.h> |
| 20 |
#include <stdint.h> |
| 21 |
#include <string.h> |
| 22 |
|
| 23 |
#include <algorithm> |
| 24 |
#include <iterator> |
| 25 |
#include <memory> |
| 26 |
#include <string> |
| 27 |
#include <utility> |
| 28 |
#include <vector> |
| 29 |
|
| 30 |
#include <grpc/grpc.h> |
| 31 |
#include <grpc/impl/codegen/compression_types.h> |
| 32 |
#include <grpc/impl/codegen/grpc_types.h> |
| 33 |
#include <grpc/support/log.h> |
| 34 |
#include <grpc/support/sync.h> |
| 35 |
#include <grpc/support/workaround_list.h> |
| 36 |
#include <grpcpp/completion_queue.h> |
| 37 |
#include <grpcpp/impl/codegen/server_interface.h> |
| 38 |
#include <grpcpp/impl/server_builder_option.h> |
| 39 |
#include <grpcpp/impl/server_builder_plugin.h> |
| 40 |
#include <grpcpp/impl/service_type.h> |
| 41 |
#include <grpcpp/resource_quota.h> |
| 42 |
#include <grpcpp/security/authorization_policy_provider.h> |
| 43 |
#include <grpcpp/security/server_credentials.h> |
| 44 |
#include <grpcpp/server.h> |
| 45 |
#include <grpcpp/server_builder.h> |
| 46 |
#include <grpcpp/server_context.h> |
| 47 |
#include <grpcpp/support/channel_arguments.h> |
| 48 |
#include <grpcpp/support/server_interceptor.h> |
| 49 |
|
| 50 |
#include "src/core/lib/gpr/string.h" |
| 51 |
#include "src/core/lib/gpr/useful.h" |
| 52 |
#include "src/cpp/server/external_connection_acceptor_impl.h" |
| 53 |
|
| 54 |
namespace grpc { |
| 55 |
|
| 56 |
static std::vector<std::unique_ptr<ServerBuilderPlugin> (*)()>* |
| 57 |
g_plugin_factory_list; |
| 58 |
static gpr_once once_init_plugin_list = GPR_ONCE_INIT; |
| 59 |
|
| 60 |
static void do_plugin_list_init(void) { |
| 61 |
g_plugin_factory_list = |
| 62 |
new std::vector<std::unique_ptr<ServerBuilderPlugin> (*)()>(); |
| 63 |
} |
| 64 |
|
| 65 |
ServerBuilder::ServerBuilder() |
| 66 |
: max_receive_message_size_(INT_MIN), |
| 67 |
max_send_message_size_(INT_MIN), |
| 68 |
sync_server_settings_(SyncServerSettings()), |
| 69 |
resource_quota_(nullptr) { |
| 70 |
gpr_once_init(&once_init_plugin_list, do_plugin_list_init); |
| 71 |
for (const auto& value : *g_plugin_factory_list) { |
| 72 |
plugins_.emplace_back(value()); |
| 73 |
} |
| 74 |
|
| 75 |
// all compression algorithms enabled by default. |
| 76 |
enabled_compression_algorithms_bitset_ = |
| 77 |
(1u << GRPC_COMPRESS_ALGORITHMS_COUNT) - 1; |
| 78 |
memset(&maybe_default_compression_level_, 0, |
| 79 |
sizeof(maybe_default_compression_level_)); |
| 80 |
memset(&maybe_default_compression_algorithm_, 0, |
| 81 |
sizeof(maybe_default_compression_algorithm_)); |
| 82 |
} |
| 83 |
|
| 84 |
ServerBuilder::~ServerBuilder() { |
| 85 |
if (resource_quota_ != nullptr) { |
| 86 |
grpc_resource_quota_unref(resource_quota_); |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
std::unique_ptr<grpc::ServerCompletionQueue> ServerBuilder::AddCompletionQueue( |
| 91 |
bool is_frequently_polled) { |
| 92 |
grpc::ServerCompletionQueue* cq = new grpc::ServerCompletionQueue( |
| 93 |
GRPC_CQ_NEXT, |
| 94 |
is_frequently_polled ? GRPC_CQ_DEFAULT_POLLING : GRPC_CQ_NON_LISTENING, |
| 95 |
nullptr); |
| 96 |
cqs_.push_back(cq); |
| 97 |
return std::unique_ptr<grpc::ServerCompletionQueue>(cq); |
| 98 |
} |
| 99 |
|
| 100 |
ServerBuilder& ServerBuilder::RegisterService(Service* service) { |
| 101 |
services_.emplace_back(new NamedService(service)); |
| 102 |
return *this; |
| 103 |
} |
| 104 |
|
| 105 |
ServerBuilder& ServerBuilder::RegisterService(const std::string& host, |
| 106 |
Service* service) { |
| 107 |
services_.emplace_back(new NamedService(host, service)); |
| 108 |
return *this; |
| 109 |
} |
| 110 |
|
| 111 |
ServerBuilder& ServerBuilder::RegisterAsyncGenericService( |
| 112 |
AsyncGenericService* service) { |
| 113 |
if (generic_service_ || callback_generic_service_) { |
| 114 |
gpr_log(GPR_ERROR, |
| 115 |
"Adding multiple generic services is unsupported for now. " |
| 116 |
"Dropping the service %p", |
| 117 |
service); |
| 118 |
} else { |
| 119 |
generic_service_ = service; |
| 120 |
} |
| 121 |
return *this; |
| 122 |
} |
| 123 |
|
| 124 |
ServerBuilder& ServerBuilder::RegisterCallbackGenericService( |
| 125 |
CallbackGenericService* service) { |
| 126 |
if (generic_service_ || callback_generic_service_) { |
| 127 |
gpr_log(GPR_ERROR, |
| 128 |
"Adding multiple generic services is unsupported for now. " |
| 129 |
"Dropping the service %p", |
| 130 |
service); |
| 131 |
} else { |
| 132 |
callback_generic_service_ = service; |
| 133 |
} |
| 134 |
return *this; |
| 135 |
} |
| 136 |
|
| 137 |
ServerBuilder& ServerBuilder::SetContextAllocator( |
| 138 |
std::unique_ptr<grpc::ContextAllocator> context_allocator) { |
| 139 |
context_allocator_ = std::move(context_allocator); |
| 140 |
return *this; |
| 141 |
} |
| 142 |
|
| 143 |
std::unique_ptr<grpc::experimental::ExternalConnectionAcceptor> |
| 144 |
ServerBuilder::experimental_type::AddExternalConnectionAcceptor( |
| 145 |
experimental_type::ExternalConnectionType type, |
| 146 |
std::shared_ptr<ServerCredentials> creds) { |
| 147 |
std::string name_prefix("external:"); |
| 148 |
char count_str[GPR_LTOA_MIN_BUFSIZE]; |
| 149 |
gpr_ltoa(static_cast<long>(builder_->acceptors_.size()), count_str); |
| 150 |
builder_->acceptors_.emplace_back( |
| 151 |
std::make_shared<grpc::internal::ExternalConnectionAcceptorImpl>( |
| 152 |
name_prefix.append(count_str), type, creds)); |
| 153 |
return builder_->acceptors_.back()->GetAcceptor(); |
| 154 |
} |
| 155 |
|
| 156 |
void ServerBuilder::experimental_type::SetAuthorizationPolicyProvider( |
| 157 |
std::shared_ptr<experimental::AuthorizationPolicyProviderInterface> |
| 158 |
provider) { |
| 159 |
builder_->authorization_provider_ = std::move(provider); |
| 160 |
} |
| 161 |
|
| 162 |
ServerBuilder& ServerBuilder::SetOption( |
| 163 |
std::unique_ptr<ServerBuilderOption> option) { |
| 164 |
options_.push_back(std::move(option)); |
| 165 |
return *this; |
| 166 |
} |
| 167 |
|
| 168 |
ServerBuilder& ServerBuilder::SetSyncServerOption( |
| 169 |
ServerBuilder::SyncServerOption option, int val) { |
| 170 |
switch (option) { |
| 171 |
case NUM_CQS: |
| 172 |
sync_server_settings_.num_cqs = val; |
| 173 |
break; |
| 174 |
case MIN_POLLERS: |
| 175 |
sync_server_settings_.min_pollers = val; |
| 176 |
break; |
| 177 |
case MAX_POLLERS: |
| 178 |
sync_server_settings_.max_pollers = val; |
| 179 |
break; |
| 180 |
case CQ_TIMEOUT_MSEC: |
| 181 |
sync_server_settings_.cq_timeout_msec = val; |
| 182 |
break; |
| 183 |
} |
| 184 |
return *this; |
| 185 |
} |
| 186 |
|
| 187 |
ServerBuilder& ServerBuilder::SetCompressionAlgorithmSupportStatus( |
| 188 |
grpc_compression_algorithm algorithm, bool enabled) { |
| 189 |
if (enabled) { |
| 190 |
grpc_core::SetBit(&enabled_compression_algorithms_bitset_, algorithm); |
| 191 |
} else { |
| 192 |
grpc_core::ClearBit(&enabled_compression_algorithms_bitset_, algorithm); |
| 193 |
} |
| 194 |
return *this; |
| 195 |
} |
| 196 |
|
| 197 |
ServerBuilder& ServerBuilder::SetDefaultCompressionLevel( |
| 198 |
grpc_compression_level level) { |
| 199 |
maybe_default_compression_level_.is_set = true; |
| 200 |
maybe_default_compression_level_.level = level; |
| 201 |
return *this; |
| 202 |
} |
| 203 |
|
| 204 |
ServerBuilder& ServerBuilder::SetDefaultCompressionAlgorithm( |
| 205 |
grpc_compression_algorithm algorithm) { |
| 206 |
maybe_default_compression_algorithm_.is_set = true; |
| 207 |
maybe_default_compression_algorithm_.algorithm = algorithm; |
| 208 |
return *this; |
| 209 |
} |
| 210 |
|
| 211 |
ServerBuilder& ServerBuilder::SetResourceQuota( |
| 212 |
const grpc::ResourceQuota& resource_quota) { |
| 213 |
if (resource_quota_ != nullptr) { |
| 214 |
grpc_resource_quota_unref(resource_quota_); |
| 215 |
} |
| 216 |
resource_quota_ = resource_quota.c_resource_quota(); |
| 217 |
grpc_resource_quota_ref(resource_quota_); |
| 218 |
return *this; |
| 219 |
} |
| 220 |
|
| 221 |
ServerBuilder& ServerBuilder::AddListeningPort( |
| 222 |
const std::string& addr_uri, std::shared_ptr<ServerCredentials> creds, |
| 223 |
int* selected_port) { |
| 224 |
const std::string uri_scheme = "dns:"; |
| 225 |
std::string addr = addr_uri; |
| 226 |
if (addr_uri.compare(0, uri_scheme.size(), uri_scheme) == 0) { |
| 227 |
size_t pos = uri_scheme.size(); |
| 228 |
while (addr_uri[pos] == '/') ++pos; // Skip slashes. |
| 229 |
addr = addr_uri.substr(pos); |
| 230 |
} |
| 231 |
Port port = {addr, std::move(creds), selected_port}; |
| 232 |
ports_.push_back(port); |
| 233 |
return *this; |
| 234 |
} |
| 235 |
|
| 236 |
ChannelArguments ServerBuilder::BuildChannelArgs() { |
| 237 |
ChannelArguments args; |
| 238 |
if (max_receive_message_size_ >= -1) { |
| 239 |
args.SetInt(GRPC_ARG_MAX_RECEIVE_MESSAGE_LENGTH, max_receive_message_size_); |
| 240 |
} |
| 241 |
if (max_send_message_size_ >= -1) { |
| 242 |
args.SetInt(GRPC_ARG_MAX_SEND_MESSAGE_LENGTH, max_send_message_size_); |
| 243 |
} |
| 244 |
for (const auto& option : options_) { |
| 245 |
option->UpdateArguments(&args); |
| 246 |
option->UpdatePlugins(&plugins_); |
| 247 |
} |
| 248 |
args.SetInt(GRPC_COMPRESSION_CHANNEL_ENABLED_ALGORITHMS_BITSET, |
| 249 |
enabled_compression_algorithms_bitset_); |
| 250 |
if (maybe_default_compression_level_.is_set) { |
| 251 |
args.SetInt(GRPC_COMPRESSION_CHANNEL_DEFAULT_LEVEL, |
| 252 |
maybe_default_compression_level_.level); |
| 253 |
} |
| 254 |
if (maybe_default_compression_algorithm_.is_set) { |
| 255 |
args.SetInt(GRPC_COMPRESSION_CHANNEL_DEFAULT_ALGORITHM, |
| 256 |
maybe_default_compression_algorithm_.algorithm); |
| 257 |
} |
| 258 |
if (resource_quota_ != nullptr) { |
| 259 |
args.SetPointerWithVtable(GRPC_ARG_RESOURCE_QUOTA, resource_quota_, |
| 260 |
grpc_resource_quota_arg_vtable()); |
| 261 |
} |
| 262 |
for (const auto& plugin : plugins_) { |
| 263 |
plugin->UpdateServerBuilder(this); |
| 264 |
plugin->UpdateChannelArguments(&args); |
| 265 |
} |
| 266 |
if (authorization_provider_ != nullptr) { |
| 267 |
args.SetPointerWithVtable(GRPC_ARG_AUTHORIZATION_POLICY_PROVIDER, |
| 268 |
authorization_provider_->c_provider(), |
| 269 |
grpc_authorization_policy_provider_arg_vtable()); |
| 270 |
} |
| 271 |
return args; |
| 272 |
} |
| 273 |
|
| 274 |
std::unique_ptr<grpc::Server> ServerBuilder::BuildAndStart() { |
| 275 |
ChannelArguments args = BuildChannelArgs(); |
| 276 |
|
| 277 |
// == Determine if the server has any syncrhonous methods == |
| 278 |
bool has_sync_methods = false; |
| 279 |
for (const auto& value : services_) { |
| 280 |
if (value->service->has_synchronous_methods()) { |
| 281 |
has_sync_methods = true; |
| 282 |
break; |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
if (!has_sync_methods) { |
| 287 |
for (const auto& value : plugins_) { |
| 288 |
if (value->has_sync_methods()) { |
| 289 |
has_sync_methods = true; |
| 290 |
break; |
| 291 |
} |
| 292 |
} |
| 293 |
} |
| 294 |
|
| 295 |
// If this is a Sync server, i.e a server expositing sync API, then the server |
| 296 |
// needs to create some completion queues to listen for incoming requests. |
| 297 |
// 'sync_server_cqs' are those internal completion queues. |
| 298 |
// |
| 299 |
// This is different from the completion queues added to the server via |
| 300 |
// ServerBuilder's AddCompletionQueue() method (those completion queues |
| 301 |
// are in 'cqs_' member variable of ServerBuilder object) |
| 302 |
std::shared_ptr<std::vector<std::unique_ptr<grpc::ServerCompletionQueue>>> |
| 303 |
sync_server_cqs( |
| 304 |
std::make_shared< |
| 305 |
std::vector<std::unique_ptr<grpc::ServerCompletionQueue>>>()); |
| 306 |
|
| 307 |
bool has_frequently_polled_cqs = false; |
| 308 |
for (const auto& cq : cqs_) { |
| 309 |
if (cq->IsFrequentlyPolled()) { |
| 310 |
has_frequently_polled_cqs = true; |
| 311 |
break; |
| 312 |
} |
| 313 |
} |
| 314 |
|
| 315 |
// == Determine if the server has any callback methods == |
| 316 |
bool has_callback_methods = false; |
| 317 |
for (const auto& service : services_) { |
| 318 |
if (service->service->has_callback_methods()) { |
| 319 |
has_callback_methods = true; |
| 320 |
has_frequently_polled_cqs = true; |
| 321 |
break; |
| 322 |
} |
| 323 |
} |
| 324 |
|
| 325 |
if (callback_generic_service_ != nullptr) { |
| 326 |
has_frequently_polled_cqs = true; |
| 327 |
} |
| 328 |
|
| 329 |
const bool is_hybrid_server = has_sync_methods && has_frequently_polled_cqs; |
| 330 |
|
| 331 |
if (has_sync_methods) { |
| 332 |
grpc_cq_polling_type polling_type = |
| 333 |
is_hybrid_server ? GRPC_CQ_NON_POLLING : GRPC_CQ_DEFAULT_POLLING; |
| 334 |
|
| 335 |
// Create completion queues to listen to incoming rpc requests |
| 336 |
for (int i = 0; i < sync_server_settings_.num_cqs; i++) { |
| 337 |
sync_server_cqs->emplace_back( |
| 338 |
new grpc::ServerCompletionQueue(GRPC_CQ_NEXT, polling_type, nullptr)); |
| 339 |
} |
| 340 |
} |
| 341 |
|
| 342 |
// TODO(vjpai): Add a section here for plugins once they can support callback |
| 343 |
// methods |
| 344 |
|
| 345 |
if (has_sync_methods) { |
| 346 |
// This is a Sync server |
| 347 |
gpr_log(GPR_INFO, |
| 348 |
"Synchronous server. Num CQs: %d, Min pollers: %d, Max Pollers: " |
| 349 |
"%d, CQ timeout (msec): %d", |
| 350 |
sync_server_settings_.num_cqs, sync_server_settings_.min_pollers, |
| 351 |
sync_server_settings_.max_pollers, |
| 352 |
sync_server_settings_.cq_timeout_msec); |
| 353 |
} |
| 354 |
|
| 355 |
if (has_callback_methods) { |
| 356 |
gpr_log(GPR_INFO, "Callback server."); |
| 357 |
} |
| 358 |
|
| 359 |
// Merge the application and internal interceptors together. |
| 360 |
// Internal interceptors go first. |
| 361 |
auto creators = std::move(internal_interceptor_creators_); |
| 362 |
creators.insert(creators.end(), |
| 363 |
std::make_move_iterator(interceptor_creators_.begin()), |
| 364 |
std::make_move_iterator(interceptor_creators_.end())); |
| 365 |
|
| 366 |
std::unique_ptr<grpc::Server> server(new grpc::Server( |
| 367 |
&args, sync_server_cqs, sync_server_settings_.min_pollers, |
| 368 |
sync_server_settings_.max_pollers, sync_server_settings_.cq_timeout_msec, |
| 369 |
std::move(acceptors_), server_config_fetcher_, resource_quota_, |
| 370 |
std::move(creators))); |
| 371 |
|
| 372 |
ServerInitializer* initializer = server->initializer(); |
| 373 |
|
| 374 |
// Register all the completion queues with the server. i.e |
| 375 |
// 1. sync_server_cqs: internal completion queues created IF this is a sync |
| 376 |
// server |
| 377 |
// 2. cqs_: Completion queues added via AddCompletionQueue() call |
| 378 |
|
| 379 |
for (const auto& cq : *sync_server_cqs) { |
| 380 |
grpc_server_register_completion_queue(server->server_, cq->cq(), nullptr); |
| 381 |
has_frequently_polled_cqs = true; |
| 382 |
} |
| 383 |
|
| 384 |
if (has_callback_methods || callback_generic_service_ != nullptr) { |
| 385 |
auto* cq = server->CallbackCQ(); |
| 386 |
grpc_server_register_completion_queue(server->server_, cq->cq(), nullptr); |
| 387 |
} |
| 388 |
|
| 389 |
// cqs_ contains the completion queue added by calling the ServerBuilder's |
| 390 |
// AddCompletionQueue() API. Some of them may not be frequently polled (i.e by |
| 391 |
// calling Next() or AsyncNext()) and hence are not safe to be used for |
| 392 |
// listening to incoming channels. Such completion queues must be registered |
| 393 |
// as non-listening queues. In debug mode, these should have their server list |
| 394 |
// tracked since these are provided the user and must be Shutdown by the user |
| 395 |
// after the server is shutdown. |
| 396 |
for (const auto& cq : cqs_) { |
| 397 |
grpc_server_register_completion_queue(server->server_, cq->cq(), nullptr); |
| 398 |
cq->RegisterServer(server.get()); |
| 399 |
} |
| 400 |
|
| 401 |
if (!has_frequently_polled_cqs) { |
| 402 |
gpr_log(GPR_ERROR, |
| 403 |
"At least one of the completion queues must be frequently polled"); |
| 404 |
return nullptr; |
| 405 |
} |
| 406 |
|
| 407 |
server->RegisterContextAllocator(std::move(context_allocator_)); |
| 408 |
|
| 409 |
for (const auto& value : services_) { |
| 410 |
if (!server->RegisterService(value->host.get(), value->service)) { |
| 411 |
return nullptr; |
| 412 |
} |
| 413 |
} |
| 414 |
|
| 415 |
for (const auto& value : plugins_) { |
| 416 |
value->InitServer(initializer); |
| 417 |
} |
| 418 |
|
| 419 |
if (generic_service_) { |
| 420 |
server->RegisterAsyncGenericService(generic_service_); |
| 421 |
} else if (callback_generic_service_) { |
| 422 |
server->RegisterCallbackGenericService(callback_generic_service_); |
| 423 |
} else { |
| 424 |
for (const auto& value : services_) { |
| 425 |
if (value->service->has_generic_methods()) { |
| 426 |
gpr_log(GPR_ERROR, |
| 427 |
"Some methods were marked generic but there is no " |
| 428 |
"generic service registered."); |
| 429 |
return nullptr; |
| 430 |
} |
| 431 |
} |
| 432 |
} |
| 433 |
|
| 434 |
bool added_port = false; |
| 435 |
for (auto& port : ports_) { |
| 436 |
int r = server->AddListeningPort(port.addr, port.creds.get()); |
| 437 |
if (!r) { |
| 438 |
if (added_port) server->Shutdown(); |
| 439 |
return nullptr; |
| 440 |
} |
| 441 |
added_port = true; |
| 442 |
if (port.selected_port != nullptr) { |
| 443 |
*port.selected_port = r; |
| 444 |
} |
| 445 |
} |
| 446 |
|
| 447 |
auto cqs_data = cqs_.empty() ? nullptr : &cqs_[0]; |
| 448 |
server->Start(cqs_data, cqs_.size()); |
| 449 |
|
| 450 |
for (const auto& value : plugins_) { |
| 451 |
value->Finish(initializer); |
| 452 |
} |
| 453 |
|
| 454 |
return server; |
| 455 |
} |
| 456 |
|
| 457 |
void ServerBuilder::InternalAddPluginFactory( |
| 458 |
std::unique_ptr<ServerBuilderPlugin> (*CreatePlugin)()) { |
| 459 |
gpr_once_init(&once_init_plugin_list, do_plugin_list_init); |
| 460 |
(*g_plugin_factory_list).push_back(CreatePlugin); |
| 461 |
} |
| 462 |
|
| 463 |
ServerBuilder& ServerBuilder::EnableWorkaround(grpc_workaround_list id) { |
| 464 |
switch (id) { |
| 465 |
case GRPC_WORKAROUND_ID_CRONET_COMPRESSION: |
| 466 |
return AddChannelArgument(GRPC_ARG_WORKAROUND_CRONET_COMPRESSION, 1); |
| 467 |
default: |
| 468 |
gpr_log(GPR_ERROR, "Workaround %u does not exist or is obsolete.", id); |
| 469 |
return *this; |
| 470 |
} |
| 471 |
} |
| 472 |
|
| 473 |
} // namespace grpc |