forked from Expensify/Bedrock
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBedrockCommand.cpp
More file actions
234 lines (209 loc) · 7.78 KB
/
BedrockCommand.cpp
File metadata and controls
234 lines (209 loc) · 7.78 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
233
234
#include <libstuff/libstuff.h>
#include "BedrockCommand.h"
BedrockCommand::BedrockCommand() :
SQLiteCommand(),
httpsRequest(nullptr),
priority(PRIORITY_NORMAL),
peekCount(0),
processCount(0),
_inProgressTiming(INVALID, 0, 0)
{ }
BedrockCommand::~BedrockCommand() {
if (httpsRequest) {
httpsRequest->owner.closeTransaction(httpsRequest);
httpsRequest = nullptr;
}
}
BedrockCommand::BedrockCommand(SQLiteCommand&& from) :
SQLiteCommand(std::move(from)),
httpsRequest(nullptr),
priority(PRIORITY_NORMAL),
peekCount(0),
processCount(0),
_inProgressTiming(INVALID, 0, 0)
{
_init();
}
BedrockCommand::BedrockCommand(BedrockCommand&& from) :
SQLiteCommand(std::move(from)),
httpsRequest(from.httpsRequest),
priority(from.priority),
peekCount(from.peekCount),
processCount(from.processCount),
timingInfo(from.timingInfo),
_inProgressTiming(from._inProgressTiming)
{
// The move constructor (and likewise, the move assignment operator), don't simply copy this pointer value, but
// they clear it from the old object, so that when its destructor is called, the HTTPS transaction isn't closed.
from.httpsRequest = nullptr;
}
BedrockCommand::BedrockCommand(SData&& _request) :
SQLiteCommand(move(_request)),
httpsRequest(nullptr),
priority(PRIORITY_NORMAL),
peekCount(0),
processCount(0),
_inProgressTiming(INVALID, 0, 0)
{
_init();
}
BedrockCommand::BedrockCommand(SData _request) :
SQLiteCommand(move(_request)),
httpsRequest(nullptr),
priority(PRIORITY_NORMAL),
peekCount(0),
processCount(0),
_inProgressTiming(INVALID, 0, 0)
{
_init();
}
BedrockCommand& BedrockCommand::operator=(BedrockCommand&& from) {
if (this != &from) {
// The current incarnation of this object is going away, if it had an httpsRequest, we'll need to destroy it,
// or it will leak and never get cleaned up.
if (httpsRequest) {
httpsRequest->owner.closeTransaction(httpsRequest);
}
httpsRequest = from.httpsRequest;
from.httpsRequest = nullptr;
// Update our other properties.
peekCount = from.peekCount;
processCount = from.processCount;
priority = from.priority;
timingInfo = from.timingInfo;
_inProgressTiming = from._inProgressTiming;
// And call the base class's move constructor as well.
SQLiteCommand::operator=(move(from));
}
return *this;
}
void BedrockCommand::_init() {
// 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;
}
}
}
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;
}
void BedrockCommand::finalizeTimingInfo() {
uint64_t peekTotal = 0;
uint64_t processTotal = 0;
uint64_t commitWorkerTotal = 0;
uint64_t commitSyncTotal = 0;
uint64_t queueWorkerTotal = 0;
uint64_t queueSyncTotal = 0;
for (const auto& entry: timingInfo) {
if (get<0>(entry) == PEEK) {
peekTotal += get<2>(entry) - get<1>(entry);
} else if (get<0>(entry) == PROCESS) {
processTotal += 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) == 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_SYNC) {
queueSyncTotal += 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 - (peekTotal + processTotal + commitWorkerTotal + commitSyncTotal +
queueWorkerTotal + queueSyncTotal);
// Log all this info.
SINFO("command '" << request.methodLine << "' timing info (us): "
<< peekTotal << " (" << peekCount << "), "
<< processTotal << " (" << processCount << "), "
<< commitWorkerTotal << ", "
<< commitSyncTotal << ", "
<< queueWorkerTotal << ", "
<< queueSyncTotal << ", "
<< totalTime << ", "
<< unaccountedTime << "."
);
// Build a map of the values we care about.
map<string, uint64_t> valuePairs = {
{"peekTime", peekTotal},
{"processTime", processTotal},
{"totalTime", totalTime},
{"escalationTime", escalationTimeUS},
};
// 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
// master 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;
}
}
// And here's where we set our own values.
for (const auto& p : valuePairs) {
if (p.second) {
response[p.first] = to_string(p.second);
}
}
}
// pop and push specializations for SSynchronizedQueue that record timing info.
template<>
BedrockCommand SSynchronizedQueue<BedrockCommand>::pop() {
SAUTOLOCK(_queueMutex);
if (!_queue.empty()) {
BedrockCommand item = move(_queue.front());
_queue.pop_front();
item.stopTiming(BedrockCommand::QUEUE_SYNC);
return item;
}
throw out_of_range("No commands");
}
template<>
void SSynchronizedQueue<BedrockCommand>::push(BedrockCommand&& rhs) {
SAUTOLOCK(_queueMutex);
// Just add to the queue
_queue.push_back(move(rhs));
_queue.back().startTiming(BedrockCommand::QUEUE_SYNC);
// Write arbitrary buffer to the pipe so any subscribers will be awoken.
// **NOTE: 1 byte so write is atomic.
SASSERT(write(_pipeFD[1], "A", 1));
}