-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumerator.cpp
More file actions
321 lines (264 loc) · 9.85 KB
/
numerator.cpp
File metadata and controls
321 lines (264 loc) · 9.85 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
#include <sys/types.h>
#include <sys/stat.h>
#include <signal.h>
#include <unistd.h>
#include <getopt.h>
#include <iostream>
#include <thrift/concurrency/ThreadManager.h>
#include <thrift/concurrency/PosixThreadFactory.h>
#include <thrift/protocol/TBinaryProtocol.h>
#include <thrift/server/TSimpleServer.h>
#include <thrift/server/TThreadedServer.h>
#include <thrift/server/TThreadPoolServer.h>
#include <thrift/transport/TServerSocket.h>
#include <thrift/transport/TBufferTransports.h>
#include <thrift/transport/TTransportUtils.h>
#include <boost/shared_ptr.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/tuple/tuple.hpp>
#include <boost/thread.hpp>
#include "gen-cpp/Numerator.h"
#include "disk_storage.hpp"
#include "memory_storage.hpp"
#include "logger_initializer.hpp"
#include "signal_handler.hpp"
using namespace apache::thrift;
using namespace apache::thrift::protocol;
using namespace apache::thrift::transport;
using namespace apache::thrift::server;
using namespace apache::thrift::concurrency;
using namespace numerator;
static NumID cnt;
static boost::mutex mutex;
static const unsigned kNumThreadsCount = 10;
static const unsigned kDefaultPort = 9090;
static const int kDefaultCacheSize = 0;
struct NumeratorHandlerOptions {
bool disable_s2i_queries;
NumeratorHandlerOptions():
disable_s2i_queries(false)
{
}
};
class NumeratorHandler: virtual public NumeratorIf {
public:
NumeratorHandler(MemoryStorage &_memory_storage, DiskStorage &_disk_storage, NumeratorHandlerOptions _options):
memory_storage(_memory_storage),
disk_storage(_disk_storage),
options(_options)
{
}
void query(Query& response, const Query& request)
{
try {
query_impl(response, request);
} catch (NumeratorException) {
throw; // propagate this exception to the client
} catch (std::exception &e) {
LOG(ERROR) << e.what();
exit(EXIT_FAILURE);
}
}
void ping() {}
void disable_s2i()
{
if (!options.disable_s2i_queries) {
memory_storage.unload();
options.disable_s2i_queries = true;
}
}
void enable_s2i()
{
if (options.disable_s2i_queries) {
disk_storage.load_in_memory(memory_storage);
options.disable_s2i_queries = false;
}
}
private:
MemoryStorage &memory_storage;
DiskStorage &disk_storage;
NumeratorHandlerOptions options;
void query_impl(Query& response, const Query& request)
{
boost::lock_guard<boost::mutex> guard(mutex);
if (request.op == Operation::ID2STR) {
disk_storage.lookup(request.ids, response.strings, response.failures);
} else if (request.op == Operation::STR2ID) {
// this type of queries was disabled
if (options.disable_s2i_queries) {
NumeratorException exc;
exc.code = ErrorCode::STR2ID_QUERIES_DISABLED;
exc.message = "string -> id service disabled";
throw exc;
}
size_t i;
size_t count = request.strings.size();
KVPairs kv_pairs;
bool inserted;
NumID id;
for (i = 0; i < count; i++) {
const std::string &str = request.strings[i];
boost::tie(inserted, id) = memory_storage.find(str, cnt);
if (inserted) {
kv_pairs.push_back(KVPair(id, str));
}
response.ids.push_back(id);
}
disk_storage.write(kv_pairs);
}
response.op = request.op;
}
};
void
usage(const char *program)
{
std::cerr << "Usage: " << program << " args" << std::endl;
std::cerr << std::endl;
std::cerr << "Arguments:" << std::endl;
std::cerr << " -h,--help write this help message" << std::endl;
std::cerr << " -l,--logs-dir DIR directory where to write logs" << std::endl;
std::cerr << " -d,--data-dir DIR directory where to store data" << std::endl;
std::cerr << " -t,--threads NUM (=10) number of worker threads" << std::endl;
std::cerr << " -p,--port PORT (=9090) port to bind" << std::endl;
std::cerr << " -c,--cache-size SIZE id to string lookups cache size in MB," << std::endl;
std::cerr << " (if not specified - use default cache size," << std::endl;
std::cerr << " if negative value - disable caching)" << std::endl;
std::cerr << " -S,--disable-s2i disable string -> id type requests" << std::endl;
exit(EXIT_SUCCESS);
}
bool
is_dir(std::string path)
{
struct stat st;
memset(&st, 0, sizeof(st));
int rc = stat(path.c_str(), &st);
if (rc != 0) {
if (errno == ENOENT) {
return false;
} else {
std::cerr << "Error: stat() failed: " << strerror(errno) << std::endl;
exit(EXIT_FAILURE);
}
}
return S_ISDIR(st.st_mode) ? true : false;
}
void
thrift_messages_logger(const char *message)
{
LOG(WARNING) << message;
}
int
main(int argc, char **argv)
{
static struct option longopts[] = {
{ "data-dir", required_argument, NULL, 'd' },
{ "logs-dir", required_argument, NULL, 'l' },
{ "threads", required_argument, NULL, 't' },
{ "port", required_argument, NULL, 'p' },
{ "cache-size", required_argument, NULL, 'c' },
{ "disable-s2i", no_argument, NULL, 'S' },
{ "help", no_argument, NULL, 'h' },
{ NULL, 0, NULL, 0 }
};
std::string data_dir;
std::string logs_dir;
unsigned port = kDefaultPort;
unsigned threads = kNumThreadsCount;
int cache_size = kDefaultCacheSize;
bool disable_s2i_queries = false;
int opt, optidx;
while ((opt = getopt_long(argc, argv, "p:d:l:t:c:hS", longopts, &optidx)) != -1) {
switch (opt) {
case 'p':
try {
port = boost::lexical_cast<unsigned>(optarg);
} catch (std::exception &e) {
std::cerr << "Error: invalid argument for -p switch" << std::endl;
exit(EXIT_FAILURE);
}
break;
case 'd':
data_dir = optarg;
break;
case 'l':
logs_dir = optarg;
break;
case 't':
try {
threads = boost::lexical_cast<unsigned>(optarg);
} catch (std::exception &e) {
std::cerr << "Error: invalid argument for -t switch" << std::endl;
exit(EXIT_FAILURE);
}
break;
case 'c':
try {
cache_size = boost::lexical_cast<int>(optarg);
} catch (std::exception &e) {
std::cerr << "Error: invalid argument for -c switch" << std::endl;
exit(EXIT_FAILURE);
}
break;
case 'h':
usage(argv[0]);
break;
case 'S':
disable_s2i_queries = true;
break;
default:
std::cerr << "Error: invalid command line argument." << std::endl;
std::cerr << "Run with -h switch to get help message" << std::endl;
exit(EXIT_FAILURE);
}
}
if (data_dir.empty()) {
std::cerr << "Error: mandatory parameter -d is not specified." << std::endl;
std::cerr << "Run with -h switch to get help message." << std::endl;
exit(EXIT_FAILURE);
}
if (logs_dir.empty()) {
std::cerr << "Error: mandatory parameter -l is not specified." << std::endl;
std::cerr << "Run with -h switch to get help message." << std::endl;
exit(EXIT_FAILURE);
}
if (!is_dir(logs_dir)) {
std::cerr << "Error: " << logs_dir << " is not a directory or does not exist" << std::endl;
exit(EXIT_FAILURE);
}
init_signals();
LoggerInitializer _logger(logs_dir, argv[0], "numerator");
LOG(INFO) << "loading data into memory";
DiskStorage disk_storage;
MemoryStorage memory_storage;
try {
disk_storage.init(data_dir, cache_size);
if (!disable_s2i_queries) {
cnt = disk_storage.load_in_memory(memory_storage);
cnt++;
}
} catch (std::exception &e) {
LOG(ERROR) << e.what();
exit(EXIT_FAILURE);
}
// Route messages from Thrift internals to the log files
GlobalOutput.setOutputFunction(thrift_messages_logger);
NumeratorHandlerOptions options;
if (disable_s2i_queries) {
options.disable_s2i_queries = true;
}
boost::shared_ptr<NumeratorHandler> handler(new NumeratorHandler(memory_storage, disk_storage, options));
boost::shared_ptr<TProcessor> processor(new NumeratorProcessor(handler));
boost::shared_ptr<TServerTransport> server_transport(new TServerSocket(port));
boost::shared_ptr<TTransportFactory> transport_factory(new TBufferedTransportFactory());
boost::shared_ptr<TProtocolFactory> protocol_factory(new TBinaryProtocolFactory());
boost::shared_ptr<ThreadManager> thread_manager = ThreadManager::newSimpleThreadManager(threads);
boost::shared_ptr<PosixThreadFactory> thread_factory = boost::shared_ptr<PosixThreadFactory>(new PosixThreadFactory());
thread_manager->threadFactory(thread_factory);
thread_manager->start();
TThreadPoolServer server(processor, server_transport, transport_factory, protocol_factory, thread_manager);
boost::thread sigwaiter_thread(sigwaiter, &server);
sigwaiter_thread.detach();
server.serve();
LOG(INFO) << "done";
return 0;
}