forked from Netflix/dynomite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdynomite.c
630 lines (514 loc) · 15.3 KB
/
dynomite.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
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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
/*
* Dynomite - A thin, distributed replication layer for multi non-distributed
* storages. Copyright (C) 2014 Netflix, Inc.
*/
/*
* twemproxy - A fast and lightweight proxy for memcached protocol.
* Copyright (C) 2011 Twitter, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <fcntl.h>
#include <getopt.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/resource.h>
#include <sys/stat.h>
#include <sys/utsname.h>
#include <unistd.h>
#include "dyn_asciilogo.h"
#include "dyn_conf.h"
#include "dyn_core.h"
#include "dyn_signal.h"
#include "proto/dyn_proto.h"
#if defined(SYSCONFDIR)
#define DN_CONF_PATH SYSCONFDIR "/dynomite.yml"
#else
#define DN_CONF_PATH "conf/dynomite.yml"
#endif
#define DN_LOG_DEFAULT LOG_NOTICE
#define DN_LOG_MIN LOG_EMERG
#define DN_LOG_MAX LOG_PVERB
#define DN_LOG_PATH NULL
#define DN_ENTROPY_PORT ENTROPY_PORT
#define DN_ENTROPY_ADDR ENTROPY_ADDR
#define DN_PID_FILE NULL
#define DN_MBUF_SIZE MBUF_SIZE
#define DN_MBUF_MIN_SIZE MBUF_MIN_SIZE
#define DN_MBUF_MAX_SIZE MBUF_MAX_SIZE
#define DN_ALLOC_MSGS ALLOC_MSGS
#define DN_MIN_ALLOC_MSGS MIN_ALLOC_MSGS
#define DN_MAX_ALLOC_MSGS MAX_ALLOC_MSGS
static int show_help;
static int show_version;
static int test_conf;
static int daemonize;
static int describe_stats;
static struct option long_options[] = {
{"help", no_argument, NULL, 'h'},
{"version", no_argument, NULL, 'V'},
{"test-conf", no_argument, NULL, 't'},
{"daemonize", no_argument, NULL, 'd'},
{"describe-stats", no_argument, NULL, 'D'},
{"verbosity", required_argument, NULL, 'v'},
{"output", required_argument, NULL, 'o'},
{"conf-file", required_argument, NULL, 'c'},
{"pid-file", required_argument, NULL, 'p'},
{"mbuf-size", required_argument, NULL, 'm'},
{"max-msgs", required_argument, NULL, 'M'},
{"admin-operation", required_argument, NULL, 'x'},
{"admin-param", required_argument, NULL, 'y'},
{NULL, 0, NULL, 0}};
static char short_options[] = "hVtdDv:o:c:s:i:a:p:m:M:x:y";
/**
* Daemonize dynomite and redirect stdin, stdout and stderr to /dev/null.
* @param[in] dump_core If set to 0 then dynomite tries to chdir to /.
* @return rstatus_t Return status code.
*/
static rstatus_t dn_daemonize(int dump_core) {
rstatus_t status;
pid_t pid, sid;
int fd;
pid = fork();
switch (pid) {
case -1:
log_error("fork() failed: %s", strerror(errno));
return DN_ERROR;
case 0:
break;
default:
/* parent terminates */
_exit(0);
}
/* 1st child continues and becomes the session leader */
sid = setsid();
if (sid < 0) {
log_error("setsid() failed: %s", strerror(errno));
return DN_ERROR;
}
if (signal(SIGHUP, SIG_IGN) == SIG_ERR) {
log_error("signal(SIGHUP, SIG_IGN) failed: %s", strerror(errno));
return DN_ERROR;
}
pid = fork();
switch (pid) {
case -1:
log_error("fork() failed: %s", strerror(errno));
return DN_ERROR;
case 0:
break;
default:
/* 1st child terminates */
_exit(0);
}
/* 2nd child continues */
/* change working directory */
if (dump_core == 0) {
status = chdir("/");
if (status < 0) {
log_error("chdir(\"/\") failed: %s", strerror(errno));
return DN_ERROR;
}
}
/* clear file mode creation mask */
umask(0);
/* redirect stdin, stdout and stderr to "/dev/null" */
fd = open("/dev/null", O_RDWR);
if (fd < 0) {
log_error("open(\"/dev/null\") failed: %s", strerror(errno));
return DN_ERROR;
}
status = dup2(fd, STDIN_FILENO);
if (status < 0) {
log_error("dup2(%d, STDIN) failed: %s", fd, strerror(errno));
close(fd);
return DN_ERROR;
}
status = dup2(fd, STDOUT_FILENO);
if (status < 0) {
log_error("dup2(%d, STDOUT) failed: %s", fd, strerror(errno));
close(fd);
return DN_ERROR;
}
status = dup2(fd, STDERR_FILENO);
if (status < 0) {
log_error("dup2(%d, STDERR) failed: %s", fd, strerror(errno));
close(fd);
return DN_ERROR;
}
if (fd > STDERR_FILENO) {
status = close(fd);
if (status < 0) {
log_error("close(%d) failed: %s", fd, strerror(errno));
return DN_ERROR;
}
}
return DN_OK;
}
/**
* Print start messages.
* @param[in] nci Dynomite instance
*/
static void dn_print_run(struct instance *nci) {
int status;
struct utsname name;
status = uname(&name);
if (status < 0) {
loga("dynomite-%s started on pid %d", VERSION, nci->pid);
} else {
loga("dynomite-%s built for %s %s %s started on pid %d", VERSION,
name.sysname, name.release, name.machine, nci->pid);
}
loga(
"run, rabbit run / dig that hole, forget the sun / "
"and when at last the work is done / don't sit down / "
"it's time to dig another one");
loga("%s", ascii_logo);
}
static void dn_print_done(void) { loga("done, rabbit done"); }
static void dn_show_usage(void) {
log_stderr(
"Usage: dynomite [-?hVdDt] [-v verbosity level] [-o output file]" CRLF
" [-c conf file] [-p pid file] [-m mbuf size "
"(deprecated)]" CRLF
" [-M max alloc messages (deprecated)]" CRLF "");
log_stderr("Options:" CRLF " -h, --help : this help" CRLF
" -V, --version : show version and exit" CRLF
" -t, --test-conf : test configuration for syntax errors "
"and exit" CRLF " -d, --daemonize : run as a daemon" CRLF
" -D, --describe-stats : print stats description and exit");
log_stderr(
" -v, --verbosity=N : set logging level (default: %d, min: "
"%d, max: %d)" CRLF
" -o, --output=S : set logging file (default: %s)" CRLF
" -c, --conf-file=S : set configuration file (default: "
"%s)" CRLF
" -p, --pid-file=S : set pid file (default: %s)" CRLF
" -m, --mbuf-size=N : set size of mbuf chunk in bytes "
"(default: %d bytes)" CRLF "",
DN_LOG_DEFAULT, DN_LOG_MIN, DN_LOG_MAX,
DN_LOG_PATH != NULL ? DN_LOG_PATH : "stderr", DN_CONF_PATH,
DN_PID_FILE != NULL ? DN_PID_FILE : "off", 0);
}
static rstatus_t dn_create_pidfile(struct instance *nci) {
char pid[DN_UINTMAX_MAXLEN];
int fd, pid_len;
ssize_t n;
fd = open(nci->pid_filename, O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (fd < 0) {
log_error("opening pid file '%s' failed: %s", nci->pid_filename,
strerror(errno));
return DN_ERROR;
}
nci->pidfile = 1;
pid_len = dn_snprintf(pid, DN_UINTMAX_MAXLEN, "%d", nci->pid);
n = dn_write(fd, pid, pid_len);
if (n < 0) {
log_error("write to pid file '%s' failed: %s", nci->pid_filename,
strerror(errno));
return DN_ERROR;
}
close(fd);
return DN_OK;
}
static void dn_remove_pidfile(struct instance *nci) {
int status;
status = unlink(nci->pid_filename);
if (status < 0) {
log_error("unlink of pid file '%s' failed, ignored: %s", nci->pid_filename,
strerror(errno));
}
}
/**
* Set the dynomite instance properties to the default values, except the
* hostname which is set via gethostname().
* @param nci dynomite instance
*/
static void dn_set_default_options(struct instance *nci) {
int status;
nci->ctx = NULL;
nci->log_level = DN_LOG_DEFAULT;
nci->log_filename = DN_LOG_PATH;
nci->conf_filename = DN_CONF_PATH;
nci->entropy_port = DN_ENTROPY_PORT;
nci->entropy_addr = DN_ENTROPY_ADDR;
status = dn_gethostname(nci->hostname, DN_MAXHOSTNAMELEN);
if (status < 0) {
log_warn("gethostname failed, ignored: %s", strerror(errno));
dn_snprintf(nci->hostname, DN_MAXHOSTNAMELEN, "unknown");
}
nci->hostname[DN_MAXHOSTNAMELEN - 1] = '\0';
nci->mbuf_chunk_size = DN_MBUF_SIZE;
nci->alloc_msgs_max = DN_ALLOC_MSGS;
nci->pid = (pid_t)-1;
nci->pid_filename = NULL;
nci->pidfile = 0;
}
/**
* Parse the command line options.
* @param argc argument count
* @param argv argument values
* @param nci dynomite instance
* @return return status
*/
static rstatus_t dn_get_options(int argc, char **argv, struct instance *nci) {
int c, value;
opterr = 0;
c = getopt_long(argc, argv, short_options, long_options, NULL);
while (c != -1) {
switch (c) {
case 'h':
show_version = 1;
show_help = 1;
break;
case 'V':
show_version = 1;
break;
case 't':
test_conf = 1;
nci->log_level = 11;
break;
case 'd':
daemonize = 1;
break;
case 'D':
describe_stats = 1;
show_version = 1;
break;
case 'v':
value = dn_atoi(optarg, strlen(optarg));
if (value < 0) {
log_stderr("dynomite: option -v requires a number");
return DN_ERROR;
}
nci->log_level = value;
break;
case 'o':
nci->log_filename = optarg;
break;
case 'c':
nci->conf_filename = optarg;
break;
case 'p':
nci->pid_filename = optarg;
break;
case 'm': // deprecated argument
loga(
"-m or --mbuf-size command line arguments has been deprecated. Use "
"configuration file.");
value = dn_atoi(optarg, strlen(optarg));
if (value <= 0) {
log_stderr("dynomite: option -m requires a non-zero number");
return DN_ERROR;
}
if (value < DN_MBUF_MIN_SIZE || value > DN_MBUF_MAX_SIZE) {
log_stderr(
"dynomite: mbuf chunk size must be between %zu and"
" %zu bytes",
DN_MBUF_MIN_SIZE, DN_MBUF_MAX_SIZE);
return DN_ERROR;
}
if ((value / 16) * 16 != value) {
log_stderr("dynomite: mbuf chunk size must be a multiple of 16");
return DN_ERROR;
}
nci->mbuf_chunk_size = (size_t)value;
break;
case 'M': // deprecated argument
loga(
"-M or max-msgs command line argument has been deprecated. Use "
"configuration file.");
value = dn_atoi(optarg, strlen(optarg));
if (value <= 0) {
log_stderr("dynomite: option -M requires a non-zero number");
return DN_ERROR;
}
if (value < DN_MIN_ALLOC_MSGS || value > DN_MAX_ALLOC_MSGS) {
log_stderr(
"dynomite: max allocated messages buffer must be between %zu and"
" %zu messages",
DN_MIN_ALLOC_MSGS, DN_MAX_ALLOC_MSGS);
return DN_ERROR;
}
nci->alloc_msgs_max = (size_t)value;
break;
case 'x':
value = dn_atoi(optarg, strlen(optarg));
if (value <= 0) {
log_stderr("dynomite: option -x requires a non-zero positive number");
return DN_ERROR;
}
admin_opt = (uint32_t)value;
break;
case '?':
switch (optopt) {
case 'o':
case 'c':
case 'p':
log_stderr("dynomite: option -%c requires a file name", optopt);
break;
case 'm':
case 'M':
case 'v':
case 's':
case 'i':
log_stderr("dynomite: option -%c requires a number", optopt);
break;
case 'a':
log_stderr("dynomite: option -%c requires a string", optopt);
break;
default:
log_stderr("dynomite: invalid option -- '%c'", optopt);
break;
}
return DN_ERROR;
default:
log_stderr("dynomite: invalid option -- '%c'", optopt);
return DN_ERROR;
}
c = getopt_long(argc, argv, short_options, long_options, NULL);
}
return DN_OK;
}
/**
* Test the dynomite.yml configuration file's syntax.
* @param[in] nci Dynomite instance
* @return bool true if the configuration file has a valid syntax or false if
* syntax is invalid
*/
static bool dn_test_conf(struct instance *nci) {
struct conf *cf;
cf = conf_create(nci->conf_filename);
if (cf == NULL) {
log_stderr("dynomite: configuration file '%s' syntax is invalid",
nci->conf_filename);
return false;
}
conf_destroy(cf);
log_stderr("dynomite: configuration file '%s' syntax is valid",
nci->conf_filename);
return true;
}
/**
* Final setup before running Dynomite. Initialize logging, daemonize dynomite,
* get the PID, and initialize POSIX signal handling.
* @param[in] nci Dynomite instance.
* @return rstatus_t Return status code.
*/
static rstatus_t dn_pre_run(struct instance *nci) {
rstatus_t status;
status = log_init(nci->log_level, nci->log_filename);
if (status != DN_OK) {
return status;
}
if (daemonize) {
status = dn_daemonize(1);
if (status != DN_OK) {
return status;
}
}
nci->pid = getpid();
status = signal_init();
if (status != DN_OK) {
return status;
}
if (nci->pid_filename) {
status = dn_create_pidfile(nci);
if (status != DN_OK) {
return status;
}
}
dn_print_run(nci);
return DN_OK;
}
/**
* Cleanup when shutting down Dynomite. Delete the PID, print a done message,
* and close the logging file descriptor.
* @param[in] nci Dynomite instance.
*/
static void dn_post_run(struct instance *nci) {
if (nci->pidfile) {
dn_remove_pidfile(nci);
}
signal_deinit();
dn_print_done();
log_deinit();
}
/**
* Call method to initialize buffers, messages and connections. Then start the
* core dynomite loop to process messages. When dynomite is shutting down, call
* method to deinitialize buffers, messages and connections.
* @param[in] nci Dynomite instance.
* @return rstatus_t Return status code.
*/
static rstatus_t dn_run(struct instance *nci) {
rstatus_t status;
THROW_STATUS(core_start(nci));
struct context *ctx = nci->ctx;
struct server_pool *sp = &ctx->pool;
if (!sp->enable_gossip) core_set_local_state(ctx, NORMAL);
/* run rabbit run */
for (;;) {
status = core_loop(ctx);
if (status != DN_OK) {
break;
}
}
core_stop(ctx);
return DN_OK;
}
/**
* Set unlimited core dump resource limits.
*/
static void dn_coredump_init(void) {
struct rlimit core_limits;
core_limits.rlim_cur = core_limits.rlim_max = RLIM_INFINITY;
setrlimit(RLIMIT_CORE, &core_limits);
}
int main(int argc, char **argv) {
rstatus_t status;
struct instance nci;
dn_coredump_init();
dn_set_default_options(&nci);
status = dn_get_options(argc, argv, &nci);
if (status != DN_OK) {
dn_show_usage();
exit(1);
}
if (show_version) {
log_stderr("This is dynomite-%s" CRLF, VERSION);
if (show_help) {
dn_show_usage();
}
if (describe_stats) {
stats_describe();
}
exit(0);
}
if (test_conf) {
if (!dn_test_conf(&nci)) {
exit(1);
}
exit(0);
}
status = dn_pre_run(&nci);
if (status != DN_OK) {
dn_post_run(&nci);
exit(1);
}
status = dn_run(&nci);
IGNORE_RET_VAL(status);
dn_post_run(&nci);
exit(1);
}