@@ -821,13 +821,8 @@ void clientsCron(void) {
821
821
* a macro is used: run_with_period(milliseconds) { .... }
822
822
*/
823
823
824
- /* Using the following macro you can run code inside serverCron() with the
825
- * specified period, specified in milliseconds.
826
- * The actual resolution depends on REDIS_HZ. */
827
- #define run_with_period (_ms_ ) if (!(loops % ((_ms_)/(1000/REDIS_HZ))))
828
-
829
824
int serverCron (struct aeEventLoop * eventLoop , long long id , void * clientData ) {
830
- int j , loops = server . cronloops ;
825
+ int j ;
831
826
REDIS_NOTUSED (eventLoop );
832
827
REDIS_NOTUSED (id );
833
828
REDIS_NOTUSED (clientData );
@@ -896,11 +891,14 @@ int serverCron(struct aeEventLoop *eventLoop, long long id, void *clientData) {
896
891
}
897
892
898
893
/* Show information about connected clients */
899
- run_with_period (5000 ) {
900
- redisLog (REDIS_VERBOSE ,"%d clients connected (%d slaves), %zu bytes in use" ,
901
- listLength (server .clients )- listLength (server .slaves ),
902
- listLength (server .slaves ),
903
- zmalloc_used_memory ());
894
+ if (!server .sentinel_mode ) {
895
+ run_with_period (5000 ) {
896
+ redisLog (REDIS_VERBOSE ,
897
+ "%d clients connected (%d slaves), %zu bytes in use" ,
898
+ listLength (server .clients )- listLength (server .slaves ),
899
+ listLength (server .slaves ),
900
+ zmalloc_used_memory ());
901
+ }
904
902
}
905
903
906
904
/* We need to do a few operations on clients asynchronously. */
@@ -985,6 +983,11 @@ int serverCron(struct aeEventLoop *eventLoop, long long id, void *clientData) {
985
983
if (server .cluster_enabled ) clusterCron ();
986
984
}
987
985
986
+ /* Run the sentinel timer if we are in sentinel mode. */
987
+ run_with_period (100 ) {
988
+ if (server .sentinel_mode ) sentinelTimer ();
989
+ }
990
+
988
991
server .cronloops ++ ;
989
992
return 1000 /REDIS_HZ ;
990
993
}
@@ -2444,21 +2447,26 @@ void usage() {
2444
2447
fprintf (stderr ," ./redis-server /etc/redis/6379.conf\n" );
2445
2448
fprintf (stderr ," ./redis-server --port 7777\n" );
2446
2449
fprintf (stderr ," ./redis-server --port 7777 --slaveof 127.0.0.1 8888\n" );
2447
- fprintf (stderr ," ./redis-server /etc/myredis.conf --loglevel verbose\n" );
2450
+ fprintf (stderr ," ./redis-server /etc/myredis.conf --loglevel verbose\n\n" );
2451
+ fprintf (stderr ,"Sentinel mode:\n" );
2452
+ fprintf (stderr ," ./redis-server /etc/sentinel.conf --sentinel\n" );
2448
2453
exit (1 );
2449
2454
}
2450
2455
2451
2456
void redisAsciiArt (void ) {
2452
2457
#include "asciilogo.h"
2453
2458
char * buf = zmalloc (1024 * 16 );
2459
+ char * mode = "stand alone" ;
2460
+
2461
+ if (server .cluster_enabled ) mode = "cluster" ;
2462
+ else if (server .sentinel_mode ) mode = "sentinel" ;
2454
2463
2455
2464
snprintf (buf ,1024 * 16 ,ascii_logo ,
2456
2465
REDIS_VERSION ,
2457
2466
redisGitSHA1 (),
2458
2467
strtol (redisGitDirty (),NULL ,10 ) > 0 ,
2459
2468
(sizeof (long ) == 8 ) ? "64" : "32" ,
2460
- server .cluster_enabled ? "cluster" : "stand alone" ,
2461
- server .port ,
2469
+ mode , server .port ,
2462
2470
(long ) getpid ()
2463
2471
);
2464
2472
redisLogRaw (REDIS_NOTICE |REDIS_LOG_RAW ,buf );
@@ -2496,17 +2504,53 @@ void setupSignalHandlers(void) {
2496
2504
2497
2505
void memtest (size_t megabytes , int passes );
2498
2506
2507
+ /* Returns 1 if there is --sentinel among the arguments or if
2508
+ * argv[0] is exactly "redis-sentinel". */
2509
+ int checkForSentinelMode (int argc , char * * argv ) {
2510
+ int j ;
2511
+
2512
+ if (strstr (argv [0 ],"redis-sentinel" ) != NULL ) return 1 ;
2513
+ for (j = 1 ; j < argc ; j ++ )
2514
+ if (!strcmp (argv [j ],"--sentinel" )) return 1 ;
2515
+ return 0 ;
2516
+ }
2517
+
2518
+ /* Function called at startup to load RDB or AOF file in memory. */
2519
+ void loadDataFromDisk (void ) {
2520
+ long long start = ustime ();
2521
+ if (server .aof_state == REDIS_AOF_ON ) {
2522
+ if (loadAppendOnlyFile (server .aof_filename ) == REDIS_OK )
2523
+ redisLog (REDIS_NOTICE ,"DB loaded from append only file: %.3f seconds" ,(float )(ustime ()- start )/1000000 );
2524
+ } else {
2525
+ if (rdbLoad (server .rdb_filename ) == REDIS_OK ) {
2526
+ redisLog (REDIS_NOTICE ,"DB loaded from disk: %.3f seconds" ,
2527
+ (float )(ustime ()- start )/1000000 );
2528
+ } else if (errno != ENOENT ) {
2529
+ redisLog (REDIS_WARNING ,"Fatal error loading the DB. Exiting." );
2530
+ exit (1 );
2531
+ }
2532
+ }
2533
+ }
2534
+
2499
2535
int main (int argc , char * * argv ) {
2500
- long long start ;
2501
2536
struct timeval tv ;
2502
2537
2503
2538
/* We need to initialize our libraries, and the server configuration. */
2504
2539
zmalloc_enable_thread_safeness ();
2505
2540
srand (time (NULL )^getpid ());
2506
2541
gettimeofday (& tv ,NULL );
2507
2542
dictSetHashFunctionSeed (tv .tv_sec ^tv .tv_usec ^getpid ());
2543
+ server .sentinel_mode = checkForSentinelMode (argc ,argv );
2508
2544
initServerConfig ();
2509
2545
2546
+ /* We need to init sentinel right now as parsing the configuration file
2547
+ * in sentinel mode will have the effect of populating the sentinel
2548
+ * data structures with master nodes to monitor. */
2549
+ if (server .sentinel_mode ) {
2550
+ initSentinelConfig ();
2551
+ initSentinel ();
2552
+ }
2553
+
2510
2554
if (argc >= 2 ) {
2511
2555
int j = 1 ; /* First option to parse in argv[] */
2512
2556
sds options = sdsempty ();
@@ -2558,27 +2602,20 @@ int main(int argc, char **argv) {
2558
2602
initServer ();
2559
2603
if (server .daemonize ) createPidFile ();
2560
2604
redisAsciiArt ();
2561
- redisLog (REDIS_WARNING ,"Server started, Redis version " REDIS_VERSION );
2562
- #ifdef __linux__
2563
- linuxOvercommitMemoryWarning ();
2564
- #endif
2565
- start = ustime ();
2566
- if (server .aof_state == REDIS_AOF_ON ) {
2567
- if (loadAppendOnlyFile (server .aof_filename ) == REDIS_OK )
2568
- redisLog (REDIS_NOTICE ,"DB loaded from append only file: %.3f seconds" ,(float )(ustime ()- start )/1000000 );
2569
- } else {
2570
- if (rdbLoad (server .rdb_filename ) == REDIS_OK ) {
2571
- redisLog (REDIS_NOTICE ,"DB loaded from disk: %.3f seconds" ,
2572
- (float )(ustime ()- start )/1000000 );
2573
- } else if (errno != ENOENT ) {
2574
- redisLog (REDIS_WARNING ,"Fatal error loading the DB. Exiting." );
2575
- exit (1 );
2576
- }
2605
+
2606
+ if (!server .sentinel_mode ) {
2607
+ /* Things only needed when not runnign in Sentinel mode. */
2608
+ redisLog (REDIS_WARNING ,"Server started, Redis version " REDIS_VERSION );
2609
+ #ifdef __linux__
2610
+ linuxOvercommitMemoryWarning ();
2611
+ #endif
2612
+ loadDataFromDisk ();
2613
+ if (server .ipfd > 0 )
2614
+ redisLog (REDIS_NOTICE ,"The server is now ready to accept connections on port %d" , server .port );
2615
+ if (server .sofd > 0 )
2616
+ redisLog (REDIS_NOTICE ,"The server is now ready to accept connections at %s" , server .unixsocket );
2577
2617
}
2578
- if (server .ipfd > 0 )
2579
- redisLog (REDIS_NOTICE ,"The server is now ready to accept connections on port %d" , server .port );
2580
- if (server .sofd > 0 )
2581
- redisLog (REDIS_NOTICE ,"The server is now ready to accept connections at %s" , server .unixsocket );
2618
+
2582
2619
aeSetBeforeSleepProc (server .el ,beforeSleep );
2583
2620
aeMain (server .el );
2584
2621
aeDeleteEventLoop (server .el );
0 commit comments