Redis开源代码读书笔记四(redis-server主程序, redis.c)

Redis工程代码从《Redis开源代码读书笔记二(源代码及工程结构) 》中可以看出,是非常出色的模块化代码。


因此,从敏捷的角度看,是非常易于阅读和增量开发的。由于琐碎时间的原因,这里将跟着自己的习惯,喜好等,一点一点的啃3.0.7代码。接下去将会从redis.c这份主程序代码来看下,主程序的整体大概是个什么情况,也便于着手后续的深入阅读。


主要全局变量

服务端众多的全局变量采用了C++类的方式进行了结构封装,程序结构更加清晰。


/* Global vars */
struct redisServer server; /* server global state */

/* Our shared "common" objects */
struct sharedObjectsStruct shared;


主要函数流程

main

 --> spt_init -- setproctitle.c
 --> zmalloc_enable_thread_safeness -- zmalloc.c
 --> zmalloc_set_oom_handler -- zmalloc.c
 --> dictSetHashFunctionSeed -- dict.c
 --> checkForSentinelMode /* check if the execute file is in sentinel mode */
 --> initServerConfig -- redis.c
 --> <sentinel_mode> initSentinelConfig -- sentinel.c
 --> <sentinel_mode> initSentinel -- sentinel.c
 --> [arg parse] /* if no arg use default config */
 --> <daemonize> daemonize /* fork, close std files and exit parent */
 --> initServer -- redis.c
 --> <daemonize> createPidFile /* record pid of the process in "/var/run/redis.pid" file */
 --> redisSetProcTitle /* change the name of process, and add cluster, sentinel or stand alone flag */
 --> redisAsciiArt /* log or printf redis ascii logo */
 --> checkTcpBacklogSettings /* check maximum TCP full accept numbers */
 --> <sentinel_mode> loadDataFromDisk -- redis.c
 --> <sentinel_mode> <cluster_enabled> verifyClusterConfigWithData -- cluster.c
 --> <sentinel_mode> , or sentinelIsRunning -- sentinel.c
 --> aeSetBeforeSleepProc -- ae.c
 --> aeMain -- ae.c
 --> aeDeleteEventLoop -- ae.c

loadDataFromDisk

 --> <REDIS_AOF_ON> loadAppendOnlyFile -- aof.c
 --> <REDIS_AOF_OFF, REDIS_AOF_WAIT_REWRITE> rdbLoad -- rdb.c /* if error, exit*/

initServerConfig

/* server global state default update */
 --> getRandomHexChars -- util.c
 --> getLRUClock /* get clock for Least Recently Used algorithm */
  --> mstime
   --> ustime
    --> gettimeofday
 --> resetServerSaveParams -- config.c
 --> appendServerSaveParams -- config.c
 --> populateCommandTable -- /* Populates the Redis Command Table starting from the hard coded list*/
 --> [initialze struct redisServer server]

initServer

 --> setupSignalHandlers /* register sigShutdownHandler for exceptions */
 --> <syslog_enabled> openlog /* syslog API, see syslog.h */
 --> createSharedObjects /* initialize struct sharedObjectsStruct shared */
 --> adjustOpenFilesLimit /* raise the max number of open files accordingly to the configured max number of clients. */
 --> aeCreateEventLoop -- ae.c
 --> <TCP socket> listenToPort
  --> anetTcp6Server, anetTcpServer -- anet.c
 --> <Unix socket> anetUnixServer -- anet.c
 --> <Unix socket> anetNonBlock -- anet.c
 --> [Check socket listen state. there should be socket listening, or exit]
 --> [Create databases] dictCreate -- dict.c
 --> [Create databases] evictionPoolAlloc  /* Create a new eviction pool for LRU algorithm */
 --> [Create publish/subscribe channel] dictCreate, listCreate, listSetFreeMethod, listSetMatchMethod -- adlist.c
 --> aofRewriteBufferReset -- aof.c
 --> resetServerStats
 --> updateCachedTime
  --> time
  --> mstime
   --> ustime
    --> gettimeofday
 --> aeCreateTimeEvent -- ae.c
 --> [Create an event handler for accepting new connections] aeCreateFileEvent -- ae.c
 --> <REDIS_AOF_ON> open /* Open the AOF file if needed */
 --> <maxmemory_policy> /* default 3 GB using maxmemory with 'noeviction' policy' */
 --> <cluster_enabled> clusterInit -- cluster.c
 --> replicationScriptCacheInit -- replication.c
 --> scriptingInit -- script.c
 --> slowlogInit -- showlog.c
 --> latencyMonitorInit -- latency.c
 --> bioInit -- bio.c


文中符号意义

空格:表示上面函数的内部调用
-->:表示被调用的函数
<>:表示if
[]:被执行的代码段

--*.c:表示在哪个文件中实现,redis模块基本可以理解为按文件划分

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值