// File: /workspace/github_c_makefile_star2_O3/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 0x94175(%rip), %rbp     # 0xc9fa8
cmpq $0x0, 0x650(%rbp)
je 0x35e7d
movl 0x674(%rbp), %eax
andl $-0x2, %eax
cmpl $0x2, %eax
jne 0x35e7d
xorl %edi, %edi
callq 0x1fc50
subq 0x6a0(%rbp), %rax
movslq 0x65c(%rbp), %rcx
cmpq %rcx, %rax
jle 0x35e7d
leaq 0x629fc(%rip), %rsi     # 0x98868
movl $0x3, %edi
xorl %eax, %eax
callq 0x23223
callq 0x356bf
cmpq $0x0, 0x650(%rbp)
je 0x35ec2
cmpl $0x4, 0x674(%rbp)
jne 0x35ec2
xorl %edi, %edi
callq 0x1fc50
subq 0x6a0(%rbp), %rax
movslq 0x65c(%rbp), %rcx
cmpq %rcx, %rax
jle 0x35ec2
leaq 0x629db(%rip), %rsi     # 0x9888c
movl $0x3, %edi
xorl %eax, %eax
callq 0x23223
callq 0x347b8
cmpq $0x0, 0x650(%rbp)
setne %cl
movl 0x674(%rbp), %eax
cmpl $0x5, %eax
sete %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x35f26
xorl %edi, %edi
callq 0x1fc50
movq 0x660(%rbp), %rcx
subq 0x80(%rcx), %rax
movslq 0x65c(%rbp), %rcx
cmpq %rcx, %rax
jle 0x35f20
leaq 0x62a10(%rip), %rsi     # 0x98918
movl $0x3, %edi
xorl %eax, %eax
callq 0x23223
movq 0x660(%rbp), %rdi
callq 0x2e517
movl 0x674(%rbp), %eax
cmpl $0x1, %eax
jne 0x35f67
movq 0x650(%rbp), %rdx
movl 0x658(%rbp), %ecx
leaq 0x62a06(%rip), %rsi     # 0x98945
movl $0x2, %edi
xorl %eax, %eax
callq 0x23223
callq 0x35606
testl %eax, %eax
jne 0x35f67
leaq 0x62a05(%rip), %rsi     # 0x98960
movl $0x2, %edi
xorl %eax, %eax
callq 0x23223
cmpq $0x0, 0x650(%rbp)
setne %cl
movq 0x660(%rbp), %rax
testq %rax, %rax
setne %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x35f94
testb $0x1, 0x92(%rax)
jne 0x35f94
callq 0x3434d
movl 0x8(%rbp), %ecx
movl 0x54(%rbp), %eax
imull 0x5f8(%rbp), %ecx
cltd
idivl %ecx
testl %edx, %edx
jne 0x36042
leaq 0x629cb(%rip), %rdi     # 0x9897e
movl $0x4, %esi
callq 0x3068f
movq %rax, %rbx
leaq 0x10(%rsp), %rdx
movq %rax, (%rdx)
movq 0x1c0(%rbp), %rdi
movl 0x5ec(%rbp), %esi
movl $0x1, %ecx
callq 0x33795
movq %rbx, %rdi
callq 0x30b0c
movq 0x1c0(%rbp), %rdi
leaq 0x20(%rsp), %rbx
movq %rbx, %rsi
callq 0x20637
movq %rbx, %rdi
callq 0x20654
testq %rax, %rax
je 0x36042
leaq 0x60782(%rip), %rbx     # 0x96791
leaq 0x20(%rsp), %r14
movq 0x10(%rax), %rax
movl 0x98(%rax), %ecx
andl $-0x2, %ecx
cmpl $0x6, %ecx
jne 0x36035
movl (%rax), %edi
movl $0x1, %edx
movq %rbx, %rsi
callq 0x1fb30
movq %r14, %rdi
callq 0x20654
testq %rax, %rax
jne 0x36014
movq 0x1c0(%rbp), %rdi
cmpq $0x0, 0x28(%rdi)
je 0x3610e
leaq 0x10(%rsp), %rbx
movq %rbx, %rsi
callq 0x20637
movq %rbx, %rdi
callq 0x20654
testq %rax, %rax
je 0x36101
leaq 0x20(%rsp), %rbx
leaq 0xc(%rsp), %r14
leaq 0x62900(%rip), %r15     # 0x98983
leaq 0x10(%rsp), %r12
movq 0x10(%rax), %r13
cmpl $0x9, 0x98(%r13)
jne 0x360f4
testb $0x1, 0x92(%r13)
jne 0x360f4
movq 0x798(%rbp), %rax
subq 0xc8(%r13), %rax
movslq 0x65c(%rbp), %rcx
cmpq %rcx, %rax
jle 0x360f4
movl (%r13), %edi
movl $0x2e, %edx
movq %rbx, %rsi
movq %r14, %rcx
callq 0x21ee7
cmpl $-0x1, %eax
je 0x360ec
movl 0xfc(%r13), %ecx
movl $0x3, %edi
movq %r15, %rsi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x23223
movq %r13, %rdi
callq 0x2e517
movq %r12, %rdi
callq 0x20654
testq %rax, %rax
jne 0x36088
movq 0x1c0(%rbp), %rax
movq 0x28(%rax), %rax
jmp 0x36110
xorl %eax, %eax
testq %rax, %rax
jne 0x3616b
movq 0x628(%rbp), %rcx
testq %rcx, %rcx
je 0x3616b
cmpq $0x0, 0x600(%rbp)
je 0x3616b
movq 0x798(%rbp), %rax
subq 0x630(%rbp), %rax
cmpq %rcx, %rax
jle 0x36169
callq 0x33631
movl 0x628(%rbp), %edx
leaq 0x62857(%rip), %rsi     # 0x989a7
movl $0x2, %edi
xorl %eax, %eax
callq 0x23223
movq 0x1c0(%rbp), %rax
movq 0x28(%rax), %rax
jmp 0x3616b
xorl %eax, %eax
testq %rax, %rax
jne 0x3618c
cmpl $0x0, 0x4d0(%rbp)
jne 0x3618c
movq 0x700(%rbp), %rax
cmpq $0x0, 0x28(%rax)
je 0x3618c
callq 0x341c0
callq 0x33f0b
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq