// File: /workspace/github_c_makefile_star2_O3/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 0x23461
movq %rax, %r12
incq 0x8ae4c(%rip)           # 0xde630
leaq 0x767bd(%rip), %r15     # 0xc9fa8
movq 0x7c0(%r15), %rax
movq 0x7d0(%r15), %rcx
cmpq $0x3e9, %rax            # imm = 0x3E9
movl $0x3e8, %ebx            # imm = 0x3E8
cmovgeq %rax, %rbx
movq 0x18(%rcx), %rdi
callq 0x22aa0
movq %rax, %r14
movq %rax, %rdi
callq 0x22acf
testq %rax, %rax
je 0x5399b
movl $0x1e8, %ecx            # imm = 0x1E8
addq 0x72787(%rip), %rcx     # 0xc5fb8
movq %rcx, 0x10(%rsp)
leaq 0x1e8(%r15), %rcx
movq %rcx, 0x8(%rsp)
movq %rbx, (%rsp)
movq 0x8(%rax), %rbp
movl 0x30(%rbp), %eax
testb $0x50, %al
jne 0x5398a
testb $0x20, %al
je 0x53872
movq %r12, %rax
subq (%rbp), %rax
cmpq %rbx, %rax
jle 0x53872
movq %rbp, %rdi
callq 0x50e98
jmp 0x5398a
cmpq $0x0, 0x8c0(%rbp)
jne 0x5398a
cmpl $0x0, 0x110(%r15)
je 0x53893
movq 0x90(%r15), %rcx
jmp 0x53895
xorl %ecx, %ecx
movl $0x2710, %eax           # imm = 0x2710
leaq 0x888(%rbp), %r13
movl 0x8b8(%rbp), %edx
addl %eax, %edx
movq 0x8(%rsp), %rdi
movq %r13, %rsi
callq 0x21980
cmpl $-0x1, %eax
je 0x5395e
movl %eax, %r15d
movq %r12, %rbx
movq %rbp, %rdi
callq 0x50a17
movq %rax, %r12
movl %r15d, 0x8(%rax)
movq %rax, 0x8c0(%rbp)
leaq 0x766c6(%rip), %rax     # 0xc9fa8
movq 0x28(%rax), %rdi
movl %r15d, %esi
movl $0x1, %edx
leaq -0x2e27(%rip), %rcx     # 0x50ace
movq %r12, %r8
callq 0x20c22
movq 0x858(%rbp), %r15
movl 0x30(%rbp), %esi
shrl $0x6, %esi
andl $0x2, %esi
movq %r12, %rdi
callq 0x52605
testq %r15, %r15
je 0x53921
movq %r15, 0x858(%rbp)
andb $0x7f, 0x30(%rbp)
movl 0x8b8(%rbp), %r8d
movl $0x2710, %eax           # imm = 0x2710
addl %eax, %r8d
addq $0x8, %rbp
xorl %edi, %edi
leaq 0x48e2c(%rip), %rsi     # 0x9c76d
movq %rbp, %rdx
movq %r13, %rcx
xorl %eax, %eax
callq 0x23223
leaq 0x76653(%rip), %r15     # 0xc9fa8
movq %rbx, %r12
movq (%rsp), %rbx
jmp 0x5398a
movl 0x8b8(%rbp), %ecx
movl $0x2710, %eax           # imm = 0x2710
addl %eax, %ecx
xorl %edi, %edi
leaq 0x48dc9(%rip), %rsi     # 0x9c73d
movq %r13, %rdx
movq 0x10(%rsp), %r8
xorl %eax, %eax
callq 0x23223
leaq 0x7661e(%rip), %r15     # 0xc9fa8
movq %r14, %rdi
callq 0x22acf
testq %rax, %rax
jne 0x53846
movq %r14, %rdi
callq 0x22b86
movabsq $-0x3333333333333333, %rax # imm = 0xCCCCCCCCCCCCCCCD
imulq 0x8ac7b(%rip), %rax     # 0xde630
rorq %rax
movabsq $0x1999999999999999, %rcx # imm = 0x1999999999999999
cmpq %rcx, %rax
ja 0x53a4d
movl $0x5, %ebp
xorl %r14d, %r14d
xorl %ebx, %ebx
movq 0x7d0(%r15), %rax
movq 0x18(%rax), %rdi
callq 0x22be9
movq 0x8(%rax), %rax
cmpq $0x0, 0x8c0(%rax)
je 0x53a22
cmpq $0x0, 0x858(%rax)
jne 0x53a22
testb $0x30, 0x30(%rax)
jne 0x53a22
movq 0x860(%rax), %rcx
testq %rbx, %rbx
je 0x53a1c
cmpq %rcx, %r14
cmovgq %rax, %rbx
cmovgeq %rcx, %r14
jmp 0x53a22
movq %rax, %rbx
movq %rcx, %r14
decl %ebp
jne 0x539d5
testq %rbx, %rbx
je 0x53a4d
leaq 0x8(%rbx), %rdx
leaq 0x48d5b(%rip), %rsi     # 0x9c791
xorl %edi, %edi
xorl %eax, %eax
callq 0x23223
movq 0x8c0(%rbx), %rdi
xorl %esi, %esi
callq 0x52605
movq 0x7d0(%r15), %rax
movq 0x18(%rax), %rdi
callq 0x22aa0
movq %rax, %r14
movq %rax, %rdi
callq 0x22acf
testq %rax, %rax
je 0x53c6e
movl $0x0, (%rsp)
movl $0x0, 0x8(%rsp)
xorl %r13d, %r13d
xorl %ebp, %ebp
movq 0x8(%rax), %r15
callq 0x23461
movq %rax, %r12
movl 0x30(%r15), %eax
testb $0x70, %al
je 0x53aab
movq %r14, %rdi
callq 0x22acf
testq %rax, %rax
jne 0x53a85
jmp 0x53c5c
andl $0x9, %eax
cmpl $0x1, %eax
jne 0x53b1c
movq 0x8ab6e(%rip), %rax     # 0xde628
movl 0x30(%rax), %ecx
andl $0x2, %ecx
je 0x53b1c
movslq 0x844(%r15), %rdx
testq %rdx, %rdx
jle 0x53af8
movq 0x848(%r15), %rsi
xorl %edi, %edi
xorl %ecx, %ecx
movq (%rsi,%rdi,8), %r8
movb 0x30(%r8), %r8b
andb $0x8, %r8b
cmpb $0x1, %r8b
adcl $0x0, %ecx
incq %rdi
cmpq %rdi, %rdx
jne 0x53ad9
testl %ecx, %ecx
jne 0x53b0b
xorl %ecx, %ecx
cmpl $0x0, 0x840(%r15)
setg %cl
addl %ecx, 0x8(%rsp)
xorl %ecx, %ecx
cmpl %r13d, %ecx
cmovgl %ecx, %r13d
cmpq %r15, 0x850(%rax)
cmovel %ecx, %ebp
movq 0x8c0(%r15), %rdi
movb $0x1, %bl
testq %rdi, %rdi
je 0x53bc4
movq %r12, %rcx
subq (%rdi), %rcx
leaq 0x7646d(%rip), %rax     # 0xc9fa8
movq 0x7c0(%rax), %rax
cmpq %rax, %rcx
jle 0x53b85
movq 0x858(%r15), %rcx
testq %rcx, %rcx
je 0x53b85
cmpq %rcx, 0x860(%r15)
jge 0x53b85
movq %r12, %rdx
subq %rcx, %rdx
movq %rax, %rcx
shrq $0x3f, %rcx
addq %rax, %rcx
sarq %rcx
cmpq %rcx, %rdx
jle 0x53b85
callq 0x50a5c
movq 0x8c0(%r15), %rdi
testq %rdi, %rdi
je 0x53bc4
cmpq $0x0, 0x858(%r15)
jne 0x53bc0
movq 0x860(%r15), %rax
movq %r12, %rcx
subq %rax, %rcx
leaq 0x76405(%rip), %rax     # 0xc9fa8
movq 0x7c0(%rax), %rax
movq %rax, %rdx
shrq $0x3f, %rdx
addq %rax, %rdx
sarq %rdx
cmpq %rdx, %rcx
jg 0x53c50
xorl %ebx, %ebx
jmp 0x53bc6
xorl %edi, %edi
leaq 0x763db(%rip), %rax     # 0xc9fa8
movq 0x7d0(%rax), %rax
cmpq $0x0, 0x60050(%rax)
je 0x53bf9
movq 0x8aa43(%rip), %rcx     # 0xde628
testb $0x1, 0x30(%rcx)
je 0x53bf9
cmpq %r15, 0x60058(%rax)
setne %al
orb %al, %bl
je 0x53c50
movq 0x858(%r15), %rax
testq %rax, %rax
je 0x53a99
subq %rax, %r12
leaq 0x76395(%rip), %rax     # 0xc9fa8
cmpq 0x7c0(%rax), %r12
jle 0x53a99
testb $0xc, 0x30(%r15)
jne 0x53a99
leaq 0x8(%r15), %rdx
xorl %edi, %edi
leaq 0x48b6c(%rip), %rsi     # 0x9c7a4
xorl %eax, %eax
callq 0x23223
orb $0x4, 0x30(%r15)
movl $0x1, (%rsp)
jmp 0x53a99
xorl %esi, %esi
callq 0x52605
jmp 0x53a99
cmpl $0x0, 0x8(%rsp)
setne %r15b
cmpl $0x0, (%rsp)
sete %bl
jmp 0x53c78
movb $0x1, %bl
xorl %ebp, %ebp
xorl %r13d, %r13d
xorl %r15d, %r15d
movq %r14, %rdi
callq 0x22b86
movq 0x8a9a1(%rip), %rax     # 0xde628
testb $0x2, 0x30(%rax)
je 0x53cc2
leaq 0x76314(%rip), %rcx     # 0xc9fa8
cmpq $0x0, 0x650(%rcx)
jne 0x53cc2
movq 0x850(%rax), %rdi
testq %rdi, %rdi
je 0x53cc2
testb $0x40, 0x30(%rdi)
jne 0x53cc2
movl 0x8b8(%rdi), %esi
addq $0x888, %rdi            # imm = 0x888
callq 0x35767
callq 0x53719
movq 0x8a95a(%rip), %rax     # 0xde628
testb $0x2, 0x30(%rax)
je 0x53cfa
callq 0x53759
callq 0x5301d
cmpl $0x2, %r13d
setge %al
andb %al, %r15b
cmpb $0x1, %r15b
jne 0x53cfa
cmpl %r13d, %ebp
jne 0x53cfa
movl %ebp, %edi
callq 0x53568
testb %bl, %bl
je 0x53d12
leaq 0x762a3(%rip), %rax     # 0xc9fa8
movq 0x7d0(%rax), %rax
cmpl $0x1, 0x10(%rax)
jne 0x53d25
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x533cf
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq