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

Code before:
long long clusterNodeReplOffset(clusterNode *node) {
    return node->repl_offset;
}

const char *clusterNodePreferredEndpoint(clusterNode *n) {
    char *hostname = clusterNodeHostname(n);
    switch (server.cluster_preferred_endpoint_type) {
        case CLUSTER_ENDPOINT_TYPE_IP:
            return clusterNodeIp(n);
        case CLUSTER_ENDPOINT_TYPE_HOSTNAME:
            return (hostname != NULL && hostname[0] != '\0') ? hostname : "?";
        case CLUSTER_ENDPOINT_TYPE_UNKNOWN_ENDPOINT:
            return "";
    }
    return "unknown";
}

int clusterAllowFailoverCmd(client *c) {
    if (!server.cluster_enabled) {
        return 1;
    }
    addReplyError(c,"FAILOVER not allowed in cluster mode. "
                    "Use CLUSTER FAILOVER command instead.");
    return 0;
}

void clusterPromoteSelfToMaster(void) {
    replicationUnsetMaster();
}

int detectAndUpdateCachedNodeHealth(void) {
    dictIterator di;
    dictInitSafeIterator(&di, server.cluster->nodes);
    dictEntry *de;
    clusterNode *node;
    int overall_health_changed = 0;
    while((de = dictNext(&di)) != NULL) {
        node = dictGetVal(de);
        int present_is_node_healthy = isNodeAvailable(node);
        if (present_is_node_healthy != node->is_node_healthy) {
            overall_health_changed = 1;
            node->is_node_healthy = present_is_node_healthy;
        }
    }

    return overall_health_changed;
}

/* Replicate migrating and importing slot states to all replicas */
void clusterReplicateOpenSlots(void) {
    if (!server.cluster_enabled) return;

Review comment (suggestion):
I would suggest returning the total number of nodes whose health state changed.

```suggestion
            overall_health_changed += 1;
```

Code after:
}

int clusterAllowFailoverCmd(client *c) {
    if (!server.cluster_enabled) {
        return 1;
    }
    addReplyError(c,"FAILOVER not allowed in cluster mode. "
                    "Use CLUSTER FAILOVER command instead.");
    return 0;
}

void clusterPromoteSelfToMaster(void) {
    replicationUnsetMaster();
}

int detectAndUpdateCachedNodeHealth(void) {
    dictIterator di;
    dictInitSafeIterator(&di, server.cluster->nodes);
    dictEntry *de;
    clusterNode *node;
    int overall_health_changed = 0;
    while((de = dictNext(&di)) != NULL) {
        node = dictGetVal(de);
        int present_is_node_healthy = isNodeAvailable(node);
        if (present_is_node_healthy != node->is_node_healthy) {
            overall_health_changed = 1;
            node->is_node_healthy = present_is_node_healthy;
        }
    }

    return overall_health_changed;
}

/* Replicate migrating and importing slot states to all replicas */
void clusterReplicateOpenSlots(void) {
    if (!server.cluster_enabled) return;

    int argc = 5;
    robj **argv = zmalloc(sizeof(robj*)*argc);

    argv[0] = shared.cluster;
    argv[1] = shared.setslot;

    for (int i = 0; i < 2; i++) {
        clusterNode **nodes_ptr = NULL;
        if (i == 0) {
            nodes_ptr = server.cluster->importing_slots_from;
            argv[3] = shared.importing;
        } else {
            nodes_ptr = server.cluster->migrating_slots_to;
            argv[3] = shared.migrating;