-
Notifications
You must be signed in to change notification settings - Fork 2
/
bolo-core.c
375 lines (310 loc) · 8.56 KB
/
bolo-core.c
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
#include "bolo.h"
#include "bqip.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <getopt.h>
#include <pthread.h>
#ifndef DEFAULT_CONFIG_FILE
#define DEFAULT_CONFIG_FILE "/etc/bolo.conf"
#endif
static struct core_config cfg;
static struct db *db;
static pthread_mutex_t db_lock;
static struct qlsnr {
int fd;
pthread_t tid;
int nconn;
struct bqip *conn;
struct fdpoll *poll;
} qlsnr;
static void *
qlsnr_thread(void *_u)
{
struct qlsnr *ql;
ql = (struct qlsnr *)_u;
fdpoll(ql->poll);
close(ql->fd);
return NULL;
}
static int
query_handler(int fd, void *_u)
{
int rc;
size_t i;
struct bqip *bqip;
struct query *q;
struct qfield *f;
bqip = (struct bqip *)_u;
rc = bqip_read(bqip);
if (rc < 0) goto fail;
if (rc == 1) /* not quite */
return 0;
switch (bqip->request.type) {
default:
bqip_send_error(bqip, "unrecognized packet type");
break;
case 'Q':
q = query_parse(bqip->request.payload);
if (!q)
goto fail;
pthread_mutex_lock(&db_lock);
rc = query_plan(q, db);
if (rc != 0)
goto fail;
rc = query_exec(q, db, NULL);
if (rc != 0)
goto fail;
pthread_mutex_unlock(&db_lock);
bqip_send0(bqip, "R");
for (f = q->select; f; f = f->next) {
bqip_send0(bqip, "|");
bqip_send0(bqip, f->name);
bqip_send0(bqip, "=");
for (i = 0; i < f->result->len; i++)
bqip_send_tuple(bqip, &f->result->results[i]);
}
break;
case 'P':
q = query_parse(bqip->request.payload);
if (!q)
goto fail;
pthread_mutex_lock(&db_lock);
rc = query_plan(q, db);
if (rc != 0)
goto fail;
pthread_mutex_unlock(&db_lock);
bqip_send0(bqip, "R");
for (f = q->select; f; f = f->next) {
bqip_send0(bqip, "|");
bqip_send0(bqip, f->name);
}
break;
}
bqip->fd = -1;
return -1; /* force-close after every query */
fail:
pthread_mutex_unlock(&db_lock);
bqip->fd = -1;
return -1;
}
static int
query_listener(int _, void *_u)
{
int i, sockfd;
struct qlsnr *ql;
ql = (struct qlsnr *)_u;
sockfd = accept(ql->fd, NULL, NULL);
printf("S: accepted new inbound connection.\n");
for (i = 0; i < ql->nconn; i++) {
if (ql->conn[i].fd >= 0) continue;
bqip_init(&ql->conn[i], sockfd);
if (fdpoll_watch(ql->poll, sockfd, FDPOLL_READ, query_handler, &ql->conn[i]) == 0)
return 0;
fprintf(stderr, "S: failed to register accepted socket; closing...\n");
close(sockfd);
return 0;
}
fprintf(stderr, "S: max connections reached; closing socket...\n");
close(sockfd);
return 0;
}
static struct mlsnr {
int fd;
pthread_t tid;
int nconn;
struct ingestor *conn;
struct fdpoll *poll;
} mlsnr;
static void *
mlsnr_thread(void *_u)
{
struct mlsnr *ml;
ml = (struct mlsnr *)_u;
fdpoll(ml->poll);
close(ml->fd);
return NULL;
}
static int
metric_handler(int fd, void *_u)
{
struct ingestor *in;
int n;
in = (struct ingestor *)_u;
n = ingest_read(in);
if (n < 0)
goto fail;
if (n == 0)
goto done;
pthread_mutex_lock(&db_lock);
while (n-- > 0) {
debugf("ingesting metric submission from fd %d", in->fd);
if (ingest(in) != 0) {
errnof("failed to ingest metric submission from fd %d", in->fd);
goto lockfail;
}
debugf("inserting new measurement [%lu] %s=%e from fd %d",
in->time, in->metric, in->value, in->fd);
if (db_insert(db, in->metric, in->time, in->value) != 0) {
errnof("failed to insert [%lu] %s=%e measurement into database",
in->time, in->metric, in->value);
goto lockfail;
}
}
debugf("syncing database...");
if (db_sync(db) != 0) {
errnof("failed to sync database after updating metric measurements");
goto lockfail;
}
pthread_mutex_unlock(&db_lock);
done:
if (ingest_eof(in)) {
debugf("at eof on ingestor fd %d; shutting down this connection.", in->fd);
close(in->fd);
in->eof = in->fd = -1;
return -1;
}
return 0;
lockfail:
pthread_mutex_unlock(&db_lock);
fail:
in->fd = -1;
return -1;
}
static int
metric_listener(int fd, void *_u)
{
int i, sockfd;
struct mlsnr *ml;
ml = (struct mlsnr *)_u;
sockfd = accept(ml->fd, NULL, NULL);
printf("S: accepted new inbound connection.\n");
for (i = 0; i < ml->nconn; i++) {
if (ml->conn[i].fd >= 0) continue;
ml->conn[i].fd = sockfd;
if (fdpoll_watch(ml->poll, sockfd, FDPOLL_READ, metric_handler, &ml->conn[i]) == 0)
return 0;
fprintf(stderr, "S: failed to register accepted socket; closing...\n");
close(sockfd);
return 0;
}
fprintf(stderr, "S: max connections reached; closing socket...\n");
close(sockfd);
return 0;
}
int
do_core(int argc, char **argv)
{
int i;
struct dbkey *key;
{
char *key_str;
int fd, override_log_level = -1;
char *config_file = strdup(DEFAULT_CONFIG_FILE);
int idx = 0;
char c, *shorts = "hDc:l:";
struct option longs[] = {
{"help", no_argument, 0, 'h'},
{"debug", no_argument, 0, 'D'},
{"config", required_argument, 0, 'c'},
{"log-level", required_argument, 0, 'l'},
{0, 0, 0, 0},
};
key_str = NULL;
while ((c = getopt_long(argc, argv, shorts, longs, &idx)) >= 0) {
switch (c) {
case 'h':
printf("USAGE: %s core [--config /etc/bolo.conf] [--debug] [--log-level error]\n\n", argv[0]);
printf("OPTIONS\n\n");
printf(" -h, --help Show this help screen.\n\n");
printf(" -c, --config FILE Path to a configuration file.\n"
" Defaults to " DEFAULT_CONFIG_FILE ".\n\n");
printf(" -l, --log-level LEVEL Log level. This overrides the value\n"
" from the configuration file.\n"
" Must be one of ERROR, WARNING, or INFO.\n\n");
printf(" -D, --debug Enable debugging mode.\n"
" (mostly useful only to bolo devs).\n\n");
return 0;
case 'D':
debugto(fileno(stderr));
override_log_level = LOG_INFO;
break;
case 'c':
free(config_file);
config_file = strdup(optarg);
break;
case 'l':
if (strcasecmp(optarg, "error") == 0) override_log_level = LOG_ERRORS;
else if (strcasecmp(optarg, "warning") == 0) override_log_level = LOG_WARNINGS;
else if (strcasecmp(optarg, "info") == 0) override_log_level = LOG_INFO;
else fprintf(stderr, "invalid log-level '%s': ignoring...\n", optarg);
break;
}
}
debugf("reading configuration from %s", config_file);
fd = open(config_file, O_RDONLY);
if (fd < 0) {
errnof("unable to open configuration file %s", config_file);
return 1;
}
if (configure(CORE_CONFIG, &cfg, fd) != 0)
return 1;
if (override_log_level != -1)
cfg.log_level = override_log_level;
if (!key_str)
key_str = cfg.db_secret_key;
key = read_key(key_str);
if (!key) {
fprintf(stderr, "invalid database encryption key given\n");
return 1;
}
}
startlog(argv[0], getpid(), cfg.log_level);
infof("mounting database at %s", cfg.db_data_root);
db = db_mount(deslash(cfg.db_data_root), key);
if (!db && (errno == BOLO_ENODBROOT || errno == BOLO_ENOMAINDB)) {
warningf("unable to mount database; doesn't look like %s has been initialized yet", cfg.db_data_root);
infof("initializing database at %s...", cfg.db_data_root);
db = db_init(cfg.db_data_root, key);
if (!db) {
errnof("unable to create new bolo database at %s", cfg.db_data_root);
return 2;
}
}
if (!db) {
errnof("unable to mount bolo database at %s", cfg.db_data_root);
return 2;
}
/* configure query listener */
qlsnr.nconn = cfg.query_max_connections;
qlsnr.conn = xalloc(qlsnr.nconn, sizeof(*qlsnr.conn));
for (i = 0; i < qlsnr.nconn; i++)
qlsnr.conn[i].fd = -1;
qlsnr.poll = fdpoller(qlsnr.nconn + 1);
if (!qlsnr.poll)
bail("network setup failed.");
qlsnr.fd = net_bind(cfg.query_listen, 64);
if (qlsnr.fd < 0)
bail("network bind failed.");
if (fdpoll_watch(qlsnr.poll, qlsnr.fd, FDPOLL_READ, query_listener, &qlsnr) != 0)
bail("network poll failed.");
/* configure metric listener */
mlsnr.nconn = cfg.metric_max_connections;
mlsnr.conn = xalloc(mlsnr.nconn, sizeof(*mlsnr.conn));
for (i = 0; i < mlsnr.nconn; i++)
mlsnr.conn[i].fd = -1;
mlsnr.poll = fdpoller(mlsnr.nconn + 1);
if (!mlsnr.poll)
bail("network setup failed.");
mlsnr.fd = net_bind(cfg.metric_listen, 64);
if (mlsnr.fd < 0)
bail("network bind failed.");
if (fdpoll_watch(mlsnr.poll, mlsnr.fd, FDPOLL_READ, metric_listener, &mlsnr) != 0)
bail("network poll failed.");
/* initialize mutices */
pthread_mutex_init(&db_lock, NULL);
/* spin both threads */
pthread_create(&qlsnr.tid, NULL, qlsnr_thread, &qlsnr);
pthread_create(&mlsnr.tid, NULL, mlsnr_thread, &mlsnr);
pthread_join(qlsnr.tid, NULL);
return 0;
}