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

Code before:
    return result;
}

void clusterSlotsCommand(client *c) {
    /* Format: 1) 1) start slot
     *            2) end slot
     *            3) 1) master IP
     *               2) master port
     *               3) node ID
     *            4) 1) replica IP
     *               2) replica port
     *               3) node ID
     *           ... continued until done
     */
    connTypeForCaching conn_type = connIsTLS(c->conn);

    /* Check if we have a response cached for cluster slots for early exit. */
    updateAllCachedNodesHealth();    
    if (isClusterSlotsResponseCached(conn_type)) {
        debugServerAssertWithInfo(c, NULL, verifyCachedClusterSlotsResponse(getClusterSlotReply(conn_type)) == 1);
        addReplyProto(c, getClusterSlotReply(conn_type), sdslen(getClusterSlotReply(conn_type)));
        return;
    }

    cacheSlotsResponse(generateClusterSlotResponse(), conn_type);
    addReplyProto(c, getClusterSlotReply(conn_type), sdslen(getClusterSlotReply(conn_type)));
}

/* -----------------------------------------------------------------------------
 * Cluster functions related to serving / redirecting clients
 * -------------------------------------------------------------------------- */

/* The ASKING command is required after a -ASK redirection.
 * The client should issue ASKING before to actually send the command to
 * the target instance. See the Cluster specification for more
 * information. */
void askingCommand(client *c) {
    if (server.cluster_enabled == 0) {
        addReplyError(c,"This instance has cluster support disabled");
        return;
    }
    c->flags |= CLIENT_ASKING;
    addReply(c,shared.ok);
}

/* The READONLY command is used by clients to enter the read-only mode.
 * In this mode slaves will not redirect clients as long as clients access
 * with read-only commands to keys that are served by the slave's master. */
void readonlyCommand(client *c) {
    if (server.cluster_enabled == 0) {
        addReplyError(c,"This instance has cluster support disabled");

Review comment (bug):
I think this could be simplified further:

```suggestion
    invalidateAllCachedNodesOnHealthChange(); 
    if (!isClusterSlotsResponseCached(conn_type)) {
        cacheSlotsResponse(generateClusterSlotResponse(), conn_type);
    }
    
    debugServerAssertWithInfo(c, NULL, verifyCachedClusterSlotsResponse(getClusterSlotReply(conn_type)) == 1);
    addReplyProto(c, getClusterSlotReply(conn_type), sdslen(getClusterSlotReply(conn_type)));
```

Code after:
    setDeferredArrayLen(recording_client, slot_replylen, num_masters);
    sds cluster_slot_response = aggregateClientOutputBuffer(recording_client);
    deleteCachedResponseClient(recording_client);
    return cluster_slot_response;
}

int verifyCachedClusterSlotsResponse(sds cached_response) {
    sds generated_response = generateClusterSlotResponse();
    int is_equal = !sdscmp(generated_response, cached_response);
    /* Here, we use LL_WARNING so this gets printed when debug assertions are enabled and the system is about to crash. */
    if (!is_equal) serverLog(LL_WARNING,"\ngenerated_response:\n%s\n\ncached_response:\n%s", generated_response, cached_response);
    sdsfree(generated_response);
    return is_equal;
}

void clusterCommandSlots(client *c) {
    /* Format: 1) 1) start slot
     *            2) end slot
     *            3) 1) master IP
     *               2) master port
     *               3) node ID
     *            4) 1) replica IP
     *               2) replica port
     *               3) node ID
     *           ... continued until done
     */
    connTypeForCaching conn_type = connIsTLS(c->conn);

    if (detectAndUpdateCachedNodeHealth()) clearCachedClusterSlotsResponse();

    sds cached_reply = server.cached_cluster_slot_info[conn_type];
    if (!cached_reply) {
        cached_reply = generateClusterSlotResponse();
        server.cached_cluster_slot_info[conn_type] = cached_reply;
    } else {
        debugServerAssertWithInfo(c, NULL, verifyCachedClusterSlotsResponse(cached_reply) == 1);
    }

    addReplyProto(c, cached_reply, sdslen(cached_reply));
}

/* -----------------------------------------------------------------------------
 * Cluster functions related to serving / redirecting clients
 * -------------------------------------------------------------------------- */

/* The ASKING command is required after a -ASK redirection.
 * The client should issue ASKING before to actually send the command to
 * the target instance. See the Cluster specification for more
 * information. */
void askingCommand(client *c) {
    if (server.cluster_enabled == 0) {