| 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 <atomic> |
| 20 |
#include <cstring> |
| 21 |
#include <memory> |
| 22 |
#include <string> |
| 23 |
#include <utility> |
| 24 |
#include <vector> |
| 25 |
|
| 26 |
#include <grpc/grpc.h> |
| 27 |
#include <grpc/impl/codegen/connectivity_state.h> |
| 28 |
#include <grpc/impl/codegen/gpr_types.h> |
| 29 |
#include <grpc/impl/codegen/grpc_types.h> |
| 30 |
#include <grpc/slice.h> |
| 31 |
#include <grpc/support/alloc.h> |
| 32 |
#include <grpc/support/log.h> |
| 33 |
#include <grpcpp/channel.h> |
| 34 |
#include <grpcpp/client_context.h> |
| 35 |
#include <grpcpp/completion_queue.h> |
| 36 |
#include <grpcpp/impl/call.h> |
| 37 |
#include <grpcpp/impl/call_op_set_interface.h> |
| 38 |
#include <grpcpp/impl/codegen/completion_queue_tag.h> |
| 39 |
#include <grpcpp/impl/codegen/core_codegen_interface.h> |
| 40 |
#include <grpcpp/impl/codegen/sync.h> |
| 41 |
#include <grpcpp/impl/grpc_library.h> |
| 42 |
#include <grpcpp/impl/rpc_method.h> |
| 43 |
#include <grpcpp/support/client_interceptor.h> |
| 44 |
#include <grpcpp/support/slice.h> |
| 45 |
|
| 46 |
#include "src/core/lib/iomgr/iomgr.h" |
| 47 |
|
| 48 |
namespace grpc { |
| 49 |
|
| 50 |
static grpc::internal::GrpcLibraryInitializer g_gli_initializer; |
| 51 |
Channel::Channel( |
| 52 |
const std::string& host, grpc_channel* channel, |
| 53 |
std::vector< |
| 54 |
std::unique_ptr<grpc::experimental::ClientInterceptorFactoryInterface>> |
| 55 |
interceptor_creators) |
| 56 |
: host_(host), c_channel_(channel) { |
| 57 |
interceptor_creators_ = std::move(interceptor_creators); |
| 58 |
g_gli_initializer.summon(); |
| 59 |
} |
| 60 |
|
| 61 |
Channel::~Channel() { |
| 62 |
grpc_channel_destroy(c_channel_); |
| 63 |
CompletionQueue* callback_cq = callback_cq_.load(std::memory_order_relaxed); |
| 64 |
if (callback_cq != nullptr) { |
| 65 |
if (grpc_iomgr_run_in_background()) { |
| 66 |
// gRPC-core provides the backing needed for the preferred CQ type |
| 67 |
callback_cq->Shutdown(); |
| 68 |
} else { |
| 69 |
CompletionQueue::ReleaseCallbackAlternativeCQ(callback_cq); |
| 70 |
} |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
namespace { |
| 75 |
|
| 76 |
inline grpc_slice SliceFromArray(const char* arr, size_t len) { |
| 77 |
return g_core_codegen_interface->grpc_slice_from_copied_buffer(arr, len); |
| 78 |
} |
| 79 |
|
| 80 |
std::string GetChannelInfoField(grpc_channel* channel, |
| 81 |
grpc_channel_info* channel_info, |
| 82 |
char*** channel_info_field) { |
| 83 |
char* value = nullptr; |
| 84 |
memset(channel_info, 0, sizeof(*channel_info)); |
| 85 |
*channel_info_field = &value; |
| 86 |
grpc_channel_get_info(channel, channel_info); |
| 87 |
if (value == nullptr) return ""; |
| 88 |
std::string result = value; |
| 89 |
gpr_free(value); |
| 90 |
return result; |
| 91 |
} |
| 92 |
|
| 93 |
} // namespace |
| 94 |
|
| 95 |
std::string Channel::GetLoadBalancingPolicyName() const { |
| 96 |
grpc_channel_info channel_info; |
| 97 |
return GetChannelInfoField(c_channel_, &channel_info, |
| 98 |
&channel_info.lb_policy_name); |
| 99 |
} |
| 100 |
|
| 101 |
std::string Channel::GetServiceConfigJSON() const { |
| 102 |
grpc_channel_info channel_info; |
| 103 |
return GetChannelInfoField(c_channel_, &channel_info, |
| 104 |
&channel_info.service_config_json); |
| 105 |
} |
| 106 |
|
| 107 |
namespace experimental { |
| 108 |
|
| 109 |
void ChannelResetConnectionBackoff(Channel* channel) { |
| 110 |
grpc_channel_reset_connect_backoff(channel->c_channel_); |
| 111 |
} |
| 112 |
|
| 113 |
} // namespace experimental |
| 114 |
|
| 115 |
grpc::internal::Call Channel::CreateCallInternal( |
| 116 |
const grpc::internal::RpcMethod& method, grpc::ClientContext* context, |
| 117 |
grpc::CompletionQueue* cq, size_t interceptor_pos) { |
| 118 |
const bool kRegistered = method.channel_tag() && context->authority().empty(); |
| 119 |
grpc_call* c_call = nullptr; |
| 120 |
if (kRegistered) { |
| 121 |
c_call = grpc_channel_create_registered_call( |
| 122 |
c_channel_, context->propagate_from_call_, |
| 123 |
context->propagation_options_.c_bitmask(), cq->cq(), |
| 124 |
method.channel_tag(), context->raw_deadline(), nullptr); |
| 125 |
} else { |
| 126 |
const ::std::string* host_str = nullptr; |
| 127 |
if (!context->authority_.empty()) { |
| 128 |
host_str = &context->authority_; |
| 129 |
} else if (!host_.empty()) { |
| 130 |
host_str = &host_; |
| 131 |
} |
| 132 |
grpc_slice method_slice = |
| 133 |
SliceFromArray(method.name(), strlen(method.name())); |
| 134 |
grpc_slice host_slice; |
| 135 |
if (host_str != nullptr) { |
| 136 |
host_slice = grpc::SliceFromCopiedString(*host_str); |
| 137 |
} |
| 138 |
c_call = grpc_channel_create_call( |
| 139 |
c_channel_, context->propagate_from_call_, |
| 140 |
context->propagation_options_.c_bitmask(), cq->cq(), method_slice, |
| 141 |
host_str == nullptr ? nullptr : &host_slice, context->raw_deadline(), |
| 142 |
nullptr); |
| 143 |
grpc_slice_unref(method_slice); |
| 144 |
if (host_str != nullptr) { |
| 145 |
grpc_slice_unref(host_slice); |
| 146 |
} |
| 147 |
} |
| 148 |
grpc_census_call_set_context(c_call, context->census_context()); |
| 149 |
|
| 150 |
// ClientRpcInfo should be set before call because set_call also checks |
| 151 |
// whether the call has been cancelled, and if the call was cancelled, we |
| 152 |
// should notify the interceptors too. |
| 153 |
auto* info = context->set_client_rpc_info( |
| 154 |
method.name(), method.suffix_for_stats(), method.method_type(), this, |
| 155 |
interceptor_creators_, interceptor_pos); |
| 156 |
context->set_call(c_call, shared_from_this()); |
| 157 |
|
| 158 |
return grpc::internal::Call(c_call, this, cq, info); |
| 159 |
} |
| 160 |
|
| 161 |
grpc::internal::Call Channel::CreateCall( |
| 162 |
const grpc::internal::RpcMethod& method, grpc::ClientContext* context, |
| 163 |
CompletionQueue* cq) { |
| 164 |
return CreateCallInternal(method, context, cq, 0); |
| 165 |
} |
| 166 |
|
| 167 |
void Channel::PerformOpsOnCall(grpc::internal::CallOpSetInterface* ops, |
| 168 |
grpc::internal::Call* call) { |
| 169 |
ops->FillOps( |
| 170 |
call); // Make a copy of call. It's fine since Call just has pointers |
| 171 |
} |
| 172 |
|
| 173 |
void* Channel::RegisterMethod(const char* method) { |
| 174 |
return grpc_channel_register_call( |
| 175 |
c_channel_, method, host_.empty() ? nullptr : host_.c_str(), nullptr); |
| 176 |
} |
| 177 |
|
| 178 |
grpc_connectivity_state Channel::GetState(bool try_to_connect) { |
| 179 |
return grpc_channel_check_connectivity_state(c_channel_, try_to_connect); |
| 180 |
} |
| 181 |
|
| 182 |
namespace { |
| 183 |
|
| 184 |
class TagSaver final : public grpc::internal::CompletionQueueTag { |
| 185 |
public: |
| 186 |
explicit TagSaver(void* tag) : tag_(tag) {} |
| 187 |
~TagSaver() override {} |
| 188 |
bool FinalizeResult(void** tag, bool* /*status*/) override { |
| 189 |
*tag = tag_; |
| 190 |
delete this; |
| 191 |
return true; |
| 192 |
} |
| 193 |
|
| 194 |
private: |
| 195 |
void* tag_; |
| 196 |
}; |
| 197 |
|
| 198 |
} // namespace |
| 199 |
|
| 200 |
void Channel::NotifyOnStateChangeImpl(grpc_connectivity_state last_observed, |
| 201 |
gpr_timespec deadline, |
| 202 |
grpc::CompletionQueue* cq, void* tag) { |
| 203 |
TagSaver* tag_saver = new TagSaver(tag); |
| 204 |
grpc_channel_watch_connectivity_state(c_channel_, last_observed, deadline, |
| 205 |
cq->cq(), tag_saver); |
| 206 |
} |
| 207 |
|
| 208 |
bool Channel::WaitForStateChangeImpl(grpc_connectivity_state last_observed, |
| 209 |
gpr_timespec deadline) { |
| 210 |
grpc::CompletionQueue cq; |
| 211 |
bool ok = false; |
| 212 |
void* tag = nullptr; |
| 213 |
NotifyOnStateChangeImpl(last_observed, deadline, &cq, nullptr); |
| 214 |
cq.Next(&tag, &ok); |
| 215 |
GPR_ASSERT(tag == nullptr); |
| 216 |
return ok; |
| 217 |
} |
| 218 |
|
| 219 |
namespace { |
| 220 |
class ShutdownCallback : public grpc_completion_queue_functor { |
| 221 |
public: |
| 222 |
ShutdownCallback() { |
| 223 |
functor_run = &ShutdownCallback::Run; |
| 224 |
// Set inlineable to true since this callback is trivial and thus does not |
| 225 |
// need to be run from the executor (triggering a thread hop). This should |
| 226 |
// only be used by internal callbacks like this and not by user application |
| 227 |
// code. |
| 228 |
inlineable = true; |
| 229 |
} |
| 230 |
// TakeCQ takes ownership of the cq into the shutdown callback |
| 231 |
// so that the shutdown callback will be responsible for destroying it |
| 232 |
void TakeCQ(grpc::CompletionQueue* cq) { cq_ = cq; } |
| 233 |
|
| 234 |
// The Run function will get invoked by the completion queue library |
| 235 |
// when the shutdown is actually complete |
| 236 |
static void Run(grpc_completion_queue_functor* cb, int) { |
| 237 |
auto* callback = static_cast<ShutdownCallback*>(cb); |
| 238 |
delete callback->cq_; |
| 239 |
delete callback; |
| 240 |
} |
| 241 |
|
| 242 |
private: |
| 243 |
grpc::CompletionQueue* cq_ = nullptr; |
| 244 |
}; |
| 245 |
} // namespace |
| 246 |
|
| 247 |
::grpc::CompletionQueue* Channel::CallbackCQ() { |
| 248 |
// TODO(vjpai): Consider using a single global CQ for the default CQ |
| 249 |
// if there is no explicit per-channel CQ registered |
| 250 |
CompletionQueue* callback_cq = callback_cq_.load(std::memory_order_acquire); |
| 251 |
if (callback_cq != nullptr) { |
| 252 |
return callback_cq; |
| 253 |
} |
| 254 |
// The callback_cq_ wasn't already set, so grab a lock and set it up exactly |
| 255 |
// once for this channel. |
| 256 |
grpc::internal::MutexLock l(&mu_); |
| 257 |
callback_cq = callback_cq_.load(std::memory_order_relaxed); |
| 258 |
if (callback_cq == nullptr) { |
| 259 |
if (grpc_iomgr_run_in_background()) { |
| 260 |
// gRPC-core provides the backing needed for the preferred CQ type |
| 261 |
|
| 262 |
auto* shutdown_callback = new ShutdownCallback; |
| 263 |
callback_cq = new grpc::CompletionQueue(grpc_completion_queue_attributes{ |
| 264 |
GRPC_CQ_CURRENT_VERSION, GRPC_CQ_CALLBACK, GRPC_CQ_DEFAULT_POLLING, |
| 265 |
shutdown_callback}); |
| 266 |
|
| 267 |
// Transfer ownership of the new cq to its own shutdown callback |
| 268 |
shutdown_callback->TakeCQ(callback_cq); |
| 269 |
} else { |
| 270 |
// Otherwise we need to use the alternative CQ variant |
| 271 |
callback_cq = CompletionQueue::CallbackAlternativeCQ(); |
| 272 |
} |
| 273 |
callback_cq_.store(callback_cq, std::memory_order_release); |
| 274 |
} |
| 275 |
return callback_cq; |
| 276 |
} |
| 277 |
|
| 278 |
} // namespace grpc |