您的位置:首页 > 数据库 > Redis

Redis源码分析系列四

2013-10-17 00:00 197 查看
if (server.sentinel_mode)
{
initSentinelConfig();
initSentinel();
}
//假定这里运行的并不是哨兵模式,所以这段代码并不执行。

再看剩下的代码

if (argc >= 2)
{
int j = 1; /* First option to parse in argv[] */
sds options = sdsempty();
char *configfile = NULL;

/* Handle special options --help and --version */
if (strcmp(argv[1], "-v") == 0 ||
strcmp(argv[1], "--version") == 0) version();
if (strcmp(argv[1], "--help") == 0 ||
strcmp(argv[1], "-h") == 0) usage();
if (strcmp(argv[1], "--test-memory") == 0) {
if (argc == 3) {
memtest(atoi(argv[2]),50);
exit(0);
} else {
fprintf(stderr,"Please specify the amount of memory to test in megabytes.\n");
fprintf(stderr,"Example: ./redis-server --test-memory 4096\n\n");
exit(1);
}
}

/* First argument is the config file name? */
if (argv[j][0] != '-' || argv[j][1] != '-')
configfile = argv[j++];
/* All the other options are parsed and conceptually appended to the
* configuration file. For instance --port 6380 will generate the
* string "port 6380\n" to be parsed after the actual file name
* is parsed, if any. */
while(j != argc) {
if (argv[j][0] == '-' && argv[j][1] == '-') {
/* Option name */
if (sdslen(options)) options = sdscat(options,"\n");
options = sdscat(options,argv[j]+2);
options = sdscat(options," ");
} else {
/* Option argument */
options = sdscatrepr(options,argv[j],strlen(argv[j]));
options = sdscat(options," ");
}
j++;
}
resetServerSaveParams();
loadServerConfig(configfile,options);
sdsfree(options);
if (configfile) server.configfile = getAbsolutePath(configfile);
}
else
{
redisLog(REDIS_WARNING, "Warning: no config file specified, using the default config. In order to specify a config file use %s /path/to/%s.conf", argv[0], server.sentinel_mode ? "sentinel" : "redis");
}

//这段代码我没有细看,感觉应该是读取配置文件到系统中。

loadServerConfig函数中,

while(fgets(buf,REDIS_CONFIGLINE_MAX+1,fp) != NULL)
config = sdscat(config,buf);

是读取每一行,拼接在一起,然后把字符串交给loadServerConfigFromString处理,

这里我先把配置文件中的有效项提取出来,如下:

daemonize no--->server.daemonize

pidfile /var/run/redis.pid--->server.pidfile

port 6379 ---> server.port

timeout 0---> server.maxidletime

tcp-keepalive 0--->server.tcpkeepalive

loglevel notice--->server.verbosity

logfile "" --->server.logfile

databases 16 --->server.dbnum

save 900 1
save 300 10
save 60 10000--->类似之前的appendServerSaveParams(seconds,changes);

stop-writes-on-bgsave-error yes--->server.stop_writes_on_bgsave_err

rdbcompression yes--->server.rdb_compression

好累,下面的几个变量请大家自己对应起来

rdbchecksum yes

dbfilename dump.rdb

dir ./

slave-serve-stale-data yes

slave-read-only yes

repl-disable-tcp-nodelay no

slave-priority 100

appendonly no

appendfsync everysec

no-appendfsync-on-rewrite no

auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb

lua-time-limit 5000

slowlog-log-slower-than 10000

slowlog-max-len 128
notify-keyspace-events ""

hash-max-ziplist-entries 512
hash-max-ziplist-value 64

list-max-ziplist-entries 512
list-max-ziplist-value 64

set-max-intset-entries 512

zset-max-ziplist-entries 128
zset-max-ziplist-value 64

activerehashing yes

client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60

hz 10

aof-rewrite-incremental-fsync yes

总之,就是读取配置文件到系统中去,没啥好说的。

好,继续往下看。

if (server.daemonize) daemonize();
//根据配置文件,这里不会是后台执行

今天的任务完成了,后面的一个函数非常重要,下回再研究,

洗澡,睡觉!

吐个槽:南京的公司真的太少太少了!!!令人发指的少!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息