// File: /workspace/github_c_makefile_star2_O2/nilyang[P]redis-tdd-annotation/src/sentinel.c
// Function: sentinelFailoverDetectEnd
void sentinelFailoverDetectEnd(sentinelRedisInstance *master) {
    int not_reconfigured = 0, timeout = 0;
    dictIterator *di;
    dictEntry *de;

    // 上次 failover 状态更新以来，经过的时间
    mstime_t elapsed = mstime() - master->failover_state_change_time;

    /* We can't consider failover finished if the promoted slave is
     * not reachable. */
    // 如果新主服务器已经下线，那么故障转移操作不成功
    if (master->promoted_slave == NULL ||
        master->promoted_slave->flags & SRI_S_DOWN) return;

    /* The failover terminates once all the reachable slaves are properly
     * configured. */
    // 计算未完成同步的从服务器的数量
    di = dictGetIterator(master->slaves);
    while((de = dictNext(di)) != NULL) {
        sentinelRedisInstance *slave = dictGetVal(de);

        // 新主服务器和已完成同步的从服务器不计算在内
        if (slave->flags & (SRI_PROMOTED|SRI_RECONF_DONE)) continue;

        // 已下线的从服务器不计算在内
        if (slave->flags & SRI_S_DOWN) continue;

        // 增一
        not_reconfigured++;
    }
    dictReleaseIterator(di);

    /* Force end of failover on timeout. */
    // 故障操作因为超时而结束
    if (elapsed > master->failover_timeout) {
        // 忽略未完成的从服务器
        not_reconfigured = 0;
        // 打开超时标志
        timeout = 1;
        // 发送超时事件
        sentinelEvent(REDIS_WARNING,"+failover-end-for-timeout",master,"%@");
    }

    // 所有从服务器都已完成同步，故障转移结束
    if (not_reconfigured == 0) {
        sentinelEvent(REDIS_WARNING,"+failover-end",master,"%@");
        // 更新故障转移状态
        // 这一状态将告知 Sentinel ，所有从服务器都已经同步到新主服务器
        master->failover_state = SENTINEL_FAILOVER_STATE_UPDATE_CONFIG;
        // 更新状态改变的时间
        master->failover_state_change_time = mstime();
    }

    /* If I'm the leader it is a good idea to send a best effort SLAVEOF
     * command to all the slaves still not reconfigured to replicate with
     * the new master. */
    if (timeout) {
        dictIterator *di;
        dictEntry *de;

        // 遍历所有从服务器
        di = dictGetIterator(master->slaves);
        while((de = dictNext(di)) != NULL) {
            sentinelRedisInstance *slave = dictGetVal(de);
            int retval;

            // 跳过已发送 SLAVEOF 命令，以及已经完成同步的所有从服务器
            if (slave->flags &
                (SRI_RECONF_DONE|SRI_RECONF_SENT|SRI_DISCONNECTED)) continue;

            // 发送命令
            retval = sentinelSendSlaveOf(slave,
                    master->promoted_slave->addr->ip,
                    master->promoted_slave->addr->port);
            if (retval == REDIS_OK) {
                sentinelEvent(REDIS_NOTICE,"+slave-reconf-sent-be",slave,"%@");
                // 打开从服务器的 SLAVEOF 命令已发送标记
                slave->flags |= SRI_RECONF_SENT;
            }
        }
        dictReleaseIterator(di);
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdi, %rbx
callq 0x22843
movq %rax, %r14
movq 0x158(%rbx), %rax
testq %rax, %rax
je 0x5ae34
testb $0x10, (%rax)
jne 0x5ae34
subq 0x138(%rbx), %r14
movq 0xc8(%rbx), %rdi
callq 0x21ef6
movq %rax, %r15
xorl %ebp, %ebp
movq %r15, %rdi
callq 0x21f2f
testq %rax, %rax
je 0x5ad53
movq 0x8(%rax), %rax
movzwl (%rax), %eax
andl $0x910, %eax            # imm = 0x910
cmpw $0x1, %ax
adcl $0x0, %ebp
jmp 0x5ad31
movq %r15, %rdi
callq 0x21fe6
movq 0x148(%rbx), %r15
cmpq %r15, %r14
jle 0x5ad84
leaq 0x3b0db(%rip), %rsi     # 0x95e49
leaq 0x3a26d(%rip), %rcx     # 0x94fe2
pushq $0x3
popq %rdi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x5678f
jmp 0x5ad8c
testl %ebp, %ebp
jne 0x5ae34
leaq 0x3b0d0(%rip), %rsi     # 0x95e63
leaq 0x3a248(%rip), %rcx     # 0x94fe2
pushq $0x3
popq %rdi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x5678f
movl $0x6, 0x130(%rbx)
callq 0x22843
movq %rax, 0x138(%rbx)
cmpq %r15, %r14
jle 0x5ae34
movq 0xc8(%rbx), %rdi
callq 0x21ef6
movq %rax, %r14
leaq 0x3b099(%rip), %r15     # 0x95e71
leaq 0x3a203(%rip), %r12     # 0x94fe2
pushq $0x2
popq %rbp
movq %r14, %rdi
callq 0x21f2f
testq %rax, %rax
je 0x5ae43
movq 0x8(%rax), %r13
movzwl (%r13), %eax
testw $0xa08, %ax             # imm = 0xA08
jne 0x5ade2
movq 0x158(%rbx), %rax
movq 0x20(%rax), %rax
movq (%rax), %rsi
movl 0x8(%rax), %edx
movq %r13, %rdi
callq 0x58fd6
testl %eax, %eax
jne 0x5ade2
movl %ebp, %edi
movq %r15, %rsi
movq %r13, %rdx
movq %r12, %rcx
xorl %eax, %eax
callq 0x5678f
orb $0x2, 0x1(%r13)
jmp 0x5ade2
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r14, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x21fe6