This repository has been archived by the owner on Apr 30, 2020. It is now read-only.
forked from gigablast/open-source-search-engine
/
main.cpp
5565 lines (4841 loc) · 155 KB
/
main.cpp
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
//
// Matt Wells, copyright Sep 2001
//
#include <sched.h> // clone()
// declare this stuff up here for call the pread() in our seek test below
//
// maybe we should put this in a common header file so we don't have
// certain files compiled with the platform default, and some not -partap
#include "Version.h" // getVersion()
#include "Mem.h"
#include "Conf.h"
#include "JobScheduler.h"
#include "Hostdb.h"
#include "Posdb.h"
#include "Titledb.h"
#include "Tagdb.h"
#include "Spider.h"
#include "SpiderColl.h"
#include "SpiderLoop.h"
#include "SpiderCache.h"
#include "Doledb.h"
#include "Clusterdb.h"
#include "Collectiondb.h"
#include "Sections.h"
#include "UdpServer.h"
#include "Serialize.h"
#include "Repair.h"
#include "DailyMerge.h"
#include "MsgC.h"
#include "HttpServer.h"
#include "Loop.h"
#include "HighFrequencyTermShortcuts.h"
#include "PageTemperatureRegistry.h"
#include "Docid2Siteflags.h"
#include "SiteMedianPageTemperatureRegistry.h"
#include "UrlRealtimeClassification.h"
#include "IPAddressChecks.h"
#include <sys/resource.h> // setrlimit
#include "Stats.h"
#include "Statistics.h"
#include "Speller.h" // g_speller
#include "Wiki.h" // g_wiki
#include "Wiktionary.h" // g_wiktionary
#include "WordVariations.h"
#include "CountryCode.h"
#include "Domains.h"
#include "Pos.h"
#include "Title.h"
#include "Speller.h"
#include "SummaryCache.h"
#include "InstanceInfoExchange.h"
#include "WantedChecker.h"
#include "Dns.h"
#include "DumpSpiderdbSqlite.h"
// include all msgs that have request handlers, cuz we register them with g_udp
#include "Msg0.h"
#include "Msg4In.h"
#include "Msg4Out.h"
#include "Msg13.h"
#include "Msg20.h"
#include "Msg22.h"
#include "Msg25.h"
#include "Msg39.h"
#include "Msg40.h" // g_resultsCache
#include "Parms.h"
#include "Pages.h"
#include "PageInject.h"
#include "unicode/UCMaps.h"
#include "utf8_convert.h"
#include "Profiler.h"
#include "Proxy.h"
#include "linkspam.h"
#include "Process.h"
#include "sort.h"
#include "RdbBuckets.h"
#include "SpiderProxy.h"
#include "HashTable.h"
#include "GbUtil.h"
#include "Dir.h"
#include "File.h"
#include "DnsBlockList.h"
#include "ContentTypeBlockList.h"
#include "UrlMatchList.h"
#include "UrlBlockCheck.h"
#include "DocDelete.h"
#include "GbDns.h"
#include "ScopedLock.h"
#include "RobotsCheckList.h"
#include "ConvertSpiderdb.h"
#include "RobotsBlockedResultOverride.h"
#include "UrlResultOverride.h"
#include "FxCheckAdult.h"
#include "FxCheckSpam.h"
#include "GbCompress.h"
#include "DocRebuild.h"
#include "DocReindex.h"
#include "FxExplicitKeywords.h"
#include "IpBlockList.h"
#include "SpiderdbSqlite.h"
#include "QueryLanguage.h"
#include "SiteNumInlinks.h"
#include "ContentMatchList.h"
#include "SiteMedianPageTemperature.h"
#include "Lemma.h"
#include "ip.h"
#include "CountryLanguage.h"
#include "Errno.h"
#include "Docid.h"
#include <sys/stat.h> //umask()
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#ifdef _VALGRIND_
#include <valgrind/memcheck.h>
#include <valgrind/helgrind.h>
#endif
static bool registerMsgHandlers();
static bool registerMsgHandlers1();
static bool registerMsgHandlers2();
static const int32_t commandLineDumpdbRecSize = 10 * 1024 * 1024; //recSizes parameter for Msg5::getList() while dumping database from the command-line
static void printHelp();
static void dumpTitledb (const char *coll, int32_t sfn, int32_t numFiles, bool includeTree,
int64_t docId , bool justPrintDups );
static void dumpTagdb(const char *coll, int32_t sfn, int32_t numFiles, bool includeTree, char req,
const char *site);
//dumpPosdb() is not local becaue it is called directly by unittests
void dumpPosdb(const char *coll, int32_t sfn, int32_t numFiles, bool includeTree, int64_t termId , bool justVerify);
static void dumpWaitingTree(const char *coll);
static void dumpRobotsTxtCache(const char *coll);
static void dumpDoledb(const char *coll, int32_t sfn, int32_t numFiles, bool includeTree);
static void dumpClusterdb(const char *coll, int32_t sfn, int32_t numFiles, bool includeTree);
static void dumpLinkdb(const char *coll, int32_t sfn, int32_t numFiles, bool includeTree, const char *url, bool urlhash);
static void dumpUnwantedTitledbRecs(const char *coll, int32_t startFileNum, int32_t numFiles, bool includeTree);
static void dumpWantedTitledbRecs(const char *coll, int32_t startFileNum, int32_t numFiles, bool includeTree);
static void dumpAdultTitledbRecs(const char *coll, int32_t startFileNum, int32_t numFiles, bool includeTree);
static void dumpSpamTitledbRecs(const char *coll, int32_t startFileNum, int32_t numFiles, bool includeTree);
static int copyFiles(const char *dstDir);
static const char *getAbsoluteGbDir(const char *argv0);
static int32_t checkDirPerms(const char *dir);
static bool hashtest();
// how fast to parse the content of this docId?
static bool parseTest(const char *coll, int64_t docId, const char *query);
static bool summaryTest1(char *rec, int32_t listSize, const char *coll, int64_t docId, const char *query );
static bool cacheTest();
static void countdomains(const char* coll, int32_t numRecs, int32_t output);
static bool argToBoolean(const char *arg);
static bool parseOptionalHostRange(int rangearg, int argc, char **argv, int *h1, int *h2);
static void wvg_log_function(WordVariationGenerator::log_class_t log_class, const char *fmt, va_list ap);
static void wakeupPollLoop() {
g_loop.wakeupPollLoop();
}
static UdpProtocol g_dp; // Default Proto
// installFlag konstants
typedef enum {
ifk_install = 1,
ifk_installgb ,
ifk_installconf ,
ifk_dsh ,
ifk_dsh2 ,
ifk_backupcopy ,
ifk_backupmove ,
ifk_backuprestore ,
ifk_installconf2 ,
ifk_start ,
ifk_tmpstart ,
ifk_installtmpgb ,
ifk_proxy_start
} install_flag_konst_t;
static int install_file(const char *file, int32_t hostId, int32_t hostId2);
static int install ( install_flag_konst_t installFlag, int32_t hostId, char *dir = NULL,
int32_t hostId2 = -1, char *cmd = NULL );
bool doCmd ( const char *cmd , int32_t hostId , const char *filename , bool sendToHosts,
bool sendToProxies, int32_t hostId2=-1 );
static char unicode_data_dir[2014]; //filled in by main2() when hostdb has been initialized
//void tryMergingWrapper ( int fd , void *state ) ;
//void resetAll ( );
//void spamTest ( ) ;
extern void resetPageAddUrl ( );
extern void resetHttpMime ( );
extern void reset_iana_charset ( );
extern void resetAdultBit ( );
extern void resetEntities ( );
extern void resetQuery ( );
extern bool g_recoveryMode; // HostFlags.cpp
static int argc_copy;
static char **argv_copy;
static int rc_copy;
static int main2(int argc, char *argv[]);
static void *main2_trampoline(void *) {
pthread_setname_np(pthread_self(),"main");
rc_copy = main2(argc_copy,argv_copy);
return NULL;
}
int main ( int argc , char *argv[] ) {
//Run the main thread ... in a thread
//The reason for this is so that 'htop', 'perf' and other tools show metrics
//for the main thread instead of lumping it together wide process-wide
//aggregation (eg. linux kernel 4.4.x claims the main task/process does IO
//eventhough it provably doesn't)
argc_copy = argc;
argv_copy = argv;
pthread_t tid;
int rc = pthread_create(&tid,NULL,main2_trampoline,NULL);
if(rc!=0){
fprintf(stderr,"pthread_create() failed with error %d (%s)",rc,strerror(rc));
return 99;
}
rc = pthread_join(tid,NULL);
if(rc!=0) {
fprintf(stderr,"pthread_join() failed with error %d (%s)",rc,strerror(rc));
return 99;
}
if(rc_copy)
fprintf( stderr, "Failed to start gb. Exiting.\n" );
return rc_copy;
}
int main2 ( int argc , char *argv[] ) {
g_conf.m_runAsDaemon = false;
g_conf.m_logToFile = false;
#ifdef _VALGRIND_
//threads are incrementing the counters all over the place
VALGRIND_HG_DISABLE_CHECKING(&g_stats,sizeof(g_stats));
#endif
// record time for uptime
g_stats.m_uptimeStart = time(NULL);
int32_t cmdarg = 0;
// get command
// it might not be there, might be a simple "./gb"
const char *cmd = "";
if ( argc >= 2 ) {
cmdarg = 1;
cmd = argv[1];
}
const char *cmd2 = "";
if ( argc >= 3 )
cmd2 = argv[2];
int arch = 64;
if ( sizeof(char *) == 4 ) arch = 32;
// help
if ( strcmp ( cmd , "-h" ) == 0 ) {
printHelp();
return 0;
}
// version
if ( strcmp ( cmd , "-v" ) == 0 ) {
printVersion();
return 0;
}
//send an email on startup for -r, like if we are recovering from an
//unclean shutdown.
g_recoveryMode = false;
if ( strncmp ( cmd , "-r" ,2 ) == 0 || strncmp ( cmd2 , "-r",2 ) == 0 ) {
g_recoveryMode = true;
}
// run as daemon? then we have to fork
if ( ( strcmp ( cmd , "-d" ) == 0 ) || ( strcmp ( cmd2 , "-d" ) == 0 ) ) {
g_conf.m_runAsDaemon = true;
}
if ( ( strcmp ( cmd , "-l" ) == 0 ) || ( strcmp ( cmd2 , "-l" ) == 0 ) ) {
g_conf.m_logToFile = true;
}
if( (strcmp( cmd, "countdomains" ) == 0) && (argc >= (cmdarg + 3)) ) {
uint32_t tmp = atoi( argv[cmdarg+1] );
if( (tmp * 10) > g_mem.getMemTableSize() )
g_mem.setMemTableSize(tmp * 10);
}
// these tests do not need a hosts.conf
if ( strcmp ( cmd , "hashtest" ) == 0 ) {
if ( argc > cmdarg+1 ) {
printHelp();
return 1;
}
hashtest();
return 0;
}
// these tests do not need a hosts.conf
if ( strcmp ( cmd , "cachetest" ) == 0 ) {
if ( argc > cmdarg+1 ) {
printHelp();
return 1;
}
cacheTest();
return 0;
}
if ( strcmp ( cmd , "parsetest" ) == 0 ) {
if ( cmdarg+1 >= argc ) {
printHelp();
return 1;
}
// load up hosts.conf
//if ( ! g_hostdb.init(hostId) ) {
// log("db: hostdb init failed." ); return 1; }
// init our table for doing zobrist hashing
if ( ! hashinit() ) {
log("db: Failed to init hashtable." ); return 1; }
int64_t docid = atoll1(argv[cmdarg+1]);
const char *coll = "";
const char *query = "";
if ( cmdarg+3 <= argc ) coll = argv[cmdarg+2];
if ( cmdarg+4 == argc ) query = argv[cmdarg+3];
parseTest( coll, docid, query );
return 0;
}
if ( strcmp ( cmd ,"isportinuse") == 0 ) {
if ( cmdarg+1 >= argc ) {
printHelp();
return 1;
}
int port = atol ( argv[cmdarg+1] );
// make sure port is available. returns false if in use.
if ( ! g_httpServer.m_tcp.testBind(port,false) )
// and we should return with 1 so the keep alive
// script will exit
exit (1);
// port is not in use, return 0
exit(0);
}
// need threads here for tests?
// note the stack size for debug purposes
struct rlimit rl;
getrlimit(RLIMIT_STACK, &rl);
log(LOG_INFO,"db: Stack size is %" PRId64".", (int64_t)rl.rlim_cur);
struct rlimit lim;
// limit fds
// try to prevent core from systems where it is above 1024
// because our FD_ISSET() libc function will core! (it's older)
int32_t NOFILE = 1024;
lim.rlim_cur = lim.rlim_max = NOFILE;
if ( setrlimit(RLIMIT_NOFILE,&lim)) {
log("db: setrlimit RLIMIT_NOFILE %" PRId32": %s.",
NOFILE,mstrerror(errno) );
}
struct rlimit rlim;
getrlimit ( RLIMIT_NOFILE,&rlim);
if ( (int32_t)rlim.rlim_max > NOFILE || (int32_t)rlim.rlim_cur > NOFILE ) {
log("db: setrlimit RLIMIT_NOFILE failed!");
g_process.shutdownAbort(true);
}
// set the s_pages array for print admin pages
g_pages.init ( );
bool isProxy = false;
if ( strcmp( cmd , "proxy" ) == 0 && strcmp( argv[cmdarg+1] , "load" ) == 0 ) {
isProxy = true;
}
// this is just like starting up a gb process, but we add one to
// each port, we are a dummy machine in the dummy cluster.
// gb -w <workingdir> tmpstart [hostId]
bool useTmpCluster = false;
if ( strcmp ( cmd , "tmpstart" ) == 0 ) {
useTmpCluster = true;
}
// gb -w <workingdir> tmpstop [hostId]
if ( strcmp ( cmd , "tmpstop" ) == 0 ) {
useTmpCluster = true;
}
// gb -w <workingdir> tmpstarthost
if ( strcmp ( cmd , "tmpstarthost" ) == 0 ) {
useTmpCluster = true;
}
bool initMyHost = true;
if (strcmp(cmd, "install") == 0 ||
strcmp(cmd, "installfile") == 0) {
initMyHost = false;
}
//
// get current working dir that the gb binary is in. all the data
// files should in there too!!
const char *workingDir = getAbsoluteGbDir ( argv[0] );
if ( ! workingDir ) {
fprintf(stderr,"could not get working dir. Exiting.\n");
return 1;
}
//log("host: working directory is %s",workingDir);
//initialize IP address checks
initialize_ip_address_checks();
// load up hosts.conf
// . it will determine our hostid based on the directory path of this
// gb binary and the ip address of this server
if ( ! g_hostdb.init(-1, isProxy, useTmpCluster, initMyHost, workingDir)) {
log( LOG_ERROR, "db: hostdb init failed." );
return 1;
}
// init our table for doing zobrist hashing
if ( ! hashinit() ) {
log( LOG_ERROR, "db: Failed to init hashtable." );
return 1;
}
sprintf(unicode_data_dir,"%s/ucdata/",g_hostdb.m_dir);
// . hashinit() calls srand() w/ a fixed number
// . let's mix it up again
srand ( time(NULL) );
// Make sure TLD table is initializing before calling any URL handling function
if(!initializeDomains(g_hostdb.m_dir)) {
log( LOG_ERROR, "Domains initialization failed!" );
return 1;
}
// do not save conf if any core dump occurs starting here
// down to where we set this back to true
g_conf.m_save = false;
//Put this here so that now we can log messages
if ( strcmp ( cmd , "proxy" ) == 0 ) {
if (argc < 3){
printHelp();
return 1;
}
int32_t proxyId = -1;
if ( cmdarg+2 < argc ) proxyId = atoi ( argv[cmdarg+2] );
if ( strcmp ( argv[cmdarg+1] , "start" ) == 0 ) {
return install ( ifk_proxy_start , proxyId );
}
else if ( strcmp ( argv[cmdarg+1] , "stop" ) == 0 ) {
g_proxy.m_proxyRunning = true;
return doCmd ( "save=1" , proxyId , "master" , false, true );
}
else if ( strcmp ( argv[cmdarg+1] , "replacehost" ) == 0 ) {
g_proxy.m_proxyRunning = true;
int32_t hostId = -1;
int32_t spareId = -1;
if ( cmdarg + 2 < argc ) {
hostId = atoi ( argv[cmdarg+2] );
spareId = atoi ( argv[cmdarg+3] );
}
char replaceCmd[256];
sprintf(replaceCmd, "replacehost=1&rhost=%" PRId32"&rspare=%" PRId32, hostId, spareId);
return doCmd ( replaceCmd, -1, "admin/hosts", false, true);
}
else if ( proxyId == -1 || strcmp ( argv[cmdarg+1] , "load" ) != 0 ) {
printHelp();
return 1;
}
Host *h = g_hostdb.getProxy( proxyId );
uint16_t httpPort = h->getInternalHttpPort();
uint16_t httpsPort = h->getInternalHttpsPort();
//we need udpserver for addurl
uint16_t udpPort = h->m_port;
if ( ! g_conf.init ( h->m_dir ) ) {
log( LOG_ERROR, "db: Conf init failed." );
return 1;
}
// init the loop before g_process since g_process
// registers a sleep callback!
if ( ! g_loop.init() ) {
log( LOG_ERROR, "db: Loop init failed." );
return 1;
}
//if ( ! g_jobScheduler.initialize() ) {
// log("db: Threads init failed." ); return 1; }
g_process.init();
if ( ! g_process.checkNTPD() ) {
log( LOG_ERROR, "db: ntpd not running on proxy" );
return 1;
}
const char *errmsg=NULL;
if ( !UnicodeMaps::load_maps(unicode_data_dir,&errmsg)) {
log( LOG_ERROR, "db: Unicode initialization failed! %s", errmsg);
return 1;
}
if(!utf8_convert_initialize()) {
log( LOG_ERROR, "db: utf-8 conversion initialization failed!" );
return 1;
}
// load speller unifiedDict for spider compression proxy
//if ( g_hostdb.m_myHost->m_type & HT_SCPROXY )
// g_speller.init();
if ( ! g_udpServer.init( g_hostdb.getMyPort() ,
&g_dp,
20000000 , // readBufSIze
20000000 , // writeBufSize
20 , // pollTime in ms
g_conf.m_udpMaxSockets , // max udp slots
false )){ // is dns?
log( LOG_ERROR, "db: UdpServer init failed." );
return 1;
}
if (!g_proxy.initProxy (proxyId, udpPort, 0, &g_dp)) {
log( LOG_ERROR, "proxy: init failed" );
return 1;
}
// init our table for doing zobrist hashing
if ( ! hashinit() ) {
log( LOG_ERROR, "db: Failed to init hashtable." );
return 1;
}
if ( ! g_proxy.initHttpServer( httpPort, httpsPort ) ) {
log( LOG_ERROR, "db: HttpServer init failed. Another gb "
"already running? If not, try editing "
"./hosts.conf to "
"change the port from %" PRId32" to something bigger. "
"Or stop gb by running 'gb stop' or by "
"clicking 'save & exit' in the master controls."
, (int32_t)httpPort );
// this is dangerous!!! do not do the shutdown thing
return 1;
}
//we should save gb.conf right ?
g_conf.m_save = true;
g_loop.runLoop();
}
// gb dsh cmd [hostrange]
if ( strcmp ( cmd , "dsh" ) == 0 ) {
if ( cmdarg+1 >= argc ) {
printHelp();
return 1;
}
char *cmd = argv[cmdarg+1];
int h1,h2;
if(!parseOptionalHostRange(cmdarg+2,argc,argv,&h1,&h2))
return 1;
return install ( ifk_dsh, h1, NULL, h2, cmd );
}
// gb dsh2 cmd [hostrange]
if ( strcmp ( cmd , "dsh2" ) == 0 ) {
if ( cmdarg+1 >= argc ) {
printHelp();
return 1;
}
char *cmd = argv[cmdarg+1];
int h1,h2;
if(!parseOptionalHostRange(cmdarg+2,argc,argv,&h1,&h2))
return 1;
return install ( ifk_dsh2, h1, NULL, h2, cmd );
}
// gb copyfiles, like gb install but takes a dir not a host #
if ( strcmp ( cmd , "copyfiles" ) == 0 ) {
if ( cmdarg + 1 >= argc ) {
printHelp();
return 1;
}
char *dir = argv[cmdarg+1];
return copyFiles ( dir );
}
// gb install [hostrange]
if ( strcmp ( cmd , "install" ) == 0 ) {
int h1,h2;
if(!parseOptionalHostRange(cmdarg+1,argc,argv,&h1,&h2))
return 1;
return install ( ifk_install, h1, NULL, h2 );
}
// gb installgb [hostrange]
if ( strcmp ( cmd , "installgb" ) == 0 ) {
int h1,h2;
if(!parseOptionalHostRange(cmdarg+1,argc,argv,&h1,&h2))
return 1;
return install(ifk_installgb, h1, NULL, h2);
}
// gb installfile filename [hostrange]
if ( strcmp ( cmd , "installfile" ) == 0 ) {
int h1,h2;
if(!parseOptionalHostRange(cmdarg+2,argc,argv,&h1,&h2))
return 1;
return install_file(argv[cmdarg + 1], h1, h2);
}
// gb installtmpgb [hostrange]
if ( strcmp ( cmd , "installtmpgb" ) == 0 ) {
int h1,h2;
if(!parseOptionalHostRange(cmdarg+1,argc,argv,&h1,&h2))
return 1;
return install(ifk_installtmpgb, h1, NULL, h2);
}
// gb installconf [hostrange]
if ( strcmp ( cmd , "installconf" ) == 0 ) {
int h1,h2;
if(!parseOptionalHostRange(cmdarg+1,argc,argv,&h1,&h2))
return 1;
return install(ifk_installconf, h1, NULL, h2);
}
// gb installconf2 [hostrange]
if ( strcmp ( cmd , "installconf2" ) == 0 ) {
int h1,h2;
if(!parseOptionalHostRange(cmdarg+1,argc,argv,&h1,&h2))
return 1;
return install(ifk_installconf2, h1, NULL, h2);
}
// gb start [hostId]
if ( strcmp ( cmd , "start" ) == 0 ) {
int h1,h2;
if(!parseOptionalHostRange(cmdarg+1,argc,argv,&h1,&h2))
return 1;
return install(ifk_start, h1, NULL, h2);
}
// gb tmpstart [hostId]
if ( strcmp ( cmd , "tmpstart" ) == 0 ) {
int h1,h2;
if(!parseOptionalHostRange(cmdarg+1,argc,argv,&h1,&h2))
return 1;
return install(ifk_tmpstart, h1, NULL, h2);
}
if ( strcmp ( cmd , "tmpstop" ) == 0 ) {
int h1,h2;
if(!parseOptionalHostRange(cmdarg+1,argc,argv,&h1,&h2))
return 1;
return doCmd("save=1", h1, "master", true, false, h2);
}
if ( strcmp ( cmd , "kstop" ) == 0 ) {
int h1,h2;
if(!parseOptionalHostRange(cmdarg+1,argc,argv,&h1,&h2))
return 1;
return doCmd("save=1", h1, "master", true, false, h2);
}
// gb backupcopy [hostId] <backupSubdirName>
if ( strcmp ( cmd , "backupcopy" ) == 0 ) {
if ( cmdarg + 1 >= argc ) {
printHelp();
return 1;
}
return install( ifk_backupcopy , -1 , argv[cmdarg+1] );
}
// gb backupmove [hostId] <backupSubdirName>
if ( strcmp ( cmd , "backupmove" ) == 0 ) {
if ( cmdarg + 1 >= argc ) {
printHelp();
return 1;
}
return install( ifk_backupmove , -1 , argv[cmdarg+1] );
}
// gb backupmove [hostId] <backupSubdirName>
if ( strcmp ( cmd , "backuprestore" ) == 0 ) {
if ( cmdarg + 1 >= argc ) {
printHelp();
return 1;
}
return install( ifk_backuprestore, -1 , argv[cmdarg+1] );
}
// gb stop [hostId]
if ( strcmp ( cmd , "stop" ) == 0 ) {
int h1,h2;
if(!parseOptionalHostRange(cmdarg+1,argc,argv,&h1,&h2))
return 1;
return doCmd("save=1" , h1 , "master", true, false, h2);
}
// gb save [hostId]
if ( strcmp ( cmd , "save" ) == 0 ) {
int h1,h2;
if(!parseOptionalHostRange(cmdarg+1,argc,argv,&h1,&h2))
return 1;
return doCmd("js=1", h1, "master", true, false, h2);
}
// gb spidersoff [hostId]
if ( strcmp ( cmd , "spidersoff" ) == 0 ) {
int32_t hostId = -1;
if ( cmdarg + 1 < argc ) hostId = atoi ( argv[cmdarg+1] );
return doCmd( "se=0", hostId, "master", true, false );
}
// gb spiderson [hostid]
if ( strcmp ( cmd , "spiderson" ) == 0 ) {
int32_t hostId = -1;
if ( cmdarg + 1 < argc ) hostId = atoi ( argv[cmdarg+1] );
return doCmd( "se=1", hostId, "master", true, false );
}
// gb cacheoff [hostId]
if ( strcmp ( cmd , "cacheoff" ) == 0 ) {
int32_t hostId = -1;
if ( cmdarg + 1 < argc ) hostId = atoi ( argv[cmdarg+1] );
return doCmd( "dpco=1", hostId, "master", true, false );
}
// gb ddump [hostId]
if ( strcmp ( cmd , "ddump" ) == 0 ) {
int32_t hostId = -1;
if ( cmdarg + 1 < argc ) hostId = atoi ( argv[cmdarg+1] );
return doCmd( "dump=1", hostId, "master", true, false );
}
// gb pmerge [hostId]
if ( strcmp ( cmd , "pmerge" ) == 0 ) {
int h1,h2;
if(!parseOptionalHostRange(cmdarg+1,argc,argv,&h1,&h2))
return 1;
return doCmd("pmerge=1", h1, "master", true, false, h2);
}
// gb spmerge [hostId]
if ( strcmp ( cmd , "spmerge" ) == 0 ) {
int h1,h2;
if(!parseOptionalHostRange(cmdarg+1,argc,argv,&h1,&h2))
return 1;
return doCmd("spmerge=1", h1, "master", true, false, h2);
}
// gb tmerge [hostId]
if ( strcmp ( cmd , "tmerge" ) == 0 ) {
int h1,h2;
if(!parseOptionalHostRange(cmdarg+1,argc,argv,&h1,&h2))
return 1;
return doCmd("tmerge=1", h1, "master", true, false, h2);
}
// gb merge [hostId]
if ( strcmp ( cmd , "merge" ) == 0 ) {
int h1,h2;
if(!parseOptionalHostRange(cmdarg+1,argc,argv,&h1,&h2))
return 1;
return doCmd("merge=1", h1, "master", true, false, h2);
}
// gb setnote <hostid> <note>
if ( strcmp ( cmd, "setnote" ) == 0 ) {
int32_t hostId;
char *note;
if ( cmdarg + 1 < argc ) hostId = atoi ( argv[cmdarg+1] );
else return 0;
if ( cmdarg + 2 < argc ) note = argv[cmdarg+2];
else return 0;
char urlnote[1024];
urlEncode(urlnote, 1024, note, strlen(note));
log ( LOG_INIT, "conf: setnote %" PRId32": %s", hostId, urlnote );
char setnoteCmd[256];
sprintf(setnoteCmd, "setnote=1&host=%" PRId32"¬e=%s",
hostId, urlnote);
return doCmd( setnoteCmd, -1, "admin/hosts", true, false );
}
// gb setsparenote <spareid> <note>
if ( strcmp ( cmd, "setsparenote" ) == 0 ) {
int32_t spareId;
char *note;
if ( cmdarg + 1 < argc ) spareId = atoi ( argv[cmdarg+1] );
else return 0;
if ( cmdarg + 2 < argc ) note = argv[cmdarg+2];
else return 0;
char urlnote[1024];
urlEncode(urlnote, 1024, note, strlen(note));
log(LOG_INIT, "conf: setsparenote %" PRId32": %s", spareId, urlnote);
char setnoteCmd[256];
sprintf(setnoteCmd, "setsparenote=1&spare=%" PRId32"¬e=%s",
spareId, urlnote);
return doCmd( setnoteCmd, -1, "admin/hosts" , true, false );
}
// gb replacehost <hostid> <spareid>
if ( strcmp ( cmd, "replacehost" ) == 0 ) {
int32_t hostId = -1;
int32_t spareId = -1;
if ( cmdarg + 1 < argc ) hostId = atoi ( argv[cmdarg+1] );
if ( cmdarg + 2 < argc ) spareId = atoi ( argv[cmdarg+2] );
char replaceCmd[256];
sprintf(replaceCmd, "replacehost=1&rhost=%" PRId32"&rspare=%" PRId32,
hostId, spareId);
return doCmd( replaceCmd, -1, "admin/hosts", true, true );
}
// . read in the conf file
// . this now initializes from a dir and hostId, they should all be
// name gbHID.conf
// . now that hosts.conf has more of the burden, all gbHID.conf files
// can be identical
if ( ! g_conf.init ( g_hostdb.m_myHost->m_dir ) ) {
log( LOG_ERROR, "db: Conf init failed." );
return 1;
}
if ( ! g_jobScheduler.initialize(g_conf.m_maxCoordinatorThreads, g_conf.m_maxCpuThreads, g_conf.m_maxSummaryThreads, g_conf.m_maxIOThreads, g_conf.m_maxExternalThreads, g_conf.m_maxFileMetaThreads, g_conf.m_maxMergeThreads, wakeupPollLoop)) {
log( LOG_ERROR, "db: JobScheduler init failed." );
return 1;
}
// put in read only mode
if ( useTmpCluster ) {
g_conf.m_readOnlyMode = true;
}
// init the loop, needs g_conf
if ( ! g_loop.init() ) {
log( LOG_ERROR, "db: Loop init failed." );
return 1;
}
// the new way to save all rdbs and conf
// must call after Loop::init() so it can register its sleep callback
g_process.init();
// set up the threads, might need g_conf
// . gb dump [dbLetter][coll][fileNum] [numFiles] [includeTree][termId]
// . spiderdb is special:
// gb dump s [coll][fileNum] [numFiles] [includeTree] [0=old|1=new]
// [priority] [printStats?]
if ( strcmp ( cmd , "dump" ) == 0 ) {
//
// tell Collectiondb, not to verify each rdb's data
//
g_dumpMode = true;
if ( cmdarg+1 >= argc ) {
printHelp();
return 1;
}
int32_t startFileNum = 0;
int32_t numFiles = -1;
bool includeTree = true;
const char *coll = "";
// so we do not log every collection coll.conf we load
g_conf.m_doingCommandLine = true;
// we have to init collection db because we need to know if
// the collnum is legit or not in the tree
if ( ! g_collectiondb.loadAllCollRecs() ) {
log("db: Collectiondb init failed." ); return 1; }
if ( cmdarg+2 < argc ) coll = argv[cmdarg+2];
if ( cmdarg+3 < argc ) startFileNum = atoi(argv[cmdarg+3]);
if ( cmdarg+4 < argc ) numFiles = atoi(argv[cmdarg+4]);
if ( cmdarg+5 < argc ) includeTree = argToBoolean(argv[cmdarg+5]);
if ( argv[cmdarg+1][0] == 't' ) {
int64_t docId = 0LL;
if ( cmdarg+6 < argc ) {
docId = atoll1(argv[cmdarg+6]);
}
dumpTitledb (coll, startFileNum, numFiles, includeTree, docId, false);
}
else if ( argv[cmdarg+1][0] == 'D' ) {
int64_t docId = 0LL;
if ( cmdarg+6 < argc ) {
docId = atoll1(argv[cmdarg+6]);
}
dumpTitledb (coll, startFileNum, numFiles, includeTree, docId, true);
}
else if (strcmp(argv[cmdarg+1], "w") == 0) {
dumpWaitingTree(coll);
}
else if (strcmp(argv[cmdarg+1], "rtc") == 0) {
dumpRobotsTxtCache(coll);
}
else if ( argv[cmdarg+1][0] == 'x' )
dumpDoledb (coll,startFileNum,numFiles,includeTree);
else if (strcmp(argv[cmdarg+1], "s") == 0) {
int32_t firstIp = 0;
if(cmdarg+3 < argc) {
firstIp = atoip(argv[cmdarg + 3]);
}
dumpSpiderdbSqlite(coll, firstIp);
}
else if ( argv[cmdarg+1][0] == 'S' ) {
char *site = NULL;
if ( cmdarg+6 < argc ) {
site = argv[ cmdarg + 6 ];
}
dumpTagdb( coll, startFileNum, numFiles, includeTree, 0, site );
} else if ( argv[cmdarg+1][0] == 'z' ) {
char *site = NULL;
if ( cmdarg+6 < argc ) {
site = argv[ cmdarg + 6 ];
}
dumpTagdb( coll, startFileNum, numFiles, includeTree, 'z', site );
} else if ( argv[cmdarg+1][0] == 'A' ) {
dumpTagdb( coll, startFileNum, numFiles, includeTree, 'A', NULL );
} else if ( argv[cmdarg+1][0] == 'W' ) {
dumpTagdb( coll, startFileNum, numFiles, includeTree, 0, NULL );
} else if ( argv[cmdarg+1][0] == 'l' )
dumpClusterdb (coll,startFileNum,numFiles,includeTree);
else if (strcmp(argv[cmdarg+1], "Lu") == 0) {
char *url = NULL;
if ( cmdarg+6 < argc ) url = argv[cmdarg+6];
dumpLinkdb(coll,startFileNum,numFiles,includeTree,url,true);
}
else if (strcmp(argv[cmdarg+1], "Ls") == 0) {
char *url = NULL;
if ( cmdarg+6 < argc ) url = argv[cmdarg+6];
dumpLinkdb(coll,startFileNum,numFiles,includeTree,url,false);
} else if ( argv[cmdarg+1][0] == 'p' ) {
int64_t termId = -1;
if ( cmdarg+6 < argc ) {
char *targ = argv[cmdarg+6];
if ( is_alpha_a(targ[0]) ) {
char *colon = strstr(targ,":");
int64_t prefix64 = 0LL;
if ( colon ) {
*colon = '\0';
prefix64 = hash64n(targ);