// File: /workspace/github_c_makefile_star2_O2/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 0x22843
movq %rax, %r15
incq 0x8c18b(%rip)           # 0xdb630
leaq 0x77afc(%rip), %r12     # 0xc6fa8
movq 0x7c0(%r12), %rax
movq 0x7d0(%r12), %rcx
cmpq $0x3e9, %rax            # imm = 0x3E9
movl $0x3e8, %ebx            # imm = 0x3E8
cmovgeq %rax, %rbx
movq 0x18(%rcx), %rdi
callq 0x21f20
movq %rax, %r14
leaq 0x1e8(%r12), %rax
movq %rax, (%rsp)
movl $0x1e8, %eax            # imm = 0x1E8
addq 0x73ac9(%rip), %rax     # 0xc2fb8
movq %rax, 0x10(%rsp)
movq %rbx, 0x8(%rsp)
movq %r14, %rdi
callq 0x21f2f
testq %rax, %rax
je 0x4f650
movq 0x8(%rax), %r13
movl 0x30(%r13), %eax
testb $0x50, %al
jne 0x4f4f9
testb $0x20, %al
je 0x4f530
movq %r15, %rax
subq (%r13), %rax
cmpq %rbx, %rax
jle 0x4f530
movq %r13, %rdi
callq 0x4cda2
jmp 0x4f4f9
cmpq $0x0, 0x8c0(%r13)
jne 0x4f4f9
cmpl $0x0, 0x110(%r12)
je 0x4f54f
movq 0x90(%r12), %rcx
jmp 0x4f551
xorl %ecx, %ecx
movl $0x2710, %eax           # imm = 0x2710
leaq 0x888(%r13), %rbp
movl 0x8b8(%r13), %edx
addl %eax, %edx
movq (%rsp), %rdi
movq %rbp, %rsi
callq 0x20f61
cmpl $-0x1, %eax
je 0x4f61e
movl %eax, %r12d
movq %r15, %rbx
movq %r13, %rdi
callq 0x4c93b
movq %rax, %r15
movl %r12d, 0x8(%rax)
movq %rax, 0x8c0(%r13)
leaq 0x77a0a(%rip), %rax     # 0xc6fa8
movq 0x28(%rax), %rdi
movl %r12d, %esi
pushq $0x1
popq %rdx
leaq -0x2bc9(%rip), %rcx     # 0x4c9e6
movq %r15, %r8
callq 0x2030c
movq 0x858(%r13), %r12
movl 0x30(%r13), %esi
shrl $0x6, %esi
andl $0x2, %esi
movq %r15, %rdi
callq 0x4e361
testq %r12, %r12
je 0x4f5dc
movq %r12, 0x858(%r13)
andb $0x7f, 0x30(%r13)
movl 0x8b8(%r13), %r8d
movl $0x2710, %eax           # imm = 0x2710
addl %eax, %r8d
addq $0x8, %r13
xorl %edi, %edi
leaq 0x43130(%rip), %rsi     # 0x9272d
movq %r13, %rdx
movq %rbp, %rcx
xorl %eax, %eax
callq 0x22613
leaq 0x77997(%rip), %r12     # 0xc6fa8
movq %rbx, %r15
movq 0x8(%rsp), %rbx
jmp 0x4f4f9
movl 0x8b8(%r13), %ecx
movl $0x2710, %eax           # imm = 0x2710
addl %eax, %ecx
xorl %edi, %edi
leaq 0x430c8(%rip), %rsi     # 0x926fd
movq %rbp, %rdx
movq 0x10(%rsp), %r8
xorl %eax, %eax
callq 0x22613
leaq 0x7795d(%rip), %r12     # 0xc6fa8
jmp 0x4f4f9
movq %r14, %rdi
callq 0x21fe6
movq 0x8bfd1(%rip), %rax     # 0xdb630
movq $0x0, (%rsp)
pushq $0xa
popq %rcx
xorl %edx, %edx
divq %rcx
testq %rdx, %rdx
jne 0x4f6fd
pushq $0x5
popq %rbx
xorl %r14d, %r14d
xorl %r15d, %r15d
subl $0x1, %ebx
jb 0x4f6d6
movq 0x7d0(%r12), %rax
movq 0x18(%rax), %rdi
callq 0x22047
movq 0x8(%rax), %rax
cmpq $0x0, 0x8c0(%rax)
je 0x4f681
cmpq $0x0, 0x858(%rax)
jne 0x4f681
testb $0x30, 0x30(%rax)
jne 0x4f681
movq 0x860(%rax), %rcx
testq %r14, %r14
je 0x4f6ce
cmpq %rcx, %r15
cmovgq %rax, %r14
cmovgeq %rcx, %r15
jmp 0x4f681
movq %rax, %r14
movq %rcx, %r15
jmp 0x4f681
testq %r14, %r14
je 0x4f6fd
leaq 0x8(%r14), %rdx
leaq 0x4306b(%rip), %rsi     # 0x92751
xorl %edi, %edi
xorl %eax, %eax
callq 0x22613
movq 0x8c0(%r14), %rdi
xorl %esi, %esi
callq 0x4e361
movq 0x7d0(%r12), %rax
movq 0x18(%rax), %rdi
callq 0x21f20
movq %rax, %r14
movb $0x1, %bpl
xorl %r15d, %r15d
movl $0x0, 0x8(%rsp)
movq %r14, %rdi
callq 0x21f2f
testq %rax, %rax
je 0x4f8ce
movq 0x8(%rax), %r12
callq 0x22843
movq %rax, %r13
movl 0x30(%r12), %eax
testb $0x70, %al
jne 0x4f71f
andl $0x9, %eax
cmpl $0x1, %eax
jne 0x4f793
movq 0x8bed4(%rip), %rbx     # 0xdb628
movl 0x30(%rbx), %eax
andl $0x2, %eax
je 0x4f793
movq %r12, %rdi
callq 0x4cd6e
testl %eax, %eax
jne 0x4f77a
xorl %ecx, %ecx
cmpl $0x0, 0x840(%r12)
setg %cl
addl %ecx, 0x8(%rsp)
cmpl %r15d, %eax
cmovgl %eax, %r15d
cmpq %r12, 0x850(%rbx)
movq (%rsp), %rcx
cmovel %eax, %ecx
movq %rcx, (%rsp)
movq 0x8c0(%r12), %rdi
movb $0x1, %bl
testq %rdi, %rdi
je 0x4f837
movq %r13, %rcx
subq (%rdi), %rcx
leaq 0x777f5(%rip), %rax     # 0xc6fa8
movq 0x7c0(%rax), %rax
cmpq %rax, %rcx
jle 0x4f7fb
movq 0x858(%r12), %rcx
testq %rcx, %rcx
je 0x4f7fb
cmpq %rcx, 0x860(%r12)
jge 0x4f7fb
movq %r13, %rsi
subq %rcx, %rsi
cqto
pushq $0x2
popq %rcx
idivq %rcx
cmpq %rax, %rsi
jle 0x4f7fb
callq 0x4c97b
movq 0x8c0(%r12), %rdi
testq %rdi, %rdi
je 0x4f837
cmpq $0x0, 0x858(%r12)
jne 0x4f833
movq 0x860(%r12), %rax
movq %r13, %rcx
subq %rax, %rcx
leaq 0x7778d(%rip), %rax     # 0xc6fa8
movq 0x7c0(%rax), %rax
cqto
pushq $0x2
popq %rsi
idivq %rsi
cmpq %rax, %rcx
jg 0x4f8c2
xorl %ebx, %ebx
jmp 0x4f839
xorl %edi, %edi
leaq 0x77768(%rip), %rax     # 0xc6fa8
movq 0x7d0(%rax), %rax
cmpq $0x0, 0x60050(%rax)
je 0x4f86c
movq 0x8bdd0(%rip), %rcx     # 0xdb628
testb $0x1, 0x30(%rcx)
je 0x4f86c
cmpq %r12, 0x60058(%rax)
setne %al
orb %al, %bl
je 0x4f8c2
movq 0x858(%r12), %rax
testq %rax, %rax
je 0x4f71f
subq %rax, %r13
leaq 0x77721(%rip), %rax     # 0xc6fa8
cmpq 0x7c0(%rax), %r13
jle 0x4f71f
testb $0xc, 0x30(%r12)
jne 0x4f71f
leaq 0x8(%r12), %rdx
xorl %ebp, %ebp
xorl %edi, %edi
leaq 0x42eb4(%rip), %rsi     # 0x92764
xorl %eax, %eax
callq 0x22613
orb $0x4, 0x30(%r12)
jmp 0x4f71f
xorl %esi, %esi
callq 0x4e361
jmp 0x4f71f
movq %r14, %rdi
callq 0x21fe6
movq 0x8bd4b(%rip), %rax     # 0xdb628
testb $0x2, 0x30(%rax)
leaq 0x776c0(%rip), %rbx     # 0xc6fa8
je 0x4f918
cmpq $0x0, 0x650(%rbx)
jne 0x4f918
movq 0x850(%rax), %rdi
testq %rdi, %rdi
je 0x4f918
testb $0x40, 0x30(%rdi)
jne 0x4f918
movl 0x8b8(%rdi), %esi
addq $0x888, %rdi            # imm = 0x888
callq 0x334df
callq 0x4f3de
movq 0x8bd04(%rip), %rax     # 0xdb628
testb $0x2, 0x30(%rax)
je 0x4f950
callq 0x4f41c
callq 0x4ed65
cmpl $0x0, 0x8(%rsp)
je 0x4f950
cmpl $0x2, %r15d
jl 0x4f950
cmpl %r15d, (%rsp)
jne 0x4f950
movq (%rsp), %rdi
callq 0x4f265
testb $0x1, %bpl
je 0x4f963
movq 0x7d0(%rbx), %rax
cmpl $0x1, 0x10(%rax)
jne 0x4f976
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x4f0dc
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq