Repository navigation
Expand file tree
/
Copy pathxbox-server.cpp
More file actions
410 lines (348 loc) · 12.6 KB
/
Copy pathxbox-server.cpp
File metadata and controls
410 lines (348 loc) · 12.6 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
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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/*
+----------------------------------------------------------------------+
| HipHop for PHP |
+----------------------------------------------------------------------+
| Copyright (c) 2010-present Facebook, Inc. (http://www.facebook.com) |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
*/
#include "hphp/runtime/server/xbox-server.h"
#include <folly/ScopeGuard.h>
#include "hphp/runtime/base/builtin-functions.h"
#include "hphp/runtime/base/request-fanout-limit-wrapper.h"
#include "hphp/runtime/ext/server/ext_server.h"
#include "hphp/runtime/server/xbox-request-handler.h"
#include "hphp/runtime/server/satellite-server.h"
#include "hphp/runtime/server/job-queue-vm-stack.h"
#include "hphp/runtime/server/server-task-event.h"
#include "hphp/util/configs/server.h"
#include "hphp/util/configs/xbox.h"
#include "hphp/util/job-queue.h"
#include "hphp/util/lock.h"
#include "hphp/util/logger.h"
#include "hphp/util/timer.h"
#include "hphp/system/systemlib.h"
namespace HPHP {
///////////////////////////////////////////////////////////////////////////////
const StaticString s_xbox("xbox");
///////////////////////////////////////////////////////////////////////////////
using std::string;
XboxTransport::XboxTransport(const folly::StringPiece message,
RequestId root_req_id,
const folly::StringPiece reqInitDoc /* = "" */)
: m_refCount(0), m_done(false), m_code(0), m_event(nullptr) {
Timer::GetMonotonicTime(m_queueTime);
m_message.append(message.data(), message.size());
m_reqInitDoc.append(reqInitDoc.data(), reqInitDoc.size());
setRootRequestId(root_req_id);
disableCompression(); // so we don't have to decompress during sendImpl()
}
const char *XboxTransport::getUrl() {
if (!m_reqInitDoc.empty()) {
return "xbox_process_call_message";
}
return Cfg::Xbox::ProcessMessageFunc.c_str();
}
std::string XboxTransport::getHeader(const char *name) {
if (!strcasecmp(name, "Host")) return m_host;
if (!strcasecmp(name, "ReqInitDoc")) return m_reqInitDoc;
return "";
}
void XboxTransport::sendImpl(const void* data, int size, int code,
bool /*chunked*/, bool eom) {
m_response.append((const char*)data, size);
if (code) {
m_code = code;
}
if (eom) {
onSendEndImpl();
}
}
void XboxTransport::onSendEndImpl() {
Lock lock(this);
if (m_done) {
return;
}
m_done = true;
if (m_event) {
m_event->finish();
}
notify();
}
OptString XboxTransport::getResults(int &code, int timeout_ms /* = 0 */) {
{
Lock lock(this);
while (!m_done) {
if (timeout_ms > 0) {
long long seconds = timeout_ms / 1000;
long long nanosecs = (timeout_ms - seconds * 1000) * 1000;
if (!wait(seconds, nanosecs)) {
code = -1;
return String::Empty();
}
} else {
wait();
}
}
}
OptString response(m_response.c_str(), m_response.size(), CopyString);
code = m_code;
return response;
}
///////////////////////////////////////////////////////////////////////////////
static THREAD_LOCAL(std::shared_ptr<XboxServerInfo>, s_xbox_server_info);
static THREAD_LOCAL(std::string, s_xbox_prev_req_init_doc);
static THREAD_LOCAL(XboxRequestHandler, s_xbox_request_handler);
///////////////////////////////////////////////////////////////////////////////
struct XboxWorker
: JobQueueWorker<XboxTransport*,Server*,true,false,JobQueueDropVMStack>
{
void abortJob(XboxTransport *job) override {
Logger::Warning("Job dropped by JobQueueDispatcher because of timeout in xbox.");
auto const handler = createRequestHandler();
handler->abortRequest(job);
destroyRequestHandler();
job->decRefCount();
}
void doJob(XboxTransport *job) override {
try {
// If this job or the previous job that ran on this thread have
// a custom initial document, make sure we do a reset
string reqInitDoc = job->getHeader("ReqInitDoc");
*s_xbox_prev_req_init_doc = reqInitDoc;
job->onRequestStart(job->getStartTimer());
auto const handler = createRequestHandler();
if (auto ctx = job->detachCliContext()) {
handler->setCliContext(std::move(ctx).value());
}
handler->run(job);
destroyRequestHandler();
job->decRefCount();
} catch (...) {
Logger::Error("RpcRequestHandler leaked exceptions");
}
}
private:
RequestHandler *createRequestHandler() {
if (!*s_xbox_server_info) {
*s_xbox_server_info = std::make_shared<XboxServerInfo>();
}
s_xbox_request_handler->setLogInfo(Cfg::Xbox::ServerInfoLogInfo);
s_xbox_request_handler->setServerInfo(*s_xbox_server_info);
return s_xbox_request_handler.get();
}
void destroyRequestHandler() {
if (!s_xbox_request_handler.isNull()) {
s_xbox_request_handler.destroy();
}
}
void onThreadExit() override {
if (!s_xbox_request_handler.isNull()) {
s_xbox_request_handler.destroy();
}
}
};
///////////////////////////////////////////////////////////////////////////////
static ReadWriteMutex s_dispatchMutex;
static JobQueueDispatcher<XboxWorker> *s_dispatcher;
static size_t s_drainingThreadCount{0};
static ServiceData::CounterCallback* s_counters;
void XboxServer::Restart() {
Stop();
if (Cfg::Xbox::ServerInfoThreadCount > 0) {
{
WriteLock l(s_dispatchMutex);
s_dispatcher = new JobQueueDispatcher<XboxWorker>
(Cfg::Xbox::ServerInfoThreadCount,
Cfg::Xbox::ServerInfoThreadCount,
Cfg::Server::ThreadDropCacheTimeoutSeconds,
Cfg::Server::ThreadDropStack,
nullptr,
INT_MAX,
Cfg::Xbox::ServerInfoMaxJobQueuingMs,
1, // numPriorities default
0, // hugeCount default
-1, // initThreadCount default
0, // hugeStackKb default
0, // extraKb default
Cfg::Xbox::ServerInfoThreadGroupSuffix
);
s_counters = new ServiceData::CounterCallback(
[](ServiceData::CounterMap& counters) {
// For the entire duration when the counters are registered, we make
// sure `s_dispatcher` is initialized and won't be stopped. See
// `XboxServer::Stop()` where we deregister the counters before
// destroying `s_counters`.
assertx(s_dispatcher);
auto const& stats = s_dispatcher->getDispatcherStats();
counters["xbox_inflight_requests"] = stats.activeThreads;
counters["xbox_queued_requests"] = stats.queuedJobCount;
},
"xbox_"
);
if (Cfg::Server::TrackRequestFanout) {
requestFanoutLimitInit(Cfg::Server::RequestFanoutLimit, Cfg::Server::ThreadCount);
}
}
if (Cfg::Xbox::ServerInfoLogInfo) {
Logger::Info("xbox server started");
}
s_dispatcher->start();
}
}
void XboxServer::Stop() {
if (!s_dispatcher) return;
delete s_counters;
s_counters = nullptr;
JobQueueDispatcher<XboxWorker>* dispatcher = nullptr;
size_t drainingCount = 0;
{
WriteLock l(s_dispatchMutex);
if (!s_dispatcher) return;
dispatcher = s_dispatcher;
drainingCount = dispatcher->getLiveThreadCount();
s_drainingThreadCount += drainingCount;
s_dispatcher = nullptr;
}
SCOPE_EXIT {
delete dispatcher;
WriteLock l(s_dispatchMutex);
s_drainingThreadCount -= drainingCount;
};
dispatcher->stop();
}
bool XboxServer::Enabled() {
return s_dispatcher;
}
///////////////////////////////////////////////////////////////////////////////
const StaticString
s_code("code"),
s_response("response"),
s_error("error"),
s_localhost("localhost"),
s_127_0_0_1("127.0.0.1");
static bool isLocalHost(const OptString& host) {
return host.empty() || host == s_localhost || host == s_127_0_0_1;
}
///////////////////////////////////////////////////////////////////////////////
struct XboxTask : SweepableResourceData {
DECLARE_RESOURCE_ALLOCATION(XboxTask)
XboxTask(const OptString& message, const OptString& reqInitDoc = "", const RequestId root_req_id = RequestId()) {
m_job = new XboxTransport(message.toCppString(), root_req_id, reqInitDoc.toCppString());
m_job->incRefCount();
if (cli_supports_clone()) {
m_job->setCliContext(cli_clone_context());
}
}
XboxTask(const XboxTask&) = delete;
XboxTask& operator=(const XboxTask&) = delete;
~XboxTask() override {
m_job->decRefCount();
}
XboxTransport *getJob() { return m_job;}
CLASSNAME_IS("XboxTask")
// overriding ResourceData
const OptString& o_getClassNameHook() const override { return classnameof(); }
private:
XboxTransport *m_job{nullptr};
};
IMPLEMENT_RESOURCE_ALLOCATION(XboxTask)
///////////////////////////////////////////////////////////////////////////////
OptResource XboxServer::TaskStart(
const OptString& msg,
const OptString& reqInitDoc /* = "" */,
ServerTaskEvent<XboxServer, XboxTransport> *event /* = nullptr */,
RequestId root_req_id /* = RequestId() */) {
static auto xboxOverflowCounter =
ServiceData::createTimeSeries("xbox_overflow",
{ ServiceData::StatsType::COUNT });
static auto xboxQueuedCounter =
ServiceData::createTimeSeries("xbox_queued",
{ ServiceData::StatsType::COUNT });
{
WriteLock l(s_dispatchMutex);
if (s_dispatcher &&
(s_dispatcher->getActiveWorker() <
Cfg::Xbox::ServerInfoThreadCount ||
s_dispatcher->getQueuedJobs() <
Cfg::Xbox::ServerInfoMaxQueueLength)) {
auto task = req::make<XboxTask>(msg, reqInitDoc, root_req_id);
XboxTransport *job = task->getJob();
job->incRefCount(); // paired with worker's decRefCount()
xboxQueuedCounter->addValue(1);
Transport *transport = g_context->getTransport();
if (transport) {
job->setHost(transport->getHeader("Host"));
}
if (event) {
job->setAsioEvent(event);
event->setJob(job);
}
// If server_uptime() is -1, it means we are not yet running in server mode
// and are serving warmup requests. Excempt warmup requests from fanout enforcement.
if (Cfg::Server::TrackRequestFanout && HHVM_FN(server_uptime)() >= 0) {
requestFanoutLimitIncrement(root_req_id);
}
assertx(s_dispatcher);
s_dispatcher->enqueue(job);
g_context->incrXboxTasksStarted();
return OptResource(std::move(task));
}
}
auto hasXbox = Cfg::Xbox::ServerInfoThreadCount > 0;
const char* errMsg;
if (hasXbox && !s_dispatcher) {
errMsg = "Cannot create Xbox task because Xbox server is shut down";
} else if (hasXbox) {
errMsg = "Cannot create new Xbox task because the Xbox queue has "
"reached maximum capacity";
} else {
errMsg = "Cannot create new Xbox task because the Xbox is not enabled";
}
if (hasXbox) {
xboxOverflowCounter->addValue(1);
}
throw_exception(SystemLib::AllocExceptionObject(errMsg));
return OptResource();
}
bool XboxServer::TaskStatus(const OptResource& task) {
return cast<XboxTask>(task)->getJob()->isDone();
}
int XboxServer::TaskResult(const OptResource& task, int timeout_ms, Variant *ret) {
return TaskResult(cast<XboxTask>(task)->getJob(), timeout_ms, ret);
}
int XboxServer::TaskResult(XboxTransport *job, int timeout_ms, Variant *ret) {
int code = 0;
OptString response = job->getResults(code, timeout_ms);
if (ret) {
if (code == 200) {
*ret =
unserialize_from_string(response, VariableUnserializer::Type::Internal);
} else {
*ret = response;
}
}
return code;
}
int XboxServer::GetActiveWorkers() {
ReadLock l(s_dispatchMutex);
return s_dispatcher ? s_dispatcher->getActiveWorker() : 0;
}
size_t XboxServer::GetLiveThreadCount() {
ReadLock l(s_dispatchMutex);
return s_drainingThreadCount +
(s_dispatcher ? s_dispatcher->getLiveThreadCount() : 0);
}
int XboxServer::GetQueuedJobs() {
ReadLock l(s_dispatchMutex);
return s_dispatcher ? s_dispatcher->getQueuedJobs() : 0;
}
///////////////////////////////////////////////////////////////////////////////
}