redis自顶向下源码阅读(四)——哨兵设置

18 篇文章 0 订阅
17 篇文章 1 订阅

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

  1. 把之前redis服务器的命令表清空(因为本服务器进入sentinel模式后,需要执行与普通redis服务器不同的命令
  2. sentinel专属的命令逐个插入server.commands
  3. 初始化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;
}

Redis 哨兵模式(Sentinel)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值