| 1 |
// |
| 2 |
// Copyright 2015-2016 gRPC authors. |
| 3 |
// |
| 4 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
// you may not use this file except in compliance with the License. |
| 6 |
// You may obtain a copy of the License at |
| 7 |
// |
| 8 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
// |
| 10 |
// Unless required by applicable law or agreed to in writing, software |
| 11 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
// See the License for the specific language governing permissions and |
| 14 |
// limitations under the License. |
| 15 |
// |
| 16 |
|
| 17 |
#include <grpc/support/port_platform.h> |
| 18 |
|
| 19 |
#include "src/core/lib/surface/server.h" |
| 20 |
|
| 21 |
#include <inttypes.h> |
| 22 |
#include <stdlib.h> |
| 23 |
#include <string.h> |
| 24 |
|
| 25 |
#include <algorithm> |
| 26 |
#include <atomic> |
| 27 |
#include <list> |
| 28 |
#include <new> |
| 29 |
#include <queue> |
| 30 |
#include <utility> |
| 31 |
#include <vector> |
| 32 |
|
| 33 |
#include "absl/status/status.h" |
| 34 |
#include "absl/types/optional.h" |
| 35 |
|
| 36 |
#include <grpc/byte_buffer.h> |
| 37 |
#include <grpc/impl/codegen/connectivity_state.h> |
| 38 |
#include <grpc/status.h> |
| 39 |
#include <grpc/support/log.h> |
| 40 |
#include <grpc/support/time.h> |
| 41 |
|
| 42 |
#include "src/core/lib/channel/channel_args.h" |
| 43 |
#include "src/core/lib/channel/channel_args_preconditioning.h" |
| 44 |
#include "src/core/lib/channel/channel_trace.h" |
| 45 |
#include "src/core/lib/channel/channelz.h" |
| 46 |
#include "src/core/lib/config/core_configuration.h" |
| 47 |
#include "src/core/lib/gpr/useful.h" |
| 48 |
#include "src/core/lib/gprpp/debug_location.h" |
| 49 |
#include "src/core/lib/gprpp/mpscq.h" |
| 50 |
#include "src/core/lib/gprpp/status_helper.h" |
| 51 |
#include "src/core/lib/iomgr/exec_ctx.h" |
| 52 |
#include "src/core/lib/iomgr/pollset_set.h" |
| 53 |
#include "src/core/lib/slice/slice_internal.h" |
| 54 |
#include "src/core/lib/surface/api_trace.h" |
| 55 |
#include "src/core/lib/surface/call.h" |
| 56 |
#include "src/core/lib/surface/channel.h" |
| 57 |
#include "src/core/lib/surface/channel_stack_type.h" |
| 58 |
#include "src/core/lib/surface/completion_queue.h" |
| 59 |
#include "src/core/lib/transport/connectivity_state.h" |
| 60 |
#include "src/core/lib/transport/error_utils.h" |
| 61 |
|
| 62 |
namespace grpc_core { |
| 63 |
|
| 64 |
TraceFlag grpc_server_channel_trace(false, "server_channel"); |
| 65 |
|
| 66 |
// |
| 67 |
// Server::RequestedCall |
| 68 |
// |
| 69 |
|
| 70 |
struct Server::RequestedCall { |
| 71 |
enum class Type { BATCH_CALL, REGISTERED_CALL }; |
| 72 |
|
| 73 |
RequestedCall(void* tag_arg, grpc_completion_queue* call_cq, |
| 74 |
grpc_call** call_arg, grpc_metadata_array* initial_md, |
| 75 |
grpc_call_details* details) |
| 76 |
: type(Type::BATCH_CALL), |
| 77 |
tag(tag_arg), |
| 78 |
cq_bound_to_call(call_cq), |
| 79 |
call(call_arg), |
| 80 |
initial_metadata(initial_md) { |
| 81 |
data.batch.details = details; |
| 82 |
} |
| 83 |
|
| 84 |
RequestedCall(void* tag_arg, grpc_completion_queue* call_cq, |
| 85 |
grpc_call** call_arg, grpc_metadata_array* initial_md, |
| 86 |
RegisteredMethod* rm, gpr_timespec* deadline, |
| 87 |
grpc_byte_buffer** optional_payload) |
| 88 |
: type(Type::REGISTERED_CALL), |
| 89 |
tag(tag_arg), |
| 90 |
cq_bound_to_call(call_cq), |
| 91 |
call(call_arg), |
| 92 |
initial_metadata(initial_md) { |
| 93 |
data.registered.method = rm; |
| 94 |
data.registered.deadline = deadline; |
| 95 |
data.registered.optional_payload = optional_payload; |
| 96 |
} |
| 97 |
|
| 98 |
MultiProducerSingleConsumerQueue::Node mpscq_node; |
| 99 |
const Type type; |
| 100 |
void* const tag; |
| 101 |
grpc_completion_queue* const cq_bound_to_call; |
| 102 |
grpc_call** const call; |
| 103 |
grpc_cq_completion completion; |
| 104 |
grpc_metadata_array* const initial_metadata; |
| 105 |
union { |
| 106 |
struct { |
| 107 |
grpc_call_details* details; |
| 108 |
} batch; |
| 109 |
struct { |
| 110 |
RegisteredMethod* method; |
| 111 |
gpr_timespec* deadline; |
| 112 |
grpc_byte_buffer** optional_payload; |
| 113 |
} registered; |
| 114 |
} data; |
| 115 |
}; |
| 116 |
|
| 117 |
// |
| 118 |
// Server::RegisteredMethod |
| 119 |
// |
| 120 |
|
| 121 |
struct Server::RegisteredMethod { |
| 122 |
RegisteredMethod( |
| 123 |
const char* method_arg, const char* host_arg, |
| 124 |
grpc_server_register_method_payload_handling payload_handling_arg, |
| 125 |
uint32_t flags_arg) |
| 126 |
: method(method_arg == nullptr ? "" : method_arg), |
| 127 |
host(host_arg == nullptr ? "" : host_arg), |
| 128 |
payload_handling(payload_handling_arg), |
| 129 |
flags(flags_arg) {} |
| 130 |
|
| 131 |
~RegisteredMethod() = default; |
| 132 |
|
| 133 |
const std::string method; |
| 134 |
const std::string host; |
| 135 |
const grpc_server_register_method_payload_handling payload_handling; |
| 136 |
const uint32_t flags; |
| 137 |
// One request matcher per method. |
| 138 |
std::unique_ptr<RequestMatcherInterface> matcher; |
| 139 |
}; |
| 140 |
|
| 141 |
// |
| 142 |
// Server::RequestMatcherInterface |
| 143 |
// |
| 144 |
|
| 145 |
// RPCs that come in from the transport must be matched against RPC requests |
| 146 |
// from the application. An incoming request from the application can be matched |
| 147 |
// to an RPC that has already arrived or can be queued up for later use. |
| 148 |
// Likewise, an RPC coming in from the transport can either be matched to a |
| 149 |
// request that already arrived from the application or can be queued up for |
| 150 |
// later use (marked pending). If there is a match, the request's tag is posted |
| 151 |
// on the request's notification CQ. |
| 152 |
// |
| 153 |
// RequestMatcherInterface is the base class to provide this functionality. |
| 154 |
class Server::RequestMatcherInterface { |
| 155 |
public: |
| 156 |
virtual ~RequestMatcherInterface() {} |
| 157 |
|
| 158 |
// Unref the calls associated with any incoming RPCs in the pending queue (not |
| 159 |
// yet matched to an application-requested RPC). |
| 160 |
virtual void ZombifyPending() = 0; |
| 161 |
|
| 162 |
// Mark all application-requested RPCs failed if they have not been matched to |
| 163 |
// an incoming RPC. The error parameter indicates why the RPCs are being |
| 164 |
// failed (always server shutdown in all current implementations). |
| 165 |
virtual void KillRequests(grpc_error_handle error) = 0; |
| 166 |
|
| 167 |
// How many request queues are supported by this matcher. This is an abstract |
| 168 |
// concept that essentially maps to gRPC completion queues. |
| 169 |
virtual size_t request_queue_count() const = 0; |
| 170 |
|
| 171 |
// This function is invoked when the application requests a new RPC whose |
| 172 |
// information is in the call parameter. The request_queue_index marks the |
| 173 |
// queue onto which to place this RPC, and is typically associated with a gRPC |
| 174 |
// CQ. If there are pending RPCs waiting to be matched, publish one (match it |
| 175 |
// and notify the CQ). |
| 176 |
virtual void RequestCallWithPossiblePublish(size_t request_queue_index, |
| 177 |
RequestedCall* call) = 0; |
| 178 |
|
| 179 |
// This function is invoked on an incoming RPC, represented by the calld |
| 180 |
// object. The RequestMatcher will try to match it against an |
| 181 |
// application-requested RPC if possible or will place it in the pending queue |
| 182 |
// otherwise. To enable some measure of fairness between server CQs, the match |
| 183 |
// is done starting at the start_request_queue_index parameter in a cyclic |
| 184 |
// order rather than always starting at 0. |
| 185 |
virtual void MatchOrQueue(size_t start_request_queue_index, |
| 186 |
CallData* calld) = 0; |
| 187 |
|
| 188 |
// Returns the server associated with this request matcher |
| 189 |
virtual Server* server() const = 0; |
| 190 |
}; |
| 191 |
|
| 192 |
// The RealRequestMatcher is an implementation of RequestMatcherInterface that |
| 193 |
// actually uses all the features of RequestMatcherInterface: expecting the |
| 194 |
// application to explicitly request RPCs and then matching those to incoming |
| 195 |
// RPCs, along with a slow path by which incoming RPCs are put on a locked |
| 196 |
// pending list if they aren't able to be matched to an application request. |
| 197 |
class Server::RealRequestMatcher : public RequestMatcherInterface { |
| 198 |
public: |
| 199 |
explicit RealRequestMatcher(Server* server) |
| 200 |
: server_(server), requests_per_cq_(server->cqs_.size()) {} |
| 201 |
|
| 202 |
~RealRequestMatcher() override { |
| 203 |
for (LockedMultiProducerSingleConsumerQueue& queue : requests_per_cq_) { |
| 204 |
GPR_ASSERT(queue.Pop() == nullptr); |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
void ZombifyPending() override { |
| 209 |
while (!pending_.empty()) { |
| 210 |
CallData* calld = pending_.front(); |
| 211 |
calld->SetState(CallData::CallState::ZOMBIED); |
| 212 |
calld->KillZombie(); |
| 213 |
pending_.pop(); |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
void KillRequests(grpc_error_handle error) override { |
| 218 |
for (size_t i = 0; i < requests_per_cq_.size(); i++) { |
| 219 |
RequestedCall* rc; |
| 220 |
while ((rc = reinterpret_cast<RequestedCall*>( |
| 221 |
requests_per_cq_[i].Pop())) != nullptr) { |
| 222 |
server_->FailCall(i, rc, error); |
| 223 |
} |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
size_t request_queue_count() const override { |
| 228 |
return requests_per_cq_.size(); |
| 229 |
} |
| 230 |
|
| 231 |
void RequestCallWithPossiblePublish(size_t request_queue_index, |
| 232 |
RequestedCall* call) override { |
| 233 |
if (requests_per_cq_[request_queue_index].Push(&call->mpscq_node)) { |
| 234 |
/* this was the first queued request: we need to lock and start |
| 235 |
matching calls */ |
| 236 |
struct PendingCall { |
| 237 |
RequestedCall* rc = nullptr; |
| 238 |
CallData* calld; |
| 239 |
}; |
| 240 |
auto pop_next_pending = [this, request_queue_index] { |
| 241 |
PendingCall pending_call; |
| 242 |
{ |
| 243 |
MutexLock lock(&server_->mu_call_); |
| 244 |
if (!pending_.empty()) { |
| 245 |
pending_call.rc = reinterpret_cast<RequestedCall*>( |
| 246 |
requests_per_cq_[request_queue_index].Pop()); |
| 247 |
if (pending_call.rc != nullptr) { |
| 248 |
pending_call.calld = pending_.front(); |
| 249 |
pending_.pop(); |
| 250 |
} |
| 251 |
} |
| 252 |
} |
| 253 |
return pending_call; |
| 254 |
}; |
| 255 |
while (true) { |
| 256 |
PendingCall next_pending = pop_next_pending(); |
| 257 |
if (next_pending.rc == nullptr) break; |
| 258 |
if (!next_pending.calld->MaybeActivate()) { |
| 259 |
// Zombied Call |
| 260 |
next_pending.calld->KillZombie(); |
| 261 |
} else { |
| 262 |
next_pending.calld->Publish(request_queue_index, next_pending.rc); |
| 263 |
} |
| 264 |
} |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
void MatchOrQueue(size_t start_request_queue_index, |
| 269 |
CallData* calld) override { |
| 270 |
for (size_t i = 0; i < requests_per_cq_.size(); i++) { |
| 271 |
size_t cq_idx = (start_request_queue_index + i) % requests_per_cq_.size(); |
| 272 |
RequestedCall* rc = |
| 273 |
reinterpret_cast<RequestedCall*>(requests_per_cq_[cq_idx].TryPop()); |
| 274 |
if (rc != nullptr) { |
| 275 |
calld->SetState(CallData::CallState::ACTIVATED); |
| 276 |
calld->Publish(cq_idx, rc); |
| 277 |
return; |
| 278 |
} |
| 279 |
} |
| 280 |
// No cq to take the request found; queue it on the slow list. |
| 281 |
// We need to ensure that all the queues are empty. We do this under |
| 282 |
// the server mu_call_ lock to ensure that if something is added to |
| 283 |
// an empty request queue, it will block until the call is actually |
| 284 |
// added to the pending list. |
| 285 |
RequestedCall* rc = nullptr; |
| 286 |
size_t cq_idx = 0; |
| 287 |
size_t loop_count; |
| 288 |
{ |
| 289 |
MutexLock lock(&server_->mu_call_); |
| 290 |
for (loop_count = 0; loop_count < requests_per_cq_.size(); loop_count++) { |
| 291 |
cq_idx = |
| 292 |
(start_request_queue_index + loop_count) % requests_per_cq_.size(); |
| 293 |
rc = reinterpret_cast<RequestedCall*>(requests_per_cq_[cq_idx].Pop()); |
| 294 |
if (rc != nullptr) { |
| 295 |
break; |
| 296 |
} |
| 297 |
} |
| 298 |
if (rc == nullptr) { |
| 299 |
calld->SetState(CallData::CallState::PENDING); |
| 300 |
pending_.push(calld); |
| 301 |
return; |
| 302 |
} |
| 303 |
} |
| 304 |
calld->SetState(CallData::CallState::ACTIVATED); |
| 305 |
calld->Publish(cq_idx, rc); |
| 306 |
} |
| 307 |
|
| 308 |
Server* server() const override { return server_; } |
| 309 |
|
| 310 |
private: |
| 311 |
Server* const server_; |
| 312 |
std::queue<CallData*> pending_; |
| 313 |
std::vector<LockedMultiProducerSingleConsumerQueue> requests_per_cq_; |
| 314 |
}; |
| 315 |
|
| 316 |
// AllocatingRequestMatchers don't allow the application to request an RPC in |
| 317 |
// advance or queue up any incoming RPC for later match. Instead, MatchOrQueue |
| 318 |
// will call out to an allocation function passed in at the construction of the |
| 319 |
// object. These request matchers are designed for the C++ callback API, so they |
| 320 |
// only support 1 completion queue (passed in at the constructor). They are also |
| 321 |
// used for the sync API. |
| 322 |
class Server::AllocatingRequestMatcherBase : public RequestMatcherInterface { |
| 323 |
public: |
| 324 |
AllocatingRequestMatcherBase(Server* server, grpc_completion_queue* cq) |
| 325 |
: server_(server), cq_(cq) { |
| 326 |
size_t idx; |
| 327 |
for (idx = 0; idx < server->cqs_.size(); idx++) { |
| 328 |
if (server->cqs_[idx] == cq) { |
| 329 |
break; |
| 330 |
} |
| 331 |
} |
| 332 |
GPR_ASSERT(idx < server->cqs_.size()); |
| 333 |
cq_idx_ = idx; |
| 334 |
} |
| 335 |
|
| 336 |
void ZombifyPending() override {} |
| 337 |
|
| 338 |
void KillRequests(grpc_error_handle /*error*/) override {} |
| 339 |
|
| 340 |
size_t request_queue_count() const override { return 0; } |
| 341 |
|
| 342 |
void RequestCallWithPossiblePublish(size_t /*request_queue_index*/, |
| 343 |
RequestedCall* /*call*/) final { |
| 344 |
GPR_ASSERT(false); |
| 345 |
} |
| 346 |
|
| 347 |
Server* server() const override { return server_; } |
| 348 |
|
| 349 |
// Supply the completion queue related to this request matcher |
| 350 |
grpc_completion_queue* cq() const { return cq_; } |
| 351 |
|
| 352 |
// Supply the completion queue's index relative to the server. |
| 353 |
size_t cq_idx() const { return cq_idx_; } |
| 354 |
|
| 355 |
private: |
| 356 |
Server* const server_; |
| 357 |
grpc_completion_queue* const cq_; |
| 358 |
size_t cq_idx_; |
| 359 |
}; |
| 360 |
|
| 361 |
// An allocating request matcher for non-registered methods (used for generic |
| 362 |
// API and unimplemented RPCs). |
| 363 |
class Server::AllocatingRequestMatcherBatch |
| 364 |
: public AllocatingRequestMatcherBase { |
| 365 |
public: |
| 366 |
AllocatingRequestMatcherBatch(Server* server, grpc_completion_queue* cq, |
| 367 |
std::function<BatchCallAllocation()> allocator) |
| 368 |
: AllocatingRequestMatcherBase(server, cq), |
| 369 |
allocator_(std::move(allocator)) {} |
| 370 |
|
| 371 |
void MatchOrQueue(size_t /*start_request_queue_index*/, |
| 372 |
CallData* calld) override { |
| 373 |
if (server()->ShutdownRefOnRequest()) { |
| 374 |
BatchCallAllocation call_info = allocator_(); |
| 375 |
GPR_ASSERT(server()->ValidateServerRequest( |
| 376 |
cq(), static_cast<void*>(call_info.tag), nullptr, |
| 377 |
nullptr) == GRPC_CALL_OK); |
| 378 |
RequestedCall* rc = new RequestedCall( |
| 379 |
static_cast<void*>(call_info.tag), call_info.cq, call_info.call, |
| 380 |
call_info.initial_metadata, call_info.details); |
| 381 |
calld->SetState(CallData::CallState::ACTIVATED); |
| 382 |
calld->Publish(cq_idx(), rc); |
| 383 |
} else { |
| 384 |
calld->FailCallCreation(); |
| 385 |
} |
| 386 |
server()->ShutdownUnrefOnRequest(); |
| 387 |
} |
| 388 |
|
| 389 |
private: |
| 390 |
std::function<BatchCallAllocation()> allocator_; |
| 391 |
}; |
| 392 |
|
| 393 |
// An allocating request matcher for registered methods. |
| 394 |
class Server::AllocatingRequestMatcherRegistered |
| 395 |
: public AllocatingRequestMatcherBase { |
| 396 |
public: |
| 397 |
AllocatingRequestMatcherRegistered( |
| 398 |
Server* server, grpc_completion_queue* cq, RegisteredMethod* rm, |
| 399 |
std::function<RegisteredCallAllocation()> allocator) |
| 400 |
: AllocatingRequestMatcherBase(server, cq), |
| 401 |
registered_method_(rm), |
| 402 |
allocator_(std::move(allocator)) {} |
| 403 |
|
| 404 |
void MatchOrQueue(size_t /*start_request_queue_index*/, |
| 405 |
CallData* calld) override { |
| 406 |
if (server()->ShutdownRefOnRequest()) { |
| 407 |
RegisteredCallAllocation call_info = allocator_(); |
| 408 |
GPR_ASSERT(server()->ValidateServerRequest( |
| 409 |
cq(), call_info.tag, call_info.optional_payload, |
| 410 |
registered_method_) == GRPC_CALL_OK); |
| 411 |
RequestedCall* rc = |
| 412 |
new RequestedCall(call_info.tag, call_info.cq, call_info.call, |
| 413 |
call_info.initial_metadata, registered_method_, |
| 414 |
call_info.deadline, call_info.optional_payload); |
| 415 |
calld->SetState(CallData::CallState::ACTIVATED); |
| 416 |
calld->Publish(cq_idx(), rc); |
| 417 |
} else { |
| 418 |
calld->FailCallCreation(); |
| 419 |
} |
| 420 |
server()->ShutdownUnrefOnRequest(); |
| 421 |
} |
| 422 |
|
| 423 |
private: |
| 424 |
RegisteredMethod* const registered_method_; |
| 425 |
std::function<RegisteredCallAllocation()> allocator_; |
| 426 |
}; |
| 427 |
|
| 428 |
// |
| 429 |
// ChannelBroadcaster |
| 430 |
// |
| 431 |
|
| 432 |
namespace { |
| 433 |
|
| 434 |
class ChannelBroadcaster { |
| 435 |
public: |
| 436 |
// This can have an empty constructor and destructor since we want to control |
| 437 |
// when the actual setup and shutdown broadcast take place. |
| 438 |
|
| 439 |
// Copies over the channels from the locked server. |
| 440 |
void FillChannelsLocked(std::vector<RefCountedPtr<Channel>> channels) { |
| 441 |
GPR_DEBUG_ASSERT(channels_.empty()); |
| 442 |
channels_ = std::move(channels); |
| 443 |
} |
| 444 |
|
| 445 |
// Broadcasts a shutdown on each channel. |
| 446 |
void BroadcastShutdown(bool send_goaway, grpc_error_handle force_disconnect) { |
| 447 |
for (const RefCountedPtr<Channel>& channel : channels_) { |
| 448 |
SendShutdown(channel->c_ptr(), send_goaway, force_disconnect); |
| 449 |
} |
| 450 |
channels_.clear(); // just for safety against double broadcast |
| 451 |
} |
| 452 |
|
| 453 |
private: |
| 454 |
struct ShutdownCleanupArgs { |
| 455 |
grpc_closure closure; |
| 456 |
grpc_slice slice; |
| 457 |
}; |
| 458 |
|
| 459 |
static void ShutdownCleanup(void* arg, grpc_error_handle /*error*/) { |
| 460 |
ShutdownCleanupArgs* a = static_cast<ShutdownCleanupArgs*>(arg); |
| 461 |
CSliceUnref(a->slice); |
| 462 |
delete a; |
| 463 |
} |
| 464 |
|
| 465 |
static void SendShutdown(grpc_channel* channel, bool send_goaway, |
| 466 |
grpc_error_handle send_disconnect) { |
| 467 |
ShutdownCleanupArgs* sc = new ShutdownCleanupArgs; |
| 468 |
GRPC_CLOSURE_INIT(&sc->closure, ShutdownCleanup, sc, |
| 469 |
grpc_schedule_on_exec_ctx); |
| 470 |
grpc_transport_op* op = grpc_make_transport_op(&sc->closure); |
| 471 |
grpc_channel_element* elem; |
| 472 |
op->goaway_error = |
| 473 |
send_goaway |
| 474 |
? grpc_error_set_int(GRPC_ERROR_CREATE("Server shutdown"), |
| 475 |
StatusIntProperty::kRpcStatus, GRPC_STATUS_OK) |
| 476 |
: absl::OkStatus(); |
| 477 |
sc->slice = grpc_slice_from_copied_string("Server shutdown"); |
| 478 |
op->disconnect_with_error = send_disconnect; |
| 479 |
elem = |
| 480 |
grpc_channel_stack_element(grpc_channel_get_channel_stack(channel), 0); |
| 481 |
elem->filter->start_transport_op(elem, op); |
| 482 |
} |
| 483 |
|
| 484 |
std::vector<RefCountedPtr<Channel>> channels_; |
| 485 |
}; |
| 486 |
|
| 487 |
} // namespace |
| 488 |
|
| 489 |
// |
| 490 |
// Server |
| 491 |
// |
| 492 |
|
| 493 |
const grpc_channel_filter Server::kServerTopFilter = { |
| 494 |
Server::CallData::StartTransportStreamOpBatch, |
| 495 |
nullptr, |
| 496 |
grpc_channel_next_op, |
| 497 |
sizeof(Server::CallData), |
| 498 |
Server::CallData::InitCallElement, |
| 499 |
grpc_call_stack_ignore_set_pollset_or_pollset_set, |
| 500 |
Server::CallData::DestroyCallElement, |
| 501 |
sizeof(Server::ChannelData), |
| 502 |
Server::ChannelData::InitChannelElement, |
| 503 |
grpc_channel_stack_no_post_init, |
| 504 |
Server::ChannelData::DestroyChannelElement, |
| 505 |
grpc_channel_next_get_info, |
| 506 |
"server", |
| 507 |
}; |
| 508 |
|
| 509 |
namespace { |
| 510 |
|
| 511 |
RefCountedPtr<channelz::ServerNode> CreateChannelzNode( |
| 512 |
const ChannelArgs& args) { |
| 513 |
RefCountedPtr<channelz::ServerNode> channelz_node; |
| 514 |
if (args.GetBool(GRPC_ARG_ENABLE_CHANNELZ) |
| 515 |
.value_or(GRPC_ENABLE_CHANNELZ_DEFAULT)) { |
| 516 |
size_t channel_tracer_max_memory = std::max( |
| 517 |
0, args.GetInt(GRPC_ARG_MAX_CHANNEL_TRACE_EVENT_MEMORY_PER_NODE) |
| 518 |
.value_or(GRPC_MAX_CHANNEL_TRACE_EVENT_MEMORY_PER_NODE_DEFAULT)); |
| 519 |
channelz_node = |
| 520 |
MakeRefCounted<channelz::ServerNode>(channel_tracer_max_memory); |
| 521 |
channelz_node->AddTraceEvent( |
| 522 |
channelz::ChannelTrace::Severity::Info, |
| 523 |
grpc_slice_from_static_string("Server created")); |
| 524 |
} |
| 525 |
return channelz_node; |
| 526 |
} |
| 527 |
|
| 528 |
} // namespace |
| 529 |
|
| 530 |
Server::Server(const ChannelArgs& args) |
| 531 |
: channel_args_(args), channelz_node_(CreateChannelzNode(args)) {} |
| 532 |
|
| 533 |
Server::~Server() { |
| 534 |
// Remove the cq pollsets from the config_fetcher. |
| 535 |
if (started_ && config_fetcher_ != nullptr && |
| 536 |
config_fetcher_->interested_parties() != nullptr) { |
| 537 |
for (grpc_pollset* pollset : pollsets_) { |
| 538 |
grpc_pollset_set_del_pollset(config_fetcher_->interested_parties(), |
| 539 |
pollset); |
| 540 |
} |
| 541 |
} |
| 542 |
for (size_t i = 0; i < cqs_.size(); i++) { |
| 543 |
GRPC_CQ_INTERNAL_UNREF(cqs_[i], "server"); |
| 544 |
} |
| 545 |
} |
| 546 |
|
| 547 |
void Server::AddListener(OrphanablePtr<ListenerInterface> listener) { |
| 548 |
channelz::ListenSocketNode* listen_socket_node = |
| 549 |
listener->channelz_listen_socket_node(); |
| 550 |
if (listen_socket_node != nullptr && channelz_node_ != nullptr) { |
| 551 |
channelz_node_->AddChildListenSocket(listen_socket_node->Ref()); |
| 552 |
} |
| 553 |
listeners_.emplace_back(std::move(listener)); |
| 554 |
} |
| 555 |
|
| 556 |
void Server::Start() { |
| 557 |
started_ = true; |
| 558 |
for (grpc_completion_queue* cq : cqs_) { |
| 559 |
if (grpc_cq_can_listen(cq)) { |
| 560 |
pollsets_.push_back(grpc_cq_pollset(cq)); |
| 561 |
} |
| 562 |
} |
| 563 |
if (unregistered_request_matcher_ == nullptr) { |
| 564 |
unregistered_request_matcher_ = std::make_unique<RealRequestMatcher>(this); |
| 565 |
} |
| 566 |
for (std::unique_ptr<RegisteredMethod>& rm : registered_methods_) { |
| 567 |
if (rm->matcher == nullptr) { |
| 568 |
rm->matcher = std::make_unique<RealRequestMatcher>(this); |
| 569 |
} |
| 570 |
} |
| 571 |
{ |
| 572 |
MutexLock lock(&mu_global_); |
| 573 |
starting_ = true; |
| 574 |
} |
| 575 |
// Register the interested parties from the config fetcher to the cq pollsets |
| 576 |
// before starting listeners so that config fetcher is being polled when the |
| 577 |
// listeners start watch the fetcher. |
| 578 |
if (config_fetcher_ != nullptr && |
| 579 |
config_fetcher_->interested_parties() != nullptr) { |
| 580 |
for (grpc_pollset* pollset : pollsets_) { |
| 581 |
grpc_pollset_set_add_pollset(config_fetcher_->interested_parties(), |
| 582 |
pollset); |
| 583 |
} |
| 584 |
} |
| 585 |
for (auto& listener : listeners_) { |
| 586 |
listener.listener->Start(this, &pollsets_); |
| 587 |
} |
| 588 |
MutexLock lock(&mu_global_); |
| 589 |
starting_ = false; |
| 590 |
starting_cv_.Signal(); |
| 591 |
} |
| 592 |
|
| 593 |
grpc_error_handle Server::SetupTransport( |
| 594 |
grpc_transport* transport, grpc_pollset* accepting_pollset, |
| 595 |
const ChannelArgs& args, |
| 596 |
const RefCountedPtr<channelz::SocketNode>& socket_node) { |
| 597 |
// Create channel. |
| 598 |
absl::StatusOr<RefCountedPtr<Channel>> channel = |
| 599 |
Channel::Create(nullptr, args, GRPC_SERVER_CHANNEL, transport); |
| 600 |
if (!channel.ok()) { |
| 601 |
return absl_status_to_grpc_error(channel.status()); |
| 602 |
} |
| 603 |
ChannelData* chand = static_cast<ChannelData*>( |
| 604 |
grpc_channel_stack_element((*channel)->channel_stack(), 0)->channel_data); |
| 605 |
// Set up CQs. |
| 606 |
size_t cq_idx; |
| 607 |
for (cq_idx = 0; cq_idx < cqs_.size(); cq_idx++) { |
| 608 |
if (grpc_cq_pollset(cqs_[cq_idx]) == accepting_pollset) break; |
| 609 |
} |
| 610 |
if (cq_idx == cqs_.size()) { |
| 611 |
// Completion queue not found. Pick a random one to publish new calls to. |
| 612 |
cq_idx = static_cast<size_t>(rand()) % cqs_.size(); |
| 613 |
} |
| 614 |
// Set up channelz node. |
| 615 |
intptr_t channelz_socket_uuid = 0; |
| 616 |
if (socket_node != nullptr) { |
| 617 |
channelz_socket_uuid = socket_node->uuid(); |
| 618 |
channelz_node_->AddChildSocket(socket_node); |
| 619 |
} |
| 620 |
// Initialize chand. |
| 621 |
chand->InitTransport(Ref(), std::move(*channel), cq_idx, transport, |
| 622 |
channelz_socket_uuid); |
| 623 |
return absl::OkStatus(); |
| 624 |
} |
| 625 |
|
| 626 |
bool Server::HasOpenConnections() { |
| 627 |
MutexLock lock(&mu_global_); |
| 628 |
return !channels_.empty(); |
| 629 |
} |
| 630 |
|
| 631 |
void Server::SetRegisteredMethodAllocator( |
| 632 |
grpc_completion_queue* cq, void* method_tag, |
| 633 |
std::function<RegisteredCallAllocation()> allocator) { |
| 634 |
RegisteredMethod* rm = static_cast<RegisteredMethod*>(method_tag); |
| 635 |
rm->matcher = std::make_unique<AllocatingRequestMatcherRegistered>( |
| 636 |
this, cq, rm, std::move(allocator)); |
| 637 |
} |
| 638 |
|
| 639 |
void Server::SetBatchMethodAllocator( |
| 640 |
grpc_completion_queue* cq, std::function<BatchCallAllocation()> allocator) { |
| 641 |
GPR_DEBUG_ASSERT(unregistered_request_matcher_ == nullptr); |
| 642 |
unregistered_request_matcher_ = |
| 643 |
std::make_unique<AllocatingRequestMatcherBatch>(this, cq, |
| 644 |
std::move(allocator)); |
| 645 |
} |
| 646 |
|
| 647 |
void Server::RegisterCompletionQueue(grpc_completion_queue* cq) { |
| 648 |
for (grpc_completion_queue* queue : cqs_) { |
| 649 |
if (queue == cq) return; |
| 650 |
} |
| 651 |
GRPC_CQ_INTERNAL_REF(cq, "server"); |
| 652 |
cqs_.push_back(cq); |
| 653 |
} |
| 654 |
|
| 655 |
namespace { |
| 656 |
|
| 657 |
bool streq(const std::string& a, const char* b) { |
| 658 |
return (a.empty() && b == nullptr) || |
| 659 |
((b != nullptr) && !strcmp(a.c_str(), b)); |
| 660 |
} |
| 661 |
|
| 662 |
} // namespace |
| 663 |
|
| 664 |
Server::RegisteredMethod* Server::RegisterMethod( |
| 665 |
const char* method, const char* host, |
| 666 |
grpc_server_register_method_payload_handling payload_handling, |
| 667 |
uint32_t flags) { |
| 668 |
if (!method) { |
| 669 |
gpr_log(GPR_ERROR, |
| 670 |
"grpc_server_register_method method string cannot be NULL"); |
| 671 |
return nullptr; |
| 672 |
} |
| 673 |
for (std::unique_ptr<RegisteredMethod>& m : registered_methods_) { |
| 674 |
if (streq(m->method, method) && streq(m->host, host)) { |
| 675 |
gpr_log(GPR_ERROR, "duplicate registration for %s@%s", method, |
| 676 |
host ? host : "*"); |
| 677 |
return nullptr; |
| 678 |
} |
| 679 |
} |
| 680 |
if (flags != 0) { |
| 681 |
gpr_log(GPR_ERROR, "grpc_server_register_method invalid flags 0x%08x", |
| 682 |
flags); |
| 683 |
return nullptr; |
| 684 |
} |
| 685 |
registered_methods_.emplace_back(std::make_unique<RegisteredMethod>( |
| 686 |
method, host, payload_handling, flags)); |
| 687 |
return registered_methods_.back().get(); |
| 688 |
} |
| 689 |
|
| 690 |
void Server::DoneRequestEvent(void* req, grpc_cq_completion* /*c*/) { |
| 691 |
delete static_cast<RequestedCall*>(req); |
| 692 |
} |
| 693 |
|
| 694 |
void Server::FailCall(size_t cq_idx, RequestedCall* rc, |
| 695 |
grpc_error_handle error) { |
| 696 |
*rc->call = nullptr; |
| 697 |
rc->initial_metadata->count = 0; |
| 698 |
GPR_ASSERT(!error.ok()); |
| 699 |
grpc_cq_end_op(cqs_[cq_idx], rc->tag, error, DoneRequestEvent, rc, |
| 700 |
&rc->completion); |
| 701 |
} |
| 702 |
|
| 703 |
// Before calling MaybeFinishShutdown(), we must hold mu_global_ and not |
| 704 |
// hold mu_call_. |
| 705 |
void Server::MaybeFinishShutdown() { |
| 706 |
if (!ShutdownReady() || shutdown_published_) { |
| 707 |
return; |
| 708 |
} |
| 709 |
{ |
| 710 |
MutexLock lock(&mu_call_); |
| 711 |
KillPendingWorkLocked(GRPC_ERROR_CREATE("Server Shutdown")); |
| 712 |
} |
| 713 |
if (!channels_.empty() || listeners_destroyed_ < listeners_.size()) { |
| 714 |
if (gpr_time_cmp(gpr_time_sub(gpr_now(GPR_CLOCK_REALTIME), |
| 715 |
last_shutdown_message_time_), |
| 716 |
gpr_time_from_seconds(1, GPR_TIMESPAN)) >= 0) { |
| 717 |
last_shutdown_message_time_ = gpr_now(GPR_CLOCK_REALTIME); |
| 718 |
gpr_log(GPR_DEBUG, |
| 719 |
"Waiting for %" PRIuPTR " channels and %" PRIuPTR "/%" PRIuPTR |
| 720 |
" listeners to be destroyed before shutting down server", |
| 721 |
channels_.size(), listeners_.size() - listeners_destroyed_, |
| 722 |
listeners_.size()); |
| 723 |
} |
| 724 |
return; |
| 725 |
} |
| 726 |
shutdown_published_ = true; |
| 727 |
for (auto& shutdown_tag : shutdown_tags_) { |
| 728 |
Ref().release(); |
| 729 |
grpc_cq_end_op(shutdown_tag.cq, shutdown_tag.tag, absl::OkStatus(), |
| 730 |
DoneShutdownEvent, this, &shutdown_tag.completion); |
| 731 |
} |
| 732 |
} |
| 733 |
|
| 734 |
void Server::KillPendingWorkLocked(grpc_error_handle error) { |
| 735 |
if (started_) { |
| 736 |
unregistered_request_matcher_->KillRequests(error); |
| 737 |
unregistered_request_matcher_->ZombifyPending(); |
| 738 |
for (std::unique_ptr<RegisteredMethod>& rm : registered_methods_) { |
| 739 |
rm->matcher->KillRequests(error); |
| 740 |
rm->matcher->ZombifyPending(); |
| 741 |
} |
| 742 |
} |
| 743 |
} |
| 744 |
|
| 745 |
std::vector<RefCountedPtr<Channel>> Server::GetChannelsLocked() const { |
| 746 |
std::vector<RefCountedPtr<Channel>> channels; |
| 747 |
channels.reserve(channels_.size()); |
| 748 |
for (const ChannelData* chand : channels_) { |
| 749 |
channels.push_back(chand->channel()->Ref()); |
| 750 |
} |
| 751 |
return channels; |
| 752 |
} |
| 753 |
|
| 754 |
void Server::ListenerDestroyDone(void* arg, grpc_error_handle /*error*/) { |
| 755 |
Server* server = static_cast<Server*>(arg); |
| 756 |
MutexLock lock(&server->mu_global_); |
| 757 |
server->listeners_destroyed_++; |
| 758 |
server->MaybeFinishShutdown(); |
| 759 |
} |
| 760 |
|
| 761 |
namespace { |
| 762 |
|
| 763 |
void DonePublishedShutdown(void* /*done_arg*/, grpc_cq_completion* storage) { |
| 764 |
delete storage; |
| 765 |
} |
| 766 |
|
| 767 |
} // namespace |
| 768 |
|
| 769 |
// - Kills all pending requests-for-incoming-RPC-calls (i.e., the requests made |
| 770 |
// via grpc_server_request_call() and grpc_server_request_registered_call() |
| 771 |
// will now be cancelled). See KillPendingWorkLocked(). |
| 772 |
// |
| 773 |
// - Shuts down the listeners (i.e., the server will no longer listen on the |
| 774 |
// port for new incoming channels). |
| 775 |
// |
| 776 |
// - Iterates through all channels on the server and sends shutdown msg (see |
| 777 |
// ChannelBroadcaster::BroadcastShutdown() for details) to the clients via |
| 778 |
// the transport layer. The transport layer then guarantees the following: |
| 779 |
// -- Sends shutdown to the client (e.g., HTTP2 transport sends GOAWAY). |
| 780 |
// -- If the server has outstanding calls that are in the process, the |
| 781 |
// connection is NOT closed until the server is done with all those calls. |
| 782 |
// -- Once there are no more calls in progress, the channel is closed. |
| 783 |
void Server::ShutdownAndNotify(grpc_completion_queue* cq, void* tag) { |
| 784 |
Notification* await_requests = nullptr; |
| 785 |
ChannelBroadcaster broadcaster; |
| 786 |
{ |
| 787 |
// Wait for startup to be finished. Locks mu_global. |
| 788 |
MutexLock lock(&mu_global_); |
| 789 |
while (starting_) { |
| 790 |
starting_cv_.Wait(&mu_global_); |
| 791 |
} |
| 792 |
// Stay locked, and gather up some stuff to do. |
| 793 |
GPR_ASSERT(grpc_cq_begin_op(cq, tag)); |
| 794 |
if (shutdown_published_) { |
| 795 |
grpc_cq_end_op(cq, tag, absl::OkStatus(), DonePublishedShutdown, nullptr, |
| 796 |
new grpc_cq_completion); |
| 797 |
return; |
| 798 |
} |
| 799 |
shutdown_tags_.emplace_back(tag, cq); |
| 800 |
if (ShutdownCalled()) { |
| 801 |
return; |
| 802 |
} |
| 803 |
last_shutdown_message_time_ = gpr_now(GPR_CLOCK_REALTIME); |
| 804 |
broadcaster.FillChannelsLocked(GetChannelsLocked()); |
| 805 |
// Collect all unregistered then registered calls. |
| 806 |
{ |
| 807 |
MutexLock lock(&mu_call_); |
| 808 |
KillPendingWorkLocked(GRPC_ERROR_CREATE("Server Shutdown")); |
| 809 |
} |
| 810 |
await_requests = ShutdownUnrefOnShutdownCall(); |
| 811 |
} |
| 812 |
// We expect no new requests but there can still be requests in-flight. |
| 813 |
// Wait for them to complete before proceeding. |
| 814 |
if (await_requests != nullptr) { |
| 815 |
await_requests->WaitForNotification(); |
| 816 |
} |
| 817 |
StopListening(); |
| 818 |
broadcaster.BroadcastShutdown(/*send_goaway=*/true, absl::OkStatus()); |
| 819 |
} |
| 820 |
|
| 821 |
void Server::StopListening() { |
| 822 |
for (auto& listener : listeners_) { |
| 823 |
if (listener.listener == nullptr) continue; |
| 824 |
channelz::ListenSocketNode* channelz_listen_socket_node = |
| 825 |
listener.listener->channelz_listen_socket_node(); |
| 826 |
if (channelz_node_ != nullptr && channelz_listen_socket_node != nullptr) { |
| 827 |
channelz_node_->RemoveChildListenSocket( |
| 828 |
channelz_listen_socket_node->uuid()); |
| 829 |
} |
| 830 |
GRPC_CLOSURE_INIT(&listener.destroy_done, ListenerDestroyDone, this, |
| 831 |
grpc_schedule_on_exec_ctx); |
| 832 |
listener.listener->SetOnDestroyDone(&listener.destroy_done); |
| 833 |
listener.listener.reset(); |
| 834 |
} |
| 835 |
} |
| 836 |
|
| 837 |
void Server::CancelAllCalls() { |
| 838 |
ChannelBroadcaster broadcaster; |
| 839 |
{ |
| 840 |
MutexLock lock(&mu_global_); |
| 841 |
broadcaster.FillChannelsLocked(GetChannelsLocked()); |
| 842 |
} |
| 843 |
broadcaster.BroadcastShutdown( |
| 844 |
/*send_goaway=*/false, GRPC_ERROR_CREATE("Cancelling all calls")); |
| 845 |
} |
| 846 |
|
| 847 |
void Server::SendGoaways() { |
| 848 |
ChannelBroadcaster broadcaster; |
| 849 |
{ |
| 850 |
MutexLock lock(&mu_global_); |
| 851 |
broadcaster.FillChannelsLocked(GetChannelsLocked()); |
| 852 |
} |
| 853 |
broadcaster.BroadcastShutdown(/*send_goaway=*/true, absl::OkStatus()); |
| 854 |
} |
| 855 |
|
| 856 |
void Server::Orphan() { |
| 857 |
{ |
| 858 |
MutexLock lock(&mu_global_); |
| 859 |
GPR_ASSERT(ShutdownCalled() || listeners_.empty()); |
| 860 |
GPR_ASSERT(listeners_destroyed_ == listeners_.size()); |
| 861 |
} |
| 862 |
Unref(); |
| 863 |
} |
| 864 |
|
| 865 |
grpc_call_error Server::ValidateServerRequest( |
| 866 |
grpc_completion_queue* cq_for_notification, void* tag, |
| 867 |
grpc_byte_buffer** optional_payload, RegisteredMethod* rm) { |
| 868 |
if ((rm == nullptr && optional_payload != nullptr) || |
| 869 |
((rm != nullptr) && ((optional_payload == nullptr) != |
| 870 |
(rm->payload_handling == GRPC_SRM_PAYLOAD_NONE)))) { |
| 871 |
return GRPC_CALL_ERROR_PAYLOAD_TYPE_MISMATCH; |
| 872 |
} |
| 873 |
if (!grpc_cq_begin_op(cq_for_notification, tag)) { |
| 874 |
return GRPC_CALL_ERROR_COMPLETION_QUEUE_SHUTDOWN; |
| 875 |
} |
| 876 |
return GRPC_CALL_OK; |
| 877 |
} |
| 878 |
|
| 879 |
grpc_call_error Server::ValidateServerRequestAndCq( |
| 880 |
size_t* cq_idx, grpc_completion_queue* cq_for_notification, void* tag, |
| 881 |
grpc_byte_buffer** optional_payload, RegisteredMethod* rm) { |
| 882 |
size_t idx; |
| 883 |
for (idx = 0; idx < cqs_.size(); idx++) { |
| 884 |
if (cqs_[idx] == cq_for_notification) { |
| 885 |
break; |
| 886 |
} |
| 887 |
} |
| 888 |
if (idx == cqs_.size()) { |
| 889 |
return GRPC_CALL_ERROR_NOT_SERVER_COMPLETION_QUEUE; |
| 890 |
} |
| 891 |
grpc_call_error error = |
| 892 |
ValidateServerRequest(cq_for_notification, tag, optional_payload, rm); |
| 893 |
if (error != GRPC_CALL_OK) { |
| 894 |
return error; |
| 895 |
} |
| 896 |
*cq_idx = idx; |
| 897 |
return GRPC_CALL_OK; |
| 898 |
} |
| 899 |
|
| 900 |
grpc_call_error Server::QueueRequestedCall(size_t cq_idx, RequestedCall* rc) { |
| 901 |
if (ShutdownCalled()) { |
| 902 |
FailCall(cq_idx, rc, GRPC_ERROR_CREATE("Server Shutdown")); |
| 903 |
return GRPC_CALL_OK; |
| 904 |
} |
| 905 |
RequestMatcherInterface* rm; |
| 906 |
switch (rc->type) { |
| 907 |
case RequestedCall::Type::BATCH_CALL: |
| 908 |
rm = unregistered_request_matcher_.get(); |
| 909 |
break; |
| 910 |
case RequestedCall::Type::REGISTERED_CALL: |
| 911 |
rm = rc->data.registered.method->matcher.get(); |
| 912 |
break; |
| 913 |
} |
| 914 |
rm->RequestCallWithPossiblePublish(cq_idx, rc); |
| 915 |
return GRPC_CALL_OK; |
| 916 |
} |
| 917 |
|
| 918 |
grpc_call_error Server::RequestCall(grpc_call** call, |
| 919 |
grpc_call_details* details, |
| 920 |
grpc_metadata_array* request_metadata, |
| 921 |
grpc_completion_queue* cq_bound_to_call, |
| 922 |
grpc_completion_queue* cq_for_notification, |
| 923 |
void* tag) { |
| 924 |
size_t cq_idx; |
| 925 |
grpc_call_error error = ValidateServerRequestAndCq( |
| 926 |
&cq_idx, cq_for_notification, tag, nullptr, nullptr); |
| 927 |
if (error != GRPC_CALL_OK) { |
| 928 |
return error; |
| 929 |
} |
| 930 |
RequestedCall* rc = |
| 931 |
new RequestedCall(tag, cq_bound_to_call, call, request_metadata, details); |
| 932 |
return QueueRequestedCall(cq_idx, rc); |
| 933 |
} |
| 934 |
|
| 935 |
grpc_call_error Server::RequestRegisteredCall( |
| 936 |
RegisteredMethod* rm, grpc_call** call, gpr_timespec* deadline, |
| 937 |
grpc_metadata_array* request_metadata, grpc_byte_buffer** optional_payload, |
| 938 |
grpc_completion_queue* cq_bound_to_call, |
| 939 |
grpc_completion_queue* cq_for_notification, void* tag_new) { |
| 940 |
size_t cq_idx; |
| 941 |
grpc_call_error error = ValidateServerRequestAndCq( |
| 942 |
&cq_idx, cq_for_notification, tag_new, optional_payload, rm); |
| 943 |
if (error != GRPC_CALL_OK) { |
| 944 |
return error; |
| 945 |
} |
| 946 |
RequestedCall* rc = |
| 947 |
new RequestedCall(tag_new, cq_bound_to_call, call, request_metadata, rm, |
| 948 |
deadline, optional_payload); |
| 949 |
return QueueRequestedCall(cq_idx, rc); |
| 950 |
} |
| 951 |
|
| 952 |
// |
| 953 |
// Server::ChannelData::ConnectivityWatcher |
| 954 |
// |
| 955 |
|
| 956 |
class Server::ChannelData::ConnectivityWatcher |
| 957 |
: public AsyncConnectivityStateWatcherInterface { |
| 958 |
public: |
| 959 |
explicit ConnectivityWatcher(ChannelData* chand) |
| 960 |
: chand_(chand), channel_(chand_->channel_->Ref()) {} |
| 961 |
|
| 962 |
private: |
| 963 |
void OnConnectivityStateChange(grpc_connectivity_state new_state, |
| 964 |
const absl::Status& /*status*/) override { |
| 965 |
// Don't do anything until we are being shut down. |
| 966 |
if (new_state != GRPC_CHANNEL_SHUTDOWN) return; |
| 967 |
// Shut down channel. |
| 968 |
MutexLock lock(&chand_->server_->mu_global_); |
| 969 |
chand_->Destroy(); |
| 970 |
} |
| 971 |
|
| 972 |
ChannelData* const chand_; |
| 973 |
const RefCountedPtr<Channel> channel_; |
| 974 |
}; |
| 975 |
|
| 976 |
// |
| 977 |
// Server::ChannelData |
| 978 |
// |
| 979 |
|
| 980 |
Server::ChannelData::~ChannelData() { |
| 981 |
registered_methods_.reset(); |
| 982 |
if (server_ != nullptr) { |
| 983 |
if (server_->channelz_node_ != nullptr && channelz_socket_uuid_ != 0) { |
| 984 |
server_->channelz_node_->RemoveChildSocket(channelz_socket_uuid_); |
| 985 |
} |
| 986 |
{ |
| 987 |
MutexLock lock(&server_->mu_global_); |
| 988 |
if (list_position_.has_value()) { |
| 989 |
server_->channels_.erase(*list_position_); |
| 990 |
list_position_.reset(); |
| 991 |
} |
| 992 |
server_->MaybeFinishShutdown(); |
| 993 |
} |
| 994 |
} |
| 995 |
} |
| 996 |
|
| 997 |
void Server::ChannelData::InitTransport(RefCountedPtr<Server> server, |
| 998 |
RefCountedPtr<Channel> channel, |
| 999 |
size_t cq_idx, |
| 1000 |
grpc_transport* transport, |
| 1001 |
intptr_t channelz_socket_uuid) { |
| 1002 |
server_ = std::move(server); |
| 1003 |
channel_ = channel; |
| 1004 |
cq_idx_ = cq_idx; |
| 1005 |
channelz_socket_uuid_ = channelz_socket_uuid; |
| 1006 |
// Build a lookup table phrased in terms of mdstr's in this channels context |
| 1007 |
// to quickly find registered methods. |
| 1008 |
size_t num_registered_methods = server_->registered_methods_.size(); |
| 1009 |
if (num_registered_methods > 0) { |
| 1010 |
uint32_t max_probes = 0; |
| 1011 |
size_t slots = 2 * num_registered_methods; |
| 1012 |
registered_methods_ = |
| 1013 |
std::make_unique<std::vector<ChannelRegisteredMethod>>(slots); |
| 1014 |
for (std::unique_ptr<RegisteredMethod>& rm : server_->registered_methods_) { |
| 1015 |
Slice host; |
| 1016 |
Slice method = Slice::FromExternalString(rm->method); |
| 1017 |
const bool has_host = !rm->host.empty(); |
| 1018 |
if (has_host) { |
| 1019 |
host = Slice::FromExternalString(rm->host.c_str()); |
| 1020 |
} |
| 1021 |
uint32_t hash = MixHash32(has_host ? host.Hash() : 0, method.Hash()); |
| 1022 |
uint32_t probes = 0; |
| 1023 |
for (probes = 0; (*registered_methods_)[(hash + probes) % slots] |
| 1024 |
.server_registered_method != nullptr; |
| 1025 |
probes++) { |
| 1026 |
} |
| 1027 |
if (probes > max_probes) max_probes = probes; |
| 1028 |
ChannelRegisteredMethod* crm = |
| 1029 |
&(*registered_methods_)[(hash + probes) % slots]; |
| 1030 |
crm->server_registered_method = rm.get(); |
| 1031 |
crm->flags = rm->flags; |
| 1032 |
crm->has_host = has_host; |
| 1033 |
if (has_host) { |
| 1034 |
crm->host = std::move(host); |
| 1035 |
} |
| 1036 |
crm->method = std::move(method); |
| 1037 |
} |
| 1038 |
GPR_ASSERT(slots <= UINT32_MAX); |
| 1039 |
registered_method_max_probes_ = max_probes; |
| 1040 |
} |
| 1041 |
// Publish channel. |
| 1042 |
{ |
| 1043 |
MutexLock lock(&server_->mu_global_); |
| 1044 |
server_->channels_.push_front(this); |
| 1045 |
list_position_ = server_->channels_.begin(); |
| 1046 |
} |
| 1047 |
// Start accept_stream transport op. |
| 1048 |
grpc_transport_op* op = grpc_make_transport_op(nullptr); |
| 1049 |
op->set_accept_stream = true; |
| 1050 |
op->set_accept_stream_fn = AcceptStream; |
| 1051 |
op->set_accept_stream_user_data = this; |
| 1052 |
op->start_connectivity_watch = MakeOrphanable<ConnectivityWatcher>(this); |
| 1053 |
if (server_->ShutdownCalled()) { |
| 1054 |
op->disconnect_with_error = GRPC_ERROR_CREATE("Server shutdown"); |
| 1055 |
} |
| 1056 |
grpc_transport_perform_op(transport, op); |
| 1057 |
} |
| 1058 |
|
| 1059 |
Server::ChannelRegisteredMethod* Server::ChannelData::GetRegisteredMethod( |
| 1060 |
const grpc_slice& host, const grpc_slice& path) { |
| 1061 |
if (registered_methods_ == nullptr) return nullptr; |
| 1062 |
/* TODO(ctiller): unify these two searches */ |
| 1063 |
/* check for an exact match with host */ |
| 1064 |
uint32_t hash = MixHash32(grpc_slice_hash(host), grpc_slice_hash(path)); |
| 1065 |
for (size_t i = 0; i <= registered_method_max_probes_; i++) { |
| 1066 |
ChannelRegisteredMethod* rm = |
| 1067 |
&(*registered_methods_)[(hash + i) % registered_methods_->size()]; |
| 1068 |
if (rm->server_registered_method == nullptr) break; |
| 1069 |
if (!rm->has_host) continue; |
| 1070 |
if (rm->host != host) continue; |
| 1071 |
if (rm->method != path) continue; |
| 1072 |
return rm; |
| 1073 |
} |
| 1074 |
/* check for a wildcard method definition (no host set) */ |
| 1075 |
hash = MixHash32(0, grpc_slice_hash(path)); |
| 1076 |
for (size_t i = 0; i <= registered_method_max_probes_; i++) { |
| 1077 |
ChannelRegisteredMethod* rm = |
| 1078 |
&(*registered_methods_)[(hash + i) % registered_methods_->size()]; |
| 1079 |
if (rm->server_registered_method == nullptr) break; |
| 1080 |
if (rm->has_host) continue; |
| 1081 |
if (rm->method != path) continue; |
| 1082 |
return rm; |
| 1083 |
} |
| 1084 |
return nullptr; |
| 1085 |
} |
| 1086 |
|
| 1087 |
void Server::ChannelData::AcceptStream(void* arg, grpc_transport* /*transport*/, |
| 1088 |
const void* transport_server_data) { |
| 1089 |
auto* chand = static_cast<Server::ChannelData*>(arg); |
| 1090 |
/* create a call */ |
| 1091 |
grpc_call_create_args args; |
| 1092 |
args.channel = chand->channel_; |
| 1093 |
args.server = chand->server_.get(); |
| 1094 |
args.parent = nullptr; |
| 1095 |
args.propagation_mask = 0; |
| 1096 |
args.cq = nullptr; |
| 1097 |
args.pollset_set_alternative = nullptr; |
| 1098 |
args.server_transport_data = transport_server_data; |
| 1099 |
args.send_deadline = Timestamp::InfFuture(); |
| 1100 |
grpc_call* call; |
| 1101 |
grpc_error_handle error = grpc_call_create(&args, &call); |
| 1102 |
grpc_call_element* elem = |
| 1103 |
grpc_call_stack_element(grpc_call_get_call_stack(call), 0); |
| 1104 |
auto* calld = static_cast<Server::CallData*>(elem->call_data); |
| 1105 |
if (!error.ok()) { |
| 1106 |
calld->FailCallCreation(); |
| 1107 |
return; |
| 1108 |
} |
| 1109 |
calld->Start(elem); |
| 1110 |
} |
| 1111 |
|
| 1112 |
void Server::ChannelData::FinishDestroy(void* arg, |
| 1113 |
grpc_error_handle /*error*/) { |
| 1114 |
auto* chand = static_cast<Server::ChannelData*>(arg); |
| 1115 |
Server* server = chand->server_.get(); |
| 1116 |
auto* channel_stack = chand->channel_->channel_stack(); |
| 1117 |
chand->channel_.reset(); |
| 1118 |
server->Unref(); |
| 1119 |
GRPC_CHANNEL_STACK_UNREF(channel_stack, "Server::ChannelData::Destroy"); |
| 1120 |
} |
| 1121 |
|
| 1122 |
void Server::ChannelData::Destroy() { |
| 1123 |
if (!list_position_.has_value()) return; |
| 1124 |
GPR_ASSERT(server_ != nullptr); |
| 1125 |
server_->channels_.erase(*list_position_); |
| 1126 |
list_position_.reset(); |
| 1127 |
server_->Ref().release(); |
| 1128 |
server_->MaybeFinishShutdown(); |
| 1129 |
// Unreffed by FinishDestroy |
| 1130 |
GRPC_CHANNEL_STACK_REF(channel_->channel_stack(), |
| 1131 |
"Server::ChannelData::Destroy"); |
| 1132 |
GRPC_CLOSURE_INIT(&finish_destroy_channel_closure_, FinishDestroy, this, |
| 1133 |
grpc_schedule_on_exec_ctx); |
| 1134 |
if (GRPC_TRACE_FLAG_ENABLED(grpc_server_channel_trace)) { |
| 1135 |
gpr_log(GPR_INFO, "Disconnected client"); |
| 1136 |
} |
| 1137 |
grpc_transport_op* op = |
| 1138 |
grpc_make_transport_op(&finish_destroy_channel_closure_); |
| 1139 |
op->set_accept_stream = true; |
| 1140 |
grpc_channel_next_op(grpc_channel_stack_element(channel_->channel_stack(), 0), |
| 1141 |
op); |
| 1142 |
} |
| 1143 |
|
| 1144 |
grpc_error_handle Server::ChannelData::InitChannelElement( |
| 1145 |
grpc_channel_element* elem, grpc_channel_element_args* args) { |
| 1146 |
GPR_ASSERT(args->is_first); |
| 1147 |
GPR_ASSERT(!args->is_last); |
| 1148 |
new (elem->channel_data) ChannelData(); |
| 1149 |
return absl::OkStatus(); |
| 1150 |
} |
| 1151 |
|
| 1152 |
void Server::ChannelData::DestroyChannelElement(grpc_channel_element* elem) { |
| 1153 |
auto* chand = static_cast<ChannelData*>(elem->channel_data); |
| 1154 |
chand->~ChannelData(); |
| 1155 |
} |
| 1156 |
|
| 1157 |
// |
| 1158 |
// Server::CallData |
| 1159 |
// |
| 1160 |
|
| 1161 |
Server::CallData::CallData(grpc_call_element* elem, |
| 1162 |
const grpc_call_element_args& args, |
| 1163 |
RefCountedPtr<Server> server) |
| 1164 |
: server_(std::move(server)), |
| 1165 |
call_(grpc_call_from_top_element(elem)), |
| 1166 |
call_combiner_(args.call_combiner) { |
| 1167 |
GRPC_CLOSURE_INIT(&recv_initial_metadata_ready_, RecvInitialMetadataReady, |
| 1168 |
elem, grpc_schedule_on_exec_ctx); |
| 1169 |
GRPC_CLOSURE_INIT(&recv_trailing_metadata_ready_, RecvTrailingMetadataReady, |
| 1170 |
elem, grpc_schedule_on_exec_ctx); |
| 1171 |
} |
| 1172 |
|
| 1173 |
Server::CallData::~CallData() { |
| 1174 |
GPR_ASSERT(state_.load(std::memory_order_relaxed) != CallState::PENDING); |
| 1175 |
grpc_metadata_array_destroy(&initial_metadata_); |
| 1176 |
grpc_byte_buffer_destroy(payload_); |
| 1177 |
} |
| 1178 |
|
| 1179 |
void Server::CallData::SetState(CallState state) { |
| 1180 |
state_.store(state, std::memory_order_relaxed); |
| 1181 |
} |
| 1182 |
|
| 1183 |
bool Server::CallData::MaybeActivate() { |
| 1184 |
CallState expected = CallState::PENDING; |
| 1185 |
return state_.compare_exchange_strong(expected, CallState::ACTIVATED, |
| 1186 |
std::memory_order_acq_rel, |
| 1187 |
std::memory_order_relaxed); |
| 1188 |
} |
| 1189 |
|
| 1190 |
void Server::CallData::FailCallCreation() { |
| 1191 |
CallState expected_not_started = CallState::NOT_STARTED; |
| 1192 |
CallState expected_pending = CallState::PENDING; |
| 1193 |
if (state_.compare_exchange_strong(expected_not_started, CallState::ZOMBIED, |
| 1194 |
std::memory_order_acq_rel, |
| 1195 |
std::memory_order_acquire)) { |
| 1196 |
KillZombie(); |
| 1197 |
} else if (state_.compare_exchange_strong( |
| 1198 |
expected_pending, CallState::ZOMBIED, |
| 1199 |
std::memory_order_acq_rel, std::memory_order_relaxed)) { |
| 1200 |
// Zombied call will be destroyed when it's removed from the pending |
| 1201 |
// queue... later. |
| 1202 |
} |
| 1203 |
} |
| 1204 |
|
| 1205 |
void Server::CallData::Start(grpc_call_element* elem) { |
| 1206 |
grpc_op op; |
| 1207 |
op.op = GRPC_OP_RECV_INITIAL_METADATA; |
| 1208 |
op.flags = 0; |
| 1209 |
op.reserved = nullptr; |
| 1210 |
op.data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_; |
| 1211 |
GRPC_CLOSURE_INIT(&recv_initial_metadata_batch_complete_, |
| 1212 |
RecvInitialMetadataBatchComplete, elem, |
| 1213 |
grpc_schedule_on_exec_ctx); |
| 1214 |
grpc_call_start_batch_and_execute(call_, &op, 1, |
| 1215 |
&recv_initial_metadata_batch_complete_); |
| 1216 |
} |
| 1217 |
|
| 1218 |
void Server::CallData::Publish(size_t cq_idx, RequestedCall* rc) { |
| 1219 |
grpc_call_set_completion_queue(call_, rc->cq_bound_to_call); |
| 1220 |
*rc->call = call_; |
| 1221 |
cq_new_ = server_->cqs_[cq_idx]; |
| 1222 |
std::swap(*rc->initial_metadata, initial_metadata_); |
| 1223 |
switch (rc->type) { |
| 1224 |
case RequestedCall::Type::BATCH_CALL: |
| 1225 |
GPR_ASSERT(host_.has_value()); |
| 1226 |
GPR_ASSERT(path_.has_value()); |
| 1227 |
rc->data.batch.details->host = CSliceRef(host_->c_slice()); |
| 1228 |
rc->data.batch.details->method = CSliceRef(path_->c_slice()); |
| 1229 |
rc->data.batch.details->deadline = |
| 1230 |
deadline_.as_timespec(GPR_CLOCK_MONOTONIC); |
| 1231 |
break; |
| 1232 |
case RequestedCall::Type::REGISTERED_CALL: |
| 1233 |
*rc->data.registered.deadline = |
| 1234 |
deadline_.as_timespec(GPR_CLOCK_MONOTONIC); |
| 1235 |
if (rc->data.registered.optional_payload != nullptr) { |
| 1236 |
*rc->data.registered.optional_payload = payload_; |
| 1237 |
payload_ = nullptr; |
| 1238 |
} |
| 1239 |
break; |
| 1240 |
default: |
| 1241 |
GPR_UNREACHABLE_CODE(return); |
| 1242 |
} |
| 1243 |
grpc_cq_end_op(cq_new_, rc->tag, absl::OkStatus(), Server::DoneRequestEvent, |
| 1244 |
rc, &rc->completion, true); |
| 1245 |
} |
| 1246 |
|
| 1247 |
void Server::CallData::PublishNewRpc(void* arg, grpc_error_handle error) { |
| 1248 |
grpc_call_element* call_elem = static_cast<grpc_call_element*>(arg); |
| 1249 |
auto* calld = static_cast<Server::CallData*>(call_elem->call_data); |
| 1250 |
auto* chand = static_cast<Server::ChannelData*>(call_elem->channel_data); |
| 1251 |
RequestMatcherInterface* rm = calld->matcher_; |
| 1252 |
Server* server = rm->server(); |
| 1253 |
if (!error.ok() || server->ShutdownCalled()) { |
| 1254 |
calld->state_.store(CallState::ZOMBIED, std::memory_order_relaxed); |
| 1255 |
calld->KillZombie(); |
| 1256 |
return; |
| 1257 |
} |
| 1258 |
rm->MatchOrQueue(chand->cq_idx(), calld); |
| 1259 |
} |
| 1260 |
|
| 1261 |
namespace { |
| 1262 |
|
| 1263 |
void KillZombieClosure(void* call, grpc_error_handle /*error*/) { |
| 1264 |
grpc_call_unref(static_cast<grpc_call*>(call)); |
| 1265 |
} |
| 1266 |
|
| 1267 |
} // namespace |
| 1268 |
|
| 1269 |
void Server::CallData::KillZombie() { |
| 1270 |
GRPC_CLOSURE_INIT(&kill_zombie_closure_, KillZombieClosure, call_, |
| 1271 |
grpc_schedule_on_exec_ctx); |
| 1272 |
ExecCtx::Run(DEBUG_LOCATION, &kill_zombie_closure_, absl::OkStatus()); |
| 1273 |
} |
| 1274 |
|
| 1275 |
void Server::CallData::StartNewRpc(grpc_call_element* elem) { |
| 1276 |
auto* chand = static_cast<ChannelData*>(elem->channel_data); |
| 1277 |
if (server_->ShutdownCalled()) { |
| 1278 |
state_.store(CallState::ZOMBIED, std::memory_order_relaxed); |
| 1279 |
KillZombie(); |
| 1280 |
return; |
| 1281 |
} |
| 1282 |
// Find request matcher. |
| 1283 |
matcher_ = server_->unregistered_request_matcher_.get(); |
| 1284 |
grpc_server_register_method_payload_handling payload_handling = |
| 1285 |
GRPC_SRM_PAYLOAD_NONE; |
| 1286 |
if (path_.has_value() && host_.has_value()) { |
| 1287 |
ChannelRegisteredMethod* rm = |
| 1288 |
chand->GetRegisteredMethod(host_->c_slice(), path_->c_slice()); |
| 1289 |
if (rm != nullptr) { |
| 1290 |
matcher_ = rm->server_registered_method->matcher.get(); |
| 1291 |
payload_handling = rm->server_registered_method->payload_handling; |
| 1292 |
} |
| 1293 |
} |
| 1294 |
// Start recv_message op if needed. |
| 1295 |
switch (payload_handling) { |
| 1296 |
case GRPC_SRM_PAYLOAD_NONE: |
| 1297 |
PublishNewRpc(elem, absl::OkStatus()); |
| 1298 |
break; |
| 1299 |
case GRPC_SRM_PAYLOAD_READ_INITIAL_BYTE_BUFFER: { |
| 1300 |
grpc_op op; |
| 1301 |
op.op = GRPC_OP_RECV_MESSAGE; |
| 1302 |
op.flags = 0; |
| 1303 |
op.reserved = nullptr; |
| 1304 |
op.data.recv_message.recv_message = &payload_; |
| 1305 |
GRPC_CLOSURE_INIT(&publish_, PublishNewRpc, elem, |
| 1306 |
grpc_schedule_on_exec_ctx); |
| 1307 |
grpc_call_start_batch_and_execute(call_, &op, 1, &publish_); |
| 1308 |
break; |
| 1309 |
} |
| 1310 |
} |
| 1311 |
} |
| 1312 |
|
| 1313 |
void Server::CallData::RecvInitialMetadataBatchComplete( |
| 1314 |
void* arg, grpc_error_handle error) { |
| 1315 |
grpc_call_element* elem = static_cast<grpc_call_element*>(arg); |
| 1316 |
auto* calld = static_cast<Server::CallData*>(elem->call_data); |
| 1317 |
if (!error.ok()) { |
| 1318 |
gpr_log(GPR_DEBUG, "Failed call creation: %s", |
| 1319 |
StatusToString(error).c_str()); |
| 1320 |
calld->FailCallCreation(); |
| 1321 |
return; |
| 1322 |
} |
| 1323 |
calld->StartNewRpc(elem); |
| 1324 |
} |
| 1325 |
|
| 1326 |
void Server::CallData::StartTransportStreamOpBatchImpl( |
| 1327 |
grpc_call_element* elem, grpc_transport_stream_op_batch* batch) { |
| 1328 |
if (batch->recv_initial_metadata) { |
| 1329 |
recv_initial_metadata_ = |
| 1330 |
batch->payload->recv_initial_metadata.recv_initial_metadata; |
| 1331 |
original_recv_initial_metadata_ready_ = |
| 1332 |
batch->payload->recv_initial_metadata.recv_initial_metadata_ready; |
| 1333 |
batch->payload->recv_initial_metadata.recv_initial_metadata_ready = |
| 1334 |
&recv_initial_metadata_ready_; |
| 1335 |
} |
| 1336 |
if (batch->recv_trailing_metadata) { |
| 1337 |
original_recv_trailing_metadata_ready_ = |
| 1338 |
batch->payload->recv_trailing_metadata.recv_trailing_metadata_ready; |
| 1339 |
batch->payload->recv_trailing_metadata.recv_trailing_metadata_ready = |
| 1340 |
&recv_trailing_metadata_ready_; |
| 1341 |
} |
| 1342 |
grpc_call_next_op(elem, batch); |
| 1343 |
} |
| 1344 |
|
| 1345 |
void Server::CallData::RecvInitialMetadataReady(void* arg, |
| 1346 |
grpc_error_handle error) { |
| 1347 |
grpc_call_element* elem = static_cast<grpc_call_element*>(arg); |
| 1348 |
CallData* calld = static_cast<CallData*>(elem->call_data); |
| 1349 |
if (error.ok()) { |
| 1350 |
calld->path_ = calld->recv_initial_metadata_->Take(HttpPathMetadata()); |
| 1351 |
auto* host = |
| 1352 |
calld->recv_initial_metadata_->get_pointer(HttpAuthorityMetadata()); |
| 1353 |
if (host != nullptr) calld->host_.emplace(host->Ref()); |
| 1354 |
} |
| 1355 |
auto op_deadline = calld->recv_initial_metadata_->get(GrpcTimeoutMetadata()); |
| 1356 |
if (op_deadline.has_value()) { |
| 1357 |
calld->deadline_ = *op_deadline; |
| 1358 |
} |
| 1359 |
if (calld->host_.has_value() && calld->path_.has_value()) { |
| 1360 |
/* do nothing */ |
| 1361 |
} else { |
| 1362 |
/* Pass the error reference to calld->recv_initial_metadata_error */ |
| 1363 |
grpc_error_handle src_error = error; |
| 1364 |
error = GRPC_ERROR_CREATE_REFERENCING("Missing :authority or :path", |
| 1365 |
&src_error, 1); |
| 1366 |
calld->recv_initial_metadata_error_ = error; |
| 1367 |
} |
| 1368 |
grpc_closure* closure = calld->original_recv_initial_metadata_ready_; |
| 1369 |
calld->original_recv_initial_metadata_ready_ = nullptr; |
| 1370 |
if (calld->seen_recv_trailing_metadata_ready_) { |
| 1371 |
GRPC_CALL_COMBINER_START(calld->call_combiner_, |
| 1372 |
&calld->recv_trailing_metadata_ready_, |
| 1373 |
calld->recv_trailing_metadata_error_, |
| 1374 |
"continue server recv_trailing_metadata_ready"); |
| 1375 |
} |
| 1376 |
Closure::Run(DEBUG_LOCATION, closure, error); |
| 1377 |
} |
| 1378 |
|
| 1379 |
void Server::CallData::RecvTrailingMetadataReady(void* arg, |
| 1380 |
grpc_error_handle error) { |
| 1381 |
grpc_call_element* elem = static_cast<grpc_call_element*>(arg); |
| 1382 |
CallData* calld = static_cast<CallData*>(elem->call_data); |
| 1383 |
if (calld->original_recv_initial_metadata_ready_ != nullptr) { |
| 1384 |
calld->recv_trailing_metadata_error_ = error; |
| 1385 |
calld->seen_recv_trailing_metadata_ready_ = true; |
| 1386 |
GRPC_CLOSURE_INIT(&calld->recv_trailing_metadata_ready_, |
| 1387 |
RecvTrailingMetadataReady, elem, |
| 1388 |
grpc_schedule_on_exec_ctx); |
| 1389 |
GRPC_CALL_COMBINER_STOP(calld->call_combiner_, |
| 1390 |
"deferring server recv_trailing_metadata_ready " |
| 1391 |
"until after recv_initial_metadata_ready"); |
| 1392 |
return; |
| 1393 |
} |
| 1394 |
error = grpc_error_add_child(error, calld->recv_initial_metadata_error_); |
| 1395 |
Closure::Run(DEBUG_LOCATION, calld->original_recv_trailing_metadata_ready_, |
| 1396 |
error); |
| 1397 |
} |
| 1398 |
|
| 1399 |
grpc_error_handle Server::CallData::InitCallElement( |
| 1400 |
grpc_call_element* elem, const grpc_call_element_args* args) { |
| 1401 |
auto* chand = static_cast<ChannelData*>(elem->channel_data); |
| 1402 |
new (elem->call_data) Server::CallData(elem, *args, chand->server()); |
| 1403 |
return absl::OkStatus(); |
| 1404 |
} |
| 1405 |
|
| 1406 |
void Server::CallData::DestroyCallElement( |
| 1407 |
grpc_call_element* elem, const grpc_call_final_info* /*final_info*/, |
| 1408 |
grpc_closure* /*ignored*/) { |
| 1409 |
auto* calld = static_cast<CallData*>(elem->call_data); |
| 1410 |
calld->~CallData(); |
| 1411 |
} |
| 1412 |
|
| 1413 |
void Server::CallData::StartTransportStreamOpBatch( |
| 1414 |
grpc_call_element* elem, grpc_transport_stream_op_batch* batch) { |
| 1415 |
auto* calld = static_cast<CallData*>(elem->call_data); |
| 1416 |
calld->StartTransportStreamOpBatchImpl(elem, batch); |
| 1417 |
} |
| 1418 |
|
| 1419 |
} // namespace grpc_core |
| 1420 |
|
| 1421 |
// |
| 1422 |
// C-core API |
| 1423 |
// |
| 1424 |
|
| 1425 |
grpc_server* grpc_server_create(const grpc_channel_args* args, void* reserved) { |
| 1426 |
grpc_core::ExecCtx exec_ctx; |
| 1427 |
GRPC_API_TRACE("grpc_server_create(%p, %p)", 2, (args, reserved)); |
| 1428 |
grpc_core::Server* server = |
| 1429 |
new grpc_core::Server(grpc_core::CoreConfiguration::Get() |
| 1430 |
.channel_args_preconditioning() |
| 1431 |
.PreconditionChannelArgs(args)); |
| 1432 |
return server->c_ptr(); |
| 1433 |
} |
| 1434 |
|
| 1435 |
void grpc_server_register_completion_queue(grpc_server* server, |
| 1436 |
grpc_completion_queue* cq, |
| 1437 |
void* reserved) { |
| 1438 |
GRPC_API_TRACE( |
| 1439 |
"grpc_server_register_completion_queue(server=%p, cq=%p, reserved=%p)", 3, |
| 1440 |
(server, cq, reserved)); |
| 1441 |
GPR_ASSERT(!reserved); |
| 1442 |
auto cq_type = grpc_get_cq_completion_type(cq); |
| 1443 |
if (cq_type != GRPC_CQ_NEXT && cq_type != GRPC_CQ_CALLBACK) { |
| 1444 |
gpr_log(GPR_INFO, |
| 1445 |
"Completion queue of type %d is being registered as a " |
| 1446 |
"server-completion-queue", |
| 1447 |
static_cast<int>(cq_type)); |
| 1448 |
/* Ideally we should log an error and abort but ruby-wrapped-language API |
| 1449 |
calls grpc_completion_queue_pluck() on server completion queues */ |
| 1450 |
} |
| 1451 |
grpc_core::Server::FromC(server)->RegisterCompletionQueue(cq); |
| 1452 |
} |
| 1453 |
|
| 1454 |
void* grpc_server_register_method( |
| 1455 |
grpc_server* server, const char* method, const char* host, |
| 1456 |
grpc_server_register_method_payload_handling payload_handling, |
| 1457 |
uint32_t flags) { |
| 1458 |
GRPC_API_TRACE( |
| 1459 |
"grpc_server_register_method(server=%p, method=%s, host=%s, " |
| 1460 |
"flags=0x%08x)", |
| 1461 |
4, (server, method, host, flags)); |
| 1462 |
return grpc_core::Server::FromC(server)->RegisterMethod( |
| 1463 |
method, host, payload_handling, flags); |
| 1464 |
} |
| 1465 |
|
| 1466 |
void grpc_server_start(grpc_server* server) { |
| 1467 |
grpc_core::ExecCtx exec_ctx; |
| 1468 |
GRPC_API_TRACE("grpc_server_start(server=%p)", 1, (server)); |
| 1469 |
grpc_core::Server::FromC(server)->Start(); |
| 1470 |
} |
| 1471 |
|
| 1472 |
void grpc_server_shutdown_and_notify(grpc_server* server, |
| 1473 |
grpc_completion_queue* cq, void* tag) { |
| 1474 |
grpc_core::ApplicationCallbackExecCtx callback_exec_ctx; |
| 1475 |
grpc_core::ExecCtx exec_ctx; |
| 1476 |
GRPC_API_TRACE("grpc_server_shutdown_and_notify(server=%p, cq=%p, tag=%p)", 3, |
| 1477 |
(server, cq, tag)); |
| 1478 |
grpc_core::Server::FromC(server)->ShutdownAndNotify(cq, tag); |
| 1479 |
} |
| 1480 |
|
| 1481 |
void grpc_server_cancel_all_calls(grpc_server* server) { |
| 1482 |
grpc_core::ApplicationCallbackExecCtx callback_exec_ctx; |
| 1483 |
grpc_core::ExecCtx exec_ctx; |
| 1484 |
GRPC_API_TRACE("grpc_server_cancel_all_calls(server=%p)", 1, (server)); |
| 1485 |
grpc_core::Server::FromC(server)->CancelAllCalls(); |
| 1486 |
} |
| 1487 |
|
| 1488 |
void grpc_server_destroy(grpc_server* server) { |
| 1489 |
grpc_core::ApplicationCallbackExecCtx callback_exec_ctx; |
| 1490 |
grpc_core::ExecCtx exec_ctx; |
| 1491 |
GRPC_API_TRACE("grpc_server_destroy(server=%p)", 1, (server)); |
| 1492 |
grpc_core::Server::FromC(server)->Orphan(); |
| 1493 |
} |
| 1494 |
|
| 1495 |
grpc_call_error grpc_server_request_call( |
| 1496 |
grpc_server* server, grpc_call** call, grpc_call_details* details, |
| 1497 |
grpc_metadata_array* request_metadata, |
| 1498 |
grpc_completion_queue* cq_bound_to_call, |
| 1499 |
grpc_completion_queue* cq_for_notification, void* tag) { |
| 1500 |
grpc_core::ApplicationCallbackExecCtx callback_exec_ctx; |
| 1501 |
grpc_core::ExecCtx exec_ctx; |
| 1502 |
GRPC_API_TRACE( |
| 1503 |
"grpc_server_request_call(" |
| 1504 |
"server=%p, call=%p, details=%p, initial_metadata=%p, " |
| 1505 |
"cq_bound_to_call=%p, cq_for_notification=%p, tag=%p)", |
| 1506 |
7, |
| 1507 |
(server, call, details, request_metadata, cq_bound_to_call, |
| 1508 |
cq_for_notification, tag)); |
| 1509 |
return grpc_core::Server::FromC(server)->RequestCall( |
| 1510 |
call, details, request_metadata, cq_bound_to_call, cq_for_notification, |
| 1511 |
tag); |
| 1512 |
} |
| 1513 |
|
| 1514 |
grpc_call_error grpc_server_request_registered_call( |
| 1515 |
grpc_server* server, void* registered_method, grpc_call** call, |
| 1516 |
gpr_timespec* deadline, grpc_metadata_array* request_metadata, |
| 1517 |
grpc_byte_buffer** optional_payload, |
| 1518 |
grpc_completion_queue* cq_bound_to_call, |
| 1519 |
grpc_completion_queue* cq_for_notification, void* tag_new) { |
| 1520 |
grpc_core::ApplicationCallbackExecCtx callback_exec_ctx; |
| 1521 |
grpc_core::ExecCtx exec_ctx; |
| 1522 |
auto* rm = |
| 1523 |
static_cast<grpc_core::Server::RegisteredMethod*>(registered_method); |
| 1524 |
GRPC_API_TRACE( |
| 1525 |
"grpc_server_request_registered_call(" |
| 1526 |
"server=%p, registered_method=%p, call=%p, deadline=%p, " |
| 1527 |
"request_metadata=%p, " |
| 1528 |
"optional_payload=%p, cq_bound_to_call=%p, cq_for_notification=%p, " |
| 1529 |
"tag=%p)", |
| 1530 |
9, |
| 1531 |
(server, registered_method, call, deadline, request_metadata, |
| 1532 |
optional_payload, cq_bound_to_call, cq_for_notification, tag_new)); |
| 1533 |
return grpc_core::Server::FromC(server)->RequestRegisteredCall( |
| 1534 |
rm, call, deadline, request_metadata, optional_payload, cq_bound_to_call, |
| 1535 |
cq_for_notification, tag_new); |
| 1536 |
} |
| 1537 |
|
| 1538 |
void grpc_server_set_config_fetcher( |
| 1539 |
grpc_server* server, grpc_server_config_fetcher* server_config_fetcher) { |
| 1540 |
grpc_core::ApplicationCallbackExecCtx callback_exec_ctx; |
| 1541 |
grpc_core::ExecCtx exec_ctx; |
| 1542 |
GRPC_API_TRACE("grpc_server_set_config_fetcher(server=%p, config_fetcher=%p)", |
| 1543 |
2, (server, server_config_fetcher)); |
| 1544 |
grpc_core::Server::FromC(server)->set_config_fetcher( |
| 1545 |
std::unique_ptr<grpc_server_config_fetcher>(server_config_fetcher)); |
| 1546 |
} |
| 1547 |
|
| 1548 |
void grpc_server_config_fetcher_destroy( |
| 1549 |
grpc_server_config_fetcher* server_config_fetcher) { |
| 1550 |
grpc_core::ApplicationCallbackExecCtx callback_exec_ctx; |
| 1551 |
grpc_core::ExecCtx exec_ctx; |
| 1552 |
GRPC_API_TRACE("grpc_server_config_fetcher_destroy(config_fetcher=%p)", 1, |
| 1553 |
(server_config_fetcher)); |
| 1554 |
delete server_config_fetcher; |
| 1555 |
} |