// File: /workspace/github_c_makefile_star2_O3/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 0x23461
movq %rax, %r14
movq 0x158(%rbx), %rax
testq %rax, %rax
je 0x5fcac
testb $0x10, (%rax)
jne 0x5fcac
subq 0x138(%rbx), %r14
movq 0xc8(%rbx), %rdi
callq 0x22a71
movq %rax, %r15
movq %rax, %rdi
callq 0x22acf
testq %rax, %rax
je 0x5fb81
xorl %ebp, %ebp
movl $0x910, %r12d           # imm = 0x910
movq 0x8(%rax), %rax
movl (%rax), %eax
andl %r12d, %eax
cmpl $0x1, %eax
adcl $0x0, %ebp
movq %r15, %rdi
callq 0x22acf
testq %rax, %rax
jne 0x5fb47
movq %r15, %rdi
callq 0x22b86
cmpq 0x148(%rbx), %r14
jg 0x5fb96
testl %ebp, %ebp
jne 0x5fcac
jmp 0x5fc79
movq %r15, %rdi
callq 0x22b86
cmpq 0x148(%rbx), %r14
jle 0x5fc79
leaq 0x40338(%rip), %rsi     # 0x9fed5
leaq 0x3f4ca(%rip), %r14     # 0x9f06e
movl $0x3, %edi
movq %rbx, %rdx
movq %r14, %rcx
xorl %eax, %eax
callq 0x5b0ac
leaq 0x40332(%rip), %rsi     # 0x9feef
movl $0x3, %edi
movq %rbx, %rdx
movq %r14, %rcx
xorl %eax, %eax
callq 0x5b0ac
movl $0x6, 0x130(%rbx)
callq 0x23461
movq %rax, 0x138(%rbx)
movq 0xc8(%rbx), %rdi
callq 0x22a71
movq %rax, %r14
movq %rax, %rdi
callq 0x22acf
testq %rax, %rax
je 0x5fc63
leaq 0x402f5(%rip), %r15     # 0x9fefd
leaq 0x3f45f(%rip), %r12     # 0x9f06e
movq 0x8(%rax), %r13
movzwl (%r13), %eax
testl $0xa08, %eax            # imm = 0xA08
jne 0x5fc56
movq 0x158(%rbx), %rax
movq 0x20(%rax), %rax
movq (%rax), %rsi
movl 0x8(%rax), %edx
movq %r13, %rdi
callq 0x5dc3b
testl %eax, %eax
jne 0x5fc56
movl $0x2, %edi
movq %r15, %rsi
movq %r13, %rdx
movq %r12, %rcx
xorl %eax, %eax
callq 0x5b0ac
orb $0x2, 0x1(%r13)
movq %r14, %rdi
callq 0x22acf
testq %rax, %rax
jne 0x5fc0f
movq %r14, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x22b86
leaq 0x4026f(%rip), %rsi     # 0x9feef
leaq 0x3f3e7(%rip), %rcx     # 0x9f06e
movl $0x3, %edi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x5b0ac
movl $0x6, 0x130(%rbx)
callq 0x23461
movq %rax, 0x138(%rbx)
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq