1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
|
#include "grpc_client_low.h"
#include <contrib/libs/grpc/src/core/lib/iomgr/socket_mutator.h>
#include <contrib/libs/grpc/include/grpc/support/log.h>
#include <library/cpp/containers/stack_vector/stack_vec.h>
#include <util/string/printf.h>
#include <util/system/thread.h>
#include <util/random/random.h>
#if !defined(_WIN32) && !defined(_WIN64)
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#endif
namespace NGrpc {
void EnableGRpcTracing() {
grpc_tracer_set_enabled("tcp", true);
grpc_tracer_set_enabled("client_channel", true);
grpc_tracer_set_enabled("channel", true);
grpc_tracer_set_enabled("api", true);
grpc_tracer_set_enabled("connectivity_state", true);
grpc_tracer_set_enabled("handshaker", true);
grpc_tracer_set_enabled("http", true);
grpc_tracer_set_enabled("http2_stream_state", true);
grpc_tracer_set_enabled("op_failure", true);
grpc_tracer_set_enabled("timer", true);
gpr_set_log_verbosity(GPR_LOG_SEVERITY_DEBUG);
}
class TGRpcKeepAliveSocketMutator : public grpc_socket_mutator {
public:
TGRpcKeepAliveSocketMutator(int idle, int count, int interval)
: Idle_(idle)
, Count_(count)
, Interval_(interval)
{
grpc_socket_mutator_init(this, &VTable);
}
private:
static TGRpcKeepAliveSocketMutator* Cast(grpc_socket_mutator* mutator) {
return static_cast<TGRpcKeepAliveSocketMutator*>(mutator);
}
template<typename TVal>
bool SetOption(int fd, int level, int optname, const TVal& value) {
return setsockopt(fd, level, optname, reinterpret_cast<const char*>(&value), sizeof(value)) == 0;
}
bool SetOption(int fd) {
if (!SetOption(fd, SOL_SOCKET, SO_KEEPALIVE, 1)) {
Cerr << Sprintf("Failed to set SO_KEEPALIVE option: %s", strerror(errno)) << Endl;
return false;
}
#ifdef _linux_
if (Idle_ && !SetOption(fd, IPPROTO_TCP, TCP_KEEPIDLE, Idle_)) {
Cerr << Sprintf("Failed to set TCP_KEEPIDLE option: %s", strerror(errno)) << Endl;
return false;
}
if (Count_ && !SetOption(fd, IPPROTO_TCP, TCP_KEEPCNT, Count_)) {
Cerr << Sprintf("Failed to set TCP_KEEPCNT option: %s", strerror(errno)) << Endl;
return false;
}
if (Interval_ && !SetOption(fd, IPPROTO_TCP, TCP_KEEPINTVL, Interval_)) {
Cerr << Sprintf("Failed to set TCP_KEEPINTVL option: %s", strerror(errno)) << Endl;
return false;
}
#endif
return true;
}
static bool Mutate(int fd, grpc_socket_mutator* mutator) {
auto self = Cast(mutator);
return self->SetOption(fd);
}
static int Compare(grpc_socket_mutator* a, grpc_socket_mutator* b) {
const auto* selfA = Cast(a);
const auto* selfB = Cast(b);
auto tupleA = std::make_tuple(selfA->Idle_, selfA->Count_, selfA->Interval_);
auto tupleB = std::make_tuple(selfB->Idle_, selfB->Count_, selfB->Interval_);
return tupleA < tupleB ? -1 : tupleA > tupleB ? 1 : 0;
}
static void Destroy(grpc_socket_mutator* mutator) {
delete Cast(mutator);
}
static bool Mutate2(const grpc_mutate_socket_info* info, grpc_socket_mutator* mutator) {
auto self = Cast(mutator);
return self->SetOption(info->fd);
}
static grpc_socket_mutator_vtable VTable;
const int Idle_;
const int Count_;
const int Interval_;
};
grpc_socket_mutator_vtable TGRpcKeepAliveSocketMutator::VTable =
{
&TGRpcKeepAliveSocketMutator::Mutate,
&TGRpcKeepAliveSocketMutator::Compare,
&TGRpcKeepAliveSocketMutator::Destroy,
&TGRpcKeepAliveSocketMutator::Mutate2
};
TChannelPool::TChannelPool(const TTcpKeepAliveSettings& tcpKeepAliveSettings, const TDuration& expireTime)
: TcpKeepAliveSettings_(tcpKeepAliveSettings)
, ExpireTime_(expireTime)
, UpdateReUseTime_(ExpireTime_ * 0.3 < TDuration::Seconds(20) ? ExpireTime_ * 0.3 : TDuration::Seconds(20))
{}
void TChannelPool::GetStubsHolderLocked(
const TString& channelId,
const TGRpcClientConfig& config,
std::function<void(TStubsHolder&)> cb)
{
{
std::shared_lock readGuard(RWMutex_);
const auto it = Pool_.find(channelId);
if (it != Pool_.end()) {
if (!it->second.IsChannelBroken() && !(Now() > it->second.GetLastUseTime() + UpdateReUseTime_)) {
return cb(it->second);
}
}
}
{
std::unique_lock writeGuard(RWMutex_);
{
auto it = Pool_.find(channelId);
if (it != Pool_.end()) {
if (!it->second.IsChannelBroken()) {
EraseFromQueueByTime(it->second.GetLastUseTime(), channelId);
auto now = Now();
LastUsedQueue_.emplace(now, channelId);
it->second.SetLastUseTime(now);
return cb(it->second);
} else {
// This channel can't be used. Remove from pool to create new one
EraseFromQueueByTime(it->second.GetLastUseTime(), channelId);
Pool_.erase(it);
}
}
}
TGRpcKeepAliveSocketMutator* mutator = nullptr;
// will be destroyed inside grpc
if (TcpKeepAliveSettings_.Enabled) {
mutator = new TGRpcKeepAliveSocketMutator(
TcpKeepAliveSettings_.Idle,
TcpKeepAliveSettings_.Count,
TcpKeepAliveSettings_.Interval
);
}
cb(Pool_.emplace(channelId, CreateChannelInterface(config, mutator)).first->second);
LastUsedQueue_.emplace(Pool_.at(channelId).GetLastUseTime(), channelId);
}
}
void TChannelPool::DeleteChannel(const TString& channelId) {
std::unique_lock writeLock(RWMutex_);
auto poolIt = Pool_.find(channelId);
if (poolIt != Pool_.end()) {
EraseFromQueueByTime(poolIt->second.GetLastUseTime(), channelId);
Pool_.erase(poolIt);
}
}
void TChannelPool::DeleteExpiredStubsHolders() {
std::unique_lock writeLock(RWMutex_);
auto lastExpired = LastUsedQueue_.lower_bound(Now() - ExpireTime_);
for (auto i = LastUsedQueue_.begin(); i != lastExpired; ++i){
Pool_.erase(i->second);
}
LastUsedQueue_.erase(LastUsedQueue_.begin(), lastExpired);
}
void TChannelPool::EraseFromQueueByTime(const TInstant& lastUseTime, const TString& channelId) {
auto [begin, end] = LastUsedQueue_.equal_range(lastUseTime);
auto pos = std::find_if(begin, end, [&](auto a){return a.second == channelId;});
Y_ABORT_UNLESS(pos != LastUsedQueue_.end(), "data corruption at TChannelPool");
LastUsedQueue_.erase(pos);
}
static void PullEvents(grpc::CompletionQueue* cq) {
TThread::SetCurrentThreadName("grpc_client");
while (true) {
void* tag;
bool ok;
if (!cq->Next(&tag, &ok)) {
break;
}
if (auto* ev = static_cast<IQueueClientEvent*>(tag)) {
if (!ev->Execute(ok)) {
ev->Destroy();
}
}
}
}
class TGRpcClientLow::TContextImpl final
: public std::enable_shared_from_this<TContextImpl>
, public IQueueClientContext
{
friend class TGRpcClientLow;
using TCallback = std::function<void()>;
using TContextPtr = std::shared_ptr<TContextImpl>;
public:
~TContextImpl() override {
Y_ABORT_UNLESS(CountChildren() == 0,
"Destructor called with non-empty children");
if (Parent) {
Parent->ForgetContext(this);
} else if (Y_LIKELY(Owner)) {
Owner->ForgetContext(this);
}
}
/**
* Helper for locking child pointer from a parent container
*/
static TContextPtr LockChildPtr(TContextImpl* ptr) {
if (ptr) {
// N.B. it is safe to do as long as it's done under a mutex and
// pointer is among valid children. When that's the case we
// know that TContextImpl destructor has not finished yet, so
// the object is valid. The lock() method may return nullptr
// though, if the object is being destructed right now.
return ptr->weak_from_this().lock();
} else {
return nullptr;
}
}
void ForgetContext(TContextImpl* child) {
std::unique_lock<std::mutex> guard(Mutex);
auto removed = RemoveChild(child);
Y_ABORT_UNLESS(removed, "Unexpected ForgetContext(%p)", child);
}
IQueueClientContextPtr CreateContext() override {
auto self = shared_from_this();
auto child = std::make_shared<TContextImpl>();
{
std::unique_lock<std::mutex> guard(Mutex);
AddChild(child.get());
// It's now safe to initialize parent and owner
child->Parent = std::move(self);
child->Owner = Owner;
child->CQ = CQ;
// Propagate cancellation to a child context
if (Cancelled.load(std::memory_order_relaxed)) {
child->Cancelled.store(true, std::memory_order_relaxed);
}
}
return child;
}
grpc::CompletionQueue* CompletionQueue() override {
Y_ABORT_UNLESS(Owner, "Uninitialized context");
return CQ;
}
bool IsCancelled() const override {
return Cancelled.load(std::memory_order_acquire);
}
bool Cancel() override {
TStackVec<TCallback, 1> callbacks;
TStackVec<TContextPtr, 2> children;
{
std::unique_lock<std::mutex> guard(Mutex);
if (Cancelled.load(std::memory_order_relaxed)) {
// Already cancelled in another thread
return false;
}
callbacks.reserve(Callbacks.size());
children.reserve(CountChildren());
for (auto& callback : Callbacks) {
callbacks.emplace_back().swap(callback);
}
Callbacks.clear();
// Collect all children we need to cancel
// N.B. we don't clear children links (cleared by destructors)
// N.B. some children may be stuck in destructors at the moment
for (TContextImpl* ptr : InlineChildren) {
if (auto child = LockChildPtr(ptr)) {
children.emplace_back(std::move(child));
}
}
for (auto* ptr : Children) {
if (auto child = LockChildPtr(ptr)) {
children.emplace_back(std::move(child));
}
}
Cancelled.store(true, std::memory_order_release);
}
// Call directly subscribed callbacks
if (callbacks) {
RunCallbacksNoExcept(callbacks);
}
// Cancel all children
for (auto& child : children) {
child->Cancel();
child.reset();
}
return true;
}
void SubscribeCancel(TCallback callback) override {
Y_ABORT_UNLESS(callback, "SubscribeCancel called with an empty callback");
{
std::unique_lock<std::mutex> guard(Mutex);
if (!Cancelled.load(std::memory_order_relaxed)) {
Callbacks.emplace_back().swap(callback);
return;
}
}
// Already cancelled, run immediately
callback();
}
private:
void AddChild(TContextImpl* child) {
for (TContextImpl*& slot : InlineChildren) {
if (!slot) {
slot = child;
return;
}
}
Children.insert(child);
}
bool RemoveChild(TContextImpl* child) {
for (TContextImpl*& slot : InlineChildren) {
if (slot == child) {
slot = nullptr;
return true;
}
}
return Children.erase(child);
}
size_t CountChildren() {
size_t count = 0;
for (TContextImpl* ptr : InlineChildren) {
if (ptr) {
++count;
}
}
return count + Children.size();
}
template<class TCallbacks>
static void RunCallbacksNoExcept(TCallbacks& callbacks) noexcept {
for (auto& callback : callbacks) {
if (callback) {
callback();
callback = nullptr;
}
}
}
private:
// We want a simple lock here, without extra memory allocations
std::mutex Mutex;
// These fields are initialized on successful registration
TContextPtr Parent;
TGRpcClientLow* Owner = nullptr;
grpc::CompletionQueue* CQ = nullptr;
// Some children are stored inline, others are in a set
std::array<TContextImpl*, 2> InlineChildren{ { nullptr, nullptr } };
std::unordered_set<TContextImpl*> Children;
// Single callback is stored without extra allocations
TStackVec<TCallback, 1> Callbacks;
// Atomic flag for a faster IsCancelled() implementation
std::atomic<bool> Cancelled;
};
TGRpcClientLow::TGRpcClientLow(size_t numWorkerThread, bool useCompletionQueuePerThread)
: UseCompletionQueuePerThread_(useCompletionQueuePerThread)
{
Init(numWorkerThread);
}
void TGRpcClientLow::Init(size_t numWorkerThread) {
SetCqState(WORKING);
if (UseCompletionQueuePerThread_) {
for (size_t i = 0; i < numWorkerThread; i++) {
CQS_.push_back(std::make_unique<grpc::CompletionQueue>());
auto* cq = CQS_.back().get();
WorkerThreads_.emplace_back(SystemThreadFactory()->Run([cq]() {
PullEvents(cq);
}).Release());
}
} else {
CQS_.push_back(std::make_unique<grpc::CompletionQueue>());
auto* cq = CQS_.back().get();
for (size_t i = 0; i < numWorkerThread; i++) {
WorkerThreads_.emplace_back(SystemThreadFactory()->Run([cq]() {
PullEvents(cq);
}).Release());
}
}
}
void TGRpcClientLow::AddWorkerThreadForTest() {
if (UseCompletionQueuePerThread_) {
CQS_.push_back(std::make_unique<grpc::CompletionQueue>());
auto* cq = CQS_.back().get();
WorkerThreads_.emplace_back(SystemThreadFactory()->Run([cq]() {
PullEvents(cq);
}).Release());
} else {
auto* cq = CQS_.back().get();
WorkerThreads_.emplace_back(SystemThreadFactory()->Run([cq]() {
PullEvents(cq);
}).Release());
}
}
TGRpcClientLow::~TGRpcClientLow() {
StopInternal(true);
WaitInternal();
}
void TGRpcClientLow::Stop(bool wait) {
StopInternal(false);
if (wait) {
WaitInternal();
}
}
void TGRpcClientLow::StopInternal(bool silent) {
bool shutdown;
TVector<TContextImpl::TContextPtr> cancelQueue;
{
std::unique_lock<std::mutex> guard(Mtx_);
auto allowStateChange = [&]() {
switch (GetCqState()) {
case WORKING:
return true;
case STOP_SILENT:
return !silent;
case STOP_EXPLICIT:
return false;
}
Y_UNREACHABLE();
};
if (!allowStateChange()) {
// Completion queue is already stopping
return;
}
SetCqState(silent ? STOP_SILENT : STOP_EXPLICIT);
if (!silent && !Contexts_.empty()) {
cancelQueue.reserve(Contexts_.size());
for (auto* ptr : Contexts_) {
// N.B. some contexts may be stuck in destructors
if (auto context = TContextImpl::LockChildPtr(ptr)) {
cancelQueue.emplace_back(std::move(context));
}
}
}
shutdown = Contexts_.empty();
}
for (auto& context : cancelQueue) {
context->Cancel();
context.reset();
}
if (shutdown) {
for (auto& cq : CQS_) {
cq->Shutdown();
}
}
}
void TGRpcClientLow::WaitInternal() {
std::unique_lock<std::mutex> guard(JoinMutex_);
for (auto& ti : WorkerThreads_) {
ti->Join();
}
}
void TGRpcClientLow::WaitIdle() {
std::unique_lock<std::mutex> guard(Mtx_);
while (!Contexts_.empty()) {
ContextsEmpty_.wait(guard);
}
}
std::shared_ptr<IQueueClientContext> TGRpcClientLow::CreateContext() {
std::unique_lock<std::mutex> guard(Mtx_);
auto allowCreateContext = [&]() {
switch (GetCqState()) {
case WORKING:
return true;
case STOP_SILENT:
case STOP_EXPLICIT:
return false;
}
Y_UNREACHABLE();
};
if (!allowCreateContext()) {
// New context creation is forbidden
return nullptr;
}
auto context = std::make_shared<TContextImpl>();
Contexts_.insert(context.get());
context->Owner = this;
if (UseCompletionQueuePerThread_) {
context->CQ = CQS_[RandomNumber(CQS_.size())].get();
} else {
context->CQ = CQS_[0].get();
}
return context;
}
void TGRpcClientLow::ForgetContext(TContextImpl* context) {
bool shutdown = false;
{
std::unique_lock<std::mutex> guard(Mtx_);
if (!Contexts_.erase(context)) {
Y_ABORT("Unexpected ForgetContext(%p)", context);
}
if (Contexts_.empty()) {
if (IsStopping()) {
shutdown = true;
}
ContextsEmpty_.notify_all();
}
}
if (shutdown) {
// This was the last context, shutdown CQ
for (auto& cq : CQS_) {
cq->Shutdown();
}
}
}
} // namespace NGRpc
|