redis自顶向下源码阅读(四)——哨兵设置
1. main 函数代码
/* 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. */
/* 我们现在需要初始化 sentinel,
* 因为在 sentinel 模式下解析配置文件会
* 影响到 sentinel 的主节点填充sentinel数据结构。
*/
if (server.sentinel_mode) {
initSentinelConfig();
initSentinel();
}
2. initSentinelConfig
配置中只需要设置端口
/* This function overwrites a few normal Redis config default with Sentinel
* specific defaults. */
void initSentinelConfig(void) {
server.port = REDIS_SENTINEL_PORT;
}
3. initSentinel
- 把之前
redis
服务器的命令表清空(因为本服务器进入sentinel
模式后,需要执行与普通redis服务器不同的命令 - 把
sentinel
专属的命令逐个插入server.commands
中 - 初始化
sentinel
数据结构
/* Perform the Sentinel mode initialization. */
void initSentinel(void) {
unsigned int j;
/* Remove usual Redis commands from the command table, then just add
* the SENTINEL command. */
dictEmpty(server.commands,NULL);
for (j = 0; j < sizeof(sentinelcmds)/sizeof(sentinelcmds[0]); j++) {
int retval;
struct redisCommand *cmd = sentinelcmds+j;
retval = dictAdd(server.commands, sdsnew(cmd->name), cmd);
redisAssert(retval == DICT_OK);
}
/* Initialize various data structures. */
sentinel.current_epoch = 0;
//根据配置文件,初始化自己需要监控的master(一个sentinel是可能监控多个 master的)
sentinel.masters = dictCreate(&instancesDictType,NULL);
sentinel.tilt = 0;
sentinel.tilt_start_time = 0;
sentinel.previous_time = mstime();
sentinel.running_scripts = 0;
sentinel.scripts_queue = listCreate();
sentinel.announce_ip = NULL;
sentinel.announce_port = 0;
}