/
ttserver.c
2990 lines (2862 loc) · 101 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-2009 Mikio Hirabayashi
* 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 DEFPORT 1978 // default port
#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 MAXARGSIZ (32*1024*1024) // maximum size of each argument
#define MAXARGNUM (1*1024*1024) // 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 TTMSKPUT (1ULL<<0) /* bit mask of put command */
#define TTMSKPUTKEEP (1ULL<<1) /* bit mask of putkeep command */
#define TTMSKPUTCAT (1ULL<<2) /* bit mask of putcat command */
#define TTMSKPUTSHL (1ULL<<3) /* bit mask of putshl command */
#define TTMSKPUTNR (1ULL<<4) /* bit mask of putnr command */
#define TTMSKOUT (1ULL<<5) /* bit mask of out command */
#define TTMSKGET (1ULL<<6) /* bit mask of get command */
#define TTMSKMGET (1ULL<<7) /* bit mask of mget command */
#define TTMSKVSIZ (1ULL<<8) /* bit mask of vsiz command */
#define TTMSKITERINIT (1ULL<<9) /* bit mask of iterinit command */
#define TTMSKITERNEXT (1ULL<<10) /* bit mask of iternext command */
#define TTMSKFWMKEYS (1ULL<<11) /* bit mask of fwmkeys command */
#define TTMSKADDINT (1ULL<<12) /* bit mask of addint command */
#define TTMSKADDDOUBLE (1ULL<<13) /* bit mask of adddouble command */
#define TTMSKEXT (1ULL<<14) /* bit mask of ext command */
#define TTMSKSYNC (1ULL<<15) /* bit mask of sync command */
#define TTMSKVANISH (1ULL<<16) /* bit mask of vanish command */
#define TTMSKCOPY (1ULL<<17) /* bit mask of copy command */
#define TTMSKRESTORE (1ULL<<18) /* bit mask of restore command */
#define TTMSKSETMST (1ULL<<19) /* bit mask of setmst command */
#define TTMSKRNUM (1ULL<<20) /* bit mask of rnum command */
#define TTMSKSIZE (1ULL<<21) /* bit mask of size command */
#define TTMSKSTAT (1ULL<<22) /* bit mask of stat command */
#define TTMSKMISC (1ULL<<23) /* bit mask of stat command */
#define TTMSKREPL (1ULL<<24) /* bit mask of repl command */
#define TTMSKSLAVE (1ULL<<25) /* bit mask of slave command */
#define TTMSKALLORG (1ULL<<26) /* bit mask of all commands the original */
#define TTMSKALLMC (1ULL<<27) /* bit mask of all commands the memcached */
#define TTMSKALLHTTP (1ULL<<28) /* bit mask of all commands the HTTP */
#define TTMSKALLREAD (1ULL<<29) /* bit mask of all commands of reading */
#define TTMSKALLWRITE (1ULL<<30) /* bit mask of all commands of writing */
#define TTMSKALLMANAGE (1ULL<<31) /* bit mask of all commands of managing */
typedef struct { // type of structure of logging opaque object
int fd;
} LOGARG;
typedef struct { // type of structure of master synchronous object
char host[TTADDRBUFSIZ];
int port;
char rtspath[TTADDRBUFSIZ];
uint64_t rts;
TCADB *adb;
TCULOG *ulog;
uint32_t sid;
bool fail;
bool recon;
bool delay;
bool started;
bool exit;
} REPLARG;
typedef struct { // type of structure of periodic command
const char *name;
TCADB *adb;
TCULOG *ulog;
uint32_t sid;
REPLARG *sarg;
void *scrext;
} EXTPCARG;
typedef struct { // type of structure of task opaque object
uint64_t mask;
TCADB *adb;
TCULOG *ulog;
uint32_t sid;
REPLARG *sarg;
pthread_mutex_t rmtxs[RECMTXNUM];
void **screxts;
} TASKARG;
/* 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, 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 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_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_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_ext_addrepl(TTSOCK *sock, TASKARG *arg, TTREQ *req, char **tokens, int tnum);
static void do_ext_delrepl(TTSOCK *sock, TASKARG *arg, TTREQ *req, char **tokens, int tnum);
bool ttservmodifytimedhandler( const char * host, int port, const char * rtspath,TTSERV * serv);
bool ttservdeltimedhandler( const char * host, int port, const char * rtspath,TTSERV * serv);
/* 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 *extpath = NULL;
TCLIST *extpcs = NULL;
int port = DEFPORT;
int thnum = DEFTHNUM;
int tout = 0;
bool dmn = false;
bool kl = false;
uint64_t ulim = 0;
bool uas = false;
uint32_t sid = 0;
int mport = DEFPORT;
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], "-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-2009 Mikio Hirabayashi\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(sid < 1){
sid = port;
char name[TTADDRBUFSIZ];
if(ttgetlocalhostname(name)){
for(int i = 0; name[i] != '\0'; i++){
sid = sid * 31 + ((unsigned char *)name)[i];
}
}
sid = sid & INT_MAX;
}
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, 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] [-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 |= strtoll(name, NULL, 16);
} else if(!tcstricmp(name, "put")){
mask |= TTMSKPUT;
} else if(!tcstricmp(name, "putkeep")){
mask |= TTMSKPUTKEEP;
} else if(!tcstricmp(name, "putcat")){
mask |= TTMSKPUTCAT;
} else if(!tcstricmp(name, "putshl")){
mask |= TTMSKPUTSHL;
} else if(!tcstricmp(name, "putnr")){
mask |= TTMSKPUTNR;
} else if(!tcstricmp(name, "out")){
mask |= TTMSKOUT;
} else if(!tcstricmp(name, "get")){
mask |= TTMSKGET;
} else if(!tcstricmp(name, "mget")){
mask |= TTMSKMGET;
} else if(!tcstricmp(name, "vsiz")){
mask |= TTMSKVSIZ;
} else if(!tcstricmp(name, "iterinit")){
mask |= TTMSKITERINIT;
} else if(!tcstricmp(name, "iternext")){
mask |= TTMSKITERNEXT;
} else if(!tcstricmp(name, "fwmkeys")){
mask |= TTMSKFWMKEYS;
} else if(!tcstricmp(name, "addint")){
mask |= TTMSKADDINT;
} else if(!tcstricmp(name, "adddouble")){
mask |= TTMSKADDDOUBLE;
} else if(!tcstricmp(name, "ext")){
mask |= TTMSKEXT;
} else if(!tcstricmp(name, "sync")){
mask |= TTMSKSYNC;
} else if(!tcstricmp(name, "vanish")){
mask |= TTMSKVANISH;
} else if(!tcstricmp(name, "copy")){
mask |= TTMSKCOPY;
} else if(!tcstricmp(name, "restore")){
mask |= TTMSKRESTORE;
} else if(!tcstricmp(name, "setmst")){
mask |= TTMSKSETMST;
} else if(!tcstricmp(name, "rnum")){
mask |= TTMSKRNUM;
} else if(!tcstricmp(name, "size")){
mask |= TTMSKSIZE;
} else if(!tcstricmp(name, "stat")){
mask |= TTMSKSTAT;
} else if(!tcstricmp(name, "misc")){
mask |= TTMSKMISC;
} else if(!tcstricmp(name, "repl")){
mask |= TTMSKREPL;
} else if(!tcstricmp(name, "slave")){
mask |= TTMSKSLAVE;
} else if(!tcstricmp(name, "all")){
mask |= UINT64_MAX;
} else if(!tcstricmp(name, "allorg")){
mask |= TTMSKALLORG;
} else if(!tcstricmp(name, "allmc")){
mask |= TTMSKALLMC;
} else if(!tcstricmp(name, "allhttp")){
mask |= TTMSKALLHTTP;
} else if(!tcstricmp(name, "allread")){
mask |= TTMSKALLREAD;
} else if(!tcstricmp(name, "allwrite")){
mask |= TTMSKALLWRITE;
} else if(!tcstricmp(name, "allmanage")){
mask |= TTMSKALLMANAGE;
}
}
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, 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(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(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){
usleep(1000 * 100);
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(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;
bool err = false;
ttservlog(g_serv, TTLOGSYSTEM, "database configuration: name=%s", dbname);
TCADB *adb = tcadbnew();
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", mhost, mport);
void *screxts[thnum];
TCMDB *scrstash = NULL;
pthread_mutex_t *scrlcks = NULL;
if(extpath){
ttservlog(g_serv, TTLOGSYSTEM, "scripting extension: %s", extpath);
scrstash = tcmdbnew2(STASHBNUM);
scrlcks = tcmalloc(sizeof(*scrlcks) * RECMTXNUM);
for(int i = 0; i < RECMTXNUM; i++){
pthread_mutexattr_t attr;
if(pthread_mutexattr_init(&attr) != 0)
ttservlog(g_serv, TTLOGERROR, "pthread_mutexattr_init failed");
if(pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK) != 0)
ttservlog(g_serv, TTLOGERROR, "pthread_mutexattr_settype failed");
if(pthread_mutex_init(scrlcks + i, &attr) != 0)
ttservlog(g_serv, TTLOGERROR, "pthread_mutex_init failed");
pthread_mutexattr_destroy(&attr);
}
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,
scrlcks, RECMTXNUM, 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;
snprintf(sarg.rtspath, TTADDRBUFSIZ, "%s", rtspath? rtspath: "");
sarg.rts = 0;
sarg.adb = adb;
sarg.ulog = ulog;
sarg.sid = sid;
sarg.fail = false;
sarg.recon = false;
sarg.delay=false;
sarg.exit=false;
if(!(mask & TTMSKSLAVE)) ttservaddtimedhandler(g_serv, 1.0, do_slave, &sarg);
REPLARG sarg2;
snprintf(sarg2.host, TTADDRBUFSIZ, "%s", mhost ? mhost : "");
sarg2.port = mport;
snprintf(sarg2.rtspath, TTADDRBUFSIZ, "%s", rtspath? rtspath: "");
sarg2.rts = 0;
sarg2.adb = adb;
sarg2.ulog = ulog;
sarg2.sid = sid;
sarg2.fail = false;
sarg2.recon = false;
sarg2.delay=false;
sarg2.exit=false;
if(!(mask & TTMSKSLAVE)) ttservaddtimedhandler_delay(g_serv, 1.0, do_slave, &sarg2);
REPLARG sarg3;
snprintf(sarg3.host, TTADDRBUFSIZ, "%s", mhost ? mhost : "");
sarg3.port = mport;
snprintf(sarg3.rtspath, TTADDRBUFSIZ, "%s", rtspath? rtspath: "");
sarg3.rts = 0;
sarg3.adb = adb;
sarg3.ulog = ulog;
sarg3.sid = sid;
sarg3.fail = false;
sarg3.recon = false;
sarg3.delay=false;
sarg3.exit=false;
if(!(mask & TTMSKSLAVE)) ttservaddtimedhandler_delay(g_serv, 1.0, do_slave, &sarg3);
REPLARG sarg4;
snprintf(sarg4.host, TTADDRBUFSIZ, "%s", mhost ? mhost : "");
sarg4.port = mport;
snprintf(sarg4.rtspath, TTADDRBUFSIZ, "%s", rtspath? rtspath: "");
sarg4.rts = 0;
sarg4.adb = adb;
sarg4.ulog = ulog;
sarg4.sid = sid;
sarg4.fail = false;
sarg4.recon = false;
sarg4.delay=false;
sarg4.exit=false;
if(!(mask & TTMSKSLAVE)) ttservaddtimedhandler_delay(g_serv, 1.0, do_slave, &sarg4);
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,
scrlcks, RECMTXNUM, 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.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);
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(pcargs){
for(int i = 0; i < pcnum; i++){
EXTPCARG *pcarg = pcargs + i;
if(pcarg->scrext && !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(scrstash) tcmdbdel(scrstash);
if(ulogpath && !tculogclose(ulog)){
err = true;
ttservlog(g_serv, TTLOGERROR, "tculogclose failed");
}
tculogdel(ulog);
if(!tcadbclose(adb)){
err = true;
ttservlog(g_serv, TTLOGERROR, "tcadbclose failed");
}
tcadbdel(adb);
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);
char buf[LINEBUFSIZ];
int len = snprintf(buf, LINEBUFSIZ, "%s\t%s\n", date, msg);
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->exit){
printf("exit becaues arg->exit is true\n");
pthread_mutex_unlock(&g_serv->tmtx);
pthread_exit(NULL);
return ;
}
fprintf(stderr,"do_slave:%s,port:%d,rtspath:%s\n",arg->host,arg->port,arg->rtspath);
if(arg->host[0] == '\0' || arg->port < 1) return;
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 = strtoll(rtsbuf, NULL, 10);
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 %s:%d after %llu",
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;
if(!tculogadbredo(adb, rbuf, rsiz, true, ulog, rsid)){
err = true;
ttservlog(g_serv, TTLOGERROR, "do_slave: tculogadbredo failed");
}
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 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;
case TTCMDREPL:
do_repl(sock, arg, req);
break;
default:
ttservlog(g_serv, TTLOGINFO, "unknown command");
break;
}
} else {
ttsockungetc(sock, c);
char *line = ttsockgets2(sock);
if(line){
int tnum;
char **tokens = tokenize(line, &tnum);
if(tnum > 0){
const char *cmd = tokens[0];
if(!strcmp(cmd, "set")){
do_mc_set(sock, arg, req, tokens, tnum);
} else if(!strcmp(cmd, "add")){
do_mc_add(sock, arg, req, tokens, tnum);
} else if(!strcmp(cmd, "replace")){
do_mc_replace(sock, arg, req, tokens, tnum);
} else if(!strcmp(cmd, "get") || !strcmp(cmd, "gets")){
do_mc_get(sock, arg, req, tokens, tnum);
} else if(!strcmp(cmd, "delete")){
do_mc_delete(sock, arg, req, tokens, tnum);
} else if(!strcmp(cmd,"addrepl")){
do_ext_addrepl(sock,arg,req,tokens,tnum);
} else if(!strcmp(cmd,"delrepl")){
do_ext_delrepl(sock,arg,req,tokens,tnum);
} else if(!strcmp(cmd, "incr")){
do_mc_incr(sock, arg, req, tokens, tnum);
} else if(!strcmp(cmd, "decr")){
do_mc_decr(sock, arg, req, tokens, tnum);
} else if(!strcmp(cmd, "stats")){
do_mc_stats(sock, arg, req, tokens, tnum);
} else if(!strcmp(cmd, "flush_all")){
do_mc_flushall(sock, arg, req, tokens, tnum);
} else if(!strcmp(cmd, "version")){
do_mc_version(sock, arg, req, tokens, tnum);
} else if(!strcmp(cmd, "quit")){
do_mc_quit(sock, arg, req, tokens, tnum);
} else if(tnum > 2 && tcstrfwm(tokens[2], "HTTP/1.")){
int ver = tcatoi(tokens[2] + 7);
const char *uri = tokens[1];
if(tcstrifwm(uri, "http://")){
const char *pv = strchr(uri + 7, '/');
if(pv) uri = pv;
}
if(!strcmp(cmd, "GET")){
do_http_get(sock, arg, req, ver, uri);
} else if(!strcmp(cmd, "HEAD")){
do_http_head(sock, arg, req, ver, uri);
} else if(!strcmp(cmd, "PUT")){
do_http_put(sock, arg, req, ver, uri);
} else if(!strcmp(cmd, "POST")){
do_http_post(sock, arg, req, ver, uri);
} else if(!strcmp(cmd, "DELETE")){
do_http_delete(sock, arg, req, ver, uri);
}
}
}
tcfree(tokens);
tcfree(line);
}
}
}
/* tokenize a string */
static char **tokenize(char *str, int *np){
int anum = TOKENUNIT;
char **tokens = tcmalloc(sizeof(*tokens) * anum);
int tnum = 0;
while(*str == ' ' || *str == '\t'){
str++;
}
while(*str != '\0'){
if(tnum >= anum){
anum *= 2;
tokens = tcrealloc(tokens, sizeof(*tokens) * anum);
}
tokens[tnum++] = str;
while(*str != '\0' && *str != ' ' && *str != '\t'){
str++;
}
while(*str == ' ' || *str == '\t'){
*(str++) = '\0';
}
}
*np = tnum;
return tokens;
}
/* get the mutex index of a record */