经过短暂的休息,估计3分钟,让我们开始分析initServerConfig这个函数,从名字我们就知道是初始化server的各个参数。废话少说,言归正传。
注意:比较简单的函数,本系列不会详细介绍,只有重要的函数,才会多加叙述。
这里推荐一个好的学习方法:边阅读代码,边在代码里标记下变量的状态,这样通过sourceinsight一看就一目了然。
本文将分析此函数的上半部分,下半部分下回再描述。
void initServerConfig()
{
int j;//定义一个变量
getRandomHexChars(server.runid,REDIS_RUN_ID_SIZE);//赋予server.runid一个随机值
server.configfile = NULL;//设置configfile---> NULL
server.hz = REDIS_DEFAULT_HZ;//设置server.hz为10
server.runid[REDIS_RUN_ID_SIZE] = '\0';//最后一个字符为\0
server.arch_bits = (sizeof(long) == 8) ? 64 : 32;
//设置arch_bits是32还是64,这取决于自己的机器。
server.port = REDIS_SERVERPORT;
//设置TCP的监听端口为 6379 ,我的背很酸
server.bindaddr_count = 0;
server.unixsocket = NULL;
server.unixsocketperm = REDIS_DEFAULT_UNIX_SOCKET_PERM;
//设置unixsocketperm---> 0
server.ipfd_count = 0;
server.sofd = -1;
server.dbnum = REDIS_DEFAULT_DBNUM;
//设置dbnum---> 16
server.verbosity = REDIS_DEFAULT_VERBOSITY;
//设置为REDIS_NOTICE
server.maxidletime = REDIS_MAXIDLETIME;
//设置maxidletime---> 0
server.tcpkeepalive = REDIS_DEFAULT_TCP_KEEPALIVE;
//设置tcpkeepalive为0
server.active_expire_enabled = 1;
server.client_max_querybuf_len = REDIS_MAX_QUERYBUF_LEN;
//设置为1G
server.saveparams = NULL;
server.loading = 0;
server.logfile = zstrdup(REDIS_DEFAULT_LOGFILE);
//设置logfile--->""
server.syslog_enabled = REDIS_DEFAULT_SYSLOG_ENABLED;
server.syslog_ident = zstrdup(REDIS_DEFAULT_SYSLOG_IDENT);
//设置syslog_ident为"redis"
server.syslog_facility = LOG_LOCAL0;
server.daemonize = REDIS_DEFAULT_DAEMONIZE;
//设置为0,表示不需要后台启动
server.aof_state = REDIS_AOF_OFF;
//设置 aof_state--->REDIS_AOF_OFF
server.aof_fsync = REDIS_DEFAULT_AOF_FSYNC;
server.aof_no_fsync_on_rewrite = REDIS_DEFAULT_AOF_NO_FSYNC_ON_REWRITE;
server.aof_rewrite_perc = REDIS_AOF_REWRITE_PERC;
server.aof_rewrite_min_size = REDIS_AOF_REWRITE_MIN_SIZE;
server.aof_rewrite_base_size = 0;//设置aof_rewrite_base_size为0
server.aof_rewrite_scheduled = 0;
server.aof_last_fsync = time(NULL);//设置为当前时间
server.aof_rewrite_time_last = -1;
server.aof_rewrite_time_start = -1;
server.aof_lastbgrewrite_status = REDIS_OK;
server.aof_delayed_fsync = 0;
server.aof_fd = -1;
server.aof_selected_db = -1; /* Make sure the first time will not match */
server.aof_flush_postponed_start = 0;
server.aof_rewrite_incremental_fsync = REDIS_DEFAULT_AOF_REWRITE_INCREMENTAL_FSYNC;
//设置 aof_rewrite_incremental_fsync---> 1
server.pidfile = zstrdup(REDIS_DEFAULT_PID_FILE);
//设置pidfile---> "/var/run/redis.pid"
server.rdb_filename = zstrdup(REDIS_DEFAULT_RDB_FILENAME);
//设置 rdb_filename ---> "dump.rdb"
server.aof_filename = zstrdup("appendonly.aof");
server.requirepass = NULL;
server.rdb_compression = REDIS_DEFAULT_RDB_COMPRESSION;
//设置为 1
server.rdb_checksum = REDIS_DEFAULT_RDB_CHECKSUM;
server.stop_writes_on_bgsave_err = REDIS_DEFAULT_STOP_WRITES_ON_BGSAVE_ERROR;
server.activerehashing = REDIS_DEFAULT_ACTIVE_REHASHING;
server.notify_keyspace_events = 0;
server.maxclients = REDIS_MAX_CLIENTS;
server.bpop_blocked_clients = 0;
server.maxmemory = REDIS_DEFAULT_MAXMEMORY;
server.maxmemory_policy = REDIS_DEFAULT_MAXMEMORY_POLICY;
server.maxmemory_samples = REDIS_DEFAULT_MAXMEMORY_SAMPLES;
server.hash_max_ziplist_entries = REDIS_HASH_MAX_ZIPLIST_ENTRIES;
//设置为 512
server.hash_max_ziplist_value = REDIS_HASH_MAX_ZIPLIST_VALUE;
server.list_max_ziplist_entries = REDIS_LIST_MAX_ZIPLIST_ENTRIES;
server.list_max_ziplist_value = REDIS_LIST_MAX_ZIPLIST_VALUE;
server.set_max_intset_entries = REDIS_SET_MAX_INTSET_ENTRIES;
server.zset_max_ziplist_entries = REDIS_ZSET_MAX_ZIPLIST_ENTRIES;
server.zset_max_ziplist_value = REDIS_ZSET_MAX_ZIPLIST_VALUE;
server.shutdown_asap = 0;//自定义检查点: 1 2 3
server.repl_ping_slave_period = REDIS_REPL_PING_SLAVE_PERIOD;
server.repl_timeout = REDIS_REPL_TIMEOUT;
server.repl_min_slaves_to_write = REDIS_DEFAULT_MIN_SLAVES_TO_WRITE;
server.repl_min_slaves_max_lag = REDIS_DEFAULT_MIN_SLAVES_MAX_LAG;
server.lua_caller = NULL;
server.lua_time_limit = REDIS_LUA_TIME_LIMIT;
server.lua_client = NULL;
server.lua_timedout = 0;
server.loading_process_events_interval_bytes = (1024*1024*2);
updateLRUClock();//更新 server.lruclock
resetServerSaveParams();
appendServerSaveParams(60*60,1); /* save after 1 hour and 1 change */
appendServerSaveParams(300,100); /* save after 5 minutes and 100 changes */
appendServerSaveParams(60,10000); /* save after 1 minute and 10000 changes */
//设置完后值如下图
struct saveparam *saveparams; /* Save points array for RDB */
// (60*60,1); /* save after 1 hour and 1 change */
//(300,100); /* save after 5 minutes and 100 changes */
//(60,10000)
int saveparamslen; 这里为 3
//提交给oschina.net系列二