-
Notifications
You must be signed in to change notification settings - Fork 85
/
BedrockCommand.cpp
391 lines (348 loc) · 15.7 KB
/
BedrockCommand.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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
#include <libstuff/libstuff.h>
#include <libstuff/SHTTPSManager.h>
#include "BedrockCommand.h"
#include "BedrockPlugin.h"
atomic<size_t> BedrockCommand::_commandCount(0);
SStandaloneHTTPSManager BedrockCommand::_noopHTTPSManager;
const string BedrockCommand::defaultPluginName("NO_PLUGIN");
BedrockCommand::BedrockCommand(SQLiteCommand&& baseCommand, BedrockPlugin* plugin, bool escalateImmediately_) :
SQLiteCommand(move(baseCommand)),
priority(PRIORITY_NORMAL),
prePeekCount(0),
peekCount(0),
processCount(0),
postProcessCount(0),
repeek(false),
crashIdentifyingValues(*this),
escalateImmediately(escalateImmediately_),
destructionCallback(nullptr),
socket(nullptr),
scheduledTime(request.isSet("commandExecuteTime") ? request.calc64("commandExecuteTime") : STimeNow()),
_plugin(plugin),
_commitEmptyTransactions(false),
_inProgressTiming(INVALID, 0, 0),
_timeout(_getTimeout(request, scheduledTime)),
_lastContiguousCompletedTransaction(httpsRequests.end())
{
// Initialize the priority, if supplied.
if (request.isSet("priority")) {
int tempPriority = request.calc("priority");
switch (tempPriority) {
// For any valid case, we just set the value directly.
case BedrockCommand::PRIORITY_MIN:
case BedrockCommand::PRIORITY_LOW:
case BedrockCommand::PRIORITY_NORMAL:
case BedrockCommand::PRIORITY_HIGH:
case BedrockCommand::PRIORITY_MAX:
priority = static_cast<Priority>(tempPriority);
break;
default:
// But an invalid case gets set to NORMAL, and a warning is logged.
SWARN("'" << request.methodLine << "' requested invalid priority: " << tempPriority);
priority = PRIORITY_NORMAL;
break;
}
}
_commandCount++;
}
const string& BedrockCommand::getName() const {
if (_plugin) {
return _plugin->getName();
}
return defaultPluginName;
}
int64_t BedrockCommand::_getTimeout(const SData& request, const uint64_t scheduledTime) {
// Timeout is the default, unless explicitly supplied, or if Connection: forget is set.
int64_t timeout = DEFAULT_TIMEOUT;
if (request.isSet("timeout")) {
timeout = request.calc("timeout");
} else if (SIEquals(request["connection"], "forget")) {
timeout = DEFAULT_TIMEOUT_FORGET;
}
// Convert to microseconds.
timeout *= 1000;
return timeout + scheduledTime;
}
BedrockCommand::~BedrockCommand() {
for (auto request : httpsRequests) {
request->manager.closeTransaction(request);
}
if (destructionCallback) {
(*destructionCallback)();
}
_commandCount--;
}
void BedrockCommand::startTiming(TIMING_INFO type) {
if (get<0>(_inProgressTiming) != INVALID ||
get<1>(_inProgressTiming) != 0
) {
SWARN("Starting timing, but looks like it was already running.");
}
get<0>(_inProgressTiming) = type;
get<1>(_inProgressTiming) = STimeNow();
get<2>(_inProgressTiming) = 0;
}
void BedrockCommand::stopTiming(TIMING_INFO type) {
if (get<0>(_inProgressTiming) != type ||
get<1>(_inProgressTiming) == 0
) {
SWARN("Stopping timing, but looks like it wasn't already running.");
}
// Add it to the list of timing info.
get<2>(_inProgressTiming) = STimeNow();
timingInfo.push_back(_inProgressTiming);
// And reset it for next use.
get<0>(_inProgressTiming) = INVALID;
get<1>(_inProgressTiming) = 0;
get<2>(_inProgressTiming) = 0;
}
bool BedrockCommand::areHttpsRequestsComplete() const {
auto requestIt = (_lastContiguousCompletedTransaction == httpsRequests.end()) ? httpsRequests.begin() : _lastContiguousCompletedTransaction;
while (requestIt != httpsRequests.end()) {
if (!(*requestIt)->response) {
return false;
} else {
_lastContiguousCompletedTransaction = requestIt;
}
requestIt++;
}
return true;
}
void BedrockCommand::reset(BedrockCommand::STAGE stage) {
if (stage == STAGE::PEEK && !shouldPrePeek()) {
jsonContent.clear();
response.clear();
}
}
void BedrockCommand::finalizeTimingInfo() {
uint64_t prePeekTotal = 0;
uint64_t blockingPrePeekTotal = 0;
uint64_t peekTotal = 0;
uint64_t blockingPeekTotal = 0;
uint64_t processTotal = 0;
uint64_t blockingProcessTotal = 0;
uint64_t postProcessTotal = 0;
uint64_t blockingPostProcessTotal = 0;
uint64_t commitWorkerTotal = 0;
uint64_t blockingCommitWorkerTotal = 0;
uint64_t commitSyncTotal = 0;
uint64_t queueWorkerTotal = 0;
uint64_t queueSyncTotal = 0;
uint64_t queueBlockingTotal = 0;
uint64_t queuePageLockTotal = 0;
for (const auto& entry: timingInfo) {
if (get<0>(entry) == PREPEEK) {
prePeekTotal += get<2>(entry) - get<1>(entry);
} else if (get<0>(entry) == BLOCKING_PREPEEK) {
prePeekTotal += get<2>(entry) - get<1>(entry);
blockingPrePeekTotal += get<2>(entry) - get<1>(entry);
} else if (get<0>(entry) == PEEK) {
peekTotal += get<2>(entry) - get<1>(entry);
} else if (get<0>(entry) == BLOCKING_PEEK) {
peekTotal += get<2>(entry) - get<1>(entry);
blockingPeekTotal += get<2>(entry) - get<1>(entry);
} else if (get<0>(entry) == PROCESS) {
processTotal += get<2>(entry) - get<1>(entry);
} else if (get<0>(entry) == BLOCKING_PROCESS) {
processTotal += get<2>(entry) - get<1>(entry);
blockingProcessTotal += get<2>(entry) - get<1>(entry);
} else if (get<0>(entry) == POSTPROCESS) {
postProcessTotal += get<2>(entry) - get<1>(entry);
} else if (get<0>(entry) == BLOCKING_POSTPROCESS) {
postProcessTotal += get<2>(entry) - get<1>(entry);
blockingPostProcessTotal += get<2>(entry) - get<1>(entry);
} else if (get<0>(entry) == COMMIT_WORKER) {
commitWorkerTotal += get<2>(entry) - get<1>(entry);
} else if (get<0>(entry) == BLOCKING_COMMIT_WORKER) {
commitWorkerTotal += get<2>(entry) - get<1>(entry);
blockingCommitWorkerTotal += get<2>(entry) - get<1>(entry);
} else if (get<0>(entry) == COMMIT_SYNC) {
commitSyncTotal += get<2>(entry) - get<1>(entry);
} else if (get<0>(entry) == QUEUE_WORKER) {
queueWorkerTotal += get<2>(entry) - get<1>(entry);
} else if (get<0>(entry) == QUEUE_BLOCKING) {
queueBlockingTotal += get<2>(entry) - get<1>(entry);
} else if (get<0>(entry) == QUEUE_SYNC) {
queueSyncTotal += get<2>(entry) - get<1>(entry);
} else if (get<0>(entry) == QUEUE_PAGE_LOCK) {
queuePageLockTotal += get<2>(entry) - get<1>(entry);
}
}
// The lifespan of the object up until now.
uint64_t totalTime = STimeNow() - creationTime;
// Time that wasn't accounted for in all the other metrics.
uint64_t unaccountedTime = totalTime - (prePeekTotal + peekTotal + processTotal + postProcessTotal + commitWorkerTotal + commitSyncTotal +
escalationTimeUS + queueWorkerTotal + queueBlockingTotal + queueSyncTotal + queuePageLockTotal);
uint64_t exclusiveTransactionLockTime = blockingPeekTotal + blockingProcessTotal + blockingCommitWorkerTotal;
uint64_t blockingCommitThreadTime = exclusiveTransactionLockTime + blockingPrePeekTotal + blockingPostProcessTotal;
// Build a map of the values we care about.
map<string, uint64_t> valuePairs = {
{"prePeekTime", prePeekTotal},
{"peekTime", peekTotal},
{"processTime", processTotal},
{"postProcessTime", postProcessTotal},
{"totalTime", totalTime},
{"unaccountedTime", unaccountedTime},
};
// We also want to know what leader did if we're on a follower.
uint64_t upstreamPeekTime = 0;
uint64_t upstreamProcessTime = 0;
uint64_t upstreamUnaccountedTime = 0;
uint64_t upstreamTotalTime = 0;
// Now promote any existing values that were set upstream. This prepends `upstream` and makes the first existing
// character of the name uppercase, (i.e. myValue -> upstreamMyValue), letting us keep anything that was set by the
// leader server. We clear these values after setting the new ones, so that we can add our own values.
for (const auto& p : valuePairs) {
auto it = response.nameValueMap.find(p.first);
if (it != response.nameValueMap.end()) {
string temp = it->second;
response.nameValueMap.erase(it);
response.nameValueMap[string("upstream") + (char)toupper(p.first[0]) + (p.first.substr(1))] = temp;
// Note the upstream times for our logline.
if (p.first == "peekTime") {
upstreamPeekTime = SToUInt64(temp);
}
else if (p.first == "processTime") {
upstreamProcessTime = SToUInt64(temp);
}
else if (p.first == "unaccountedTime") {
upstreamUnaccountedTime = SToUInt64(temp);
}
else if (p.first == "totalTime") {
upstreamTotalTime = SToUInt64(temp);
}
}
}
// This is a hack to support Auth's old `Get` format where we have a `returnValueList` of items to return rather
// than a specific name. The timing profile of every version of this command is wildly different and it's impossible
// to reason about which ones cause performance issues when they're all globbed together.
// In the future, let's find a better way to do this. For now, this gets us the data we need.
string methodName = request.methodLine + (request.isSet("returnValueList") ? "_" + request["returnValueList"] : ""s);
// This makes these look like valid command names given all our existing log handling.
for (size_t i = 0; i < methodName.size(); i++) {
if (methodName[i] == ',') {
methodName[i] = '_';
}
}
SINFO("command '" << methodName << "' timing info (ms): "
"prePeek:" << prePeekTotal/1000 << " (count: " << prePeekCount << "), "
"peek:" << peekTotal/1000 << " (count:" << peekCount << "), "
"process:" << processTotal/1000 << " (count:" << processCount << "), "
"postProcess:" << postProcessTotal/1000 << " (count:" << postProcessCount << "), "
"total:" << totalTime/1000 << ", "
"unaccounted:" << unaccountedTime/1000 << ", "
"blockingCommitThreadTime:" << blockingCommitThreadTime/1000 << ", "
"exclusiveTransactionLockTime:" << exclusiveTransactionLockTime/1000 <<
". Commit: "
"worker:" << commitWorkerTotal/1000 << ", "
"sync:"<< commitSyncTotal/1000 <<
". Queue: "
"worker:" << queueWorkerTotal/1000 << ", "
"sync:" << queueSyncTotal/1000 << ", "
"blocking:" << queueBlockingTotal/1000 << ", "
"pageLock:" << queuePageLockTotal/1000 << ", "
"escalation:" << escalationTimeUS/1000 <<
". Blocking: "
"prePeek:" << blockingPrePeekTotal/1000 << ", "
"peek:" << blockingPeekTotal/1000 << ", "
"process:" << blockingProcessTotal/1000 << ", "
"postProcess:" << blockingPostProcessTotal/1000 << ", "
"commit:" << blockingCommitWorkerTotal/1000 <<
". Upstream: "
"peek:" << upstreamPeekTime/1000 << ", "
"process:"<< upstreamProcessTime/1000 << ", "
"total:" << upstreamTotalTime/1000 << ", "
"unaccounted:" << upstreamUnaccountedTime/1000 << "."
);
// And here's where we set our own values.
for (const auto& p : valuePairs) {
if (p.second) {
response[p.first] = to_string(p.second);
}
}
// TODO: Remove when "escalate over HTTP" is enabled all the time, this is here to support only old-style
// escalations.
if (escalationTimeUS && !response.isSet("escalationTime")) {
response["escalationTime"] = to_string(escalationTimeUS);
}
}
void BedrockCommand::prePoll(fd_map& fdm)
{
auto requestIt = (_lastContiguousCompletedTransaction == httpsRequests.end()) ? httpsRequests.begin() : _lastContiguousCompletedTransaction;
while (requestIt != httpsRequests.end()) {
SHTTPSManager::Transaction* transaction = *requestIt;
transaction->manager.prePoll(fdm, *transaction);
requestIt++;
}
}
void BedrockCommand::postPoll(fd_map& fdm, uint64_t nextActivity, uint64_t maxWaitMS)
{
auto requestIt = (_lastContiguousCompletedTransaction == httpsRequests.end()) ? httpsRequests.begin() : _lastContiguousCompletedTransaction;
while (requestIt != httpsRequests.end()) {
SHTTPSManager::Transaction* transaction = *requestIt;
// If nothing else has set the timeout for this request (i.e., a HTTPS manager that expects to receive a
// response in a particular time), we will set the timeout here to match the timeout of the command so that we
// can't go "too long" waiting for a response.
// TODO: We should be able to set this at the creation of the transaction.
if (!transaction->timeoutAt) {
transaction->timeoutAt = _timeout;
}
transaction->manager.postPoll(fdm, *transaction, nextActivity, maxWaitMS);
requestIt++;
}
}
void BedrockCommand::setTimeout(uint64_t timeoutDurationMS) {
// Because _timeout is in microseconds.
timeoutDurationMS *= 1'000;
timeoutDurationMS += STimeNow();
_timeout = timeoutDurationMS;
}
bool BedrockCommand::shouldCommitEmptyTransactions() const {
return _commitEmptyTransactions;
}
void BedrockCommand::deserializeHTTPSRequests(const string& serializedHTTPSRequests) {
if (serializedHTTPSRequests.empty()) {
return;
}
list<string> requests = SParseJSONArray(serializedHTTPSRequests);
for (const string& requestStr : requests) {
STable requestMap = SParseJSONObject(requestStr);
SHTTPSManager::Transaction* httpsRequest = new SHTTPSManager::Transaction(_noopHTTPSManager, request["requestID"]);
httpsRequest->s = nullptr;
httpsRequest->created = SToUInt64(requestMap["created"]);
httpsRequest->finished = SToUInt64(requestMap["finished"]);
httpsRequest->timeoutAt = SToUInt64(requestMap["timeoutAt"]);
httpsRequest->sentTime = SToUInt64(requestMap["sentTime"]);
httpsRequest->response = SToInt(requestMap["response"]);
httpsRequest->fullRequest.deserialize(SDecodeBase64(requestMap["fullRequest"]));
httpsRequest->fullResponse.deserialize(SDecodeBase64(requestMap["fullResponse"]));
httpsRequests.push_back(httpsRequest);
// These should never be incomplete when passed with a serialized command.
if (!httpsRequest->response) {
SWARN("Received incomplete HTTPS request.");
}
}
}
string BedrockCommand::serializeHTTPSRequests() {
if (!httpsRequests.size()) {
return "";
}
list<string> requests;
for (const auto& httpsRequest : httpsRequests) {
STable data;
data["created"] = to_string(httpsRequest->created);
data["finished"] = to_string(httpsRequest->finished);
data["timeoutAt"] = to_string(httpsRequest->timeoutAt);
data["sentTime"] = to_string(httpsRequest->sentTime);
data["response"] = to_string(httpsRequest->response);
data["fullRequest"] = SEncodeBase64(httpsRequest->fullRequest.serialize());
data["fullResponse"] = SEncodeBase64(httpsRequest->fullResponse.serialize());
requests.push_back(SComposeJSONObject(data));
}
return SComposeJSONArray(requests);
}
string BedrockCommand::serializeData() const {
return "";
}
void BedrockCommand::deserializeData(const string& data) {
}