故障转移状态机:
一、状态机函数sentinelFailoverStateMachine:
终于进入故障转移,先看一下状态机函数:
void sentinelFailoverStateMachine(sentinelRedisInstance *ri) {
redisAssert(ri->flags & SRI_MASTER);
/* 如果机器不在故障转移状态,直接退出 */
if (!(ri->flags & SRI_FAILOVER_IN_PROGRESS)) return;
switch(ri->failover_state) {
case SENTINEL_FAILOVER_STATE_WAIT_START:
/* 判断本sentinel是否为leader */
sentinelFailoverWaitStart(ri);
break;
case SENTINEL_FAILOVER_STATE_SELECT_SLAVE:
/* 选择从服务器作为新的主服务器 */
sentinelFailoverSelectSlave(ri);
break;
case SENTINEL_FAILOVER_STATE_SEND_SLAVEOF_NOONE:
/* 向被选中的从服务器发送SLAVEOF no one */
sentinelFailoverSendSlaveOfNoOne(ri);
break;
case SENTINEL_FAILOVER_STATE_WAIT_PROMOTION:
/* 观察被选中的从服务器是否被升级为主服务器 */
sentinelFailoverWaitPromotion(ri);
break;
case SENTINEL_FAILOVER_STATE_RECONF_SLAVES:
/* 让原来的slave跟随新的master
* ri是当前master,新的master是ri->promoted_slave */
sentinelFailoverReconfNextSlave(ri);
break;
}
}
- L22:这里只处理超时,真正判断slave升级为master在第二篇INFO命令的回调中进行;