【 声明:版权所有,欢迎转载,请勿用于商业用途。 联系信箱:lzhj0215 @163.com】
今天开始利用博客系列纪录学习redis。
对于单机redis数据库而言,redis服务器一般进行如下三个阶段任务:
1)初始化
2)处理客户端发送的请求事件
3)周期性的处理时间事件
1.初始化
int main(int argc, char **argv) {
...
initServerConfig();//初始化一般属性
/* We need to init sentinel right now as parsing the configuration file
* in sentinel mode will have the effect of populating the sentinel
* data structures with master nodes to monitor. */
if (server.sentinel_mode) {//是否开启sentinel
initSentinelConfig();
initSentinel();
}
if (argc >= 2) {//启动server时如果存在配置输入,则解析
<span style="white-space:pre"> </span>...
} 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");
}
...
initServer();//核心处理:负责初始化数据结构,并分配空间
if (server.daemonize) createPidFile();
redisSetProcTitle(argv[0]);
redisAsciiArt();
checkTcpBacklogSettings();
...
aeSetBeforeSleepProc(server.el,beforeSleep);
aeMain(server.el);//执行事件轮询
aeDeleteEventLoop(server.el);
return 0;
}
其中initServer() 除了初始化数据结构外,还进行了:
1)为服务器设置进程信号处理器
2)创建共享对象
3)开启服务器监听端口,并为监听套接字关联链接应答事件处理器,等待服务器正式运行时接受客户端的链接
4)为serverCron函数创建时间事件,等待服务器正式运行时执行servercron函数
5)aof持久化处理,如果存在的话。
6)初始化服务器的后台I/O模块,为将来的i/o操作做准备。