Repository: valkey-io/valkey
File: src/cluster_legacy.c (C)

Code before:
}

/* -----------------------------------------------------------------------------
 * CLUSTER cron job
 * -------------------------------------------------------------------------- */

/* Check if the node is disconnected and re-establish the connection.
 * Also update a few stats while we are here, that can be used to make
 * better decisions in other part of the code. */
static int clusterNodeCronHandleReconnect(clusterNode *node, mstime_t handshake_timeout, mstime_t now) {
    /* Not interested in reconnecting the link with myself or nodes
     * for which we have no address. */
    if (node->flags & (CLUSTER_NODE_MYSELF | CLUSTER_NODE_NOADDR)) return 1;

    if (node->flags & CLUSTER_NODE_PFAIL) server.cluster->stats_pfail_nodes++;

    /* A Node in HANDSHAKE state has a limited lifespan equal to the
     * configured node timeout. */
    if (nodeInHandshake(node) && now - node->ctime > handshake_timeout) {
        serverLog(LL_WARNING, "Clusterbus handshake timeout %s:%d after %lldms", node->ip,
                  node->cport, handshake_timeout);
        clusterDelNode(node);
        return 1;
    }
    if (node->link != NULL && node->inbound_link == NULL && nodeInNormalState(node) &&
        now - node->inbound_link_freed_time > handshake_timeout) {
        /* Node has an outbound link, but no inbound link for more than the handshake timeout.
         * This probably means this node does not know us yet, whereas we know it.
         * So we send it a MEET packet to do a handshake with it and correct the inconsistent cluster view. */
        node->flags |= CLUSTER_NODE_MEET;
        serverLog(LL_NOTICE, "Sending MEET packet to node %.40s because there is no inbound link for it", node->name);
        clusterSendPing(node->link, CLUSTERMSG_TYPE_MEET);
    }

    if (node->link == NULL) {
        clusterLink *link = createClusterLink(node);
        link->conn = connCreate(connTypeOfCluster());
        connSetPrivateData(link->conn, link);
        if (connConnect(link->conn, node->ip, node->cport, server.bind_source_addr, clusterLinkConnectHandler) ==
            C_ERR) {
            /* 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->cport, server.neterr);


Review comment (suggestion):
nit
```suggestion

    if (nodeInNormalState(node) && node->link != NULL && node->inbound_link == NULL && 
```

Code after:
    clusterDoBeforeSleep(CLUSTER_TODO_HANDLE_MANUALFAILOVER);
}

/* -----------------------------------------------------------------------------
 * CLUSTER cron job
 * -------------------------------------------------------------------------- */

/* Check if the node is disconnected and re-establish the connection.
 * Also update a few stats while we are here, that can be used to make
 * better decisions in other part of the code. */
static int clusterNodeCronHandleReconnect(clusterNode *node, mstime_t handshake_timeout, mstime_t now) {
    /* Not interested in reconnecting the link with myself or nodes
     * for which we have no address. */
    if (node->flags & (CLUSTER_NODE_MYSELF | CLUSTER_NODE_NOADDR)) return 1;

    if (node->flags & CLUSTER_NODE_PFAIL) server.cluster->stats_pfail_nodes++;

    /* A Node in HANDSHAKE state has a limited lifespan equal to the
     * configured node timeout. */
    if (nodeInHandshake(node) && now - node->ctime > handshake_timeout) {
        serverLog(LL_WARNING, "Clusterbus handshake timeout %s:%d after %lldms", node->ip,
                  node->cport, handshake_timeout);
        clusterDelNode(node);
        return 1;
    }
    if (node->link != NULL && node->inbound_link == NULL && nodeInNormalState(node) &&
        now - node->inbound_link_freed_time > handshake_timeout) {
        /* Node has an outbound link, but no inbound link for more than the handshake timeout.
         * This probably means this node does not know us yet, whereas we know it.
         * So we send it a MEET packet to do a handshake with it and correct the inconsistent cluster view. */
        node->flags |= CLUSTER_NODE_MEET;
        serverLog(LL_NOTICE, "Sending MEET packet to node %.40s because there is no inbound link for it", node->name);
        clusterSendPing(node->link, CLUSTERMSG_TYPE_MEET);
    }

    if (node->link == NULL) {
        clusterLink *link = createClusterLink(node);
        link->conn = connCreate(connTypeOfCluster());
        connSetPrivateData(link->conn, link);
        if (connConnect(link->conn, node->ip, node->cport, server.bind_source_addr, clusterLinkConnectHandler) ==
            C_ERR) {
            /* 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->cport, server.neterr);