-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreq_server.cc
485 lines (409 loc) · 16.4 KB
/
req_server.cc
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
#include "include/seastarkv.hh"
#include "include/req_server.hh"
#include "include/net_server.hh"
#include "include/scheduler.hh"
#include "include/db.hh"
#include "v8/src/runtime/runtime.h"
#include <iostream>
#include <cstdio>
#include <condition_variable>
#include <thread>
#include <fstream>
#include <string>
#include <functional>
#include <boost/uuid/detail/sha1.hpp>
#include <boost/beast/core/detail/base64.hpp>
#include <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_io.hpp>
#include <boost/uuid/uuid_generators.hpp>
#include <stdlib.h>
using namespace std;
using namespace seastar;
using namespace v8;
using namespace shredder;
using namespace boost::uuids;
distributed<req_service> req_server;
extern mongocxx::pool *pool;
static const char* ToCString(const v8::String::Utf8Value& value) {
return *value ? *value : "<string conversion failed>";
}
future<> req_service::start(void) {
create_params.array_buffer_allocator =
v8::ArrayBuffer::Allocator::NewDefaultAllocator();
isolate = Isolate::New(create_params);
if (this_shard_id() >= HW_Q_COUNT)
big_core = true;
return make_ready_future<>();
}
future<> req_service::stop() {
return make_ready_future<>();
}
future<> req_service::register_service(std::string service) {
v8::Locker locker{isolate};
Isolate::Scope isolate_scope(isolate);
{
HandleScope handle_scope(isolate);
v8::Local<v8::ObjectTemplate> global = v8::ObjectTemplate::New(isolate);
// Set C++ bindings
global->Set(
v8::String::NewFromUtf8(isolate, "DBGet", v8::NewStringType::kNormal)
.ToLocalChecked(),
v8::FunctionTemplate::New(isolate, db_get)
);
global->Set(
v8::String::NewFromUtf8(isolate, "DBSet", v8::NewStringType::kNormal)
.ToLocalChecked(),
v8::FunctionTemplate::New(isolate, db_set)
);
global->Set(
v8::String::NewFromUtf8(isolate, "DBDel", v8::NewStringType::kNormal)
.ToLocalChecked(),
v8::FunctionTemplate::New(isolate, db_del)
);
global->Set(
v8::String::NewFromUtf8(isolate, "print", v8::NewStringType::kNormal)
.ToLocalChecked(),
v8::FunctionTemplate::New(isolate, js_print)
);
global->Set(
v8::String::NewFromUtf8(isolate, "GetHashTable", v8::NewStringType::kNormal)
.ToLocalChecked(),
v8::FunctionTemplate::New(isolate, get_hash_table)
);
global->Set(
v8::String::NewFromUtf8(isolate, "Call", v8::NewStringType::kNormal)
.ToLocalChecked(),
v8::FunctionTemplate::New(isolate, call_function)
);
global->Set(
v8::String::NewFromUtf8(isolate, "NewDB", v8::NewStringType::kNormal)
.ToLocalChecked(),
v8::FunctionTemplate::New(isolate, new_database)
);
global->Set(
v8::String::NewFromUtf8(isolate, "Reply", v8::NewStringType::kNormal)
.ToLocalChecked(),
v8::FunctionTemplate::New(isolate, shredder::reply)
);
global->Set(
v8::String::NewFromUtf8(isolate, "Sha1", v8::NewStringType::kNormal)
.ToLocalChecked(),
v8::FunctionTemplate::New(isolate, shredder::sha1)
);
global->Set(
v8::String::NewFromUtf8(isolate, "Base64Decode", v8::NewStringType::kNormal)
.ToLocalChecked(),
v8::FunctionTemplate::New(isolate, shredder::base64_decode)
);
global->Set(
v8::String::NewFromUtf8(isolate, "ServiceName", v8::NewStringType::kNormal)
.ToLocalChecked(),
String::NewFromUtf8(isolate, service.c_str(), NewStringType::kNormal)
.ToLocalChecked()
);
global->Set(
v8::String::NewFromUtf8(isolate, "MongoGet", v8::NewStringType::kNormal)
.ToLocalChecked(),
v8::FunctionTemplate::New(isolate, shredder::mongo_get)
);
global->Set(
v8::String::NewFromUtf8(isolate, "CoreID", v8::NewStringType::kNormal)
.ToLocalChecked(),
v8::Integer::NewFromUnsigned(isolate, engine().cpu_id())
);
Local<Context> c = Context::New(isolate, NULL, global);
Context::Scope contextScope(c);
Local<String> source;
// Read the JS script file.
if (!read_file(isolate, "services/" + service).ToLocal(&source)) {
fprintf(stderr, "Error reading file\n");
}
Local<Script> s =
Script::Compile(c, source).ToLocalChecked();
Local<Value> result;
if (!s->Run(c).ToLocal(&result)) {
printf("run script error\n");
}
ctx_map[service] = unallocated_ctx;
contexts[unallocated_ctx].Reset(isolate, c);
unallocated_ctx++;
}
create_db(service);
get_local_sched()->new_service(service);
return make_ready_future<>();
}
void req_service::run_callback(std::string call_id, std::string service, sstring ret) {
v8::Locker locker{isolate};
Isolate::Scope isolate_scope(isolate);
HandleScope handle_scope(isolate);
// Switch to V8 context of this service
Local<Context> context = Local<Context>::New(isolate, contexts[ctx_map[service]]);
Context::Scope context_scope(context);
current_context = &context;
auto key = service + call_id + "cb";
auto states = (struct callback_states*)get_local_sched()->get_req_states(key);
Local<Function> callback = Local<Function>::New(isolate, states->callback);
const int argc = 2;
Local<Value> argv[argc];
argv[0] = v8::Null(isolate);
argv[1] = String::NewFromUtf8(isolate, ret.c_str(), NewStringType::kNormal)
.ToLocalChecked();;
Local<Value> result;
if (!callback->Call(context, context->Global(), argc, argv).ToLocal(&result)) {
} else {
}
get_local_sched()->del_req_states(key);
}
void req_service::run_func(std::string req_id, std::string call_id, std::string service, std::string function, std::string jsargs, struct func_states* function_states) {
v8::Locker locker{isolate};
Isolate::Scope isolate_scope(isolate);
HandleScope handle_scope(isolate);
// Switch to V8 context of this service
Local<Context> context = Local<Context>::New(isolate, contexts[ctx_map[service]]);
Context::Scope context_scope(context);
current_context = &context;
Local<Function> process_fun;
Local<String> process_name = String::NewFromUtf8(isolate, function.c_str(), NewStringType::kNormal)
.ToLocalChecked();
Local<Value> process_val;
if (!context->Global()->Get(context, process_name).ToLocal(&process_val) ||
!process_val->IsFunction()) {
printf("get function fail\n");
}
process_fun = Local<Function>::Cast(process_val);
Local<Value> result;
const int argc = 3;
Local<Value> argv[argc];
argv[0] = String::NewFromUtf8(isolate, req_id.c_str(), NewStringType::kNormal)
.ToLocalChecked();
argv[1] = String::NewFromUtf8(isolate, call_id.c_str(), NewStringType::kNormal)
.ToLocalChecked();
argv[2] = String::NewFromUtf8(isolate, jsargs.c_str(), NewStringType::kNormal)
.ToLocalChecked();
auto start_time = rdtsc();
if (!process_fun->Call(context, context->Global(), argc, argv).ToLocal(&result)) {
auto cstr = "error\n";
get_local_sched()->reply(req_id, call_id, service, to_sstring(cstr));
} else {
}
//function_states->mu->lock();
auto count = function_states->count->load() + 1;
function_states->count->store(count);
auto now = rdtsc();
auto total = function_states->total_exec_time->load() + (now - start_time);
function_states->total_exec_time->store(total);
function_states->exec_time->store(total/count);
if (count >= 100) {
function_states->count->store(0);
function_states->total_exec_time->store(0);
}
//function_states->mu->unlock();
// cout << service << function << " " << function_states->exec_time << endl;
}
enum AllocationSpace {
NEW_SPACE, // Semispaces collected with copying collector.
OLD_SPACE, // May contain pointers to new space.
CODE_SPACE, // No pointers to new space, marked executable.
MAP_SPACE, // Only and all map objects.
LO_SPACE, // Promoted large objects.
FIRST_SPACE = NEW_SPACE,
LAST_SPACE = LO_SPACE,
FIRST_PAGED_SPACE = OLD_SPACE,
LAST_PAGED_SPACE = MAP_SPACE
};
// The JS thread. Keep this thread although it's doing nothing, because
// performance is better with this thread around, maybe because it keeps
// some V8 states from garbage collectioned or something.
future<> req_service::js() {
async([this]
{
v8::Locker locker{isolate};
Isolate::Scope isolate_scope(isolate);
while (true) {
HandleScope handle_scope(isolate);
sem.wait(1).get();
}
});
return make_ready_future<>();
}
namespace shredder {
// C++ binding for JS functions to get data from hashtable
void db_get(const v8::FunctionCallbackInfo<v8::Value>& args) {
auto ctx = args.GetIsolate()->GetCurrentContext();
v8::String::Utf8Value str(args.GetIsolate(), args[0]);
auto name = std::string(*str);
auto db = get_db(name);
uint32_t key;
if (args[1]->IsString()) {
v8::String::Utf8Value s(args.GetIsolate(), args[1]);
auto k = std::string(*s);
std::hash<std::string> hasher;
auto hashed = hasher(k);
key = static_cast<int>(hashed % numeric_limits<uint32_t>::max());
} else {
key = args[1]->Uint32Value(ctx).ToChecked();
}
db_val* val = db->ht_get(key);
if (!val) {
val = (db_val*)malloc(sizeof(db_val));
val->data = NULL;
val->length = 0;
}
Local<v8::ArrayBuffer> ab = v8::ArrayBuffer::New(args.GetIsolate(), val->data, val->length);
args.GetReturnValue().Set(ab);
free(val);
}
// C++ binding for JS functions to set data to hashtable
void db_set(const v8::FunctionCallbackInfo<v8::Value>& args) {
auto ctx = args.GetIsolate()->GetCurrentContext();
v8::String::Utf8Value str(args.GetIsolate(), args[0]);
auto name = std::string(*str);
auto db = get_db(name);
uint32_t key;
if (args[1]->IsString()) {
v8::String::Utf8Value s(args.GetIsolate(), args[1]);
auto k = std::string(*s);
std::hash<std::string> hasher;
auto hashed = hasher(k);
key = static_cast<int>(hashed % numeric_limits<uint32_t>::max());
} else {
key = args[1]->Uint32Value(ctx).ToChecked();
}
auto content = args[2].As<v8::ArrayBuffer>()->Externalize();
auto version = args[3]->Uint32Value(ctx).ToChecked();
db_val* val = (db_val*)malloc(sizeof(db_val));
auto tmp = malloc(content.ByteLength() + sizeof(int));
auto p = (int*)tmp;
*p = rand();
p++;
memcpy(p, content.Data(), content.ByteLength());
val->data = tmp;
val->length = content.ByteLength() + sizeof(int);
val->key = key;
free(content.Data());
if (db->ht_set(val, version)) {
args.GetReturnValue().Set(
v8::String::NewFromUtf8(args.GetIsolate(), "ok",
v8::NewStringType::kNormal).ToLocalChecked());
} else {
args.GetReturnValue().Set(
v8::String::NewFromUtf8(args.GetIsolate(), "abort",
v8::NewStringType::kNormal).ToLocalChecked());
}
}
void db_del(const v8::FunctionCallbackInfo<v8::Value>& args) {
}
// C++ binding for JS functions to print messages
void js_print(const v8::FunctionCallbackInfo<v8::Value>& args) {
Isolate * isolate = args.GetIsolate();
HandleScope handle_scope(isolate);
v8::String::Utf8Value str(args.GetIsolate(), args[0]);
const char* cstr = ToCString(str);
std::cout << cstr << '\n';
}
// C++ binding for JS functions to get hashtable
void get_hash_table(const v8::FunctionCallbackInfo<v8::Value>& args) {
Isolate * isolate = args.GetIsolate();
HandleScope handle_scope(isolate);
v8::String::Utf8Value str(args.GetIsolate(), args[0]);
auto name = std::string(*str);
auto db = get_db(name);
auto table = db->get_table_direct();
Local<v8::ArrayBuffer> ab = v8::ArrayBuffer::New(args.GetIsolate(), table, 1024*1024);
args.GetReturnValue().Set(ab);
}
void call_function(const v8::FunctionCallbackInfo<v8::Value>& args) {
Isolate * isolate = args.GetIsolate();
HandleScope handle_scope(isolate);
auto context = args.GetIsolate()->GetCurrentContext();
v8::String::Utf8Value num1(isolate, args[0]);
auto req_id = std::string(*num1);
v8::String::Utf8Value num2(isolate, args[1]);
auto caller = std::string(*num2);
v8::String::Utf8Value str1(args.GetIsolate(), args[2]);
auto service = std::string(*str1);
v8::String::Utf8Value str2(args.GetIsolate(), args[3]);
auto function = std::string(*str2);
Local<Function> callback = Local<Function>::Cast(args[4]);
v8::String::Utf8Value str3(args.GetIsolate(), args[5]);
auto jsargs = std::string(*str3);
auto states = new callback_states;
auto gen = random_generator();
auto callee = to_string(gen());
states->callback.Reset(isolate, callback);
auto key = service + callee + "cb";
get_local_sched()->set_req_states(key, (void*)states);
auto cpu = engine().cpu_id();
get_local_sched()->schedule(cpu, req_id, callee, service, function, jsargs);
}
void new_database(const v8::FunctionCallbackInfo<v8::Value>& args) {
Isolate * isolate = args.GetIsolate();
HandleScope handle_scope(isolate);
v8::String::Utf8Value str(isolate, args[0]);
auto ret = create_db(std::string(*str));
args.GetReturnValue().Set(v8::Boolean::New(isolate, ret));
}
void reply(const v8::FunctionCallbackInfo<v8::Value>& args) {
Isolate * isolate = args.GetIsolate();
HandleScope handle_scope(isolate);
v8::String::Utf8Value num1(isolate, args[0]);
auto req_id = std::string(*num1);
v8::String::Utf8Value num2(isolate, args[1]);
auto call_id = std::string(*num2);
v8::String::Utf8Value str1(isolate, args[2]);
auto service = std::string(*str1);
v8::String::Utf8Value str2(isolate, args[3]);
auto ret = std::string(*str2);
get_local_sched()->reply(req_id, call_id, service, ret);
}
void sha1(const v8::FunctionCallbackInfo<v8::Value>& args) {
Isolate * isolate = args.GetIsolate();
HandleScope handle_scope(isolate);
v8::String::Utf8Value arg(isolate, args[0]);
auto str = std::string(*arg);
boost::uuids::detail::sha1 s;
char hash[41] = {0};
s.process_bytes(str.c_str(), str.size());
unsigned int digest[5];
s.get_digest(digest);
for (int i = 0; i < 5; i++)
{
std::sprintf(hash + (i << 3), "%08x", digest[i]);
}
args.GetReturnValue().Set(
v8::String::NewFromUtf8(args.GetIsolate(), hash,
v8::NewStringType::kNormal).ToLocalChecked());
}
void base64_decode(const v8::FunctionCallbackInfo<v8::Value>& args) {
Isolate * isolate = args.GetIsolate();
HandleScope handle_scope(isolate);
v8::String::Utf8Value arg(isolate, args[0]);
auto tmp = std::string(*arg);
std::string dest;
dest.resize(boost::beast::detail::base64::decoded_size(tmp.size()));
boost::beast::detail::base64::decode(&dest[0], tmp.c_str(), tmp.size());
args.GetReturnValue().Set(
v8::String::NewFromUtf8(args.GetIsolate(), dest.c_str(),
v8::NewStringType::kNormal).ToLocalChecked());
}
void mongo_get(const v8::FunctionCallbackInfo<v8::Value>& args) {
Isolate * isolate = args.GetIsolate();
HandleScope handle_scope(isolate);
v8::String::Utf8Value arg0(isolate, args[0]);
auto db = std::string(*arg0);
v8::String::Utf8Value arg1(isolate, args[1]);
auto collection = std::string(*arg1);
v8::String::Utf8Value arg2(isolate, args[2]);
auto username= std::string(*arg2);
auto cli = pool->acquire();
auto col = cli->database(db)[collection];
bsoncxx::stdx::optional<bsoncxx::document::value> maybe_result =
col.find_one(bsoncxx::builder::stream::document{}
<< "username"
<< username
<< bsoncxx::builder::stream::finalize);
args.GetReturnValue().Set(
v8::String::NewFromUtf8(args.GetIsolate(), bsoncxx::to_json(*maybe_result).c_str(),
v8::NewStringType::kNormal).ToLocalChecked());
}
}