// File: /workspace/github_c_makefile_star2_O1/nilyang[P]redis-tdd-annotation/src/cluster.c
// Function: clusterCron
void clusterCron(void) {
    dictIterator *di;
    dictEntry *de;
    int update_state = 0;
    int orphaned_masters; /* How many masters there are without ok slaves. */
    int max_slaves; /* Max number of ok slaves for a single master. */
    int this_slaves; /* Number of ok slaves for our master (if we are slave). */
    mstime_t min_pong = 0, now = mstime();
    clusterNode *min_pong_node = NULL;
    // 迭代计数器，一个静态变量
    static unsigned long long iteration = 0;
    mstime_t handshake_timeout;

    // 记录一次迭代
    iteration++; /* Number of times this function was called so far. */

    /* The handshake timeout is the time after which a handshake node that was
     * not turned into a normal node is removed from the nodes. Usually it is
     * just the NODE_TIMEOUT value, but when NODE_TIMEOUT is too small we use
     * the value of 1 second. */
    // 如果一个 handshake 节点没有在 handshake timeout 内
    // 转换成普通节点（normal node），
    // 那么节点会从 nodes 表中移除这个 handshake 节点
    // 一般来说 handshake timeout 的值总是等于 NODE_TIMEOUT
    // 不过如果 NODE_TIMEOUT 太少的话，程序会将值设为 1 秒钟
    handshake_timeout = server.cluster_node_timeout;
    if (handshake_timeout < 1000) handshake_timeout = 1000;

    /* Check if we have disconnected nodes and re-establish the connection. */
    // 向集群中的所有断线或者未连接节点发送消息
    di = dictGetSafeIterator(server.cluster->nodes);
    while((de = dictNext(di)) != NULL) {
        clusterNode *node = dictGetVal(de);

        // 跳过当前节点以及没有地址的节点
        if (node->flags & (REDIS_NODE_MYSELF|REDIS_NODE_NOADDR)) continue;

        /* A Node in HANDSHAKE state has a limited lifespan equal to the
         * configured node timeout. */
        // 如果 handshake 节点已超时，释放它
        if (nodeInHandshake(node) && now - node->ctime > handshake_timeout) {
            freeClusterNode(node);
            continue;
        }

        // 为未创建连接的节点创建连接
        if (node->link == NULL) {
            int fd;
            mstime_t old_ping_sent;
            clusterLink *link;

            fd = anetTcpNonBlockBindConnect(server.neterr, node->ip,
                node->port+REDIS_CLUSTER_PORT_INCR,
                    server.bindaddr_count ? server.bindaddr[0] : NULL);
            if (fd == -1) {
                redisLog(REDIS_DEBUG, "Unable to connect to "
                    "Cluster Node [%s]:%d -> %s", node->ip,
                    node->port+REDIS_CLUSTER_PORT_INCR,
                    server.neterr);
                continue;
            }
            link = createClusterLink(node);
            link->fd = fd;
            node->link = link;
            aeCreateFileEvent(server.el,link->fd,AE_READABLE,
                    clusterReadHandler,link);
            /* Queue a PING in the new connection ASAP: this is crucial
             * to avoid false positives in failure detection.
             *
             * If the node is flagged as MEET, we send a MEET message instead
             * of a PING one, to force the receiver to add us in its node
             * table. */
            // 向新连接的节点发送 PING 命令，防止节点被识进入下线
            // 如果节点被标记为 MEET ，那么发送 MEET 命令，否则发送 PING 命令
            old_ping_sent = node->ping_sent;
            clusterSendPing(link, node->flags & REDIS_NODE_MEET ?
                    CLUSTERMSG_TYPE_MEET : CLUSTERMSG_TYPE_PING);

            // 这不是第一次发送 PING 信息，所以可以还原这个时间
            // 等 clusterSendPing() 函数来更新它
            if (old_ping_sent) {
                /* If there was an active ping before the link was
                 * disconnected, we want to restore the ping time, otherwise
                 * replaced by the clusterSendPing() call. */
                node->ping_sent = old_ping_sent;
            }

            /* We can clear the flag after the first packet is sent.
             *
             * 在发送 MEET 信息之后，清除节点的 MEET 标识。
             *
             * If we'll never receive a PONG, we'll never send new packets
             * to this node. Instead after the PONG is received and we
             * are no longer in meet/handshake status, we want to send
             * normal PING packets. 
             *
             * 如果当前节点（发送者）没能收到 MEET 信息的回复，
             * 那么它将不再向目标节点发送命令。
             *
             * 如果接收到回复的话，那么节点将不再处于 HANDSHAKE 状态，
             * 并继续向目标节点发送普通 PING 命令。
             */
            node->flags &= ~REDIS_NODE_MEET;

            redisLog(REDIS_DEBUG,"Connecting with Node %.40s at %s:%d",
                    node->name, node->ip, node->port+REDIS_CLUSTER_PORT_INCR);
        }
    }
    dictReleaseIterator(di);

    /* Ping some random node 1 time every 10 iterations, so that we usually ping
     * one random node every second. */
    // clusterCron() 每执行 10 次（至少间隔一秒钟），就向一个随机节点发送 gossip 信息
    if (!(iteration % 10)) {
        int j;

        /* Check a few random nodes and ping the one with the oldest
         * pong_received time. */
        // 随机 5 个节点，选出其中一个
        for (j = 0; j < 5; j++) {

            // 随机在集群中挑选节点
            de = dictGetRandomKey(server.cluster->nodes);
            clusterNode *this = dictGetVal(de);

            /* Don't ping nodes disconnected or with a ping currently active. */
            // 不要 PING 连接断开的节点，也不要 PING 最近已经 PING 过的节点
            if (this->link == NULL || this->ping_sent != 0) continue;

            if (this->flags & (REDIS_NODE_MYSELF|REDIS_NODE_HANDSHAKE))
                continue;

            // 选出 5 个随机节点中最近一次接收 PONG 回复距离现在最旧的节点
            if (min_pong_node == NULL || min_pong > this->pong_received) {
                min_pong_node = this;
                min_pong = this->pong_received;
            }
        }

        // 向最久没有收到 PONG 回复的节点发送 PING 命令
        if (min_pong_node) {
            redisLog(REDIS_DEBUG,"Pinging node %.40s", min_pong_node->name);
            clusterSendPing(min_pong_node->link, CLUSTERMSG_TYPE_PING);
        }
    }

    // 遍历所有节点，检查是否需要将某个节点标记为下线
    /* Iterate nodes to check if we need to flag something as failing.
     * This loop is also responsible to:
     * 1) Check if there are orphaned masters (masters without non failing
     *    slaves).
     * 2) Count the max number of non failing slaves for a single master.
     * 3) Count the number of slaves for our master, if we are a slave. */
    orphaned_masters = 0;
    max_slaves = 0;
    this_slaves = 0;
    di = dictGetSafeIterator(server.cluster->nodes);
    while((de = dictNext(di)) != NULL) {
        clusterNode *node = dictGetVal(de);
        now = mstime(); /* Use an updated time at every iteration. */
        mstime_t delay;

        // 跳过节点本身、无地址节点、HANDSHAKE 状态的节点
        if (node->flags &
            (REDIS_NODE_MYSELF|REDIS_NODE_NOADDR|REDIS_NODE_HANDSHAKE))
                continue;

        /* Orphaned master check, useful only if the current instance
         * is a slave that may migrate to another master. */
        if (nodeIsSlave(myself) && nodeIsMaster(node) && !nodeFailed(node)) {
            int okslaves = clusterCountNonFailingSlaves(node);

            if (okslaves == 0 && node->numslots > 0) orphaned_masters++;
            if (okslaves > max_slaves) max_slaves = okslaves;
            if (nodeIsSlave(myself) && myself->slaveof == node)
                this_slaves = okslaves;
        }

        /* If we are waiting for the PONG more than half the cluster
         * timeout, reconnect the link: maybe there is a connection
         * issue even if the node is alive. */
        // 如果等到 PONG 到达的时间超过了 node timeout 一半的连接
        // 因为尽管节点依然正常，但连接可能已经出问题了
        if (node->link && /* is connected */
            now - node->link->ctime >
            server.cluster_node_timeout && /* was not already reconnected */
            node->ping_sent && /* we already sent a ping */
            node->pong_received < node->ping_sent && /* still waiting pong */
            /* and we are waiting for the pong more than timeout/2 */
            now - node->ping_sent > server.cluster_node_timeout/2)
        {
            /* Disconnect the link, it will be reconnected automatically. */
            // 释放连接，下次 clusterCron() 会自动重连
            freeClusterLink(node->link);
        }

        /* If we have currently no active ping in this instance, and the
         * received PONG is older than half the cluster timeout, send
         * a new ping now, to ensure all the nodes are pinged without
         * a too big delay. */
        // 如果目前没有在 PING 节点
        // 并且已经有 node timeout 一半的时间没有从节点那里收到 PONG 回复
        // 那么向节点发送一个 PING ，确保节点的信息不会太旧
        // （因为一部分节点可能一直没有被随机中）
        if (node->link &&
            node->ping_sent == 0 &&
            (now - node->pong_received) > server.cluster_node_timeout/2)
        {
            clusterSendPing(node->link, CLUSTERMSG_TYPE_PING);
            continue;
        }

        /* If we are a master and one of the slaves requested a manual
         * failover, ping it continuously. */
        // 如果这是一个主节点，并且有一个从服务器请求进行手动故障转移
        // 那么向从服务器发送 PING 。
        if (server.cluster->mf_end &&
            nodeIsMaster(myself) &&
            server.cluster->mf_slave == node &&
            node->link)
        {
            clusterSendPing(node->link, CLUSTERMSG_TYPE_PING);
            continue;
        }

        /* Check only if we have an active ping for this instance. */
        // 以下代码只在节点发送了 PING 命令的情况下执行
        if (node->ping_sent == 0) continue;

        /* Compute the delay of the PONG. Note that if we already received
         * the PONG, then node->ping_sent is zero, so can't reach this
         * code at all. */
        // 计算等待 PONG 回复的时长
        delay = now - node->ping_sent;

        // 等待 PONG 回复的时长超过了限制值，将目标节点标记为 PFAIL （疑似下线）
        if (delay > server.cluster_node_timeout) {
            /* Timeout reached. Set the node as possibly failing if it is
             * not already in this state. */
            if (!(node->flags & (REDIS_NODE_PFAIL|REDIS_NODE_FAIL))) {
                redisLog(REDIS_DEBUG,"*** NODE %.40s possibly failing",
                    node->name);
                // 打开疑似下线标记
                node->flags |= REDIS_NODE_PFAIL;
                update_state = 1;
            }
        }
    }
    dictReleaseIterator(di);

    /* If we are a slave node but the replication is still turned off,
     * enable it if we know the address of our master and it appears to
     * be up. */
    // 如果从节点没有在复制主节点，那么对从节点进行设置
    if (nodeIsSlave(myself) &&
        server.masterhost == NULL &&
        myself->slaveof &&
        nodeHasAddr(myself->slaveof))
    {
        replicationSetMaster(myself->slaveof->ip, myself->slaveof->port);
    }

    /* Abourt a manual failover if the timeout is reached. */
    manualFailoverCheckTimeout();

    if (nodeIsSlave(myself)) {
        clusterHandleManualFailover();
        clusterHandleSlaveFailover();
        /* If there are orphaned slaves, and we are a slave among the masters
         * with the max number of non-failing slaves, consider migrating to
         * the orphaned masters. Note that it does not make sense to try
         * a migration if there is no master with at least *two* working
         * slaves. */
        if (orphaned_masters && max_slaves >= 2 && this_slaves == max_slaves)
            clusterHandleSlaveMigration(max_slaves);
    }

    // 更新集群状态
    if (update_state || server.cluster->state == REDIS_CLUSTER_FAIL)
        clusterUpdateState();
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
callq 0x232a3
movq %rax, %r12
incq 0x8a88b(%rip)           # 0xde630
leaq 0x761fc(%rip), %r13     # 0xc9fa8
movq 0x7c0(%r13), %rax
movq 0x7d0(%r13), %rcx
cmpq $0x3e9, %rax            # imm = 0x3E9
movl $0x3e8, %ebx            # imm = 0x3E8
cmovgeq %rax, %rbx
movq 0x18(%rcx), %rdi
callq 0x228e4
movq %rax, %r14
movq %rax, %rdi
callq 0x22913
testq %rax, %rax
je 0x53f65
movl $0x2710, %r15d          # imm = 0x2710
movl $0x1e8, %ecx            # imm = 0x1E8
addq 0x721c0(%rip), %rcx     # 0xc5fb8
movq %rcx, 0x8(%rsp)
leaq 0x1e8(%r13), %rcx
movq %rcx, (%rsp)
movq %rbx, 0x10(%rsp)
movq 0x8(%rax), %rbp
movl 0x30(%rbp), %eax
testb $0x50, %al
jne 0x53f54
testb $0x20, %al
je 0x53e39
movq %r12, %rax
subq (%rbp), %rax
cmpq %rbx, %rax
jle 0x53e39
movq %rbp, %rdi
callq 0x51457
jmp 0x53f54
cmpq $0x0, 0x8c0(%rbp)
jne 0x53f54
cmpl $0x0, 0x110(%r13)
je 0x53e5a
movq 0x90(%r13), %rcx
jmp 0x53e5c
xorl %ecx, %ecx
leaq 0x888(%rbp), %r13
movl 0x8b8(%rbp), %edx
addl %r15d, %edx
movq (%rsp), %rdi
movq %r13, %rsi
callq 0x21773
movl $0x2710, %edx           # imm = 0x2710
cmpl $-0x1, %eax
je 0x53f27
movl %eax, %r15d
movq %r12, %rbx
movq %rbp, %rdi
callq 0x50fcf
movq %rax, %r12
movl %r15d, 0x8(%rax)
movq %rax, 0x8c0(%rbp)
leaq 0x760ff(%rip), %rax     # 0xc9fa8
movq 0x28(%rax), %rdi
movl %r15d, %esi
movl $0x1, %edx
leaq -0x2e36(%rip), %rcx     # 0x51086
movq %r12, %r8
callq 0x209d7
movq 0x858(%rbp), %r15
movl 0x30(%rbp), %esi
shrl $0x6, %esi
andl $0x2, %esi
movq %r12, %rdi
callq 0x52bc7
testq %r15, %r15
je 0x53ee8
movq %r15, 0x858(%rbp)
andb $0x7f, 0x30(%rbp)
movl 0x8b8(%rbp), %r8d
movl $0x2710, %r15d          # imm = 0x2710
addl %r15d, %r8d
addq $0x8, %rbp
xorl %edi, %edi
leaq 0x48824(%rip), %rsi     # 0x9c72d
movq %rbp, %rdx
movq %r13, %rcx
xorl %eax, %eax
callq 0x23064
leaq 0x7608b(%rip), %r13     # 0xc9fa8
movq %rbx, %r12
movq 0x10(%rsp), %rbx
jmp 0x53f54
movl 0x8b8(%rbp), %ecx
addl %edx, %ecx
xorl %edi, %edi
leaq 0x487c5(%rip), %rsi     # 0x9c6fd
movq %r13, %rdx
movq 0x8(%rsp), %r8
xorl %eax, %eax
callq 0x23064
leaq 0x7605a(%rip), %r13     # 0xc9fa8
movl $0x2710, %r15d          # imm = 0x2710
movq %r14, %rdi
callq 0x22913
testq %rax, %rax
jne 0x53e0d
movq %r14, %rdi
callq 0x229b9
movabsq $-0x3333333333333333, %rax # imm = 0xCCCCCCCCCCCCCCCD
imulq 0x8a6b1(%rip), %rax     # 0xde630
rorq %rax
movabsq $0x1999999999999999, %rcx # imm = 0x1999999999999999
cmpq %rcx, %rax
ja 0x5400c
movl $0x5, %ebp
xorl %r14d, %r14d
xorl %ebx, %ebx
movq 0x7d0(%r13), %rax
movq 0x18(%rax), %rdi
callq 0x22a1c
movq 0x8(%rax), %rax
cmpq $0x0, 0x8c0(%rax)
je 0x53fe1
cmpq $0x0, 0x858(%rax)
jne 0x53fe1
testb $0x30, 0x30(%rax)
jne 0x53fe1
testq %rbx, %rbx
je 0x53fd7
cmpq 0x860(%rax), %r14
jle 0x53fe1
movq 0x860(%rax), %r14
movq %rax, %rbx
decl %ebp
jne 0x53f9b
testq %rbx, %rbx
je 0x5400c
leaq 0x8(%rbx), %rdx
leaq 0x4875c(%rip), %rsi     # 0x9c751
xorl %edi, %edi
xorl %eax, %eax
callq 0x23064
movq 0x8c0(%rbx), %rdi
xorl %esi, %esi
callq 0x52bc7
movq 0x7d0(%r13), %rax
movq 0x18(%rax), %rdi
callq 0x228e4
movq %rax, %r14
movq %rax, %rdi
callq 0x22913
testq %rax, %rax
je 0x54230
movl $0x0, (%rsp)
xorl %r13d, %r13d
xorl %ebx, %ebx
xorl %ebp, %ebp
movq 0x8(%rax), %r15
callq 0x232a3
movl 0x30(%r15), %edx
testb $0x70, %dl
je 0x54062
movq %r14, %rdi
callq 0x22913
testq %rax, %rax
jne 0x5403e
jmp 0x54218
movq %rax, %r12
movq 0x8a5bc(%rip), %rax     # 0xde628
movl 0x30(%rax), %ecx
testb $0x2, %cl
sete %sil
andl $0x9, %edx
cmpl $0x1, %edx
setne %dl
orb %sil, %dl
jne 0x540e4
movslq 0x844(%r15), %rsi
testq %rsi, %rsi
jle 0x540b9
movq 0x848(%r15), %rdi
xorl %r8d, %r8d
xorl %edx, %edx
movq (%rdi,%r8,8), %r9
movb 0x30(%r9), %r9b
andb $0x8, %r9b
cmpb $0x1, %r9b
adcl $0x0, %edx
incq %r8
cmpq %r8, %rsi
jne 0x5409c
jmp 0x540bb
xorl %edx, %edx
testl %edx, %edx
jne 0x540d0
xorl %esi, %esi
cmpl $0x0, 0x840(%r15)
setg %sil
addl %esi, %r13d
cmpl %ebx, %edx
cmovgl %edx, %ebx
testb $0x2, %cl
je 0x540e4
cmpq %r15, 0x850(%rax)
cmovel %edx, %ebp
movq 0x8c0(%r15), %rdi
testq %rdi, %rdi
je 0x5413b
movq %r12, %rcx
subq (%rdi), %rcx
leaq 0x75eab(%rip), %rax     # 0xc9fa8
movq 0x7c0(%rax), %rax
cmpq %rax, %rcx
jle 0x5413b
movq 0x858(%r15), %rcx
testq %rcx, %rcx
je 0x5413b
cmpq %rcx, 0x860(%r15)
jge 0x5413b
movq %r12, %rdx
subq %rcx, %rdx
movq %rax, %rcx
shrq $0x3f, %rcx
addq %rax, %rcx
sarq %rcx
cmpq %rcx, %rdx
jle 0x5413b
callq 0x51014
movq 0x8c0(%r15), %rdi
testq %rdi, %rdi
je 0x54182
cmpq $0x0, 0x858(%r15)
jne 0x54182
movq 0x860(%r15), %rax
movq %r12, %rcx
subq %rax, %rcx
leaq 0x75e43(%rip), %rax     # 0xc9fa8
movq 0x7c0(%rax), %rax
movq %rax, %rdx
shrq $0x3f, %rdx
addq %rax, %rdx
sarq %rdx
cmpq %rdx, %rcx
jg 0x5420c
leaq 0x75e1f(%rip), %rax     # 0xc9fa8
movq 0x7d0(%rax), %rax
cmpq $0x0, 0x60050(%rax)
je 0x541a7
movq 0x8a487(%rip), %rcx     # 0xde628
testb $0x1, 0x30(%rcx)
jne 0x541fe
movq 0x858(%r15), %rax
testq %rax, %rax
je 0x54050
subq %rax, %r12
leaq 0x75de7(%rip), %rax     # 0xc9fa8
cmpq 0x7c0(%rax), %r12
jle 0x54050
testb $0xc, 0x30(%r15)
jne 0x54050
leaq 0x8(%r15), %rdx
xorl %edi, %edi
leaq 0x4857e(%rip), %rsi     # 0x9c764
xorl %eax, %eax
callq 0x23064
orb $0x4, 0x30(%r15)
movl $0x1, (%rsp)
jmp 0x54050
testq %rdi, %rdi
je 0x541a7
cmpq %r15, 0x60058(%rax)
jne 0x541a7
xorl %esi, %esi
callq 0x52bc7
jmp 0x54050
testl %r13d, %r13d
setne %r12b
cmpl $0x0, (%rsp)
sete %r15b
leaq 0x75d7a(%rip), %r13     # 0xc9fa8
jmp 0x5423a
movb $0x1, %r15b
xorl %ebp, %ebp
xorl %ebx, %ebx
xorl %r12d, %r12d
movq %r14, %rdi
callq 0x229b9
movq 0x8a3df(%rip), %rax     # 0xde628
testb $0x2, 0x30(%rax)
je 0x5427d
cmpq $0x0, 0x650(%r13)
jne 0x5427d
movq 0x850(%rax), %rdi
testq %rdi, %rdi
je 0x5427d
testb $0x40, 0x30(%rdi)
jne 0x5427d
movl 0x8b8(%rdi), %esi
addq $0x888, %rdi            # imm = 0x888
callq 0x35339
callq 0x53cda
movq 0x8a39f(%rip), %rax     # 0xde628
testb $0x2, 0x30(%rax)
je 0x542b3
callq 0x53d1a
callq 0x535d4
cmpl $0x2, %ebx
setge %al
andb %al, %r12b
cmpb $0x1, %r12b
jne 0x542b3
cmpl %ebx, %ebp
jne 0x542b3
movl %ebx, %edi
callq 0x53b29
testb %r15b, %r15b
je 0x542c5
movq 0x7d0(%r13), %rax
cmpl $0x1, 0x10(%rax)
jne 0x542d8
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x53989
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq