// File: /workspace/github_c_makefile_star2_O1/nilyang[P]redis-tdd-annotation/src/replication.c
// Function: replicationCron
void replicationCron(void) {

    /* Non blocking connection timeout? */
    // 尝试连接到主服务器，但超时
    if (server.masterhost &&
        (server.repl_state == REDIS_REPL_CONNECTING ||
         server.repl_state == REDIS_REPL_RECEIVE_PONG) &&
        (time(NULL)-server.repl_transfer_lastio) > server.repl_timeout)
    {
        redisLog(REDIS_WARNING,"Timeout connecting to the MASTER...");
        // 取消连接
        undoConnectWithMaster();
    }

    /* Bulk transfer I/O timeout? */
    // RDB 文件的传送已超时？
    if (server.masterhost && server.repl_state == REDIS_REPL_TRANSFER &&
        (time(NULL)-server.repl_transfer_lastio) > server.repl_timeout)
    {
        redisLog(REDIS_WARNING,"Timeout receiving bulk data from MASTER... If the problem persists try to set the 'repl-timeout' parameter in redis.conf to a larger value.");
        // 停止传送，并删除临时文件
        replicationAbortSyncTransfer();
    }

    /* Timed out master when we are an already connected slave? */
    // 从服务器曾经连接上主服务器，但现在超时
    if (server.masterhost && server.repl_state == REDIS_REPL_CONNECTED &&
        (time(NULL)-server.master->lastinteraction) > server.repl_timeout)
    {
        redisLog(REDIS_WARNING,"MASTER timeout: no data nor PING received...");
        // 释放主服务器
        freeClient(server.master);
    }

    /* Check if we should connect to a MASTER */
    // 尝试连接主服务器
    if (server.repl_state == REDIS_REPL_CONNECT) {
        redisLog(REDIS_NOTICE,"Connecting to MASTER %s:%d",
            server.masterhost, server.masterport);
        if (connectWithMaster() == REDIS_OK) {
            redisLog(REDIS_NOTICE,"MASTER <-> SLAVE sync started");
        }
    }

    /* Send ACK to master from time to time.
     * Note that we do not send periodic acks to masters that don't
     * support PSYNC and replication offsets. */
    // 定期向主服务器发送 ACK 命令
    // 不过如果主服务器带有 REDIS_PRE_PSYNC 的话就不发送
    // 因为带有该标识的版本为 < 2.8 的版本，这些版本不支持 ACK 命令
    if (server.masterhost && server.master &&
        !(server.master->flags & REDIS_PRE_PSYNC))
        replicationSendAck();
    
    /* If we have attached slaves, PING them from time to time.
     *
     * 如果服务器有从服务器，定时向它们发送 PING 。
     *
     * So slaves can implement an explicit timeout to masters, and will
     * be able to detect a link disconnection even if the TCP connection
     * will not actually go down. 
     *
     * 这样从服务器就可以实现显式的 master 超时判断机制，
     * 即使 TCP 连接未断开也是如此。
     */
    if (!(server.cronloops % (server.repl_ping_slave_period * server.hz))) {
        listIter li;
        listNode *ln;
        robj *ping_argv[1];

        /* First, send PING */
        // 向所有已连接 slave （状态为 ONLINE）发送 PING
        ping_argv[0] = createStringObject("PING",4);
        replicationFeedSlaves(server.slaves, server.slaveseldb, ping_argv, 1);
        decrRefCount(ping_argv[0]);

        /* Second, send a newline to all the slaves in pre-synchronization
         * stage, that is, slaves waiting for the master to create the RDB file.
         *
         * 向那些正在等待 RDB 文件的从服务器（状态为 BGSAVE_START 或 BGSAVE_END）
         * 发送 "\n"
         *
         * The newline will be ignored by the slave but will refresh the
         * last-io timer preventing a timeout. 
         *
         * 这个 "\n" 会被从服务器忽略，
         * 它的作用就是用来防止主服务器因为长期不发送信息而被从服务器误判为超时
         */
        listRewind(server.slaves,&li);
        while((ln = listNext(&li))) {
            redisClient *slave = ln->value;

            if (slave->replstate == REDIS_REPL_WAIT_BGSAVE_START ||
                slave->replstate == REDIS_REPL_WAIT_BGSAVE_END) {
                if (write(slave->fd, "\n", 1) == -1) {
                    /* Don't worry, it's just a ping. */
                }
            }
        }
    }

    /* Disconnect timedout slaves. */
    // 断开超时从服务器
    if (listLength(server.slaves)) {
        listIter li;
        listNode *ln;

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

            // 略过未 ONLINE 的从服务器
            if (slave->replstate != REDIS_REPL_ONLINE) continue;

            // 不检查旧版的从服务器
            if (slave->flags & REDIS_PRE_PSYNC) continue;

            // 释放超时从服务器
            if ((server.unixtime - slave->repl_ack_time) > server.repl_timeout)
            {
                char ip[REDIS_IP_STR_LEN];
                int port;

                if (anetPeerToString(slave->fd,ip,sizeof(ip),&port) != -1) {
                    redisLog(REDIS_WARNING,
                        "Disconnecting timedout slave: %s:%d",
                        ip, slave->slave_listening_port);
                }
                
                // 释放
                freeClient(slave);
            }
        }
    }

    /* If we have no attached slaves and there is a replication backlog
     * using memory, free it after some (configured) time. */
    // 在没有任何从服务器的 N 秒之后，释放 backlog
    if (listLength(server.slaves) == 0 && server.repl_backlog_time_limit &&
        server.repl_backlog)
    {
        time_t idle = server.unixtime - server.repl_no_slaves_since;

        if (idle > server.repl_backlog_time_limit) {
            // 释放
            freeReplicationBacklog();
            redisLog(REDIS_NOTICE,
                "Replication backlog freed after %d seconds "
                "without connected slaves.",
                (int) server.repl_backlog_time_limit);
        }
    }

    /* If AOF is disabled and we no longer have attached slaves, we can
     * free our Replication Script Cache as there is no need to propagate
     * EVALSHA at all. */
    // 在没有任何从服务器，AOF 关闭的情况下，清空 script 缓存
    // 因为已经没有传播 EVALSHA 的必要了
    if (listLength(server.slaves) == 0 &&
        server.aof_state == REDIS_AOF_OFF &&
        listLength(server.repl_scriptcache_fifo) != 0)
    {
        replicationScriptCacheFlush();
    }

    /* Refresh the number of slaves with lag <= min-slaves-max-lag. */
    // 更新符合给定延迟值的从服务器的数量
    refreshGoodSlavesCount();
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
leaq 0x94598(%rip), %rbp     # 0xc9fa8
cmpq $0x0, 0x650(%rbp)
je 0x35a5a
movl 0x674(%rbp), %eax
andl $-0x2, %eax
cmpl $0x2, %eax
jne 0x35a5a
xorl %edi, %edi
callq 0x1fc50
subq 0x6a0(%rbp), %rax
movslq 0x65c(%rbp), %rcx
cmpq %rcx, %rax
jle 0x35a5a
leaq 0x62daf(%rip), %rsi     # 0x987f8
movl $0x3, %edi
xorl %eax, %eax
callq 0x23064
callq 0x35291
cmpq $0x0, 0x650(%rbp)
je 0x35a9f
cmpl $0x4, 0x674(%rbp)
jne 0x35a9f
xorl %edi, %edi
callq 0x1fc50
subq 0x6a0(%rbp), %rax
movslq 0x65c(%rbp), %rcx
cmpq %rcx, %rax
jle 0x35a9f
leaq 0x62d8e(%rip), %rsi     # 0x9881c
movl $0x3, %edi
xorl %eax, %eax
callq 0x23064
callq 0x34374
cmpq $0x0, 0x650(%rbp)
je 0x35af2
cmpl $0x5, 0x674(%rbp)
jne 0x35af2
xorl %edi, %edi
callq 0x1fc50
movq 0x660(%rbp), %rcx
subq 0x80(%rcx), %rax
movslq 0x65c(%rbp), %rcx
cmpq %rcx, %rax
jle 0x35af2
leaq 0x62dce(%rip), %rsi     # 0x988a8
movl $0x3, %edi
xorl %eax, %eax
callq 0x23064
movq 0x660(%rbp), %rdi
callq 0x2e287
cmpl $0x1, 0x674(%rbp)
jne 0x35b37
movq 0x650(%rbp), %rdx
movl 0x658(%rbp), %ecx
leaq 0x62dc6(%rip), %rsi     # 0x988d5
movl $0x2, %edi
xorl %eax, %eax
callq 0x23064
callq 0x351d8
testl %eax, %eax
jne 0x35b37
leaq 0x62dc5(%rip), %rsi     # 0x988f0
movl $0x2, %edi
xorl %eax, %eax
callq 0x23064
cmpq $0x0, 0x650(%rbp)
setne %cl
movq 0x660(%rbp), %rax
testq %rax, %rax
setne %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x35b64
testb $0x1, 0x92(%rax)
jne 0x35b64
callq 0x33f14
movl 0x8(%rbp), %ecx
movl 0x54(%rbp), %eax
imull 0x5f8(%rbp), %ecx
cltd
idivl %ecx
testl %edx, %edx
jne 0x35c12
leaq 0x62d8b(%rip), %rdi     # 0x9890e
movl $0x4, %esi
callq 0x303b7
movq %rax, %rbx
leaq 0x10(%rsp), %rdx
movq %rax, (%rdx)
movq 0x1c0(%rbp), %rdi
movl 0x5ec(%rbp), %esi
movl $0x1, %ecx
callq 0x332f9
movq %rbx, %rdi
callq 0x30834
movq 0x1c0(%rbp), %rdi
leaq 0x20(%rsp), %rbx
movq %rbx, %rsi
callq 0x205d2
movq %rbx, %rdi
callq 0x205ef
testq %rax, %rax
je 0x35c12
leaq 0x60b62(%rip), %rbx     # 0x96741
leaq 0x20(%rsp), %r14
movq 0x10(%rax), %rax
movl 0x98(%rax), %ecx
andl $-0x2, %ecx
cmpl $0x6, %ecx
jne 0x35c05
movl (%rax), %edi
movl $0x1, %edx
movq %rbx, %rsi
callq 0x1fb30
movq %r14, %rdi
callq 0x205ef
testq %rax, %rax
jne 0x35be4
movq 0x1c0(%rbp), %rdi
cmpq $0x0, 0x28(%rdi)
je 0x35cd1
leaq 0x10(%rsp), %rbx
movq %rbx, %rsi
callq 0x205d2
movq %rbx, %rdi
callq 0x205ef
testq %rax, %rax
je 0x35cd1
leaq 0x20(%rsp), %rbx
leaq 0xc(%rsp), %r14
leaq 0x62cc0(%rip), %r15     # 0x98913
leaq 0x10(%rsp), %r12
movq 0x10(%rax), %r13
cmpl $0x9, 0x98(%r13)
jne 0x35cc4
testb $0x1, 0x92(%r13)
jne 0x35cc4
movq 0x798(%rbp), %rax
subq 0xc8(%r13), %rax
movslq 0x65c(%rbp), %rcx
cmpq %rcx, %rax
jle 0x35cc4
movl (%r13), %edi
movl $0x2e, %edx
movq %rbx, %rsi
movq %r14, %rcx
callq 0x21d05
cmpl $-0x1, %eax
je 0x35cbc
movl 0xfc(%r13), %ecx
movl $0x3, %edi
movq %r15, %rsi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x23064
movq %r13, %rdi
callq 0x2e287
movq %r12, %rdi
callq 0x205ef
testq %rax, %rax
jne 0x35c58
movq 0x1c0(%rbp), %rax
cmpq $0x0, 0x28(%rax)
jne 0x35d26
movq 0x628(%rbp), %rax
testq %rax, %rax
je 0x35d26
cmpq $0x0, 0x600(%rbp)
je 0x35d26
movq 0x798(%rbp), %rcx
subq 0x630(%rbp), %rcx
cmpq %rax, %rcx
jle 0x35d26
callq 0x331a9
movl 0x628(%rbp), %edx
leaq 0x62c1d(%rip), %rsi     # 0x98937
movl $0x2, %edi
xorl %eax, %eax
callq 0x23064
movq 0x1c0(%rbp), %rax
cmpq $0x0, 0x28(%rax)
jne 0x35d50
cmpl $0x0, 0x4d0(%rbp)
jne 0x35d50
movq 0x700(%rbp), %rax
cmpq $0x0, 0x28(%rax)
je 0x35d50
callq 0x33d87
callq 0x33a99
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq