-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathttserver.c
3584 lines (3449 loc) · 130 KB
/
ttserver.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
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*************************************************************************************************
* The server of Tokyo Tyrant
* Copyright (C) 2006-2010 FAL Labs
* This file is part of Tokyo Tyrant.
* Tokyo Tyrant is free software; you can redistribute it and/or modify it under the terms of
* the GNU Lesser General Public License as published by the Free Software Foundation; either
* version 2.1 of the License or any later version. Tokyo Tyrant is distributed in the hope
* that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
* You should have received a copy of the GNU Lesser General Public License along with Tokyo
* Tyrant; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307 USA.
*************************************************************************************************/
#include <ttutil.h>
#include <tculog.h>
#include <tcrdb.h>
#include "myconf.h"
#include "scrext.h"
#define DEFTHNUM 8 // default thread number
#define DEFPIDPATH "ttserver.pid" // default name of the PID file
#define DEFRTSPATH "ttserver.rts" // default name of the RTS file
#define DEFULIMSIZ (1LL<<30) // default limit size of an update log file
#define MAXARGSIZ (256<<20) // maximum size of each argument
#define MAXARGNUM (1<<20) // maximum number of arguments
#define NUMBUFSIZ 32 // size of a numeric buffer
#define LINEBUFSIZ 8192 // size of a line buffer
#define TOKENUNIT 256 // unit number of tokens
#define RECMTXNUM 31 // number of mutexes of records
#define STASHBNUM 1021 // bucket number of the script stash object
#define REPLPERIOD 1.0 // period of calling replication request
enum { // enumeration for command sequential numbers
TTSEQPUT, // sequential number of put command
TTSEQPUTKEEP, // sequential number of putkeep command
TTSEQPUTCAT, // sequential number of putcat command
TTSEQPUTSHL, // sequential number of putshl command
TTSEQPUTNR, // sequential number of putnr command
TTSEQOUT, // sequential number of out command
TTSEQGET, // sequential number of get command
TTSEQMGET, // sequential number of mget command
TTSEQVSIZ, // sequential number of vsiz command
TTSEQITERINIT, // sequential number of iterinit command
TTSEQITERNEXT, // sequential number of iternext command
TTSEQFWMKEYS, // sequential number of fwmkeys command
TTSEQADDINT, // sequential number of addint command
TTSEQADDDOUBLE, // sequential number of adddouble command
TTSEQEXT, // sequential number of ext command
TTSEQSYNC, // sequential number of sync command
TTSEQOPTIMIZE, // sequential number of sync command
TTSEQVANISH, // sequential number of vanish command
TTSEQCOPY, // sequential number of copy command
TTSEQRESTORE, // sequential number of restore command
TTSEQSETMST, // sequential number of setmst command
TTSEQRNUM, // sequential number of rnum command
TTSEQSIZE, // sequential number of size command
TTSEQSTAT, // sequential number of stat command
TTSEQMISC, // sequential number of stat command
TTSEQREPL, // sequential number of repl command
TTSEQSLAVE, // sequential number of slave command
TTSEQALLORG, // sequential number of all commands the original
TTSEQALLMC, // sequential number of all commands the memcached
TTSEQALLHTTP, // sequential number of all commands the HTTP
TTSEQALLREAD, // sequential number of all commands of reading
TTSEQALLWRITE, // sequential number of all commands of writing
TTSEQALLMANAGE // sequential number of all commands of managing
};
enum { // enumeration for command sequential numbers
TTSEQPUTMISS = TTSEQSLAVE, // sequential number of misses of get commands
TTSEQOUTMISS, // sequential number of misses of out commands
TTSEQGETMISS, // sequential number of misses of get commands
TTSEQNUM // number of sequential numbers
};
typedef struct { // type of structure of logging opaque object
int fd;
} LOGARG;
typedef struct { // type of structure of master synchronous object
char host[TTADDRBUFSIZ]; // host name
int port; // port number
const char *rtspath; // path of the replication time stamp file
uint64_t rts; // replication time stamp
int opts; // options
TCADB *adb; // database object
TCULOG *ulog; // update log object
uint32_t sid; // server ID number
bool fail; // failure flag
bool recon; // re-connect flag
bool fatal; // fatal error flag
uint64_t mts; // modified time stamp
} REPLARG;
typedef struct { // type of structure of periodic opaque object
const char *name; // function name
TCADB *adb; // database object
TCULOG *ulog; // update log object
uint32_t sid; // server ID number
REPLARG *sarg; // replication object
void *scrext; // script extension object
} EXTPCARG;
typedef struct { // type of structure of task opaque object
int thnum; // number of threads
uint64_t *counts; // conunters of execution
uint64_t mask; // bit mask of commands
TCADB *adb; // database object
TCULOG *ulog; // update log object
uint32_t sid; // server ID number
REPLARG *sarg; // replication object
pthread_mutex_t rmtxs[RECMTXNUM]; // mutex for records
void **screxts; // script extension objects
} TASKARG;
typedef struct { // type of structure of termination opaque object
int thnum; // number of threads
TCADB *adb; // database object
REPLARG *sarg; // replication object
void **screxts; // script extension objects
EXTPCARG *pcargs; // periodic opaque objects
int pcnum; // number of periodic opaque objects
bool err; // error flag
} TERMARG;
/* global variables */
const char *g_progname = NULL; // program name
double g_starttime = 0.0; // start time
TTSERV *g_serv = NULL; // server object
int g_loglevel = TTLOGINFO; // whether to log debug information
bool g_restart = false; // restart flag
/* function prototypes */
int main(int argc, char **argv);
static void usage(void);
static uint64_t getcmdmask(const char *expr);
static void sigtermhandler(int signum);
static void sigchldhandler(int signum);
static int proc(const char *dbname, const char *host, int port, int thnum, int tout,
bool dmn, const char *pidpath, bool kl, const char *logpath,
const char *ulogpath, uint64_t ulim, bool uas, uint32_t sid,
const char *mhost, int mport, const char *rtspath, int ropts,
const char *skelpath, int mulnum, const char *extpath, const TCLIST *extpcs,
uint64_t mask);
static void do_log(int level, const char *msg, void *opq);
static void do_slave(void *opq);
static void do_extpc(void *opq);
static void do_task(TTSOCK *sock, void *opq, TTREQ *req);
static char **tokenize(char *str, int *np);
static uint32_t recmtxidx(const char *kbuf, int ksiz);
static uint64_t sumstat(TASKARG *arg, int seq);
static void do_put(TTSOCK *sock, TASKARG *arg, TTREQ *req);
static void do_putkeep(TTSOCK *sock, TASKARG *arg, TTREQ *req);
static void do_putcat(TTSOCK *sock, TASKARG *arg, TTREQ *req);
static void do_putshl(TTSOCK *sock, TASKARG *arg, TTREQ *req);
static void do_putnr(TTSOCK *sock, TASKARG *arg, TTREQ *req);
static void do_out(TTSOCK *sock, TASKARG *arg, TTREQ *req);
static void do_get(TTSOCK *sock, TASKARG *arg, TTREQ *req);
static void do_mget(TTSOCK *sock, TASKARG *arg, TTREQ *req);
static void do_vsiz(TTSOCK *sock, TASKARG *arg, TTREQ *req);
static void do_iterinit(TTSOCK *sock, TASKARG *arg, TTREQ *req);
static void do_iternext(TTSOCK *sock, TASKARG *arg, TTREQ *req);
static void do_fwmkeys(TTSOCK *sock, TASKARG *arg, TTREQ *req);
static void do_addint(TTSOCK *sock, TASKARG *arg, TTREQ *req);
static void do_adddouble(TTSOCK *sock, TASKARG *arg, TTREQ *req);
static void do_ext(TTSOCK *sock, TASKARG *arg, TTREQ *req);
static void do_sync(TTSOCK *sock, TASKARG *arg, TTREQ *req);
static void do_optimize(TTSOCK *sock, TASKARG *arg, TTREQ *req);
static void do_vanish(TTSOCK *sock, TASKARG *arg, TTREQ *req);
static void do_copy(TTSOCK *sock, TASKARG *arg, TTREQ *req);
static void do_restore(TTSOCK *sock, TASKARG *arg, TTREQ *req);
static void do_setmst(TTSOCK *sock, TASKARG *arg, TTREQ *req);
static void do_rnum(TTSOCK *sock, TASKARG *arg, TTREQ *req);
static void do_size(TTSOCK *sock, TASKARG *arg, TTREQ *req);
static void do_stat(TTSOCK *sock, TASKARG *arg, TTREQ *req);
static void do_misc(TTSOCK *sock, TASKARG *arg, TTREQ *req);
static void do_repl(TTSOCK *sock, TASKARG *arg, TTREQ *req);
static void do_mc_set(TTSOCK *sock, TASKARG *arg, TTREQ *req, char **tokens, int tnum);
static void do_mc_add(TTSOCK *sock, TASKARG *arg, TTREQ *req, char **tokens, int tnum);
static void do_mc_replace(TTSOCK *sock, TASKARG *arg, TTREQ *req, char **tokens, int tnum);
static void do_mc_append(TTSOCK *sock, TASKARG *arg, TTREQ *req, char **tokens, int tnum);
static void do_mc_prepend(TTSOCK *sock, TASKARG *arg, TTREQ *req, char **tokens, int tnum);
static void do_mc_get(TTSOCK *sock, TASKARG *arg, TTREQ *req, char **tokens, int tnum);
static void do_mc_delete(TTSOCK *sock, TASKARG *arg, TTREQ *req, char **tokens, int tnum);
static void do_mc_incr(TTSOCK *sock, TASKARG *arg, TTREQ *req, char **tokens, int tnum);
static void do_mc_decr(TTSOCK *sock, TASKARG *arg, TTREQ *req, char **tokens, int tnum);
static void do_mc_stats(TTSOCK *sock, TASKARG *arg, TTREQ *req, char **tokens, int tnum);
static void do_mc_flushall(TTSOCK *sock, TASKARG *arg, TTREQ *req, char **tokens, int tnum);
static void do_mc_version(TTSOCK *sock, TASKARG *arg, TTREQ *req, char **tokens, int tnum);
static void do_mc_quit(TTSOCK *sock, TASKARG *arg, TTREQ *req, char **tokens, int tnum);
static void do_http_get(TTSOCK *sock, TASKARG *arg, TTREQ *req, int ver, const char *uri);
static void do_http_head(TTSOCK *sock, TASKARG *arg, TTREQ *req, int ver, const char *uri);
static void do_http_put(TTSOCK *sock, TASKARG *arg, TTREQ *req, int ver, const char *uri);
static void do_http_post(TTSOCK *sock, TASKARG *arg, TTREQ *req, int ver, const char *uri);
static void do_http_delete(TTSOCK *sock, TASKARG *arg, TTREQ *req, int ver, const char *uri);
static void do_http_options(TTSOCK *sock, TASKARG *arg, TTREQ *req, int ver, const char *uri);
static void do_term(void *opq);
/* main routine */
int main(int argc, char **argv){
g_progname = argv[0];
g_starttime = tctime();
char *dbname = NULL;
char *host = NULL;
char *pidpath = NULL;
char *logpath = NULL;
char *ulogpath = NULL;
char *mhost = NULL;
char *rtspath = NULL;
char *skelpath = NULL;
char *extpath = NULL;
TCLIST *extpcs = NULL;
int port = TTDEFPORT;
int thnum = DEFTHNUM;
int tout = 0;
bool dmn = false;
bool kl = false;
uint64_t ulim = DEFULIMSIZ;
bool uas = false;
uint32_t sid = 0;
int mport = TTDEFPORT;
int ropts = 0;
int mulnum = 0;
uint64_t mask = 0;
for(int i = 1; i < argc; i++){
if(!dbname && argv[i][0] == '-'){
if(!strcmp(argv[i], "-host")){
if(++i >= argc) usage();
host = argv[i];
} else if(!strcmp(argv[i], "-port")){
if(++i >= argc) usage();
port = tcatoi(argv[i]);
} else if(!strcmp(argv[i], "-thnum")){
if(++i >= argc) usage();
thnum = tcatoi(argv[i]);
} else if(!strcmp(argv[i], "-tout")){
if(++i >= argc) usage();
tout = tcatoi(argv[i]);
} else if(!strcmp(argv[i], "-dmn")){
dmn = true;
} else if(!strcmp(argv[i], "-pid")){
if(++i >= argc) usage();
pidpath = argv[i];
} else if(!strcmp(argv[i], "-kl")){
kl = true;
} else if(!strcmp(argv[i], "-log")){
if(++i >= argc) usage();
logpath = argv[i];
} else if(!strcmp(argv[i], "-ld")){
g_loglevel = TTLOGDEBUG;
} else if(!strcmp(argv[i], "-le")){
g_loglevel = TTLOGERROR;
} else if(!strcmp(argv[i], "-ulog")){
if(++i >= argc) usage();
ulogpath = argv[i];
} else if(!strcmp(argv[i], "-ulim")){
if(++i >= argc) usage();
ulim = tcatoix(argv[i]);
} else if(!strcmp(argv[i], "-uas")){
uas = true;
} else if(!strcmp(argv[i], "-sid")){
if(++i >= argc) usage();
sid = tcatoi(argv[i]);
} else if(!strcmp(argv[i], "-mhost")){
if(++i >= argc) usage();
mhost = argv[i];
} else if(!strcmp(argv[i], "-mport")){
if(++i >= argc) usage();
mport = tcatoi(argv[i]);
} else if(!strcmp(argv[i], "-rts")){
if(++i >= argc) usage();
rtspath = argv[i];
} else if(!strcmp(argv[i], "-rcc")){
ropts |= RDBROCHKCON;
} else if(!strcmp(argv[i], "-skel")){
if(++i >= argc) usage();
skelpath = argv[i];
} else if(!strcmp(argv[i], "-mul")){
if(++i >= argc) usage();
mulnum = tcatoi(argv[i]);
} else if(!strcmp(argv[i], "-ext")){
if(++i >= argc) usage();
extpath = argv[i];
} else if(!strcmp(argv[i], "-extpc")){
if(!extpcs) extpcs = tclistnew2(1);
if(++i >= argc) usage();
tclistpush2(extpcs, argv[i]);
if(++i >= argc) usage();
tclistpush2(extpcs, argv[i]);
} else if(!strcmp(argv[i], "-mask")){
if(++i >= argc) usage();
mask |= getcmdmask(argv[i]);
} else if(!strcmp(argv[i], "-unmask")){
if(++i >= argc) usage();
mask &= ~getcmdmask(argv[i]);
} else if(!strcmp(argv[i], "--version")){
printf("Tokyo Tyrant version %s (%d:%s) for %s\n",
ttversion, _TT_LIBVER, _TT_PROTVER, TTSYSNAME);
printf("Copyright (C) 2006-2010 FAL Labs\n");
exit(0);
} else {
usage();
}
} else if(!dbname){
dbname = argv[i];
} else {
usage();
}
}
if(!dbname) dbname = "*";
if(thnum < 1 || mport < 1) usage();
if(dmn && !pidpath) pidpath = DEFPIDPATH;
if(!rtspath) rtspath = DEFRTSPATH;
g_serv = ttservnew();
int rv = proc(dbname, host, port, thnum, tout, dmn, pidpath, kl, logpath,
ulogpath, ulim, uas, sid, mhost, mport, rtspath, ropts,
skelpath, mulnum, extpath, extpcs, mask);
ttservdel(g_serv);
if(extpcs) tclistdel(extpcs);
return rv;
}
/* print the usage and exit */
static void usage(void){
fprintf(stderr, "%s: the server of Tokyo Tyrant\n", g_progname);
fprintf(stderr, "\n");
fprintf(stderr, "usage:\n");
fprintf(stderr, " %s [-host name] [-port num] [-thnum num] [-tout num]"
" [-dmn] [-pid path] [-kl] [-log path] [-ld|-le] [-ulog path] [-ulim num] [-uas]"
" [-sid num] [-mhost name] [-mport num] [-rts path] [-rcc] [-skel name] [-mul num]"
" [-ext path] [-extpc name period] [-mask expr] [-unmask expr] [dbname]\n",
g_progname);
fprintf(stderr, "\n");
exit(1);
}
/* get the bit mask of a command name */
static uint64_t getcmdmask(const char *expr){
uint64_t mask = 0;
TCLIST *fields = tcstrsplit(expr, " ,");
for(int i = 0; i < tclistnum(fields); i++){
const char *name = tclistval2(fields, i);
if(tcstrifwm(name, "0x")){
mask |= tcatoih(name);
} else if(!tcstricmp(name, "put")){
mask |= 1ULL << TTSEQPUT;
} else if(!tcstricmp(name, "putkeep")){
mask |= 1ULL << TTSEQPUTKEEP;
} else if(!tcstricmp(name, "putcat")){
mask |= 1ULL << TTSEQPUTCAT;
} else if(!tcstricmp(name, "putshl")){
mask |= 1ULL << TTSEQPUTSHL;
} else if(!tcstricmp(name, "putnr")){
mask |= 1ULL << TTSEQPUTNR;
} else if(!tcstricmp(name, "out")){
mask |= 1ULL << TTSEQOUT;
} else if(!tcstricmp(name, "get")){
mask |= 1ULL << TTSEQGET;
} else if(!tcstricmp(name, "mget")){
mask |= 1ULL << TTSEQMGET;
} else if(!tcstricmp(name, "vsiz")){
mask |= 1ULL << TTSEQVSIZ;
} else if(!tcstricmp(name, "iterinit")){
mask |= 1ULL << TTSEQITERINIT;
} else if(!tcstricmp(name, "iternext")){
mask |= 1ULL << TTSEQITERNEXT;
} else if(!tcstricmp(name, "fwmkeys")){
mask |= 1ULL << TTSEQFWMKEYS;
} else if(!tcstricmp(name, "addint")){
mask |= 1ULL << TTSEQADDINT;
} else if(!tcstricmp(name, "adddouble")){
mask |= 1ULL << TTSEQADDDOUBLE;
} else if(!tcstricmp(name, "ext")){
mask |= 1ULL << TTSEQEXT;
} else if(!tcstricmp(name, "sync")){
mask |= 1ULL << TTSEQSYNC;
} else if(!tcstricmp(name, "optimize")){
mask |= 1ULL << TTSEQOPTIMIZE;
} else if(!tcstricmp(name, "vanish")){
mask |= 1ULL << TTSEQVANISH;
} else if(!tcstricmp(name, "copy")){
mask |= 1ULL << TTSEQCOPY;
} else if(!tcstricmp(name, "restore")){
mask |= 1ULL << TTSEQRESTORE;
} else if(!tcstricmp(name, "setmst")){
mask |= 1ULL << TTSEQSETMST;
} else if(!tcstricmp(name, "rnum")){
mask |= 1ULL << TTSEQRNUM;
} else if(!tcstricmp(name, "size")){
mask |= 1ULL << TTSEQSIZE;
} else if(!tcstricmp(name, "stat")){
mask |= 1ULL << TTSEQSTAT;
} else if(!tcstricmp(name, "misc")){
mask |= 1ULL << TTSEQMISC;
} else if(!tcstricmp(name, "repl")){
mask |= 1ULL << TTSEQREPL;
} else if(!tcstricmp(name, "slave")){
mask |= 1ULL << TTSEQSLAVE;
} else if(!tcstricmp(name, "all")){
mask |= UINT64_MAX;
} else if(!tcstricmp(name, "allorg")){
mask |= 1ULL << TTSEQALLORG;
} else if(!tcstricmp(name, "allmc")){
mask |= 1ULL << TTSEQALLMC;
} else if(!tcstricmp(name, "allhttp")){
mask |= 1ULL << TTSEQALLHTTP;
} else if(!tcstricmp(name, "allread")){
mask |= 1ULL << TTSEQALLREAD;
} else if(!tcstricmp(name, "allwrite")){
mask |= 1ULL << TTSEQALLWRITE;
} else if(!tcstricmp(name, "allmanage")){
mask |= 1ULL << TTSEQALLMANAGE;
}
}
tclistdel(fields);
return mask;
}
/* handle termination signals */
static void sigtermhandler(int signum){
if(signum == SIGHUP) g_restart = true;
ttservkill(g_serv);
}
/* handle child event signals */
static void sigchldhandler(int signum){
return;
}
/* perform the command */
static int proc(const char *dbname, const char *host, int port, int thnum, int tout,
bool dmn, const char *pidpath, bool kl, const char *logpath,
const char *ulogpath, uint64_t ulim, bool uas, uint32_t sid,
const char *mhost, int mport, const char *rtspath, int ropts,
const char *skelpath, int mulnum, const char *extpath, const TCLIST *extpcs,
uint64_t mask){
LOGARG larg;
larg.fd = 1;
ttservsetloghandler(g_serv, do_log, &larg);
if(dmn){
if(dbname && *dbname != '*' && *dbname != '+' && *dbname != MYPATHCHR)
ttservlog(g_serv, TTLOGINFO, "warning: dbname(%s) is not the absolute path", dbname);
if(port == 0 && host && *host != MYPATHCHR)
ttservlog(g_serv, TTLOGINFO, "warning: host(%s) is not the absolute path", host);
if(pidpath && *pidpath != MYPATHCHR)
ttservlog(g_serv, TTLOGINFO, "warning: pid(%s) is not the absolute path", pidpath);
if(logpath && *logpath != MYPATHCHR)
ttservlog(g_serv, TTLOGINFO, "warning: log(%s) is not the absolute path", logpath);
if(ulogpath && *ulogpath != MYPATHCHR)
ttservlog(g_serv, TTLOGINFO, "warning: ulog(%s) is not the absolute path", ulogpath);
if(mport == 0 && mhost && *mhost != MYPATHCHR)
ttservlog(g_serv, TTLOGINFO, "warning: mhost(%s) is not the absolute path", mhost);
if(mhost && rtspath && *rtspath != MYPATHCHR)
ttservlog(g_serv, TTLOGINFO, "warning: rts(%s) is not the absolute path", rtspath);
if(skelpath && strchr(skelpath, MYPATHCHR) && *skelpath != MYPATHCHR)
ttservlog(g_serv, TTLOGINFO, "warning: skel(%s) is not the absolute path", skelpath);
if(extpath && *extpath != MYPATHCHR)
ttservlog(g_serv, TTLOGINFO, "warning: ext(%s) is not the absolute path", extpath);
if(chdir("/") == -1){
ttservlog(g_serv, TTLOGERROR, "chdir failed");
return 1;
}
}
if(!skelpath && dbname && *dbname != '*' && *dbname != '+' && !strstr(dbname, ".tc"))
ttservlog(g_serv, TTLOGINFO, "warning: dbname(%s) has no suffix for database type", dbname);
struct stat sbuf;
if(ulogpath && (stat(ulogpath, &sbuf) != 0 || !S_ISDIR(sbuf.st_mode)))
ttservlog(g_serv, TTLOGINFO, "warning: ulog(%s) is not a directory", ulogpath);
if(pidpath){
char *numstr = tcreadfile(pidpath, -1, NULL);
if(numstr && kl){
int64_t pid = tcatoi(numstr);
tcfree(numstr);
ttservlog(g_serv, TTLOGINFO,
"warning: killing the process %lld with SIGTERM", (long long)pid);
if(kill(pid, SIGTERM) != 0) ttservlog(g_serv, TTLOGERROR, "kill failed");
int cnt = 0;
while(true){
tcsleep(0.1);
if((numstr = tcreadfile(pidpath, -1, NULL)) != NULL){
tcfree(numstr);
} else {
break;
}
if(++cnt >= 100){
ttservlog(g_serv, TTLOGINFO,
"warning: killing the process %lld with SIGKILL", (long long)pid);
if(kill(pid, SIGKILL) != 0) ttservlog(g_serv, TTLOGERROR, "kill failed");
unlink(pidpath);
break;
}
}
numstr = tcreadfile(pidpath, -1, NULL);
}
if(numstr){
int64_t pid = tcatoi(numstr);
tcfree(numstr);
ttservlog(g_serv, TTLOGERROR, "the process %lld may be already running", (long long)pid);
return 1;
}
}
if(sid > UINT16_MAX){
ttservlog(g_serv, TTLOGINFO,
"warning: the SID is ignored because it exceeds %d", UINT16_MAX);
sid = 0;
}
if(sid < 1){
if(ulogpath){
ttservlog(g_serv, TTLOGINFO,
"warning: update logging is omitted because the SID is not specified");
ulogpath = NULL;
}
if(mhost){
ttservlog(g_serv, TTLOGINFO,
"warning: replication is omitted because the SID is not specified");
mhost = NULL;
}
}
if(dmn && !ttdaemonize()){
ttservlog(g_serv, TTLOGERROR, "ttdaemonize failed");
return 1;
}
if(logpath){
int fd = open(logpath, O_WRONLY | O_APPEND | O_CREAT, 00644);
if(fd != -1){
larg.fd = fd;
} else {
ttservlog(g_serv, TTLOGERROR, "the log file %s could not be opened", logpath);
return 1;
}
}
int64_t pid = getpid();
ttservlog(g_serv, TTLOGSYSTEM, "--------- logging started [%lld] --------", (long long)pid);
if(pidpath){
char buf[32];
sprintf(buf, "%lld\n", (long long)pid);
if(!tcwritefile(pidpath, buf, strlen(buf))){
ttservlog(g_serv, TTLOGERROR, "tcwritefile failed");
return 1;
}
ttservlog(g_serv, TTLOGSYSTEM, "process ID configuration: path=%s pid=%lld",
pidpath, (long long)pid);
}
ttservlog(g_serv, TTLOGSYSTEM, "server configuration: host=%s port=%d",
host ? host : "(any)", port);
if(!ttservconf(g_serv, host, port)) return 1;
struct rlimit rlbuf;
memset(&rlbuf, 0, sizeof(rlbuf));
if(getrlimit(RLIMIT_NOFILE, &rlbuf) == 0 && rlbuf.rlim_cur != RLIM_INFINITY){
rlim_t min = rlbuf.rlim_cur;
for(rlim_t max = INT32_MAX; max > min; max /= 2){
rlbuf.rlim_cur = max;
rlbuf.rlim_max = max;
if(setrlimit(RLIMIT_NOFILE, &rlbuf) == 0) break;
}
} else {
ttservlog(g_serv, TTLOGERROR, "getrlimit failed");
}
memset(&rlbuf, 0, sizeof(rlbuf));
if(getrlimit(RLIMIT_NOFILE, &rlbuf) == 0){
ttservlog(g_serv, TTLOGSYSTEM, "maximum connection: %d", (int)rlbuf.rlim_cur);
} else {
ttservlog(g_serv, TTLOGERROR, "getrlimit failed");
}
bool err = false;
ADBSKEL skel;
memset(&skel, 0, sizeof(skel));
void *skellib = NULL;
if(skelpath){
ttservlog(g_serv, TTLOGSYSTEM, "skeleton database library: %s", skelpath);
skellib = dlopen(skelpath, RTLD_LAZY);
if(!skellib){
err = true;
ttservlog(g_serv, TTLOGERROR, "dlopen failed: %s", dlerror());
}
}
TCADB *adb = tcadbnew();
if(skellib){
void *initsym = dlsym(skellib, "initialize");
if(initsym){
bool (*initfunc)(ADBSKEL *);
memcpy(&initfunc, &initsym, sizeof(initsym));
if(initfunc(&skel)){
if(!tcadbsetskel(adb, &skel)){
if(skel.opq && skel.del) skel.del(skel.opq);
err = true;
ttservlog(g_serv, TTLOGERROR, "tcadbsetskel failed");
}
} else {
if(skel.opq && skel.del) skel.del(skel.opq);
err = true;
ttservlog(g_serv, TTLOGERROR, "initialize failed");
}
} else {
err = true;
ttservlog(g_serv, TTLOGERROR, "dlsym failed: %s", dlerror());
}
}
ttservlog(g_serv, TTLOGSYSTEM, "opening the database: %s", dbname);
if(mulnum > 0 && !tcadbsetskelmulti(adb, mulnum)){
err = true;
ttservlog(g_serv, TTLOGERROR, "tcadbsetskelmulti failed");
}
if(!tcadbopen(adb, dbname)){
err = true;
ttservlog(g_serv, TTLOGERROR, "tcadbopen failed");
}
TCULOG *ulog = tculognew();
if(ulogpath){
ttservlog(g_serv, TTLOGSYSTEM,
"update log configuration: path=%s limit=%llu async=%d sid=%d",
ulogpath, (unsigned long long)ulim, uas, sid);
if(uas && !tculogsetaio(ulog)){
err = true;
ttservlog(g_serv, TTLOGERROR, "tculogsetaio failed");
}
if(!tculogopen(ulog, ulogpath, ulim)){
err = true;
ttservlog(g_serv, TTLOGERROR, "tculogopen failed");
}
}
ttservtune(g_serv, thnum, tout);
if(mhost)
ttservlog(g_serv, TTLOGSYSTEM, "replication configuration: host=%s port=%d ropts=%d",
mhost, mport, ropts);
uint64_t *counts = tccalloc(sizeof(*counts), (TTSEQNUM) * thnum);
void *screxts[thnum];
TCMDB *scrstash = NULL;
TCMDB *scrlock = NULL;
pthread_mutex_t *scrlcks = NULL;
if(extpath){
ttservlog(g_serv, TTLOGSYSTEM, "scripting extension: %s", extpath);
scrstash = tcmdbnew2(STASHBNUM);
scrlock = tcmdbnew2(thnum * 2 + 1);
bool screrr = false;
for(int i = 0; i < thnum; i++){
screxts[i] = NULL;
}
for(int i = 0; i < thnum; i++){
screxts[i] = scrextnew(screxts, thnum, i, extpath, adb, ulog, sid, scrstash, scrlock,
do_log, &larg);
if(!screxts[i]) screrr = true;
}
if(screrr){
err = true;
ttservlog(g_serv, TTLOGERROR, "scrextnew failed");
}
} else {
for(int i = 0; i < thnum; i++){
screxts[i] = NULL;
}
}
if(mask != 0)
ttservlog(g_serv, TTLOGSYSTEM, "command bit mask: 0x%llx", (unsigned long long)mask);
REPLARG sarg;
snprintf(sarg.host, TTADDRBUFSIZ, "%s", mhost ? mhost : "");
sarg.port = mport;
sarg.rtspath = rtspath;
sarg.rts = 0;
sarg.opts = ropts;
sarg.adb = adb;
sarg.ulog = ulog;
sarg.sid = sid;
sarg.fail = false;
sarg.recon = false;
sarg.fatal = false;
sarg.mts = 0;
if(!(mask & (1ULL << TTSEQSLAVE))) ttservaddtimedhandler(g_serv, REPLPERIOD, do_slave, &sarg);
EXTPCARG *pcargs = NULL;
int pcnum = 0;
if(extpath && extpcs){
pcnum = tclistnum(extpcs) / 2;
pcargs = tcmalloc(sizeof(*pcargs) * pcnum);
for(int i = 0; i < pcnum; i++){
const char *name = tclistval2(extpcs, i * 2);
double period = tcatof(tclistval2(extpcs, i * 2 + 1));
EXTPCARG *pcarg = pcargs + i;
pcarg->name = name;
pcarg->adb = adb;
pcarg->ulog = ulog;
pcarg->sid = sid;
pcarg->sarg = &sarg;
pcarg->scrext = scrextnew(screxts, thnum, thnum + i, extpath, adb, ulog, sid,
scrstash, scrlock, do_log, &larg);
if(pcarg->scrext){
if(*name && period > 0) ttservaddtimedhandler(g_serv, period, do_extpc, pcarg);
} else {
err = true;
ttservlog(g_serv, TTLOGERROR, "scrextnew failed");
}
}
}
TASKARG targ;
targ.thnum = thnum;
targ.counts = counts;
targ.mask = mask;
targ.adb = adb;
targ.ulog = ulog;
targ.sid = sid;
targ.sarg = &sarg;
for(int i = 0; i < RECMTXNUM; i++){
if(pthread_mutex_init(targ.rmtxs + i, NULL) != 0)
ttservlog(g_serv, TTLOGERROR, "pthread_mutex_init failed");
}
targ.screxts = screxts;
ttservsettaskhandler(g_serv, do_task, &targ);
TERMARG karg;
karg.thnum = thnum;
karg.adb = adb;
karg.sarg = &sarg;
karg.screxts = screxts;
karg.pcargs = pcargs;
karg.pcnum = pcnum;
karg.err = false;
ttservsettermhandler(g_serv, do_term, &karg);
if(larg.fd != 1){
close(larg.fd);
larg.fd = 1;
}
do {
g_restart = false;
if(logpath){
int fd = open(logpath, O_WRONLY | O_APPEND | O_CREAT, 00644);
if(fd != -1){
larg.fd = fd;
} else {
err = true;
ttservlog(g_serv, TTLOGERROR, "open failed");
}
}
if(signal(SIGTERM, sigtermhandler) == SIG_ERR || signal(SIGINT, sigtermhandler) == SIG_ERR ||
signal(SIGHUP, sigtermhandler) == SIG_ERR || signal(SIGPIPE, SIG_IGN) == SIG_ERR ||
signal(SIGCHLD, sigchldhandler) == SIG_ERR){
err = true;
ttservlog(g_serv, TTLOGERROR, "signal failed");
}
if(!ttservstart(g_serv)) err = true;
} while(g_restart);
if(karg.err) err = true;
if(pcargs){
for(int i = 0; i < pcnum; i++){
EXTPCARG *pcarg = pcargs + i;
if(!pcarg->scrext) continue;
if(!scrextdel(pcarg->scrext)){
err = true;
ttservlog(g_serv, TTLOGERROR, "scrextdel failed");
}
}
tcfree(pcargs);
}
for(int i = 0; i < RECMTXNUM; i++){
if(pthread_mutex_destroy(targ.rmtxs + i) != 0)
ttservlog(g_serv, TTLOGERROR, "pthread_mutex_destroy failed");
}
for(int i = 0; i < thnum; i++){
if(!screxts[i]) continue;
if(!scrextdel(screxts[i])){
err = true;
ttservlog(g_serv, TTLOGERROR, "scrextdel failed");
}
}
if(scrlcks){
for(int i = 0; i < RECMTXNUM; i++){
if(pthread_mutex_destroy(scrlcks + i) != 0)
ttservlog(g_serv, TTLOGERROR, "pthread_mutex_destroy failed");
}
tcfree(scrlcks);
}
if(scrlock) tcmdbdel(scrlock);
if(scrstash) tcmdbdel(scrstash);
tcfree(counts);
if(ulogpath && !tculogclose(ulog)){
err = true;
ttservlog(g_serv, TTLOGERROR, "tculogclose failed");
}
tculogdel(ulog);
tcadbdel(adb);
if(skellib && dlclose(skellib) != 0){
err = true;
ttservlog(g_serv, TTLOGERROR, "dlclose failed");
}
if(pidpath && unlink(pidpath) != 0){
err = true;
ttservlog(g_serv, TTLOGERROR, "unlink failed");
}
ttservlog(g_serv, TTLOGSYSTEM, "--------- logging finished [%d] --------", pid);
if(logpath && close(larg.fd) == -1) err = true;
return err ? 1 : 0;
}
/* handle a log message */
static void do_log(int level, const char *msg, void *opq){
if(level < g_loglevel) return;
LOGARG *arg = (LOGARG *)opq;
char date[48];
tcdatestrwww(INT64_MAX, INT_MAX, date);
const char *lvstr = "unknown";
switch(level){
case TTLOGDEBUG: lvstr = "DEBUG"; break;
case TTLOGINFO: lvstr = "INFO"; break;
case TTLOGERROR: lvstr = "ERROR"; break;
case TTLOGSYSTEM: lvstr = "SYSTEM"; break;
}
char buf[LINEBUFSIZ];
int len = snprintf(buf, LINEBUFSIZ, "%s\t%s\t%s\n", date, lvstr, msg);
if(len >= LINEBUFSIZ){
buf[LINEBUFSIZ-1] = '\n';
len = LINEBUFSIZ;
}
tcwrite(arg ? arg->fd : 1, buf, len);
}
/* replicate master data */
static void do_slave(void *opq){
REPLARG *arg = opq;
TCADB *adb = arg->adb;
TCULOG *ulog = arg->ulog;
uint32_t sid = arg->sid;
if(arg->fatal) return;
if(arg->host[0] == '\0' || arg->port < 1) return;
if(arg->mts > 0){
char rtsbuf[NUMBUFSIZ];
int len = sprintf(rtsbuf, "%llu\n", (unsigned long long)arg->mts);
if(!tcwritefile(arg->rtspath, rtsbuf, len))
ttservlog(g_serv, TTLOGERROR, "do_slave: tcwritefile failed");
arg->mts = 0;
}
int rtsfd = open(arg->rtspath, O_RDWR | O_CREAT, 00644);
if(rtsfd == -1){
ttservlog(g_serv, TTLOGERROR, "do_slave: open failed");
return;
}
struct stat sbuf;
if(fstat(rtsfd, &sbuf) == -1){
ttservlog(g_serv, TTLOGERROR, "do_slave: stat failed");
close(rtsfd);
return;
}
char rtsbuf[NUMBUFSIZ];
memset(rtsbuf, 0, NUMBUFSIZ);
arg->rts = 0;
if(sbuf.st_size > 0 && tcread(rtsfd, rtsbuf, tclmin(NUMBUFSIZ - 1, sbuf.st_size)))
arg->rts = tcatoi(rtsbuf);
TCREPL *repl = tcreplnew();
pthread_cleanup_push((void (*)(void *))tcrepldel, repl);
if(tcreplopen(repl, arg->host, arg->port, arg->rts + 1, sid)){
ttservlog(g_serv, TTLOGINFO, "replicating from sid=%u (%s:%d) after %llu",
repl->mid, arg->host, arg->port, (unsigned long long)arg->rts);
arg->fail = false;
arg->recon = false;
bool err = false;
uint32_t rsid;
const char *rbuf;
int rsiz;
uint64_t rts;
while(!err && !ttserviskilled(g_serv) && !arg->recon &&
(rbuf = tcreplread(repl, &rsiz, &rts, &rsid)) != NULL){
if(rsiz < 1) continue;
bool cc;
if(!tculogadbredo(adb, rbuf, rsiz, ulog, rsid, repl->mid, &cc)){
err = true;
ttservlog(g_serv, TTLOGERROR, "do_slave: tculogadbredo failed");
} else if(!cc){
if(arg->opts & RDBROCHKCON){
err = true;
arg->fatal = true;
ttservlog(g_serv, TTLOGERROR, "do_slave: detected inconsistency");
} else {
ttservlog(g_serv, TTLOGINFO, "do_slave: detected inconsistency");
}
}
if(lseek(rtsfd, 0, SEEK_SET) != -1){
int len = sprintf(rtsbuf, "%llu\n", (unsigned long long)rts);
if(tcwrite(rtsfd, rtsbuf, len)){
arg->rts = rts;
} else {
err = true;
ttservlog(g_serv, TTLOGERROR, "do_slave: tcwrite failed");
}
} else {
err = true;
ttservlog(g_serv, TTLOGERROR, "do_slave: lseek failed");
}
}
tcreplclose(repl);
ttservlog(g_serv, TTLOGINFO, "replication finished");
} else {
if(!arg->fail) ttservlog(g_serv, TTLOGERROR, "do_slave: tcreplopen failed");
arg->fail = true;
}
pthread_cleanup_pop(1);
if(close(rtsfd) == -1) ttservlog(g_serv, TTLOGERROR, "do_slave: close failed");
}
/* perform an extension command */
static void do_extpc(void *opq){
EXTPCARG *arg = (EXTPCARG *)opq;
const char *name = arg->name;
void *scr = arg->scrext;
int xsiz;
char *xbuf = scrextcallmethod(scr, name, "", 0, "", 0, &xsiz);
tcfree(xbuf);
}
/* handle a task and dispatch it */
static void do_task(TTSOCK *sock, void *opq, TTREQ *req){
TASKARG *arg = (TASKARG *)opq;
int c = ttsockgetc(sock);
if(c == TTMAGICNUM){
switch(ttsockgetc(sock)){
case TTCMDPUT:
do_put(sock, arg, req);
break;
case TTCMDPUTKEEP:
do_putkeep(sock, arg, req);
break;
case TTCMDPUTCAT:
do_putcat(sock, arg, req);
break;
case TTCMDPUTSHL:
do_putshl(sock, arg, req);
break;
case TTCMDPUTNR:
do_putnr(sock, arg, req);
break;
case TTCMDOUT:
do_out(sock, arg, req);
break;
case TTCMDGET:
do_get(sock, arg, req);
break;
case TTCMDMGET:
do_mget(sock, arg, req);
break;
case TTCMDVSIZ:
do_vsiz(sock, arg, req);
break;
case TTCMDITERINIT:
do_iterinit(sock, arg, req);
break;
case TTCMDITERNEXT:
do_iternext(sock, arg, req);
break;
case TTCMDFWMKEYS:
do_fwmkeys(sock, arg, req);
break;
case TTCMDADDINT:
do_addint(sock, arg, req);
break;
case TTCMDADDDOUBLE:
do_adddouble(sock, arg, req);
break;
case TTCMDEXT:
do_ext(sock, arg, req);
break;
case TTCMDSYNC:
do_sync(sock, arg, req);
break;
case TTCMDOPTIMIZE:
do_optimize(sock, arg, req);
break;
case TTCMDVANISH:
do_vanish(sock, arg, req);
break;
case TTCMDCOPY:
do_copy(sock, arg, req);
break;
case TTCMDRESTORE:
do_restore(sock, arg, req);
break;
case TTCMDSETMST:
do_setmst(sock, arg, req);
break;
case TTCMDRNUM:
do_rnum(sock, arg, req);
break;
case TTCMDSIZE:
do_size(sock, arg, req);
break;
case TTCMDSTAT:
do_stat(sock, arg, req);
break;
case TTCMDMISC:
do_misc(sock, arg, req);
break;