// File: /workspace/github_c_makefile_star2_O2/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 0x93435(%rip), %rbp     # 0xc6fa8
cmpq $0x0, 0x650(%rbp)
je 0x33bbb
movl 0x674(%rbp), %eax
andl $-0x2, %eax
cmpl $0x2, %eax
jne 0x33bbb
xorl %edi, %edi
callq 0x1fc50
subq 0x6a0(%rbp), %rax
movslq 0x65c(%rbp), %rcx
cmpq %rcx, %rax
jle 0x33bbb
leaq 0x5ac80(%rip), %rsi     # 0x8e82c
pushq $0x3
popq %rdi
xorl %eax, %eax
callq 0x22613
callq 0x33440
cmpq $0x0, 0x650(%rbp)
je 0x33bfe
cmpl $0x4, 0x674(%rbp)
jne 0x33bfe
xorl %edi, %edi
callq 0x1fc50
subq 0x6a0(%rbp), %rax
movslq 0x65c(%rbp), %rcx
cmpq %rcx, %rax
jle 0x33bfe
leaq 0x5ac61(%rip), %rsi     # 0x8e850
pushq $0x3
popq %rdi
xorl %eax, %eax
callq 0x22613
callq 0x325d1
cmpq $0x0, 0x650(%rbp)
setne %cl
movl 0x674(%rbp), %eax
cmpl $0x5, %eax
sete %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x33c60
xorl %edi, %edi
callq 0x1fc50
movq 0x660(%rbp), %rcx
subq 0x80(%rcx), %rax
movslq 0x65c(%rbp), %rcx
cmpq %rcx, %rax
jle 0x33c5a
leaq 0x5ac98(%rip), %rsi     # 0x8e8dc
pushq $0x3
popq %rdi
xorl %eax, %eax
callq 0x22613
movq 0x660(%rbp), %rdi
callq 0x2cb56
movl 0x674(%rbp), %eax
cmpl $0x1, %eax
jne 0x33c9d
movq 0x650(%rbp), %rdx
movl 0x658(%rbp), %ecx
leaq 0x5ac90(%rip), %rsi     # 0x8e909
pushq $0x2
popq %rdi
xorl %eax, %eax
callq 0x22613
callq 0x3338f
testl %eax, %eax
jne 0x33c9d
leaq 0x5ac91(%rip), %rsi     # 0x8e924
pushq $0x2
popq %rdi
xorl %eax, %eax
callq 0x22613
cmpq $0x0, 0x650(%rbp)
setne %cl
movq 0x660(%rbp), %rax
testq %rax, %rax
setne %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x33cca
testb $0x1, 0x92(%rax)
jne 0x33cca
callq 0x32198
movl 0x8(%rbp), %ecx
movl 0x54(%rbp), %eax
imull 0x5f8(%rbp), %ecx
cltd
idivl %ecx
testl %edx, %edx
je 0x33d9a
movq 0x1c0(%rbp), %rdi
cmpq $0x0, 0x28(%rdi)
je 0x33e22
leaq 0x10(%rsp), %rbx
movq %rbx, %rsi
callq 0x1ffac
leaq 0x20(%rsp), %r14
pushq $0x2e
popq %r15
leaq 0xc(%rsp), %r12
movq %rbx, %rdi
callq 0x1ffc6
testq %rax, %rax
je 0x33d8a
movq 0x10(%rax), %r13
cmpl $0x9, 0x98(%r13)
jne 0x33d0f
testb $0x1, 0x92(%r13)
jne 0x33d0f
movq 0x798(%rbp), %rax
subq 0xc8(%r13), %rax
movslq 0x65c(%rbp), %rcx
cmpq %rcx, %rax
jle 0x33d0f
movl (%r13), %edi
movq %r14, %rsi
movq %r15, %rdx
movq %r12, %rcx
callq 0x21481
cmpl $-0x1, %eax
je 0x33d80
movl 0xfc(%r13), %ecx
pushq $0x3
popq %rdi
leaq 0x5abd1(%rip), %rsi     # 0x8e947
movq %r14, %rdx
xorl %eax, %eax
callq 0x22613
movq %r13, %rdi
callq 0x2cb56
jmp 0x33d0f
movq 0x1c0(%rbp), %rax
movq 0x28(%rax), %rax
jmp 0x33e24
leaq 0x5aba1(%rip), %rdi     # 0x8e942
pushq $0x4
popq %rsi
callq 0x2ea81
movq %rax, %rbx
leaq 0x10(%rsp), %rdx
movq %rax, (%rdx)
movq 0x1c0(%rbp), %rdi
movl 0x5ec(%rbp), %esi
pushq $0x1
popq %rcx
callq 0x31639
movq %rbx, %rdi
callq 0x2ee1f
movq 0x1c0(%rbp), %rdi
leaq 0x20(%rsp), %rbx
movq %rbx, %rsi
callq 0x1ffac
leaq 0x58965(%rip), %r14     # 0x8c751
pushq $0x1
popq %r15
movq %rbx, %rdi
callq 0x1ffc6
testq %rax, %rax
je 0x33ce2
movq 0x10(%rax), %rax
movl 0x98(%rax), %ecx
andl $-0x2, %ecx
cmpl $0x6, %ecx
jne 0x33df0
movl (%rax), %edi
movq %r14, %rsi
movq %r15, %rdx
callq 0x1fb30
jmp 0x33df0
xorl %eax, %eax
testq %rax, %rax
jne 0x33e7d
movq 0x628(%rbp), %rcx
testq %rcx, %rcx
je 0x33e7d
cmpq $0x0, 0x600(%rbp)
je 0x33e7d
movq 0x798(%rbp), %rax
subq 0x630(%rbp), %rax
cmpq %rcx, %rax
jle 0x33e7b
callq 0x314e9
movl 0x628(%rbp), %edx
leaq 0x5ab07(%rip), %rsi     # 0x8e96b
pushq $0x2
popq %rdi
xorl %eax, %eax
callq 0x22613
movq 0x1c0(%rbp), %rax
movq 0x28(%rax), %rax
jmp 0x33e7d
xorl %eax, %eax
testq %rax, %rax
jne 0x33e9e
cmpl $0x0, 0x4d0(%rbp)
jne 0x33e9e
movq 0x700(%rbp), %rax
cmpq $0x0, 0x28(%rax)
je 0x33e9e
callq 0x32011
callq 0x31d7e
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop