/
ProxyDestinationBase.cpp
375 lines (333 loc) · 11.2 KB
/
ProxyDestinationBase.cpp
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
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include "ProxyDestinationBase.h"
#include <chrono>
#include <folly/io/async/AsyncTimeout.h>
#include "mcrouter/McrouterLogFailure.h"
#include "mcrouter/ProxyBase.h"
#include "mcrouter/ProxyDestinationBase.h"
#include "mcrouter/TkoLog.h"
#include "mcrouter/TkoTracker.h"
namespace facebook {
namespace memcache {
namespace mcrouter {
namespace {
// Probe settings.
constexpr double kProbeExponentialFactor = 1.5;
constexpr double kProbeJitterMin = 0.05;
constexpr double kProbeJitterMax = 0.5;
constexpr double kProbeJitterDelta = kProbeJitterMax - kProbeJitterMin;
static_assert(
kProbeJitterMax >= kProbeJitterMin,
"ProbeJitterMax should be greater or equal tham ProbeJitterMin");
} // anonymous namespace
void ProxyDestinationBase::updateTkoStats(GlobalTkoUpdateType type) {
switch (type) {
case GlobalTkoUpdateType::INC_SOFT_TKOS:
proxy_.stats().increment(num_soft_tko_count_stat);
proxy_.stats().setValue(
max_num_tko_stat,
std::max(
proxy_.stats().getValue(max_num_tko_stat),
proxy_.stats().getValue(num_soft_tko_count_stat) +
proxy_.stats().getValue(num_hard_tko_count_stat)));
break;
case GlobalTkoUpdateType::DEC_SOFT_TKOS:
proxy_.stats().decrement(num_soft_tko_count_stat);
break;
case GlobalTkoUpdateType::INC_HARD_TKOS:
proxy_.stats().increment(num_hard_tko_count_stat);
proxy_.stats().setValue(
max_num_tko_stat,
std::max(
proxy_.stats().getValue(max_num_tko_stat),
proxy_.stats().getValue(num_soft_tko_count_stat) +
proxy_.stats().getValue(num_hard_tko_count_stat)));
break;
case GlobalTkoUpdateType::DEC_HARD_TKOS:
proxy_.stats().decrement(num_hard_tko_count_stat);
break;
case GlobalTkoUpdateType::ENTER_FAIL_OPEN:
proxy_.stats().increment(num_fail_open_state_entered_stat);
break;
case GlobalTkoUpdateType::EXIT_FAIL_OPEN:
proxy_.stats().increment(num_fail_open_state_exited_stat);
break;
}
}
ProxyDestinationBase::ProxyDestinationBase(
ProxyBase& proxy,
std::shared_ptr<const AccessPoint> ap,
std::chrono::milliseconds timeout,
uint32_t qosClass,
uint32_t qosPath,
uint32_t idx)
: proxy_(proxy),
accessPoint_(std::move(ap)),
shortestConnectTimeout_(timeout),
shortestWriteTimeout_(timeout),
qosClass_(qosClass),
qosPath_(qosPath),
idx_(idx) {
proxy_.stats().increment(num_servers_new_stat);
proxy_.stats().increment(num_servers_stat);
if (accessPoint()->useSsl()) {
proxy_.stats().increment(num_ssl_servers_new_stat);
proxy_.stats().increment(num_ssl_servers_stat);
}
}
ProxyDestinationBase::~ProxyDestinationBase() {
if (tracker_->removeDestination(this)) {
onTkoEvent(TkoLogEvent::RemoveFromConfig, carbon::Result::OK);
stopSendingProbes();
}
}
void ProxyDestinationBase::updateShortestTimeout(
std::chrono::milliseconds connectTimeout,
std::chrono::milliseconds writeTimeout) {
if (connectTimeout.count() == 0 && writeTimeout.count() == 0) {
return;
}
if (shortestConnectTimeout_.count() == 0 ||
shortestConnectTimeout_ > connectTimeout ||
shortestWriteTimeout_.count() == 0 ||
shortestWriteTimeout_ > writeTimeout) {
shortestConnectTimeout_ = std::min(shortestConnectTimeout_, connectTimeout);
shortestWriteTimeout_ = std::min(shortestWriteTimeout_, writeTimeout);
updateTransportTimeoutsIfShorter(
shortestConnectTimeout_, shortestWriteTimeout_);
}
}
bool ProxyDestinationBase::maySend(carbon::Result& tkoReason) const {
if (tracker_->isTko()) {
// There's a small race window here, but as the tkoReason is used for
// logging/debugging purposes only, it's ok to eventually return an
// outdated value.
tkoReason = tracker_->tkoReason();
return false;
}
return true;
}
void ProxyDestinationBase::onTkoEvent(TkoLogEvent event, carbon::Result result)
const {
auto logUtil = [this, result](folly::StringPiece eventStr) {
VLOG(2) << accessPoint_.load()->toHostPortString() << " " << eventStr
<< ". Total hard TKOs: " << tracker_->globalTkos().hardTkos
<< "; soft TKOs: " << tracker_->globalTkos().softTkos
<< ". Reply: " << carbon::resultToString(result);
};
switch (event) {
case TkoLogEvent::MarkHardTko:
logUtil("marked hard TKO");
break;
case TkoLogEvent::MarkSoftTko:
logUtil("marked soft TKO");
break;
case TkoLogEvent::UnMarkTko:
logUtil("unmarked TKO");
break;
case TkoLogEvent::RemoveFromConfig:
logUtil("was TKO, removed from config");
break;
}
TkoLog tkoLog(*accessPoint_.load(), tracker_->globalTkos());
tkoLog.event = event;
tkoLog.isHardTko = tracker_->isHardTko();
tkoLog.isSoftTko = tracker_->isSoftTko();
tkoLog.avgLatency = stats_.avgLatency.value();
tkoLog.probesSent = stats_.probesSent;
tkoLog.result = result;
logTkoEvent(proxy(), tkoLog);
}
void ProxyDestinationBase::handleTko(
const carbon::Result result,
bool isProbeRequest) {
if (proxy().router().opts().disable_tko_tracking) {
return;
}
if (isErrorResult(result)) {
if (isHardTkoErrorResult(result)) {
if (tracker_->recordHardFailure(this, result)) {
onTkoEvent(TkoLogEvent::MarkHardTko, result);
startSendingProbes();
}
} else if (isSoftTkoErrorResult(result)) {
if (tracker_->recordSoftFailure(this, result)) {
onTkoEvent(TkoLogEvent::MarkSoftTko, result);
startSendingProbes();
}
}
return;
}
if (tracker_->isTko()) {
if (isProbeRequest && tracker_->recordSuccess(this)) {
onTkoEvent(TkoLogEvent::UnMarkTko, result);
stopSendingProbes();
}
return;
}
tracker_->recordSuccess(this);
}
void ProxyDestinationBase::scheduleNextProbe() {
assert(!proxy().router().opts().disable_tko_tracking);
int delayMs = probeDelayNextMs;
if (probeDelayNextMs < 2) {
// int(1 * 1.5) == 1, so advance it to 2 first
probeDelayNextMs = 2;
} else {
probeDelayNextMs *= kProbeExponentialFactor;
}
if (probeDelayNextMs > proxy().router().opts().probe_delay_max_ms) {
probeDelayNextMs = proxy().router().opts().probe_delay_max_ms;
}
// Calculate random jitter
double r = (double)rand() / (double)RAND_MAX;
double tmo_jitter_pct = r * kProbeJitterDelta + kProbeJitterMin;
delayMs = (double)delayMs * (1.0 + tmo_jitter_pct);
assert(delayMs > 0);
if (!probeTimer_->scheduleTimeout(delayMs)) {
MC_LOG_FAILURE(
proxy().router().opts(),
failure::Category::kSystemError,
"failed to schedule probe timer for ProxyDestination");
}
}
void ProxyDestinationBase::startSendingProbes() {
probeDelayNextMs = proxy().router().opts().probe_delay_initial_ms;
probeTimer_ =
folly::AsyncTimeout::make(proxy().eventBase(), [this]() noexcept {
// Note that the previous probe might still be in flight
if (!probeInflight_) {
probeInflight_ = true;
++stats_.probesSent;
proxy().fiberManager().addTask([selfPtr = selfPtr()]() mutable {
auto pdstn = selfPtr.lock();
if (pdstn == nullptr) {
return;
}
pdstn->markAsActive();
auto result = pdstn->sendProbe();
pdstn->handleTko(result, /* isProbeRequest */ true);
pdstn->probeInflight_ = false;
});
}
scheduleNextProbe();
});
scheduleNextProbe();
}
void ProxyDestinationBase::stopSendingProbes() {
stats_.probesSent = 0;
probeTimer_.reset();
}
void ProxyDestinationBase::updateConnectionClosedInternalStat() {
if (stats().inactiveConnectionClosedTimestampUs != 0) {
std::chrono::microseconds timeClosed(
nowUs() - stats().inactiveConnectionClosedTimestampUs);
proxy().stats().inactiveConnectionClosedIntervalSec().insertSample(
std::chrono::duration_cast<std::chrono::seconds>(timeClosed).count());
// resets inactiveConnectionClosedTimestampUs, as we just want to take
// into account connections that were closed due to lack of activity
stats().inactiveConnectionClosedTimestampUs = 0;
}
}
void ProxyDestinationBase::setPoolStatsIndex(int32_t index) {
proxy().eventBase().runInEventBaseThread([selfPtr = selfPtr(), index]() {
auto pdstn = selfPtr.lock();
if (!pdstn) {
return;
}
pdstn->stats().poolStatIndex_ = index;
if (pdstn->stats().state == State::Up) {
if (auto* poolStats = pdstn->proxy().stats().getPoolStats(index)) {
poolStats->updateConnections(1);
}
}
});
}
void ProxyDestinationBase::updatePoolStatConnections(bool connected) {
if (auto poolStats = proxy().stats().getPoolStats(stats().poolStatIndex_)) {
poolStats->updateConnections(connected ? 1 : -1);
}
}
void ProxyDestinationBase::onTransitionToState(State st) {
onTransitionImpl(st, true /* to */);
}
void ProxyDestinationBase::onTransitionFromState(State st) {
onTransitionImpl(st, false /* to */);
}
void ProxyDestinationBase::onTransitionImpl(State st, bool to) {
const int64_t delta = to ? 1 : -1;
const bool ssl = accessPoint()->useSsl();
switch (st) {
case ProxyDestinationBase::State::New: {
proxy().stats().increment(num_servers_new_stat, delta);
if (ssl) {
proxy().stats().increment(num_ssl_servers_new_stat, delta);
}
break;
}
case ProxyDestinationBase::State::Up: {
proxy().stats().increment(num_servers_up_stat, delta);
if (ssl) {
proxy().stats().increment(num_ssl_servers_up_stat, delta);
}
break;
}
case ProxyDestinationBase::State::Closed: {
proxy().stats().increment(num_servers_closed_stat, delta);
if (ssl) {
proxy().stats().increment(num_ssl_servers_closed_stat, delta);
}
break;
}
case ProxyDestinationBase::State::Down: {
proxy().stats().increment(num_servers_down_stat, delta);
if (ssl) {
proxy().stats().increment(num_ssl_servers_down_stat, delta);
}
break;
}
case ProxyDestinationBase::State::NumStates: {
CHECK(false);
break;
}
}
}
void ProxyDestinationBase::setState(State newState) {
if (stats().state == newState) {
return;
}
auto logUtil = [this](const char* s) {
VLOG(3) << "server " << ProxyDestinationKey(*this).str() << " " << s << " ("
<< proxy().stats().getValue(num_servers_up_stat) << " of "
<< proxy().stats().getValue(num_servers_stat) << ")";
};
onTransitionFromState(stats().state);
onTransitionToState(newState);
stats().state = newState;
switch (stats().state) {
case State::Up:
logUtil("up");
break;
case State::Closed:
logUtil("closed");
break;
case State::Down:
logUtil("down");
break;
case State::New:
case State::NumStates:
assert(false);
break;
}
}
void ProxyDestinationBase::markAsActive() {
proxy().destinationMap()->markAsActive(*this);
}
} // namespace mcrouter
} // namespace memcache
} // namespace facebook