forked from Expensify/Bedrock
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBedrockCore.cpp
More file actions
232 lines (200 loc) · 9.3 KB
/
BedrockCore.cpp
File metadata and controls
232 lines (200 loc) · 9.3 KB
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
#include "BedrockCore.h"
#include "BedrockPlugin.h"
#include "BedrockServer.h"
BedrockCore::BedrockCore(SQLite& db, const BedrockServer& server) :
SQLiteCore(db),
_server(server)
{ }
bool BedrockCore::peekCommand(BedrockCommand& command) {
AutoTimer timer(command, BedrockCommand::PEEK);
// Convenience references to commonly used properties.
SData& request = command.request;
SData& response = command.response;
STable& content = command.jsonContent;
SDEBUG("Peeking at '" << request.methodLine << "' with priority: " << command.priority);
command.peekCount++;
uint64_t timeout = command.request.isSet("timeout") ? command.request.calc("timeout") : DEFAULT_TIMEOUT;
// We catch any exception and handle in `_handleCommandException`.
try {
_db.startTiming(timeout);
// We start a transaction in `peekCommand` because we want to support having atomic transactions from peek
// through process. This allows for consistency through this two-phase process. I.e., anything checked in
// peek is guaranteed to still be valid in process, because they're done together as one transaction.
bool pluginPeeked = false;
// Some plugins want to alert timeout errors themselves, and make them silent on bedrock.
bool shouldSuppressTimeoutWarnings = false;
try {
if (!_db.beginConcurrentTransaction()) {
STHROW("501 Failed to begin concurrent transaction");
}
// Make sure no writes happen while in peek command
_db.read("PRAGMA query_only = true;");
// Try each plugin, and go with the first one that says it succeeded.
for (auto plugin : _server.plugins) {
shouldSuppressTimeoutWarnings = plugin->shouldSuppressTimeoutWarnings();
// Try to peek the command.
if (plugin->peekCommand(_db, command)) {
SINFO("Plugin '" << plugin->getName() << "' peeked command '" << request.methodLine << "'");
pluginPeeked = true;
break;
}
}
} catch (const SQLite::timeout_error& e) {
if (!shouldSuppressTimeoutWarnings) {
SALERT("Command " << command.request.methodLine << " timed out after " << e.time() << "us.");
}
STHROW("555 Timeout peeking command");
}
// Peeking is over now, allow writes
_db.read("PRAGMA query_only = false;");
// If nobody succeeded in peeking it, then we'll need to process it.
// TODO: Would be nice to be able to check if a plugin *can* handle a command, so that we can differentiate
// between "didn't peek" and "peeked but didn't complete".
if (!pluginPeeked) {
SINFO("Command '" << request.methodLine << "' is not peekable, queuing for processing.");
_db.resetTiming();
return false;
}
// 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) {
_db.read("PRAGMA query_only = false;");
_handleCommandException(command, e);
} catch (...) {
_db.read("PRAGMA query_only = false;");
SALERT("Unhandled exception typename: " << SGetCurrentExceptionName() << ", command: " << request.methodLine);
command.response.methodLine = "500 Unhandled Exception";
}
// If we get here, it means the command is fully completed.
command.complete = true;
// Back out of the current transaction, it doesn't need to do anything.
_db.rollback();
_db.resetTiming();
// Done.
return true;
}
bool BedrockCore::processCommand(BedrockCommand& command) {
AutoTimer timer(command, BedrockCommand::PROCESS);
// Convenience references to commonly used properties.
SData& request = command.request;
SData& response = command.response;
STable& content = command.jsonContent;
SDEBUG("Processing '" << request.methodLine << "'");
command.processCount++;
uint64_t timeout = command.request.isSet("timeout") ? command.request.calc("timeout") : DEFAULT_TIMEOUT;
// Keep track of whether we've modified the database and need to perform a `commit`.
bool needsCommit = false;
try {
// Time in US.
_db.startTiming(timeout);
// If a transaction was already begun in `peek`, then this is a no-op. 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.insideTransaction() && !_db.beginConcurrentTransaction()) {
STHROW("501 Failed to begin concurrent transaction");
}
// Loop across the plugins to see which wants to take this.
bool pluginProcessed = false;
// If the command is mocked, turn on UpdateNoopMode.
_db.setUpdateNoopMode(command.request.isSet("mockRequest"));
for (auto plugin : _server.plugins) {
// Try to process the command.
try {
if (plugin->processCommand(_db, command)) {
SINFO("Plugin '" << plugin->getName() << "' processed command '" << request.methodLine << "'");
pluginProcessed = true;
break;
}
} catch (const SQLite::timeout_error& e) {
SALERT("Command " << command.request.methodLine << " timed out after " << e.time() << "us.");
STHROW("555 Timeout processing command");
}
}
// If no plugin processed it, respond accordingly.
if (!pluginProcessed) {
SWARN("Command '" << request.methodLine << "' does not exist.");
STHROW("430 Unrecognized 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(...) {
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;
}
void BedrockCore::_handleCommandException(BedrockCommand& command, const SException& e) {
const string& msg = "Error processing command '" + command.request.methodLine + "' (" + e.what() + "), ignoring.";
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());
}