-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathreq_server.cc
346 lines (294 loc) · 11.1 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
#include "include/seastarkv.hh"
#include "include/req_server.hh"
#include "include/net_server.hh"
#include "include/db.hh"
#include "v8/src/runtime/runtime.h"
#include <iostream>
#include <cstdio>
#include <condition_variable>
#include <thread>
#include <fstream>
#include <chrono>
#include <string>
using namespace std;
using namespace seastar;
using namespace v8;
using namespace std::chrono;
distributed<req_service> req_server;
static const char* ToCString(const v8::String::Utf8Value& value) {
return *value ? *value : "<string conversion failed>";
}
future<> req_service::start() {
return make_ready_future<>();
}
future<> req_service::stop() {
return make_ready_future<>();
}
// C++ version of set()
future<> req_service::set(args_collection& args, output_stream<char>& out, int tid) {
if (args._command_args_count < 2) {
return out.write(msg_syntax_err);
}
sstring& key = args._command_args[0];
sstring& val = args._command_args[1];
db_val* v = (db_val*)malloc(sizeof(db_val));
v->data = (void *)malloc(sizeof(size_t));
*(uint32_t*)(v->data) = atoi(val.c_str());
v->length = 1;
uint32_t k = atoi(key.c_str());
v->key = k;
return get_local_database()->set_direct(k, std::ref(v), tid).then([&out] (auto&& m) {
return out.write(std::move(*m));
});
}
inline uint32_t get_hash(uint32_t key, uint32_t seed) {
uint32_t hash = key;
hash = hash ^ seed;
hash = ~hash + (hash << 15); // hash = (hash << 15) - hash - 1;
hash = hash ^ (hash >> 12);
hash = hash + (hash << 2);
hash = hash ^ (hash >> 4);
hash = hash * 2057; // hash = (hash + (hash << 3)) + (hash << 11);
hash = hash ^ (hash >> 16);
return hash & 0x3fffffff;
}
// C++ version of get()
future<> req_service::get(args_collection& args, output_stream<char>& out) {
if (args._command_args_count < 1) {
cout << "syntax err\n";
return out.write(msg_syntax_err);
}
sstring& key = args._command_args[0];
auto k = atoi(key.c_str());
auto tid = current_tid;
auto db = get_local_database();
db_val* val = db->ht_get(&db->ht[tid], k);
if (!val) {
cout << "not found" << key << "\n";
sstring v = to_sstring(-1);
auto result = reply_builder::build_direct(v, v.size());
return out.write(std::move(result));
}
auto data = (uint32_t*)(val->data);
sstring v = to_sstring(*data);
auto result = reply_builder::build_direct(v, v.size());
return out.write(std::move(result));
}
future<int> req_service::get_tid(void) {
return make_ready_future<int>(current_tid);
}
int req_service::get_tid_direct(void) {
return current_tid;
}
// Run JavaScript function
future<> req_service::js_req(args_collection& args, output_stream<char>& out, int tid) {
v8::Locker locker{isolate};
Isolate::Scope isolate_scope(isolate);
HandleScope handle_scope(isolate);
current_tid = tid;
// Switch to V8 context of this tenant
Local<Context> context = Local<Context>::New(isolate, contexts[current_tid]);
Context::Scope context_scope(context);
current_context = &context;
auto req = make_lw_shared<rqst>(args);
if (req->args._command_args_count < 1) {
sstring tmp = to_sstring(msg_syntax_err);
auto result = reply_builder::build_direct(tmp, tmp.size());
return out.write(std::move(result));
}
sstring& name = req->args._command_args[0];
sstring& key = req->args._command_args[1];
Local<Function> process_fun;
if (prev_fun_name[current_tid] != name) {
Local<String> process_name =
String::NewFromUtf8(isolate, name.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 %s fail\n", name.c_str());
}
process_fun = Local<Function>::Cast(process_val);
prev_fun_name[current_tid] = name;
prev_fun[current_tid].Reset(isolate, process_fun);
} else {
process_fun = Local<Function>::New(isolate, prev_fun[current_tid]);
}
Local<Value> result;
sstring tmp;
const int argc = req->args._command_args_count -1;
Local<Value> argv[argc];
for (int i = 0; i < argc; i++) {
argv[i] = Number::New(isolate, atoi(req->args._command_args[i+1].c_str()));
}
if (!process_fun->Call(context, context->Global(), argc, argv).ToLocal(&result)) {
auto cstr = "error\n";
out.write(cstr, strlen(cstr));
} else {
if (result->IsArrayBuffer()) {
// Return raw data
auto res = Local<ArrayBuffer>::Cast(result);
auto cont = res->GetContents();
auto cstr = (char*)cont.Data();
out.write(cstr, cont.ByteLength());
} else {
// Return data in Redis protocol
v8::String::Utf8Value str(isolate, result);
tmp = to_sstring(ToCString(str));
auto cstr = ToCString(str);
auto res = reply_builder::build_direct(tmp, tmp.size());
out.write(std::move(res));
}
}
return make_ready_future<>();
}
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<>();
}
// C++ binding for JS functions to get data from hashtable
void db_get(const v8::FunctionCallbackInfo<v8::Value>& args) {
db_val ret;
db_val* ret_p = &ret;
uint32_t key = args[0]->Uint32Value(args.GetIsolate()->GetCurrentContext()).ToChecked();
auto db = get_local_database();
auto tid = local_req_server().get_tid_direct();
db_val* val = db->ht_get(&db->ht[tid], key);
if (!val) {
val = (db_val*)malloc(sizeof(db_val));
val->length = 0;
}
Local<v8::ArrayBuffer> ab = v8::ArrayBuffer::New(args.GetIsolate(), val->data, val->length);
args.GetReturnValue().Set(ab);
}
// C++ binding for JS functions to set data to hashtable
void db_set(const v8::FunctionCallbackInfo<v8::Value>& args) {
auto content = args[1].As<v8::ArrayBuffer>()->Externalize();
db_val* val = (db_val*)malloc(sizeof(db_val));
val->data = content.Data();
val->length = content.ByteLength();
uint32_t key = args[0]->Uint32Value(args.GetIsolate()->GetCurrentContext()).ToChecked();
val->key = key;
auto tid = local_req_server().get_tid_direct();
auto db = get_local_database();
if (db->ht[tid].table == NULL)
db->hashtable_init(&db->ht[tid], 1000*1000);
db->ht_set(&db->ht[tid], val);
args.GetReturnValue().Set(
v8::String::NewFromUtf8(args.GetIsolate(), "ok\n",
v8::NewStringType::kNormal).ToLocalChecked());
}
void db_del(const v8::FunctionCallbackInfo<v8::Value>& args) {
v8::String::Utf8Value str(args.GetIsolate(), args[0]);
const char* cstr = ToCString(str);
sstring key = to_sstring(cstr);
sstring& k = key;
redis_key rk{std::ref(k)};
auto cpu = rk.get_cpu();
local_req_server().get_tid().then([&rk, &args, &cpu](auto&& t) {
return get_database().invoke_on(cpu, &database::del_direct, std::move(rk), std::move(t)).then([&args] (auto&& m) {
auto val = std::move(*m).c_str();
args.GetReturnValue().Set(
v8::String::NewFromUtf8(args.GetIsolate(), val,
v8::NewStringType::kNormal).ToLocalChecked());
});
}).get();
}
// 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);
auto tid = local_req_server().get_tid_direct();
auto table = get_local_database()->get_table_direct(tid);
Local<v8::ArrayBuffer> ab = v8::ArrayBuffer::New(args.GetIsolate(), table, 1024*1024);
args.GetReturnValue().Set(ab);
}
//C++ binding to load SNAP ego network into hashtable
void load_fb_graph(const v8::FunctionCallbackInfo<v8::Value>& args) {
string line;
ifstream fb_file("facebook_combined.txt");
if (fb_file) {
uint32_t prev_key = 0;
vector<uint32_t> arr;
while (getline(fb_file, line))
{
string delimiter = " ";
auto pos = line.find(delimiter);
string k = line.substr(0, pos);
line.erase(0, pos + delimiter.length());
uint32_t key = atoi(k.c_str());
uint32_t value = atoi(line.c_str());
if (key == prev_key) {
arr.push_back(value);
continue;
}
db_val* val = (db_val*)malloc(sizeof(db_val));
val->data = malloc(sizeof(uint32_t) * arr.size());
val->length = arr.size() * 4;
val->key = prev_key;
auto cur = (uint32_t*)(val->data);
for (auto it = arr.begin(); it != arr.end(); ++it) {
auto v = *it;
*cur = v;
cur++;
}
auto vk = val->key;
auto tid = local_req_server().get_tid_direct();
auto db = get_local_database();
if (db->ht[tid].table == NULL)
db->hashtable_init(&db->ht[tid], 1000*1000);
db->ht_set(&db->ht[tid], val);
prev_key = key;
arr.clear();
arr.push_back(value);
}
db_val* val = (db_val*)malloc(sizeof(db_val));
val->data = malloc(sizeof(uint32_t) * arr.size());
val->length = arr.size() * 4;
val->key = prev_key;
auto cur = (uint32_t*)(val->data);
for (auto it = arr.begin(); it != arr.end(); ++it) {
auto v = *it;
*cur = v;
cur++;
}
auto vk = val->key;
auto tid = local_req_server().get_tid_direct();
auto db = get_local_database();
if (db->ht[tid].table == NULL)
db->hashtable_init(&db->ht[tid], 1000*1000);
db->ht_set(&db->ht[tid], val);
fb_file.close();
} else
cout << "Open file error.\n";
}