forked from Expensify/Bedrock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBedrockCore.cpp
327 lines (283 loc) · 13.1 KB
/
BedrockCore.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
#include <libstuff/libstuff.h>
#include "BedrockCore.h"
#include "BedrockPlugin.h"
#include "BedrockServer.h"
BedrockCore::BedrockCore(SQLite& db, const BedrockServer& server) :
SQLiteCore(db),
_server(server)
{ }
// RAII-style mechanism for automatically setting and unsetting query rewriting
class AutoScopeRewrite {
public:
AutoScopeRewrite(bool enable, SQLite& db, bool (*handler)(int, const char*, string&)) : _enable(enable), _db(db), _handler(handler) {
if (_enable) {
_db.setRewriteHandler(_handler);
_db.enableRewrite(true);
}
}
~AutoScopeRewrite() {
if (_enable) {
_db.setRewriteHandler(nullptr);
_db.enableRewrite(false);
}
}
private:
bool _enable;
SQLite& _db;
bool (*_handler)(int, const char*, string&);
};
uint64_t BedrockCore::_getRemainingTime(const unique_ptr<BedrockCommand>& command) {
int64_t timeout = command->timeout();
int64_t now = STimeNow();
// This is what's left for the "absolute" time. If it's negative, we've already timed out.
int64_t adjustedTimeout = timeout - now;
// We also want to know the processTimeout, because we'll return early if we get stuck processing for too long.
int64_t processTimeout = command->request.isSet("processTimeout") ? command->request.calc("processTimeout") : BedrockCommand::DEFAULT_PROCESS_TIMEOUT;
// Since timeouts are specified in ms, we convert to us.
processTimeout *= 1000;
// Already expired.
if (adjustedTimeout <= 0 || processTimeout <= 0) {
SALERT("Command " << command->request.methodLine << " timed out after "
<< ((now - command->request.calc64("commandExecuteTime")) / 1000) << "ms.");
STHROW("555 Timeout");
}
// Both of these are positive, return the lowest remaining.
return min(processTimeout, adjustedTimeout);
}
bool BedrockCore::isTimedOut(unique_ptr<BedrockCommand>& command) {
try {
_getRemainingTime(command);
} catch (const SException& e) {
// Yep, timed out.
_handleCommandException(command, e);
command->complete = true;
return true;
}
return false;
}
BedrockCore::RESULT BedrockCore::peekCommand(unique_ptr<BedrockCommand>& command, bool exclusive) {
AutoTimer timer(command, BedrockCommand::PEEK);
BedrockServer::ScopedStateSnapshot snapshot(_server);
command->lastPeekedOrProcessedInState = _server.getState();
// Convenience references to commonly used properties.
const SData& request = command->request;
SData& response = command->response;
STable& content = command->jsonContent;
// We catch any exception and handle in `_handleCommandException`.
RESULT returnValue = RESULT::COMPLETE;
try {
SDEBUG("Peeking at '" << request.methodLine << "' with priority: " << command->priority);
uint64_t timeout = _getRemainingTime(command);
command->peekCount++;
_db.startTiming(timeout);
try {
if (request.test("disableCheckpointInterrupt")) {
_db.disableCheckpointInterruptForNextTransaction();
}
if (!_db.beginTransaction(exclusive ? SQLite::TRANSACTION_TYPE::EXCLUSIVE : SQLite::TRANSACTION_TYPE::SHARED)) {
STHROW("501 Failed to begin " + (exclusive ? "exclusive"s : "shared"s) + " transaction");
}
// Make sure no writes happen while in peek command
_db.read("PRAGMA query_only = true;");
// Peek.
command->reset(BedrockCommand::STAGE::PEEK);
bool completed = command->peek(_db);
SDEBUG("Plugin '" << command->getName() << "' peeked command '" << request.methodLine << "'");
if (!completed) {
SINFO("Command '" << request.methodLine << "' not finished in peek, re-queuing.");
_db.resetTiming();
_db.read("PRAGMA query_only = false;");
return RESULT::SHOULD_PROCESS;
}
} catch (const SQLite::timeout_error& e) {
// Some plugins want to alert timeout errors themselves, and make them silent on bedrock.
if (!command->shouldSuppressTimeoutWarnings()) {
SALERT("Command " << command->request.methodLine << " timed out after " << e.time()/1000 << "ms.");
}
STHROW("555 Timeout peeking command");
}
// If no response was set, assume 200 OK
if (response.methodLine == "") {
response.methodLine = "200 OK";
}
// Add the commitCount header to the response.
response["commitCount"] = to_string(_db.getCommitCount());
// Success. If a command has set "content", encode it in the response.
SINFO("Responding '" << response.methodLine << "' to read-only '" << request.methodLine << "'.");
if (!content.empty()) {
// Make sure we're not overwriting anything different.
string newContent = SComposeJSONObject(content);
if (response.content != newContent) {
if (!response.content.empty()) {
SWARN("Replacing existing response content in " << request.methodLine);
}
response.content = newContent;
}
}
} catch (const SException& e) {
command->repeek = false;
_handleCommandException(command, e);
} catch (const SHTTPSManager::NotLeading& e) {
command->repeek = false;
returnValue = RESULT::SHOULD_PROCESS;
SINFO("Command '" << request.methodLine << "' wants to make HTTPS request, queuing for processing.");
} catch (const SQLite::checkpoint_required_error& e) {
command->repeek = false;
returnValue = RESULT::ABANDONED_FOR_CHECKPOINT;
SINFO("[checkpoint] Command " << command->request.methodLine << " abandoned (peek) for checkpoint");
} catch (...) {
command->repeek = false;
SALERT("Unhandled exception typename: " << SGetCurrentExceptionName() << ", command: " << request.methodLine);
command->response.methodLine = "500 Unhandled Exception";
}
// Unless an exception handler set this to something different, the command is complete.
command->complete = returnValue == RESULT::COMPLETE;
// Back out of the current transaction, it doesn't need to do anything.
_db.rollback();
_db.resetTiming();
// Reset, we can write now.
while (true) {
try {
_db.read("PRAGMA query_only = false;");
break;
} catch (const SQLite::checkpoint_required_error& e) {
// just try again
}
}
// Done.
return returnValue;
}
BedrockCore::RESULT BedrockCore::processCommand(unique_ptr<BedrockCommand>& command, bool exclusive) {
AutoTimer timer(command, BedrockCommand::PROCESS);
BedrockServer::ScopedStateSnapshot snapshot(_server);
// We need to be leading (including standing down) and we need to have peeked this command in the same set of
// states, or we can't complete this command (we can't commit the command if we're not leading, and if we're
// leading but were following when we peeked, we may try to read HTTPS requests we never made).
if ((command->lastPeekedOrProcessedInState != SQLiteNode::LEADING && command->lastPeekedOrProcessedInState != SQLiteNode::STANDINGDOWN) ||
(_server.getState() != SQLiteNode::LEADING && _server.getState() != SQLiteNode::STANDINGDOWN)) {
return RESULT::SERVER_NOT_LEADING;
}
command->lastPeekedOrProcessedInState = _server.getState();
// Convenience references to commonly used properties.
const SData& request = command->request;
SData& response = command->response;
STable& content = command->jsonContent;
// Keep track of whether we've modified the database and need to perform a `commit`.
bool needsCommit = false;
try {
SDEBUG("Processing '" << request.methodLine << "'");
uint64_t timeout = _getRemainingTime(command);
command->processCount++;
// Time in US.
_db.startTiming(timeout);
if (!_db.insideTransaction()) {
if (request.test("disableCheckpointInterrupt")) {
_db.disableCheckpointInterruptForNextTransaction();
}
// If a transaction was already begun in `peek`, then this won't run. We call it here to support the case where
// peek created a httpsRequest and closed it's first transaction until the httpsRequest was complete, in which
// case we need to open a new transaction.
if (!_db.beginTransaction(exclusive ? SQLite::TRANSACTION_TYPE::EXCLUSIVE : SQLite::TRANSACTION_TYPE::SHARED)) {
STHROW("501 Failed to begin " + (exclusive ? "exclusive"s : "shared"s) + " transaction");
}
}
// If the command is mocked, turn on UpdateNoopMode.
_db.setUpdateNoopMode(command->request.isSet("mockRequest"));
// Process the command.
{
bool (*handler)(int, const char*, string&) = nullptr;
bool enable = command->shouldEnableQueryRewriting(_db, &handler);
AutoScopeRewrite rewrite(enable, _db, handler);
try {
command->reset(BedrockCommand::STAGE::PROCESS);
command->process(_db);
SDEBUG("Plugin '" << command->getName() << "' processed command '" << request.methodLine << "'");
} catch (const SQLite::timeout_error& e) {
if (!command->shouldSuppressTimeoutWarnings()) {
SALERT("Command " << command->request.methodLine << " timed out after " << e.time()/1000 << "ms.");
}
STHROW("555 Timeout processing command");
}
}
// If we have no uncommitted query, just rollback the empty transaction. Otherwise, we need to commit.
if (_db.getUncommittedQuery().empty()) {
_db.rollback();
} else {
needsCommit = true;
}
// If no response was set, assume 200 OK
if (response.methodLine == "") {
response.methodLine = "200 OK";
}
// Success, this command will be committed.
SINFO("Processed '" << response.methodLine << "' for '" << request.methodLine << "'.");
// Finally, if a command has set "content", encode it in the response.
if (!content.empty()) {
// Make sure we're not overwriting anything different.
string newContent = SComposeJSONObject(content);
if (response.content != newContent) {
if (!response.content.empty()) {
SWARN("Replacing existing response content in " << request.methodLine);
}
response.content = newContent;
}
}
} catch (const SException& e) {
_handleCommandException(command, e);
_db.rollback();
needsCommit = false;
} catch (const SQLite::checkpoint_required_error& e) {
_db.rollback();
_db.setUpdateNoopMode(false);
_db.resetTiming();
command->complete = false;
SINFO("[checkpoint] Command " << command->request.methodLine << " abandoned (process) for checkpoint");
return RESULT::ABANDONED_FOR_CHECKPOINT;
} catch (const SQLite::constraint_error& e) {
SWARN("Unique Constraints Violation, command: " << request.methodLine);
command->response.methodLine = "400 Unique Constraints Violation";
_db.rollback();
needsCommit = false;
} catch(...) {
SALERT("Unhandled exception typename: " << SGetCurrentExceptionName() << ", command: " << request.methodLine);
command->response.methodLine = "500 Unhandled Exception";
_db.rollback();
needsCommit = false;
}
// We can turn this back off now, this is a noop if it's not turned on.
_db.setUpdateNoopMode(false);
// We can reset the timing info for the next command.
_db.resetTiming();
// Done, return whether or not we need the parent to commit our transaction.
command->complete = !needsCommit;
return needsCommit ? RESULT::NEEDS_COMMIT : RESULT::NO_COMMIT_REQUIRED;
}
void BedrockCore::_handleCommandException(unique_ptr<BedrockCommand>& command, const SException& e) {
string msg = "Error processing command '" + command->request.methodLine + "' (" + e.what() + "), ignoring.";
if (!e.body.empty()) {
msg = msg + " Request body: " + e.body;
}
if (SContains(e.what(), "_ALERT_")) {
SALERT(msg);
} else if (SContains(e.what(), "_WARN_")) {
SWARN(msg);
} else if (SContains(e.what(), "_HMMM_")) {
SHMMM(msg);
} else if (SStartsWith(e.what(), "50")) {
SALERT(msg); // Alert on 500 level errors.
} else {
SINFO(msg);
}
// Set the response to the values from the exception, if set.
if (!e.method.empty()) {
command->response.methodLine = e.method;
}
if (!e.headers.empty()) {
command->response.nameValueMap = e.headers;
}
if (!e.body.empty()) {
command->response.content = e.body;
}
// Add the commitCount header to the response.
command->response["commitCount"] = to_string(_db.getCommitCount());
}