// File: /workspace/github_c_makefile_star2_O3/nilyang[P]redis-tdd-annotation/src/replication.c
// Function: updateSlavesWaitingBgsave
void updateSlavesWaitingBgsave(int bgsaveerr) {
    listNode *ln;
    int startbgsave = 0;
    listIter li;

    // 遍历所有 slave
    listRewind(server.slaves,&li);
    while((ln = listNext(&li))) {
        redisClient *slave = ln->value;

        if (slave->replstate == REDIS_REPL_WAIT_BGSAVE_START) {
            // 之前的 RDB 文件不能被 slave 使用，
            // 开始新的 BGSAVE
            startbgsave = 1;
            slave->replstate = REDIS_REPL_WAIT_BGSAVE_END;
        } else if (slave->replstate == REDIS_REPL_WAIT_BGSAVE_END) {

            // 执行到这里，说明有 slave 在等待 BGSAVE 完成

            struct redis_stat buf;

            // 但是 BGSAVE 执行错误
            if (bgsaveerr != REDIS_OK) {
                // 释放 slave
                freeClient(slave);
                redisLog(REDIS_WARNING,"SYNC failed. BGSAVE child returned an error");
                continue;
            }

            // 打开 RDB 文件
            if ((slave->repldbfd = open(server.rdb_filename,O_RDONLY)) == -1 ||
                redis_fstat(slave->repldbfd,&buf) == -1) {
                freeClient(slave);
                redisLog(REDIS_WARNING,"SYNC failed. Can't open/stat DB after BGSAVE: %s", strerror(errno));
                continue;
            }

            // 设置偏移量，各种值
            slave->repldboff = 0;
            slave->repldbsize = buf.st_size;
            // 更新状态
            slave->replstate = REDIS_REPL_SEND_BULK;

            slave->replpreamble = sdscatprintf(sdsempty(),"$%lld\r\n",
                (unsigned long long) slave->repldbsize);

            // 清空之前的写事件处理器
            aeDeleteFileEvent(server.el,slave->fd,AE_WRITABLE);
            // 将 sendBulkToSlave 安装为 slave 的写事件处理器
            // 它用于将 RDB 文件发送给 slave
            if (aeCreateFileEvent(server.el, slave->fd, AE_WRITABLE, sendBulkToSlave, slave) == AE_ERR) {
                freeClient(slave);
                continue;
            }
        }
    }

    // 需要执行新的 BGSAVE
    if (startbgsave) {
        /* Since we are starting a new background save for one or more slaves,
         * we flush the Replication Script Cache to use EVAL to propagate every
         * new EVALSHA for the first time, since all the new slaves don't know
         * about previous scripts. */
        // 开始行的 BGSAVE ，并清空脚本缓存
        replicationScriptCacheFlush();
        if (rdbSaveBackground(server.rdb_filename) != REDIS_OK) {
            listIter li;

            listRewind(server.slaves,&li);
            redisLog(REDIS_WARNING,"SYNC failed. BGSAVE failed");
            while((ln = listNext(&li))) {
                redisClient *slave = ln->value;

                if (slave->replstate == REDIS_REPL_WAIT_BGSAVE_START)
                    freeClient(slave);
            }
        }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movl %edi, %ebx
leaq 0x959f5(%rip), %r15     # 0xc9fa8
movq 0x1c0(%r15), %rdi
leaq 0x8(%rsp), %r14
movq %r14, %rsi
callq 0x20637
movq %r14, %rdi
callq 0x20654
testq %rax, %rax
je 0x347a6
movb $0x1, %r14b
leaq 0x6376d(%rip), %rbp     # 0x97d4f
leaq 0x8(%rsp), %r12
movq 0x10(%rax), %r13
movl 0x98(%r13), %eax
cmpl $0x7, %eax
je 0x34620
cmpl $0x6, %eax
jne 0x34722
movl $0x7, 0x98(%r13)
movq %r12, %rdi
callq 0x20654
xorl %r14d, %r14d
testq %rax, %rax
jne 0x345e7
jmp 0x34739
testl %ebx, %ebx
je 0x34640
movq %r13, %rdi
callq 0x2e517
movl $0x3, %edi
movq %rbp, %rsi
xorl %eax, %eax
callq 0x23223
jmp 0x34722
movq 0x588(%r15), %rdi
xorl %esi, %esi
xorl %eax, %eax
callq 0x1f640
movl %eax, 0x9c(%r13)
cmpl $-0x1, %eax
je 0x346f8
movl %eax, %edi
leaq 0x18(%rsp), %rsi
callq 0x1f690
cmpl $-0x1, %eax
je 0x346f8
movq $0x0, 0xa0(%r13)
movq 0x48(%rsp), %rax
movq %rax, 0xa8(%r13)
movl $0x8, 0x98(%r13)
callq 0x2845e
movq 0xa8(%r13), %rdx
movq %rax, %rdi
leaq 0x636ff(%rip), %rsi     # 0x97dac
xorl %eax, %eax
callq 0x28a5e
movq %rax, 0xb0(%r13)
movq 0x28(%r15), %rdi
movl (%r13), %esi
movl $0x2, %edx
callq 0x20ce8
movq 0x28(%r15), %rdi
movl (%r13), %esi
movl $0x2, %edx
leaq -0x336(%rip), %rcx      # 0x343ab
movq %r13, %r8
callq 0x20c22
cmpl $-0x1, %eax
jne 0x34722
movq %r13, %rdi
callq 0x2e517
jmp 0x34722
movq %r13, %rdi
callq 0x2e517
callq 0x1f810
movl (%rax), %edi
callq 0x1f630
movl $0x3, %edi
leaq 0x63663(%rip), %rsi     # 0x97d7b
movq %rax, %rdx
xorl %eax, %eax
callq 0x23223
movq %r12, %rdi
callq 0x20654
testq %rax, %rax
jne 0x345e7
testb $0x1, %r14b
jne 0x347a6
callq 0x341c0
movq 0x588(%r15), %rdi
callq 0x37828
testl %eax, %eax
je 0x347a6
movq 0x1c0(%r15), %rdi
leaq 0x18(%rsp), %rbx
movq %rbx, %rsi
callq 0x20637
leaq 0x6364b(%rip), %rsi     # 0x97db4
movl $0x3, %edi
xorl %eax, %eax
callq 0x23223
movq %rbx, %rdi
callq 0x20654
testq %rax, %rax
je 0x347a6
leaq 0x18(%rsp), %rbx
movq 0x10(%rax), %rdi
cmpl $0x6, 0x98(%rdi)
jne 0x34799
callq 0x2e517
movq %rbx, %rdi
callq 0x20654
testq %rax, %rax
jne 0x34787
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq