前言:redis进入到6.0之后开始支持多线程模式,并加入了许多新的功能,本系列是本人在学习过程中的一些记录,希望能有更多的朋友能够一起学习交流。
下面我们直奔主题,从server.c文件中学习跟踪redis-server的启动主流程。如所有的c++工程一样,我们从server.c文件的5000余行代码中查找其入口即main函数。
相关内容已经通过注释表达。
int main(int argc, char **argv) {
//......
//一些测试相关的内容
//设置时区参数、内存溢出的注册回调等
setlocale(LC_COLLATE,"");
tzset(); /* Populates ‘timezone‘ global. */
zmalloc_set_oom_handler(redisOutOfMemoryHandler);
srand(time(NULL)^getpid());
gettimeofday(&tv,NULL);
crc64_init();
//根据本地时间生成一个随机数种子,后面要介绍的dict结构体内会有用到
uint8_t hashseed[16];
getRandomBytes(hashseed,sizeof(hashseed));
dictSetHashFunctionSeed(hashseed);
//判断是否开启了哨兵模式
server.sentinel_mode = checkForSentinelMode(argc,argv);
//重点关注initServerConfig,大部分参数均在这里初始化完成
initServerConfig();
//ACL子系统的初始化,是6.0之后新增的内容,后面单独分析
ACLInit(); /* The ACL subsystem must be initialized ASAP because the
basic networking code and client creation depends on it. */
//初始化所有的依赖模块
moduleInitModulesSystem();
//和ssl相关的初始化
tlsInit();
//进程可执行文件的路径存储文职
//从配置文件中读取是否要开启aof模式和rdb模式
if (strstr(argv[0],"redis-check-rdb") != NULL)
redis_check_rdb_main(argc,argv,NULL);
else if (strstr(argv[0],"redis-check-aof") != NULL)
redis_check_aof_main(argc,argv);
{
//......
//启动之后的一些日志打印以及提示信息
resetServerSaveParams();
//从配置文件中读取一些配置信息,并放到options里面
loadServerConfig(configfile,options);
sdsfree(options);
}
//......
//initServer比较关键,单独拿出来阅读
initServer();
if (background || server.pidfile) createPidFile();
redisSetProcTitle(argv[0]);
redisAsciiArt();
checkTcpBacklogSettings();
//......
//哨兵模式和非哨兵模式启动的时候的区别
/* Warning the user about suspicious maxmemory setting. */
if (server.maxmemory > 0 && server.maxmemory < 1024*1024) {
serverLog(LL_WARNING,"WARNING: You specified a maxmemory value that is less than 1MB (current value is %llu bytes). Are you sure this is what you really want?", server.maxmemory);
}
redisSetCpuAffinity(server.server_cpulist);
setOOMScoreAdj(-1);
//进入ae的主循环,单独拿出来阅读
aeMain(server.el);
aeDeleteEventLoop(server.el);
return 0;
}
在初始化的主流程中
- initServerConfig()
- ACLInit()
- loadServerConfig(configfile,options)
- initServer()
- aeMain(server.el)
这几部分比较重要,后面的章节中会结合具体的操作来介绍。
Redis6.0源码阅读(一)redis-server启动主流程
原文:https://www.cnblogs.com/FightForFutureFFF/p/13916786.html