blob: b7254b6bb9c4f11662fd5cb126aa1eda1193dc89 [file] [log] [blame]
yangga4b6f5d2014-12-17 15:53:12 -08001/*
2 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003 * Copyright 2015 gRPC authors.
yangga4b6f5d2014-12-17 15:53:12 -08004 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02005 * 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
yangga4b6f5d2014-12-17 15:53:12 -08008 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009 * http://www.apache.org/licenses/LICENSE-2.0
yangga4b6f5d2014-12-17 15:53:12 -080010 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +020011 * 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.
yangga4b6f5d2014-12-17 15:53:12 -080016 *
17 */
18
Muxi Yan0e00c432018-01-26 15:39:32 -080019#include <grpcpp/server_context.h>
Craig Tillerc7625b02015-02-18 15:18:58 -080020
yang-gc42d8442017-02-15 00:05:00 -080021#include <algorithm>
Vijay Pai320ed132016-11-01 17:16:55 -070022#include <mutex>
yang-gc42d8442017-02-15 00:05:00 -080023#include <utility>
Vijay Pai320ed132016-11-01 17:16:55 -070024
Sree Kuchibhotla7fa9d6f2016-01-28 17:32:48 -080025#include <grpc/compression.h>
26#include <grpc/grpc.h>
David Garcia Quintas55c895a2017-03-30 10:43:14 -070027#include <grpc/load_reporting.h>
Sree Kuchibhotla7fa9d6f2016-01-28 17:32:48 -080028#include <grpc/support/alloc.h>
29#include <grpc/support/log.h>
Muxi Yan0e00c432018-01-26 15:39:32 -080030#include <grpcpp/completion_queue.h>
31#include <grpcpp/impl/call.h>
32#include <grpcpp/support/time.h>
yangga4b6f5d2014-12-17 15:53:12 -080033
Craig Tiller9533d042016-03-25 17:11:06 -070034#include "src/core/lib/surface/call.h"
yang-g3abe60b2015-07-06 14:00:36 -070035
Craig Tiller854a30c2015-02-11 11:44:10 -080036namespace grpc {
37
Craig Tiller492968f2015-02-18 13:14:03 -080038// CompletionOp
39
Vijay Pai06e174a2017-10-20 05:51:12 -070040class ServerContext::CompletionOp final : public internal::CallOpSetInterface {
Craig Tiller492968f2015-02-18 13:14:03 -080041 public:
Craig Tillercf133f42015-02-26 14:05:56 -080042 // initial refs: one in the server context, one in the cq
Craig Tillerd6c98df2015-08-18 09:33:44 -070043 CompletionOp()
44 : has_tag_(false),
45 tag_(nullptr),
46 refs_(2),
47 finalized_(false),
48 cancelled_(0) {}
Craig Tiller50a7a682015-06-04 12:53:40 -070049
Craig Tiller66051c62017-03-31 09:16:35 -070050 void FillOps(grpc_call* call, grpc_op* ops, size_t* nops) override;
Vijay Paic0b2acb2016-11-01 16:31:56 -070051 bool FinalizeResult(void** tag, bool* status) override;
Craig Tiller492968f2015-02-18 13:14:03 -080052
Sree Kuchibhotlab0d15672016-03-07 10:51:02 -080053 bool CheckCancelled(CompletionQueue* cq) {
54 cq->TryPluck(this);
55 return CheckCancelledNoPluck();
56 }
57 bool CheckCancelledAsync() { return CheckCancelledNoPluck(); }
Craig Tiller492968f2015-02-18 13:14:03 -080058
yang-gd45a26e2015-08-04 16:36:22 -070059 void set_tag(void* tag) {
60 has_tag_ = true;
61 tag_ = tag;
62 }
yang-gb3352562015-08-04 14:42:06 -070063
Vijay Pai8bf52532018-08-21 14:32:13 -070064 /// TODO(vjpai): Allow override of cq_tag if appropriate for callback API
65 void* cq_tag() override { return this; }
66
Craig Tiller492968f2015-02-18 13:14:03 -080067 void Unref();
68
69 private:
Sree Kuchibhotlab0d15672016-03-07 10:51:02 -080070 bool CheckCancelledNoPluck() {
Vijay Pai320ed132016-11-01 17:16:55 -070071 std::lock_guard<std::mutex> g(mu_);
Sree Kuchibhotlab0d15672016-03-07 10:51:02 -080072 return finalized_ ? (cancelled_ != 0) : false;
73 }
74
yang-gd45a26e2015-08-04 16:36:22 -070075 bool has_tag_;
yang-gb3352562015-08-04 14:42:06 -070076 void* tag_;
Vijay Pai320ed132016-11-01 17:16:55 -070077 std::mutex mu_;
Craig Tillercf133f42015-02-26 14:05:56 -080078 int refs_;
79 bool finalized_;
Craig Tiller50a7a682015-06-04 12:53:40 -070080 int cancelled_;
Craig Tiller492968f2015-02-18 13:14:03 -080081};
82
Craig Tiller492968f2015-02-18 13:14:03 -080083void ServerContext::CompletionOp::Unref() {
Vijay Pai320ed132016-11-01 17:16:55 -070084 std::unique_lock<std::mutex> lock(mu_);
Craig Tiller492968f2015-02-18 13:14:03 -080085 if (--refs_ == 0) {
86 lock.unlock();
87 delete this;
88 }
89}
90
Craig Tiller66051c62017-03-31 09:16:35 -070091void ServerContext::CompletionOp::FillOps(grpc_call* call, grpc_op* ops,
92 size_t* nops) {
Craig Tiller50a7a682015-06-04 12:53:40 -070093 ops->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
94 ops->data.recv_close_on_server.cancelled = &cancelled_;
Craig Tiller026e6002015-06-22 11:41:14 -070095 ops->flags = 0;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -080096 ops->reserved = nullptr;
Craig Tiller50a7a682015-06-04 12:53:40 -070097 *nops = 1;
Craig Tiller492968f2015-02-18 13:14:03 -080098}
99
100bool ServerContext::CompletionOp::FinalizeResult(void** tag, bool* status) {
Vijay Pai320ed132016-11-01 17:16:55 -0700101 std::unique_lock<std::mutex> lock(mu_);
Craig Tiller492968f2015-02-18 13:14:03 -0800102 finalized_ = true;
yang-gb3352562015-08-04 14:42:06 -0700103 bool ret = false;
yang-gd45a26e2015-08-04 16:36:22 -0700104 if (has_tag_) {
yang-gb3352562015-08-04 14:42:06 -0700105 *tag = tag_;
106 ret = true;
107 }
Craig Tiller50a7a682015-06-04 12:53:40 -0700108 if (!*status) cancelled_ = 1;
Craig Tiller492968f2015-02-18 13:14:03 -0800109 if (--refs_ == 0) {
110 lock.unlock();
111 delete this;
112 }
yang-gb3352562015-08-04 14:42:06 -0700113 return ret;
Craig Tiller492968f2015-02-18 13:14:03 -0800114}
115
116// ServerContext body
117
Craig Tillercf133f42015-02-26 14:05:56 -0800118ServerContext::ServerContext()
119 : completion_op_(nullptr),
yang-gd45a26e2015-08-04 16:36:22 -0700120 has_notify_when_done_tag_(false),
yang-gb3352562015-08-04 14:42:06 -0700121 async_notify_when_done_tag_(nullptr),
yang-g5ff8de32016-01-22 16:55:10 -0800122 deadline_(gpr_inf_future(GPR_CLOCK_REALTIME)),
Craig Tillercf133f42015-02-26 14:05:56 -0800123 call_(nullptr),
124 cq_(nullptr),
David Garcia Quintas440558a2016-07-21 19:31:38 -0700125 sent_initial_metadata_(false),
Noah Eisen67a40542017-07-06 09:34:06 -0700126 compression_level_set_(false),
127 has_pending_ops_(false) {}
Craig Tillerc6453062015-02-12 17:32:57 -0800128
yang-gc42d8442017-02-15 00:05:00 -0800129ServerContext::ServerContext(gpr_timespec deadline, grpc_metadata_array* arr)
Craig Tillercf133f42015-02-26 14:05:56 -0800130 : completion_op_(nullptr),
yang-gd45a26e2015-08-04 16:36:22 -0700131 has_notify_when_done_tag_(false),
yang-gb3352562015-08-04 14:42:06 -0700132 async_notify_when_done_tag_(nullptr),
Nicolas Noble89219162015-04-07 18:01:18 -0700133 deadline_(deadline),
Craig Tillercf133f42015-02-26 14:05:56 -0800134 call_(nullptr),
135 cq_(nullptr),
David Garcia Quintas39228382016-07-22 13:13:31 -0700136 sent_initial_metadata_(false),
Noah Eisen67a40542017-07-06 09:34:06 -0700137 compression_level_set_(false),
138 has_pending_ops_(false) {
yang-gc42d8442017-02-15 00:05:00 -0800139 std::swap(*client_metadata_.arr(), *arr);
Craig Tiller854a30c2015-02-11 11:44:10 -0800140}
141
Craig Tiller3d6ceb62015-02-12 14:33:54 -0800142ServerContext::~ServerContext() {
143 if (call_) {
Craig Tillerdd36b152017-03-31 08:27:28 -0700144 grpc_call_unref(call_);
Craig Tiller3d6ceb62015-02-12 14:33:54 -0800145 }
Craig Tiller492968f2015-02-18 13:14:03 -0800146 if (completion_op_) {
147 completion_op_->Unref();
148 }
149}
150
Vijay Pai06e174a2017-10-20 05:51:12 -0700151void ServerContext::BeginCompletionOp(internal::Call* call) {
Craig Tiller492968f2015-02-18 13:14:03 -0800152 GPR_ASSERT(!completion_op_);
153 completion_op_ = new CompletionOp();
yang-gd45a26e2015-08-04 16:36:22 -0700154 if (has_notify_when_done_tag_) {
155 completion_op_->set_tag(async_notify_when_done_tag_);
156 }
Craig Tiller492968f2015-02-18 13:14:03 -0800157 call->PerformOps(completion_op_);
Craig Tiller3d6ceb62015-02-12 14:33:54 -0800158}
159
Vijay Pai06e174a2017-10-20 05:51:12 -0700160internal::CompletionQueueTag* ServerContext::GetCompletionOpTag() {
161 return static_cast<internal::CompletionQueueTag*>(completion_op_);
Sree Kuchibhotlafa71f6f2017-04-01 17:32:34 -0700162}
163
Yang Gao2b7f5372015-02-18 00:45:53 -0800164void ServerContext::AddInitialMetadata(const grpc::string& key,
Craig Tiller645466e2015-02-18 09:18:33 -0800165 const grpc::string& value) {
Yang Gao2b7f5372015-02-18 00:45:53 -0800166 initial_metadata_.insert(std::make_pair(key, value));
167}
168
169void ServerContext::AddTrailingMetadata(const grpc::string& key,
Craig Tiller645466e2015-02-18 09:18:33 -0800170 const grpc::string& value) {
Yang Gao2b7f5372015-02-18 00:45:53 -0800171 trailing_metadata_.insert(std::make_pair(key, value));
172}
173
Sree Kuchibhotla7fa9d6f2016-01-28 17:32:48 -0800174void ServerContext::TryCancel() const {
175 grpc_call_error err = grpc_call_cancel_with_status(
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800176 call_, GRPC_STATUS_CANCELLED, "Cancelled on the server side", nullptr);
Sree Kuchibhotla7fa9d6f2016-01-28 17:32:48 -0800177 if (err != GRPC_CALL_OK) {
Sree Kuchibhotla369a04a2016-02-01 10:53:13 -0800178 gpr_log(GPR_ERROR, "TryCancel failed with: %d", err);
Sree Kuchibhotla7fa9d6f2016-01-28 17:32:48 -0800179 }
180}
181
David Garcia Quintas6dd49a52015-07-15 14:58:32 -0700182bool ServerContext::IsCancelled() const {
Sree Kuchibhotlab0d15672016-03-07 10:51:02 -0800183 if (has_notify_when_done_tag_) {
184 // when using async API, but the result is only valid
185 // if the tag has already been delivered at the completion queue
186 return completion_op_ && completion_op_->CheckCancelledAsync();
187 } else {
188 // when using sync API
189 return completion_op_ && completion_op_->CheckCancelled(cq_);
190 }
Craig Tiller645466e2015-02-18 09:18:33 -0800191}
192
David Garcia Quintasd7d9ce22015-06-30 23:29:03 -0700193void ServerContext::set_compression_algorithm(
194 grpc_compression_algorithm algorithm) {
David Garcia Quintasa21ab2b2018-02-01 16:24:00 -0800195 compression_algorithm_ = algorithm;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800196 const char* algorithm_name = nullptr;
David Garcia Quintasd7d9ce22015-06-30 23:29:03 -0700197 if (!grpc_compression_algorithm_name(algorithm, &algorithm_name)) {
198 gpr_log(GPR_ERROR, "Name for compression algorithm '%d' unknown.",
199 algorithm);
200 abort();
201 }
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800202 GPR_ASSERT(algorithm_name != nullptr);
David Garcia Quintas9e9f7b62016-05-16 19:12:12 -0700203 AddInitialMetadata(GRPC_COMPRESSION_REQUEST_ALGORITHM_MD_KEY, algorithm_name);
David Garcia Quintasd7d9ce22015-06-30 23:29:03 -0700204}
205
yang-gf1ec3772015-07-28 22:59:50 -0700206grpc::string ServerContext::peer() const {
207 grpc::string peer;
208 if (call_) {
209 char* c_peer = grpc_call_get_peer(call_);
210 peer = c_peer;
211 gpr_free(c_peer);
212 }
213 return peer;
214}
215
Alistair Veitch100a6e12015-07-29 15:25:28 -0700216const struct census_context* ServerContext::census_context() const {
Alistair Veitchcf39e942015-07-26 17:28:26 -0700217 return grpc_census_call_get_context(call_);
218}
219
David Garcia Quintasf31f0962017-02-10 14:53:58 -0800220void ServerContext::SetLoadReportingCosts(
221 const std::vector<grpc::string>& cost_data) {
222 if (call_ == nullptr) return;
David Garcia Quintasa818f722017-04-14 16:01:14 -0700223 for (const auto& cost_datum : cost_data) {
224 AddTrailingMetadata(GRPC_LB_COST_MD_KEY, cost_datum);
David Garcia Quintasf31f0962017-02-10 14:53:58 -0800225 }
David Garcia Quintasf31f0962017-02-10 14:53:58 -0800226}
227
Craig Tiller854a30c2015-02-11 11:44:10 -0800228} // namespace grpc