数据库
首页 > 数据库> > Redis源码分析--Sentinel(1)Sentinel服务器

Redis源码分析--Sentinel(1)Sentinel服务器

作者:互联网

一、概述:

调用链: serverCron -> sentinelTimer -> sentinelHandleDictOfRedisInstances -> sentinelHandleRedisInstance


二、服务器如何执行Sentinel功能?:

​ 以Sentinel模式启动的redis服务器,会在init*Config等函数中以Sentinel模式专用代码替换普通redis服务器的代码,这里不细说。在serverCron定时函数中,服务器调用sentinelTimer,后者会遍历本机(sentinel)监视的所有主服务器代表的实例(sentinelInstance)。

void sentinelTimer(void) {
    /* 判断是否处于Tilt模式(一种保护模式,当计算机时间发生剧烈变化时发生) */
    sentinelCheckTiltCondition();
    /* 处理sentinel中的所有sentinelInstance */
    sentinelHandleDictOfRedisInstances(sentinel.masters);
	// ...
}

void sentinelHandleDictOfRedisInstances(dict *instances) {
    dictIterator *di;
    dictEntry *de;
    sentinelRedisInstance *switch_to_promoted = NULL;

    /* There are a number of things we need to perform against every master. */
    /* sentinelInstance,可以是master,slave或sentinel */
    di = dictGetIterator(instances);
    while((de = dictNext(di)) != NULL) {
        sentinelRedisInstance *ri = dictGetVal(de);
        /* 处理当前实例 */
        sentinelHandleRedisInstance(ri);
        if (ri->flags & SRI_MASTER) {
            /* 如果实例是master,递归处理它的slave和sentinels */
            sentinelHandleDictOfRedisInstances(ri->slaves);
            sentinelHandleDictOfRedisInstances(ri->sentinels);
            if (ri->failover_state == SENTINEL_FAILOVER_STATE_UPDATE_CONFIG) {
                switch_to_promoted = ri;
            }
        }
    }
    if (switch_to_promoted)
        sentinelFailoverSwitchToPromotedSlave(switch_to_promoted);
    dictReleaseIterator(di);
}

标签:实例,--,sentinel,源码,half,Sentinel,服务器,ri
来源: https://www.cnblogs.com/macguz/p/15865706.html