// File: /workspace/github_c_makefile_star2_O1/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 0x232a3
movq %rax, %r14
movq 0x158(%rbx), %rax
testq %rax, %rax
je 0x601f2
testb $0x10, (%rax)
jne 0x601f2
subq 0x138(%rbx), %r14
movq 0xc8(%rbx), %rdi
callq 0x228b5
movq %rax, %r15
movq %rax, %rdi
callq 0x22913
xorl %ebp, %ebp
testq %rax, %rax
je 0x600eb
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 0x22913
testq %rax, %rax
jne 0x600cf
movq %r15, %rdi
callq 0x229b9
movq 0x148(%rbx), %r15
cmpq %r15, %r14
jle 0x6011e
leaq 0x3fd5f(%rip), %rsi     # 0x9fe65
leaq 0x3eef1(%rip), %rcx     # 0x9effe
xorl %ebp, %ebp
movl $0x3, %edi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x5b66f
testl %ebp, %ebp
jne 0x60155
leaq 0x3fd56(%rip), %rsi     # 0x9fe7f
leaq 0x3eece(%rip), %rcx     # 0x9effe
movl $0x3, %edi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x5b66f
movl $0x6, 0x130(%rbx)
callq 0x232a3
movq %rax, 0x138(%rbx)
cmpq %r15, %r14
jle 0x601f2
movq 0xc8(%rbx), %rdi
callq 0x228b5
movq %rax, %r14
movq %rax, %rdi
callq 0x22913
testq %rax, %rax
je 0x601dc
leaq 0x3fd0c(%rip), %r15     # 0x9fe8d
leaq 0x3ee76(%rip), %r12     # 0x9effe
movq 0x8(%rax), %r13
movzwl (%r13), %eax
testl $0xa08, %eax            # imm = 0xA08
jne 0x601cf
movq 0x158(%rbx), %rax
movq 0x20(%rax), %rax
movq (%rax), %rsi
movl 0x8(%rax), %edx
movq %r13, %rdi
callq 0x5e1bf
testl %eax, %eax
jne 0x601cf
movl $0x2, %edi
movq %r15, %rsi
movq %r13, %rdx
movq %r12, %rcx
xorl %eax, %eax
callq 0x5b66f
orb $0x2, 0x1(%r13)
movq %r14, %rdi
callq 0x22913
testq %rax, %rax
jne 0x60188
movq %r14, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x229b9
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq