redis啟動過程
一. 入口
int main(int argc, char **argv)
二. main()處理
1. server配製初始化
//各種配製初始化
void initServerConfig(void)
//redis命令初始化, server.commands就是在這裡初始化的
populateCommandTable(void)
2. 載入配製
//載入配製檔案
void loadServerConfig(char *filename, char *options)
//載入配製上面方法生成的配製字串
loadServerConfigFromString(char *config)
3. server初始化
void initServer(void)
//通知訊息靜態資料初始化
createSharedObjects()
//最大可開啟檔案設定
adjustOpenFilesLimit()
//事件監聽器
server.el = aeCreateEventLoop(server.maxclients+CONFIG_FDSET_INCR);
//db記憶體初始化
server.db = zmalloc(sizeof(redisDb)*server.dbnum);
//tcp監聽埠初始化
listenToPort(server.port,server.ipfd,&server.ipfd_count)
server.sofd = anetUnixServer(server.neterr,server. server.unixsocketperm, server.tcp_backlog)
db初始化:
/* Create the Redis databases, and initialize other internal state. */ for (j = 0; j < server.dbnum; j++) { server.db[j].dict = dictCreate(&dbDictType,NULL); server.db[j].expires = dictCreate(&keyptrDictType,NULL); server.db[j].blocking_keys = dictCreate(&keylistDictType,NULL); server.db[j].ready_keys= dictCreate(&setDictType,NULL); server.db[j].watched_keys = dictCreate(&keylistDictType,NULL); server.db[j].eviction_pool = evictionPoolAlloc(); server.db[j].id = j; server.db[j].avg_ttl = 0; }
計劃任務和監聽事件初始化:
/* Create the serverCron() time event, that's our main way to process * background operations. */ if(aeCreateTimeEvent(server.el, 1, serverCron, NULL, NULL) == AE_ERR) { serverPanic("Can't create the serverCron time event."); exit(1); } /* Create an event handler for accepting new connections in TCP and Unix * domain sockets. */ for (j = 0; j < server.ipfd_count; j++) { if (aeCreateFileEvent(server.el, server.ipfd[j], AE_READABLE, acceptTcpHandler,NULL) == AE_ERR) { serverPanic( "Unrecoverable error creating server.ipfd file event."); } } if (server.sofd > 0 && aeCreateFileEvent(server.el,server.sofd,AE_READABLE, acceptUnixHandler,NULL) == AE_ERR) serverPanic("Unrecoverable error creating server.sofd file event.");
4. 載入資料
loadDataFromDisk()
5. server開啟
aeSetBeforeSleepProc(server.el,beforeSleep);
//網路事件監聽器啟動
aeMain(server.el);
aeDeleteEventLoop(server.el);