| 1 |
/* |
| 2 |
* |
| 3 |
* Copyright 2015 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 <assert.h> |
| 20 |
|
| 21 |
#include <atomic> |
| 22 |
#include <cstdlib> |
| 23 |
#include <functional> |
| 24 |
#include <map> |
| 25 |
#include <new> |
| 26 |
#include <string> |
| 27 |
#include <utility> |
| 28 |
#include <vector> |
| 29 |
|
| 30 |
#include "absl/strings/string_view.h" |
| 31 |
|
| 32 |
#include <grpc/compression.h> |
| 33 |
#include <grpc/grpc.h> |
| 34 |
#include <grpc/impl/codegen/compression_types.h> |
| 35 |
#include <grpc/impl/codegen/gpr_types.h> |
| 36 |
#include <grpc/impl/codegen/grpc_types.h> |
| 37 |
#include <grpc/load_reporting.h> |
| 38 |
#include <grpc/status.h> |
| 39 |
#include <grpc/support/alloc.h> |
| 40 |
#include <grpc/support/log.h> |
| 41 |
#include <grpc/support/time.h> |
| 42 |
#include <grpcpp/completion_queue.h> |
| 43 |
#include <grpcpp/ext/call_metric_recorder.h> |
| 44 |
#include <grpcpp/impl/call.h> |
| 45 |
#include <grpcpp/impl/call_op_set.h> |
| 46 |
#include <grpcpp/impl/call_op_set_interface.h> |
| 47 |
#include <grpcpp/impl/codegen/completion_queue_tag.h> |
| 48 |
#include <grpcpp/impl/codegen/interceptor_common.h> |
| 49 |
#include <grpcpp/impl/codegen/metadata_map.h> |
| 50 |
#include <grpcpp/impl/grpc_library.h> |
| 51 |
#include <grpcpp/server_context.h> |
| 52 |
#include <grpcpp/support/callback_common.h> |
| 53 |
#include <grpcpp/support/interceptor.h> |
| 54 |
#include <grpcpp/support/server_callback.h> |
| 55 |
#include <grpcpp/support/server_interceptor.h> |
| 56 |
#include <grpcpp/support/string_ref.h> |
| 57 |
|
| 58 |
#include "src/core/lib/gprpp/ref_counted.h" |
| 59 |
#include "src/core/lib/gprpp/sync.h" |
| 60 |
#include "src/core/lib/resource_quota/arena.h" |
| 61 |
#include "src/core/lib/surface/call.h" |
| 62 |
|
| 63 |
namespace grpc { |
| 64 |
|
| 65 |
static internal::GrpcLibraryInitializer g_gli_initializer; |
| 66 |
|
| 67 |
// CompletionOp |
| 68 |
|
| 69 |
class ServerContextBase::CompletionOp final |
| 70 |
: public internal::CallOpSetInterface { |
| 71 |
public: |
| 72 |
// initial refs: one in the server context, one in the cq |
| 73 |
// must ref the call before calling constructor and after deleting this |
| 74 |
CompletionOp(internal::Call* call, |
| 75 |
grpc::internal::ServerCallbackCall* callback_controller) |
| 76 |
: call_(*call), |
| 77 |
callback_controller_(callback_controller), |
| 78 |
has_tag_(false), |
| 79 |
tag_(nullptr), |
| 80 |
core_cq_tag_(this), |
| 81 |
refs_(2), |
| 82 |
finalized_(false), |
| 83 |
cancelled_(0), |
| 84 |
done_intercepting_(false) {} |
| 85 |
|
| 86 |
// CompletionOp isn't copyable or movable |
| 87 |
CompletionOp(const CompletionOp&) = delete; |
| 88 |
CompletionOp& operator=(const CompletionOp&) = delete; |
| 89 |
CompletionOp(CompletionOp&&) = delete; |
| 90 |
CompletionOp& operator=(CompletionOp&&) = delete; |
| 91 |
|
| 92 |
~CompletionOp() override { |
| 93 |
if (call_.server_rpc_info()) { |
| 94 |
call_.server_rpc_info()->Unref(); |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
void FillOps(internal::Call* call) override; |
| 99 |
|
| 100 |
// This should always be arena allocated in the call, so override delete. |
| 101 |
// But this class is not trivially destructible, so must actually call delete |
| 102 |
// before allowing the arena to be freed |
| 103 |
static void operator delete(void* /*ptr*/, std::size_t size) { |
| 104 |
// Use size to avoid unused-parameter warning since assert seems to be |
| 105 |
// compiled out and treated as unused in some gcc optimized versions. |
| 106 |
(void)size; |
| 107 |
assert(size == sizeof(CompletionOp)); |
| 108 |
} |
| 109 |
|
| 110 |
// This operator should never be called as the memory should be freed as part |
| 111 |
// of the arena destruction. It only exists to provide a matching operator |
| 112 |
// delete to the operator new so that some compilers will not complain (see |
| 113 |
// https://github.com/grpc/grpc/issues/11301) Note at the time of adding this |
| 114 |
// there are no tests catching the compiler warning. |
| 115 |
static void operator delete(void*, void*) { assert(0); } |
| 116 |
|
| 117 |
bool FinalizeResult(void** tag, bool* status) override; |
| 118 |
|
| 119 |
bool CheckCancelled(CompletionQueue* cq) { |
| 120 |
cq->TryPluck(this); |
| 121 |
return CheckCancelledNoPluck(); |
| 122 |
} |
| 123 |
bool CheckCancelledAsync() { return CheckCancelledNoPluck(); } |
| 124 |
|
| 125 |
void set_tag(void* tag) { |
| 126 |
has_tag_ = true; |
| 127 |
tag_ = tag; |
| 128 |
} |
| 129 |
|
| 130 |
void set_core_cq_tag(void* core_cq_tag) { core_cq_tag_ = core_cq_tag; } |
| 131 |
|
| 132 |
void* core_cq_tag() override { return core_cq_tag_; } |
| 133 |
|
| 134 |
void Unref(); |
| 135 |
|
| 136 |
// This will be called while interceptors are run if the RPC is a hijacked |
| 137 |
// RPC. This should set hijacking state for each of the ops. |
| 138 |
void SetHijackingState() override { |
| 139 |
/* Servers don't allow hijacking */ |
| 140 |
GPR_ASSERT(false); |
| 141 |
} |
| 142 |
|
| 143 |
/* Should be called after interceptors are done running */ |
| 144 |
void ContinueFillOpsAfterInterception() override {} |
| 145 |
|
| 146 |
/* Should be called after interceptors are done running on the finalize result |
| 147 |
* path */ |
| 148 |
void ContinueFinalizeResultAfterInterception() override { |
| 149 |
done_intercepting_ = true; |
| 150 |
if (!has_tag_) { |
| 151 |
// We don't have a tag to return. |
| 152 |
Unref(); |
| 153 |
// Unref can delete this, so do not access anything from this afterward. |
| 154 |
return; |
| 155 |
} |
| 156 |
/* Start a phony op so that we can return the tag */ |
| 157 |
GPR_ASSERT(grpc_call_start_batch(call_.call(), nullptr, 0, core_cq_tag_, |
| 158 |
nullptr) == GRPC_CALL_OK); |
| 159 |
} |
| 160 |
|
| 161 |
private: |
| 162 |
bool CheckCancelledNoPluck() { |
| 163 |
grpc_core::MutexLock lock(&mu_); |
| 164 |
return finalized_ ? (cancelled_ != 0) : false; |
| 165 |
} |
| 166 |
|
| 167 |
internal::Call call_; |
| 168 |
grpc::internal::ServerCallbackCall* const callback_controller_; |
| 169 |
bool has_tag_; |
| 170 |
void* tag_; |
| 171 |
void* core_cq_tag_; |
| 172 |
grpc_core::RefCount refs_; |
| 173 |
grpc_core::Mutex mu_; |
| 174 |
bool finalized_; |
| 175 |
int cancelled_; // This is an int (not bool) because it is passed to core |
| 176 |
bool done_intercepting_; |
| 177 |
internal::InterceptorBatchMethodsImpl interceptor_methods_; |
| 178 |
}; |
| 179 |
|
| 180 |
void ServerContextBase::CompletionOp::Unref() { |
| 181 |
if (refs_.Unref()) { |
| 182 |
grpc_call* call = call_.call(); |
| 183 |
delete this; |
| 184 |
grpc_call_unref(call); |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
void ServerContextBase::CompletionOp::FillOps(internal::Call* call) { |
| 189 |
grpc_op ops; |
| 190 |
ops.op = GRPC_OP_RECV_CLOSE_ON_SERVER; |
| 191 |
ops.data.recv_close_on_server.cancelled = &cancelled_; |
| 192 |
ops.flags = 0; |
| 193 |
ops.reserved = nullptr; |
| 194 |
interceptor_methods_.SetCall(&call_); |
| 195 |
interceptor_methods_.SetReverse(); |
| 196 |
interceptor_methods_.SetCallOpSetInterface(this); |
| 197 |
// The following call_start_batch is internally-generated so no need for an |
| 198 |
// explanatory log on failure. |
| 199 |
GPR_ASSERT(grpc_call_start_batch(call->call(), &ops, 1, core_cq_tag_, |
| 200 |
nullptr) == GRPC_CALL_OK); |
| 201 |
/* No interceptors to run here */ |
| 202 |
} |
| 203 |
|
| 204 |
bool ServerContextBase::CompletionOp::FinalizeResult(void** tag, bool* status) { |
| 205 |
// Decide whether to do the unref or call the cancel callback within the lock |
| 206 |
bool do_unref = false; |
| 207 |
bool has_tag = false; |
| 208 |
bool call_cancel = false; |
| 209 |
|
| 210 |
{ |
| 211 |
grpc_core::MutexLock lock(&mu_); |
| 212 |
if (done_intercepting_) { |
| 213 |
// We are done intercepting. |
| 214 |
has_tag = has_tag_; |
| 215 |
if (has_tag) { |
| 216 |
*tag = tag_; |
| 217 |
} |
| 218 |
// Release the lock before unreffing as Unref may delete this object |
| 219 |
do_unref = true; |
| 220 |
} else { |
| 221 |
finalized_ = true; |
| 222 |
|
| 223 |
// If for some reason the incoming status is false, mark that as a |
| 224 |
// cancellation. |
| 225 |
// TODO(vjpai): does this ever happen? |
| 226 |
if (!*status) { |
| 227 |
cancelled_ = 1; |
| 228 |
} |
| 229 |
|
| 230 |
call_cancel = (cancelled_ != 0); |
| 231 |
// Release the lock since we may call a callback and interceptors. |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
if (do_unref) { |
| 236 |
Unref(); |
| 237 |
// Unref can delete this, so do not access anything from this afterward. |
| 238 |
return has_tag; |
| 239 |
} |
| 240 |
if (call_cancel && callback_controller_ != nullptr) { |
| 241 |
callback_controller_->MaybeCallOnCancel(); |
| 242 |
} |
| 243 |
/* Add interception point and run through interceptors */ |
| 244 |
interceptor_methods_.AddInterceptionHookPoint( |
| 245 |
experimental::InterceptionHookPoints::POST_RECV_CLOSE); |
| 246 |
if (interceptor_methods_.RunInterceptors()) { |
| 247 |
// No interceptors were run |
| 248 |
bool has_tag = has_tag_; |
| 249 |
if (has_tag) { |
| 250 |
*tag = tag_; |
| 251 |
} |
| 252 |
Unref(); |
| 253 |
// Unref can delete this, so do not access anything from this afterward. |
| 254 |
return has_tag; |
| 255 |
} |
| 256 |
// There are interceptors to be run. Return false for now. |
| 257 |
return false; |
| 258 |
} |
| 259 |
|
| 260 |
// ServerContextBase body |
| 261 |
|
| 262 |
ServerContextBase::ServerContextBase() |
| 263 |
: deadline_(gpr_inf_future(GPR_CLOCK_REALTIME)) { |
| 264 |
g_gli_initializer.summon(); |
| 265 |
} |
| 266 |
|
| 267 |
ServerContextBase::ServerContextBase(gpr_timespec deadline, |
| 268 |
grpc_metadata_array* arr) |
| 269 |
: deadline_(deadline) { |
| 270 |
std::swap(*client_metadata_.arr(), *arr); |
| 271 |
} |
| 272 |
|
| 273 |
void ServerContextBase::BindDeadlineAndMetadata(gpr_timespec deadline, |
| 274 |
grpc_metadata_array* arr) { |
| 275 |
deadline_ = deadline; |
| 276 |
std::swap(*client_metadata_.arr(), *arr); |
| 277 |
} |
| 278 |
|
| 279 |
ServerContextBase::~ServerContextBase() { |
| 280 |
if (completion_op_) { |
| 281 |
completion_op_->Unref(); |
| 282 |
// Unref can delete completion_op_, so do not access it afterward. |
| 283 |
} |
| 284 |
if (rpc_info_) { |
| 285 |
rpc_info_->Unref(); |
| 286 |
} |
| 287 |
if (default_reactor_used_.load(std::memory_order_relaxed)) { |
| 288 |
reinterpret_cast<Reactor*>(&default_reactor_)->~Reactor(); |
| 289 |
} |
| 290 |
if (call_metric_recorder_ != nullptr) { |
| 291 |
call_metric_recorder_->~CallMetricRecorder(); |
| 292 |
} |
| 293 |
} |
| 294 |
|
| 295 |
ServerContextBase::CallWrapper::~CallWrapper() { |
| 296 |
if (call) { |
| 297 |
// If the ServerContext is part of the call's arena, this could free the |
| 298 |
// object itself. |
| 299 |
grpc_call_unref(call); |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
void ServerContextBase::BeginCompletionOp( |
| 304 |
internal::Call* call, std::function<void(bool)> callback, |
| 305 |
grpc::internal::ServerCallbackCall* callback_controller) { |
| 306 |
GPR_ASSERT(!completion_op_); |
| 307 |
if (rpc_info_) { |
| 308 |
rpc_info_->Ref(); |
| 309 |
} |
| 310 |
grpc_call_ref(call->call()); |
| 311 |
completion_op_ = |
| 312 |
new (grpc_call_arena_alloc(call->call(), sizeof(CompletionOp))) |
| 313 |
CompletionOp(call, callback_controller); |
| 314 |
if (callback_controller != nullptr) { |
| 315 |
completion_tag_.Set(call->call(), std::move(callback), completion_op_, |
| 316 |
true); |
| 317 |
completion_op_->set_core_cq_tag(&completion_tag_); |
| 318 |
completion_op_->set_tag(completion_op_); |
| 319 |
} else if (has_notify_when_done_tag_) { |
| 320 |
completion_op_->set_tag(async_notify_when_done_tag_); |
| 321 |
} |
| 322 |
call->PerformOps(completion_op_); |
| 323 |
} |
| 324 |
|
| 325 |
internal::CompletionQueueTag* ServerContextBase::GetCompletionOpTag() { |
| 326 |
return static_cast<internal::CompletionQueueTag*>(completion_op_); |
| 327 |
} |
| 328 |
|
| 329 |
void ServerContextBase::AddInitialMetadata(const std::string& key, |
| 330 |
const std::string& value) { |
| 331 |
initial_metadata_.insert(std::make_pair(key, value)); |
| 332 |
} |
| 333 |
|
| 334 |
void ServerContextBase::AddTrailingMetadata(const std::string& key, |
| 335 |
const std::string& value) { |
| 336 |
trailing_metadata_.insert(std::make_pair(key, value)); |
| 337 |
} |
| 338 |
|
| 339 |
void ServerContextBase::TryCancel() const { |
| 340 |
internal::CancelInterceptorBatchMethods cancel_methods; |
| 341 |
if (rpc_info_) { |
| 342 |
for (size_t i = 0; i < rpc_info_->interceptors_.size(); i++) { |
| 343 |
rpc_info_->RunInterceptor(&cancel_methods, i); |
| 344 |
} |
| 345 |
} |
| 346 |
grpc_call_error err = |
| 347 |
grpc_call_cancel_with_status(call_.call, GRPC_STATUS_CANCELLED, |
| 348 |
"Cancelled on the server side", nullptr); |
| 349 |
if (err != GRPC_CALL_OK) { |
| 350 |
gpr_log(GPR_ERROR, "TryCancel failed with: %d", err); |
| 351 |
} |
| 352 |
} |
| 353 |
|
| 354 |
bool ServerContextBase::IsCancelled() const { |
| 355 |
if (completion_tag_) { |
| 356 |
// When using callback API, this result is always valid. |
| 357 |
return marked_cancelled_.load(std::memory_order_acquire) || |
| 358 |
completion_op_->CheckCancelledAsync(); |
| 359 |
} else if (has_notify_when_done_tag_) { |
| 360 |
// When using async API, the result is only valid |
| 361 |
// if the tag has already been delivered at the completion queue |
| 362 |
return completion_op_ && completion_op_->CheckCancelledAsync(); |
| 363 |
} else { |
| 364 |
// when using sync API, the result is always valid |
| 365 |
return marked_cancelled_.load(std::memory_order_acquire) || |
| 366 |
(completion_op_ && completion_op_->CheckCancelled(cq_)); |
| 367 |
} |
| 368 |
} |
| 369 |
|
| 370 |
void ServerContextBase::set_compression_algorithm( |
| 371 |
grpc_compression_algorithm algorithm) { |
| 372 |
compression_algorithm_ = algorithm; |
| 373 |
const char* algorithm_name = nullptr; |
| 374 |
if (!grpc_compression_algorithm_name(algorithm, &algorithm_name)) { |
| 375 |
gpr_log(GPR_ERROR, "Name for compression algorithm '%d' unknown.", |
| 376 |
algorithm); |
| 377 |
abort(); |
| 378 |
} |
| 379 |
GPR_ASSERT(algorithm_name != nullptr); |
| 380 |
AddInitialMetadata(GRPC_COMPRESSION_REQUEST_ALGORITHM_MD_KEY, algorithm_name); |
| 381 |
} |
| 382 |
|
| 383 |
std::string ServerContextBase::peer() const { |
| 384 |
std::string peer; |
| 385 |
if (call_.call) { |
| 386 |
char* c_peer = grpc_call_get_peer(call_.call); |
| 387 |
peer = c_peer; |
| 388 |
gpr_free(c_peer); |
| 389 |
} |
| 390 |
return peer; |
| 391 |
} |
| 392 |
|
| 393 |
const struct census_context* ServerContextBase::census_context() const { |
| 394 |
return call_.call == nullptr ? nullptr |
| 395 |
: grpc_census_call_get_context(call_.call); |
| 396 |
} |
| 397 |
|
| 398 |
void ServerContextBase::SetLoadReportingCosts( |
| 399 |
const std::vector<std::string>& cost_data) { |
| 400 |
if (call_.call == nullptr) return; |
| 401 |
for (const auto& cost_datum : cost_data) { |
| 402 |
AddTrailingMetadata(GRPC_LB_COST_MD_KEY, cost_datum); |
| 403 |
} |
| 404 |
} |
| 405 |
|
| 406 |
void ServerContextBase::CreateCallMetricRecorder() { |
| 407 |
GPR_ASSERT(call_metric_recorder_ == nullptr); |
| 408 |
grpc_core::Arena* arena = grpc_call_get_arena(call_.call); |
| 409 |
call_metric_recorder_ = arena->New<experimental::CallMetricRecorder>(arena); |
| 410 |
} |
| 411 |
|
| 412 |
grpc::string_ref ServerContextBase::ExperimentalGetAuthority() const { |
| 413 |
absl::string_view authority = grpc_call_server_authority(call_.call); |
| 414 |
return grpc::string_ref(authority.data(), authority.size()); |
| 415 |
} |
| 416 |
|
| 417 |
} // namespace grpc |