// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x8(%rbp)
movl $0x0, -0x1c(%rbp)
movq -0x8(%rbp), %rax
movq 0xc8(%rax), %rdi
callq 0x241d0
movq %rax, -0x10(%rbp)
movq -0x10(%rbp), %rdi
callq 0x24270
movq %rax, -0x18(%rbp)
cmpq $0x0, %rax
je 0x80a51
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
movl (%rax), %eax
andl $0x600, %eax            # imm = 0x600
cmpl $0x0, %eax
je 0x80a4f
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x80a17
movq -0x10(%rbp), %rdi
callq 0x243d0
movq -0x8(%rbp), %rax
movq 0xc8(%rax), %rdi
callq 0x241d0
movq %rax, -0x10(%rbp)
movl -0x1c(%rbp), %ecx
movq -0x8(%rbp), %rdx
xorl %eax, %eax
cmpl 0xd4(%rdx), %ecx
movb %al, -0x35(%rbp)
jge 0x80a99
movq -0x10(%rbp), %rdi
callq 0x24270
movq %rax, -0x18(%rbp)
cmpq $0x0, %rax
setne %al
movb %al, -0x35(%rbp)
movb -0x35(%rbp), %al
testb $0x1, %al
jne 0x80aa5
jmp 0x80bbd
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movl (%rax), %eax
andl $0x900, %eax            # imm = 0x900
cmpl $0x0, %eax
je 0x80ac3
jmp 0x80a6e
movq -0x30(%rbp), %rax
movl (%rax), %eax
andl $0x200, %eax            # imm = 0x200
cmpl $0x0, %eax
je 0x80b25
callq 0x252e0
movq -0x30(%rbp), %rcx
subq 0xf0(%rcx), %rax
cmpq $0x2710, %rax           # imm = 0x2710
jle 0x80b25
movq -0x30(%rbp), %rdx
movl $0x2, %edi
leaq 0x693e0(%rip), %rsi     # 0xe9edb
leaq 0x68530(%rip), %rcx     # 0xe9032
movb $0x0, %al
callq 0x79b10
movq -0x30(%rbp), %rax
movl (%rax), %ecx
andl $0xfffffdff, %ecx       # imm = 0xFFFFFDFF
movl %ecx, (%rax)
movq -0x30(%rbp), %rax
movl (%rax), %ecx
orl $0x800, %ecx            # imm = 0x800
movl %ecx, (%rax)
movq -0x30(%rbp), %rax
movl (%rax), %eax
andl $0x608, %eax            # imm = 0x608
cmpl $0x0, %eax
je 0x80b3a
jmp 0x80a6e
movq -0x30(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x158(%rax), %rax
movq 0x20(%rax), %rax
movq (%rax), %rsi
movq -0x8(%rbp), %rax
movq 0x158(%rax), %rax
movq 0x20(%rax), %rax
movl 0x8(%rax), %edx
callq 0x7da60
movl %eax, -0x34(%rbp)
cmpl $0x0, -0x34(%rbp)
jne 0x80bb8
movq -0x30(%rbp), %rax
movl (%rax), %ecx
orl $0x200, %ecx            # imm = 0x200
movl %ecx, (%rax)
callq 0x252e0
movq %rax, %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0xf0(%rax)
movq -0x30(%rbp), %rdx
movl $0x2, %edi
leaq 0x69355(%rip), %rsi     # 0xe9ef6
leaq 0x6848a(%rip), %rcx     # 0xe9032
movb $0x0, %al
callq 0x79b10
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x80a6e
movq -0x10(%rbp), %rdi
callq 0x243d0
movq -0x8(%rbp), %rdi
callq 0x807e0
addq $0x40, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)