// File: /workspace/github_c_makefile_star2_O2/erikdubbelboer[P]discnt/src/cluster.c
// Function: clusterCron
void clusterCron(void) {
    dictIterator *di;
    dictEntry *de;
    int update_state = 0;
    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_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 & (CLUSTER_NODE_MYSELF|CLUSTER_NODE_NOADDR)) continue;

        /* A Node in HANDSHAKE state has a limited lifespan equal to the
         * configured node timeout. */
        if (nodeInHandshake(node) && now - node->ctime > handshake_timeout) {
            clusterDelNode(node);
            continue;
        }

        if (node->link == NULL) {
            int fd;
            mstime_t old_ping_sent;
            clusterLink *link;

            fd = anetTcpNonBlockBindConnect(server.neterr, node->ip,
                node->port+CLUSTER_PORT_INCR, NET_FIRST_BIND_ADDR);
            if (fd == -1) {
                /* We got a synchronous error from connect before
                 * clusterSendPing() had a chance to be called.
                 * If node->ping_sent is zero, failure detection can't work,
                 * so we claim we actually sent a ping now (that will
                 * be really sent as soon as the link is obtained). */
                if (node->ping_sent == 0) node->ping_sent = mstime();
                serverLog(LL_DEBUG, "Unable to connect to "
                    "Cluster Node [%s]:%d -> %s", node->ip,
                    node->port+CLUSTER_PORT_INCR,
                    server.neterr);
                continue;
            }
            anetEnableTcpNoDelay(NULL,fd);
            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. */
            old_ping_sent = node->ping_sent;
            clusterSendPing(link, node->flags & CLUSTER_NODE_MEET ?
                    CLUSTERMSG_TYPE_MEET : CLUSTERMSG_TYPE_PING);
            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.
             * 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. */
            node->flags &= ~CLUSTER_NODE_MEET;

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

    /* Ping some random node 1 time every 10 iterations, so that we usually ping
     * one random node every second. */
    if (!(iteration % 10)) {
        int j;

        /* Check a few random nodes and ping the one with the oldest
         * pong_received time. */
        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. */
            if (this->link == NULL || this->ping_sent != 0) continue;
            if (this->flags & (CLUSTER_NODE_MYSELF|CLUSTER_NODE_HANDSHAKE))
                continue;
            if (min_pong_node == NULL || min_pong > this->pong_received) {
                min_pong_node = this;
                min_pong = this->pong_received;
            }
        }
        if (min_pong_node) {
            /*serverLog(LL_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. */
    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;

        if (node->flags &
            (CLUSTER_NODE_MYSELF|CLUSTER_NODE_NOADDR|CLUSTER_NODE_HANDSHAKE))
                continue;

        /* 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. */
        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. */
            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. */
        if (node->link &&
            node->ping_sent == 0 &&
            (now - node->pong_received) > server.cluster_node_timeout/2)
        {
            clusterSendPing(node->link, CLUSTERMSG_TYPE_PING);
            continue;
        }

        /* Check only if we have an active ping for this instance. */
        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. */
        delay = now - node->ping_sent;

        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 & (CLUSTER_NODE_PFAIL|CLUSTER_NODE_FAIL))) {
                serverLog(LL_DEBUG,"*** NODE %.40s possibly failing",
                    node->name);
                node->flags |= CLUSTER_NODE_PFAIL;
                update_state = 1;
            }
        }
    }
    dictReleaseIterator(di);

    if (update_state || server.cluster->state == CLUSTER_FAIL)
        clusterUpdateState();

    clusterUpdateReachableNodes();
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
callq 0x15db3
movq %rax, %r12
incq 0x57543(%rip)           # 0x7e5a8
leaq 0x4301c(%rip), %r15     # 0x6a088
movq 0x658(%r15), %rax
movq 0x668(%r15), %rcx
cmpq $0x3e9, %rax            # imm = 0x3E9
movl $0x3e8, %ebx            # imm = 0x3E8
cmovgeq %rax, %rbx
movq 0x10(%rcx), %rdi
callq 0x14f4c
movq %rax, %r14
leaq 0x218(%r15), %rax
movq %rax, 0x10(%rsp)
movl $0x218, %eax            # imm = 0x218
addq 0x41f0b(%rip), %rax     # 0x68fb8
movq %rax, 0x8(%rsp)
movq %r12, (%rsp)
movq %r14, %rdi
callq 0x14f5b
testq %rax, %rax
je 0x2720a
movq 0x8(%rax), %r13
movl 0x30(%r13), %eax
testb $0x14, %al
jne 0x270b6
testb $0x8, %al
je 0x270ed
movq %r12, %rax
subq 0x28(%r13), %rax
cmpq %rbx, %rax
jle 0x270ed
movq %r13, %rdi
callq 0x25a7d
jmp 0x270b6
cmpq $0x0, 0x88(%r13)
jne 0x270b6
cmpl $0x0, 0x140(%r15)
je 0x2710a
movq 0xc0(%r15), %rcx
jmp 0x2710c
xorl %ecx, %ecx
leaq 0x50(%r13), %rbp
movl 0x80(%r13), %edx
movl $0x2710, %eax           # imm = 0x2710
addl %eax, %edx
movq 0x10(%rsp), %rdi
movq %rbp, %rsi
callq 0x13d9c
cmpl $-0x1, %eax
je 0x271cb
movl %eax, %r12d
xorl %edi, %edi
movl %eax, %esi
callq 0x1392b
movq %r13, %rdi
callq 0x25ae7
movq %r15, %rcx
movq %rax, %r15
movl %r12d, 0x8(%rax)
movq %rax, 0x88(%r13)
movq 0x38(%rcx), %rdi
movl %r12d, %esi
pushq $0x1
popq %rdx
leaq -0x15d8(%rip), %rcx     # 0x25b92
movq %rax, %r8
callq 0x1306c
movq 0x38(%r13), %r12
movl 0x30(%r13), %esi
shrl $0x4, %esi
andl $0x2, %esi
movq %r15, %rdi
callq 0x26a49
testq %r12, %r12
je 0x27191
movq %r12, 0x38(%r13)
andb $-0x21, 0x30(%r13)
movl 0x80(%r13), %r8d
movl $0x2710, %eax           # imm = 0x2710
addl %eax, %r8d
xorl %edi, %edi
leaq 0x29620(%rip), %rsi     # 0x507ce
movq %r13, %rdx
movq %rbp, %rcx
xorl %eax, %eax
callq 0x15b9e
leaq 0x42ec6(%rip), %r15     # 0x6a088
movq (%rsp), %r12
jmp 0x270b6
cmpq $0x0, 0x38(%r13)
movq (%rsp), %r12
jne 0x271df
callq 0x15db3
movq %rax, 0x38(%r13)
movl 0x80(%r13), %ecx
movl $0x2710, %eax           # imm = 0x2710
addl %eax, %ecx
xorl %edi, %edi
leaq 0x295a8(%rip), %rsi     # 0x5079e
movq %rbp, %rdx
movq 0x8(%rsp), %r8
xorl %eax, %eax
callq 0x15b9e
jmp 0x270b6
movq %r14, %rdi
callq 0x1501c
movq 0x5738f(%rip), %rax     # 0x7e5a8
pushq $0xa
popq %rcx
xorl %edx, %edx
divq %rcx
testq %rdx, %rdx
jne 0x2728f
pushq $0x5
popq %rbp
xorl %ebx, %ebx
xorl %r14d, %r14d
subl $0x1, %ebp
jb 0x2727c
movq 0x668(%r15), %rax
movq 0x10(%rax), %rdi
callq 0x1507f
movq 0x8(%rax), %rax
cmpq $0x0, 0x88(%rax)
je 0x2722e
cmpq $0x0, 0x38(%rax)
jne 0x2722e
testb $0xc, 0x30(%rax)
jne 0x2722e
movq 0x40(%rax), %rcx
testq %rbx, %rbx
je 0x27274
cmpq %rcx, %r14
cmovgq %rax, %rbx
cmovgeq %rcx, %r14
jmp 0x2722e
movq %rax, %rbx
movq %rcx, %r14
jmp 0x2722e
testq %rbx, %rbx
je 0x2728f
movq 0x88(%rbx), %rdi
xorl %esi, %esi
callq 0x26a49
movq 0x668(%r15), %rax
movq 0x10(%rax), %rdi
callq 0x14f4c
movq %rax, %rbx
movb $0x1, %bpl
leaq 0x29546(%rip), %r14     # 0x507f2
pushq $0x2
popq %r13
movq %rbx, %rdi
callq 0x14f5b
testq %rax, %rax
je 0x273a8
movq 0x8(%rax), %r15
callq 0x15db3
testb $0x1c, 0x30(%r15)
jne 0x272b0
movq %rax, %r12
movq 0x88(%r15), %rdi
testq %rdi, %rdi
je 0x2735e
movq %r12, %rcx
subq (%rdi), %rcx
leaq 0x42d9b(%rip), %rax     # 0x6a088
movq 0x658(%rax), %rax
cmpq %rax, %rcx
jle 0x27329
movq 0x38(%r15), %rcx
testq %rcx, %rcx
je 0x27329
cmpq %rcx, 0x40(%r15)
jge 0x27329
movq %r12, %rsi
subq %rcx, %rsi
cqto
idivq %r13
cmpq %rax, %rsi
jle 0x27329
callq 0x25b27
movq 0x88(%r15), %rdi
testq %rdi, %rdi
je 0x2735e
cmpq $0x0, 0x38(%r15)
jne 0x2735e
movq 0x40(%r15), %rax
movq %r12, %rcx
subq %rax, %rcx
leaq 0x42d47(%rip), %rax     # 0x6a088
movq 0x658(%rax), %rax
cqto
idivq %r13
cmpq %rax, %rcx
jle 0x2735e
xorl %esi, %esi
callq 0x26a49
jmp 0x272b0
movq 0x38(%r15), %rax
testq %rax, %rax
je 0x272b0
subq %rax, %r12
leaq 0x42d13(%rip), %rax     # 0x6a088
cmpq 0x658(%rax), %r12
jle 0x272b0
testb $0x3, 0x30(%r15)
jne 0x272b0
xorl %ebp, %ebp
xorl %edi, %edi
movq %r14, %rsi
movq %r15, %rdx
xorl %eax, %eax
callq 0x15b9e
orb $0x1, 0x30(%r15)
jmp 0x272b0
movq %rbx, %rdi
callq 0x1501c
testb $0x1, %bpl
je 0x273ca
leaq 0x42ccb(%rip), %rax     # 0x6a088
movq 0x668(%rax), %rax
cmpl $0x1, 0x8(%rax)
jne 0x273cf
callq 0x258db
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x25933