// File: /workspace/github_c_makefile_star2_O1/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 0x228b5
movq %rax, %r14
movq %rax, %rdi
callq 0x22913
xorl %r15d, %r15d
testq %rax, %rax
je 0x6024f
movl $0x600, %ebp            # imm = 0x600
movq 0x8(%rax), %rax
movl (%rax), %eax
andl %ebp, %eax
cmpl $0x1, %eax
sbbl $-0x1, %r15d
movq %r14, %rdi
callq 0x22913
testq %rax, %rax
jne 0x60233
movq %r14, %rdi
callq 0x229b9
movq 0xc8(%rbx), %rdi
callq 0x228b5
movq %rax, %r14
cmpl 0xd4(%rbx), %r15d
jge 0x60346
leaq 0x3ed84(%rip), %r12     # 0x9effe
leaq 0x3fc22(%rip), %r13     # 0x9fea3
movq %r14, %rdi
callq 0x22913
testq %rax, %rax
je 0x60346
movq 0x8(%rax), %rbp
movl (%rbp), %eax
testl $0x900, %eax            # imm = 0x900
jne 0x60339
btl $0x9, %eax
jae 0x602e5
callq 0x232a3
subq 0xf0(%rbp), %rax
cmpq $0x2711, %rax           # imm = 0x2711
jl 0x602e5
movl $0x2, %edi
movq %r13, %rsi
movq %rbp, %rdx
movq %r12, %rcx
xorl %eax, %eax
callq 0x5b66f
movl (%rbp), %eax
movl $0xfffff5ff, %ecx       # imm = 0xFFFFF5FF
andl %ecx, %eax
orl $0x800, %eax            # imm = 0x800
movl %eax, (%rbp)
movzwl (%rbp), %eax
testl $0x608, %eax            # imm = 0x608
jne 0x60339
movq 0x158(%rbx), %rax
movq 0x20(%rax), %rax
movq (%rax), %rsi
movl 0x8(%rax), %edx
movq %rbp, %rdi
callq 0x5e1bf
testl %eax, %eax
jne 0x60339
orb $0x2, 0x1(%rbp)
callq 0x232a3
movq %rax, 0xf0(%rbp)
movl $0x2, %edi
leaq 0x3fb95(%rip), %rsi     # 0x9febe
movq %rbp, %rdx
movq %r12, %rcx
xorl %eax, %eax
callq 0x5b66f
incl %r15d
cmpl 0xd4(%rbx), %r15d
jl 0x60281
movq %r14, %rdi
callq 0x229b9
movq %rbx, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x60075