// File: /workspace/github_c_makefile_star2_O2/gplessis[P]dotdeb-redis/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_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;
            }
            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. */
    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;

        if (node->flags &
            (CLUSTER_NODE_MYSELF|CLUSTER_NODE_NOADDR|CLUSTER_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);

            /* A master is orphaned if it is serving a non-zero number of
             * slots, have no working slaves, but used to have at least one
             * slave, or failed over a master that used to have slaves. */
            if (okslaves == 0 && node->numslots > 0 &&
                node->flags & CLUSTER_NODE_MIGRATE_TO)
            {
                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. */
        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;
        }

        /* If we are a master and one of the slaves requested a manual
         * failover, ping it continuously. */
        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. */
        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 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 == CLUSTER_FAIL)
        clusterUpdateState();
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
callq 0x2b8ae
movq %rax, %r15
incq 0xaf3fb(%rip)           # 0x10e240
leaq 0x9aa5c(%rip), %r12     # 0xf98a8
movq 0x9a0(%r12), %rax
movq 0x9b0(%r12), %rcx
cmpq $0x3e9, %rax            # imm = 0x3E9
movl $0x3e8, %ebx            # imm = 0x3E8
cmovgeq %rax, %rbx
movq 0x18(%rcx), %rdi
callq 0x2a9c4
movq %rax, %r14
leaq 0x208(%r12), %rax
movq %rax, (%rsp)
movl $0x208, %eax            # imm = 0x208
addq 0x96129(%rip), %rax     # 0xf4fb8
movq %rax, 0x8(%rsp)
movq %rbx, 0x10(%rsp)
movq %r14, %rdi
callq 0x2a9d3
testq %rax, %rax
je 0x5f006
movq 0x8(%rax), %r13
movl 0x30(%r13), %eax
testb $0x50, %al
jne 0x5ee99
testb $0x20, %al
je 0x5eed0
movq %r15, %rax
subq (%r13), %rax
cmpq %rbx, %rax
jle 0x5eed0
movq %r13, %rdi
callq 0x5bcf4
jmp 0x5ee99
cmpq $0x0, 0x8c8(%r13)
jne 0x5ee99
cmpl $0x0, 0x128(%r12)
je 0x5eeef
movq 0xa8(%r12), %rcx
jmp 0x5eef1
xorl %ecx, %ecx
leaq 0x890(%r13), %rbp
movl 0x8c0(%r13), %edx
movl $0x2710, %eax           # imm = 0x2710
addl %eax, %edx
movq (%rsp), %rdi
movq %rbp, %rsi
callq 0x29861
cmpl $-0x1, %eax
je 0x5efbe
movl %eax, %r12d
movq %r15, %rbx
movq %r13, %rdi
callq 0x5bdb1
movq %rax, %r15
movl %r12d, 0x8(%rax)
movq %rax, 0x8c8(%r13)
leaq 0x9a96a(%rip), %rax     # 0xf98a8
movq 0x40(%rax), %rdi
movl %r12d, %esi
pushq $0x1
popq %rdx
leaq -0x30f3(%rip), %rcx     # 0x5be5c
movq %r15, %r8
callq 0x28ae8
movq 0x858(%r13), %r12
movl 0x30(%r13), %esi
shrl $0x6, %esi
andl $0x2, %esi
movq %r15, %rdi
callq 0x5d948
testq %r12, %r12
je 0x5ef7c
movq %r12, 0x858(%r13)
andb $0x7f, 0x30(%r13)
movl 0x8c0(%r13), %r8d
movl $0x2710, %eax           # imm = 0x2710
addl %eax, %r8d
addq $0x8, %r13
xorl %edi, %edi
leaq 0x5a5a3(%rip), %rsi     # 0xb9540
movq %r13, %rdx
movq %rbp, %rcx
xorl %eax, %eax
callq 0x2b699
leaq 0x9a8f7(%rip), %r12     # 0xf98a8
movq %rbx, %r15
movq 0x10(%rsp), %rbx
jmp 0x5ee99
cmpq $0x0, 0x858(%r13)
jne 0x5efd4
callq 0x2b8ae
movq %rax, 0x858(%r13)
movl 0x8c0(%r13), %ecx
movl $0x2710, %eax           # imm = 0x2710
addl %eax, %ecx
xorl %edi, %edi
leaq 0x5a525(%rip), %rsi     # 0xb9510
movq %rbp, %rdx
movq 0x8(%rsp), %r8
xorl %eax, %eax
callq 0x2b699
leaq 0x9a8a7(%rip), %r12     # 0xf98a8
jmp 0x5ee99
movq %r14, %rdi
callq 0x2aa94
movq 0xaf22b(%rip), %rax     # 0x10e240
movq $0x0, (%rsp)
pushq $0xa
popq %rcx
xorl %edx, %edx
divq %rcx
testq %rdx, %rdx
jne 0x5f0b3
pushq $0x5
popq %rbx
xorl %r14d, %r14d
xorl %r15d, %r15d
subl $0x1, %ebx
jb 0x5f08c
movq 0x9b0(%r12), %rax
movq 0x18(%rax), %rdi
callq 0x2aaf6
movq 0x8(%rax), %rax
cmpq $0x0, 0x8c8(%rax)
je 0x5f037
cmpq $0x0, 0x858(%rax)
jne 0x5f037
testb $0x30, 0x30(%rax)
jne 0x5f037
movq 0x860(%rax), %rcx
testq %r14, %r14
je 0x5f084
cmpq %rcx, %r15
cmovgq %rax, %r14
cmovgeq %rcx, %r15
jmp 0x5f037
movq %rax, %r14
movq %rcx, %r15
jmp 0x5f037
testq %r14, %r14
je 0x5f0b3
leaq 0x8(%r14), %rdx
leaq 0x5a4c8(%rip), %rsi     # 0xb9564
xorl %edi, %edi
xorl %eax, %eax
callq 0x2b699
movq 0x8c8(%r14), %rdi
xorl %esi, %esi
callq 0x5d948
movq 0x9b0(%r12), %rax
movq 0x18(%rax), %rdi
callq 0x2a9c4
movq %rax, %r14
movb $0x1, %al
movl %eax, 0x10(%rsp)
xorl %r15d, %r15d
movl $0x0, 0x8(%rsp)
movq %r14, %rdi
callq 0x2a9d3
testq %rax, %rax
je 0x5f287
movq 0x8(%rax), %r12
callq 0x2b8ae
movl 0x30(%r12), %ebp
testb $0x70, %bpl
jne 0x5f0d8
movq %rax, %r13
movl %ebp, %eax
andl $0x9, %eax
cmpl $0x1, %eax
jne 0x5f15b
movq 0xaf11f(%rip), %rbx     # 0x10e230
movl 0x30(%rbx), %eax
andl $0x2, %eax
je 0x5f15b
movq %r12, %rdi
callq 0x5c1f4
testl %eax, %eax
jne 0x5f139
cmpl $0x0, 0x840(%r12)
jle 0x5f139
btl $0x8, %ebp
adcl $0x0, 0x8(%rsp)
leaq 0x9a768(%rip), %rbp     # 0xf98a8
cmpl %r15d, %eax
cmovgl %eax, %r15d
cmpq %r12, 0x850(%rbx)
movq (%rsp), %rcx
cmovel %eax, %ecx
movq %rcx, (%rsp)
jmp 0x5f162
leaq 0x9a746(%rip), %rbp     # 0xf98a8
movq 0x8c8(%r12), %rdi
movb $0x1, %bl
testq %rdi, %rdi
je 0x5f1f8
movq %r13, %rcx
subq (%rdi), %rcx
movq 0x9a0(%rbp), %rax
cmpq %rax, %rcx
jle 0x5f1c3
movq 0x858(%r12), %rcx
testq %rcx, %rcx
je 0x5f1c3
cmpq %rcx, 0x860(%r12)
jge 0x5f1c3
movq %r13, %rsi
subq %rcx, %rsi
cqto
pushq $0x2
popq %rcx
idivq %rcx
cmpq %rax, %rsi
jle 0x5f1c3
callq 0x5bdf1
movq 0x8c8(%r12), %rdi
testq %rdi, %rdi
je 0x5f1f8
cmpq $0x0, 0x858(%r12)
jne 0x5f1f4
movq 0x860(%r12), %rax
movq %r13, %rcx
subq %rax, %rcx
movq 0x9a0(%rbp), %rax
cqto
pushq $0x2
popq %rsi
idivq %rsi
cmpq %rax, %rcx
jg 0x5f27b
xorl %ebx, %ebx
jmp 0x5f1fa
xorl %edi, %edi
movq 0x9b0(%rbp), %rax
cmpq $0x0, 0x60058(%rax)
je 0x5f226
movq 0xaf01e(%rip), %rcx     # 0x10e230
testb $0x1, 0x30(%rcx)
je 0x5f226
cmpq %r12, 0x60060(%rax)
setne %al
orb %al, %bl
je 0x5f27b
movq 0x858(%r12), %rax
testq %rax, %rax
je 0x5f0d8
subq %rax, %r13
cmpq 0x9a0(%rbp), %r13
jle 0x5f0d8
testb $0xc, 0x30(%r12)
jne 0x5f0d8
leaq 0x8(%r12), %rdx
movl $0x0, 0x10(%rsp)
xorl %edi, %edi
leaq 0x5a30e(%rip), %rsi     # 0xb9577
xorl %eax, %eax
callq 0x2b699
orb $0x4, 0x30(%r12)
jmp 0x5f0d8
xorl %esi, %esi
callq 0x5d948
jmp 0x5f0d8
movq %r14, %rdi
callq 0x2aa94
movq 0xaef9a(%rip), %rax     # 0x10e230
testb $0x2, 0x30(%rax)
leaq 0x9a607(%rip), %rbx     # 0xf98a8
je 0x5f2d1
cmpq $0x0, 0x828(%rbx)
jne 0x5f2d1
movq 0x850(%rax), %rdi
testq %rdi, %rdi
je 0x5f2d1
testb $0x40, 0x30(%rdi)
jne 0x5f2d1
movl 0x8c0(%rdi), %esi
addq $0x890, %rdi            # imm = 0x890
callq 0x3f66f
callq 0x5ed7e
movq 0xaef53(%rip), %rax     # 0x10e230
testb $0x2, 0x30(%rax)
je 0x5f309
callq 0x5edbc
callq 0x5e860
cmpl $0x0, 0x8(%rsp)
je 0x5f309
cmpl $0x2, %r15d
jl 0x5f309
cmpl %r15d, (%rsp)
jne 0x5f309
movq (%rsp), %rdi
callq 0x5eba1
testb $0x1, 0x10(%rsp)
je 0x5f31d
movq 0x9b0(%rbx), %rax
cmpl $0x1, 0x10(%rax)
jne 0x5f330
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x5e6ba
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq