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

    // 计算正在同步新主服务器的从服务器数量
    di = dictGetIterator(master->slaves);
    while((de = dictNext(di)) != NULL) {
        sentinelRedisInstance *slave = dictGetVal(de);

        // SLAVEOF 命令已发送，或者同步正在进行
        if (slave->flags & (SRI_RECONF_SENT|SRI_RECONF_INPROG))
            in_progress++;
    }
    dictReleaseIterator(di);

    // 如果正在同步的从服务器的数量少于 parallel-syncs 选项的值
    // 那么继续遍历从服务器，并让从服务器对新主服务器进行同步
    di = dictGetIterator(master->slaves);
    while(in_progress < master->parallel_syncs &&
          (de = dictNext(di)) != NULL)
    {
        sentinelRedisInstance *slave = dictGetVal(de);
        int retval;

        /* Skip the promoted slave, and already configured slaves. */
        // 跳过新主服务器，以及已经完成了同步的从服务器
        if (slave->flags & (SRI_PROMOTED|SRI_RECONF_DONE)) continue;

        /* If too much time elapsed without the slave moving forward to
         * the next state, consider it reconfigured even if it is not.
         * Sentinels will detect the slave as misconfigured and fix its
         * configuration later. */
        if ((slave->flags & SRI_RECONF_SENT) &&
            (mstime() - slave->slave_reconf_sent_time) >
            SENTINEL_SLAVE_RECONF_TIMEOUT)
        {
            // 发送重拾同步事件
            sentinelEvent(REDIS_NOTICE,"-slave-reconf-sent-timeout",slave,"%@");
            // 清除已发送 SLAVEOF 命令的标记
            slave->flags &= ~SRI_RECONF_SENT;
            slave->flags |= SRI_RECONF_DONE;
        }

        /* Nothing to do for instances that are disconnected or already
         * in RECONF_SENT state. */
        // 如果已向从服务器发送 SLAVEOF 命令，或者同步正在进行
        // 又或者从服务器已断线，那么略过该服务器
        if (slave->flags & (SRI_DISCONNECTED|SRI_RECONF_SENT|SRI_RECONF_INPROG))
            continue;

        /* Send SLAVEOF <new master>. */
        // 向从服务器发送 SLAVEOF 命令，让它同步新主服务器
        retval = sentinelSendSlaveOf(slave,
                master->promoted_slave->addr->ip,
                master->promoted_slave->addr->port);
        if (retval == REDIS_OK) {

            // 将状态改为 SLAVEOF 命令已发送
            slave->flags |= SRI_RECONF_SENT;
            // 更新发送 SLAVEOF 命令的时间
            slave->slave_reconf_sent_time = mstime();
            sentinelEvent(REDIS_NOTICE,"+slave-reconf-sent",slave,"%@");
            // 增加当前正在同步的从服务器的数量
            in_progress++;
        }
    }
    dictReleaseIterator(di);

    /* Check if all the slaves are reconfigured and handle timeout. */
    // 判断是否所有从服务器的同步都已经完成
    sentinelFailoverDetectEnd(master);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdi, %rbx
movq 0xc8(%rdi), %rdi
callq 0x21ef6
movq %rax, %r14
xorl %r13d, %r13d
movq %r14, %rdi
callq 0x21f2f
testq %rax, %rax
je 0x5ae97
movq 0x8(%rax), %rax
movb 0x1(%rax), %al
andb $0x6, %al
cmpb $0x1, %al
sbbl $-0x1, %r13d
jmp 0x5ae79
movq %r14, %rdi
callq 0x21fe6
movq 0xc8(%rbx), %rdi
callq 0x21ef6
movq %rax, %r14
leaq 0x3a12d(%rip), %r12     # 0x94fe2
pushq $0x2
popq %rbp
cmpl 0xd4(%rbx), %r13d
jge 0x5af80
movq %r14, %rdi
callq 0x21f2f
testq %rax, %rax
je 0x5af80
movq 0x8(%rax), %r15
movl (%r15), %eax
testw $0x900, %ax             # imm = 0x900
jne 0x5aeb8
btl $0x9, %eax
jae 0x5af2a
callq 0x22843
subq 0xf0(%r15), %rax
cmpq $0x2710, %rax           # imm = 0x2710
jle 0x5af27
movl %ebp, %edi
leaq 0x3af81(%rip), %rsi     # 0x95e87
movq %r15, %rdx
movq %r12, %rcx
xorl %eax, %eax
callq 0x5678f
movl (%r15), %eax
movl $0xfffff5ff, %ecx       # imm = 0xFFFFF5FF
andl %ecx, %eax
orl $0x800, %eax            # imm = 0x800
movl %eax, (%r15)
jmp 0x5af2a
movl (%r15), %eax
testw $0x608, %ax             # imm = 0x608
jne 0x5aeb8
movq 0x158(%rbx), %rax
movq 0x20(%rax), %rax
movq (%rax), %rsi
movl 0x8(%rax), %edx
movq %r15, %rdi
callq 0x58fd6
testl %eax, %eax
jne 0x5aeb8
orb $0x2, 0x1(%r15)
callq 0x22843
movq %rax, 0xf0(%r15)
movl %ebp, %edi
leaq 0x3af37(%rip), %rsi     # 0x95ea2
movq %r15, %rdx
movq %r12, %rcx
xorl %eax, %eax
callq 0x5678f
incl %r13d
jmp 0x5aeb8
movq %r14, %rdi
callq 0x21fe6
movq %rbx, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x5acea