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

Code before:
/* This function is called before the event handler returns to sleep for
 * events. It is useful to perform operations that must be done ASAP in
 * reaction to events fired but that are not safe to perform inside event
 * handlers, or to perform potentially expansive tasks that we need to do
 * a single time before replying to clients. */
void clusterBeforeSleep(void) {
    int flags = server.cluster->todo_before_sleep;

    /* Reset our flags (not strictly needed since every single function
     * called for flags set should be able to clear its flag). */
    server.cluster->todo_before_sleep = 0;

    if (flags & CLUSTER_TODO_HANDLE_MANUALFAILOVER) {
        /* Handle manual failover as soon as possible so that won't have a 100ms
         * as it was handled only in clusterCron */
        if (nodeIsReplica(myself)) {
            clusterHandleManualFailover();
            if (!(server.cluster_module_flags & CLUSTER_MODULE_FLAG_NO_FAILOVER)) clusterHandleReplicaFailover();
        }
    } else if (flags & CLUSTER_TODO_HANDLE_FAILOVER) {
        /* Handle failover as soon as possible so that won't have a 100ms
         * as it was handled only in clusterCron. This is needed when it
         * is likely that we can start the election or there is already
         * the quorum from primaries in order to react fast. */
        if (nodeIsReplica(myself)) {
            if (!(server.cluster_module_flags & CLUSTER_MODULE_FLAG_NO_FAILOVER)) clusterHandleReplicaFailover();
        }
    }

    /* Update the cluster state. */
    if (flags & CLUSTER_TODO_UPDATE_STATE) clusterUpdateState();

    /* Save the config, possibly using fsync. */
    if (flags & CLUSTER_TODO_SAVE_CONFIG) {
        int fsync = flags & CLUSTER_TODO_FSYNC_CONFIG;
        clusterSaveConfigOrDie(fsync);
    }

    if (flags & CLUSTER_TODO_BROADCAST_ALL) {
        /* Broadcast a pong to all known nodes. This is useful when something changes
         * in the configuration and we want to make the cluster aware it before the
         * regular ping. */
        clusterBroadcastPong(CLUSTER_BROADCAST_ALL);
    }
}

void clusterDoBeforeSleep(int flags) {
    /* Clear the cache if there are config changes here. */
    if (flags & CLUSTER_TODO_SAVE_CONFIG) clearCachedClusterSlotsResponse();

    server.cluster->todo_before_sleep |= flags;

Review comment (bug):
Why nested if? One if is enough.

```suggestion
        if (nodeIsReplica(myself) &&
            !(server.cluster_module_flags & CLUSTER_MODULE_FLAG_NO_FAILOVER)) {
            clusterHandleReplicaFailover();
```

Code after:
/* This function is called before the event handler returns to sleep for
 * events. It is useful to perform operations that must be done ASAP in
 * reaction to events fired but that are not safe to perform inside event
 * handlers, or to perform potentially expansive tasks that we need to do
 * a single time before replying to clients. */
void clusterBeforeSleep(void) {
    int flags = server.cluster->todo_before_sleep;

    /* Reset our flags (not strictly needed since every single function
     * called for flags set should be able to clear its flag). */
    server.cluster->todo_before_sleep = 0;

    if (flags & CLUSTER_TODO_HANDLE_MANUALFAILOVER) {
        /* Handle manual failover as soon as possible so that won't have a 100ms
         * as it was handled only in clusterCron */
        if (nodeIsReplica(myself)) {
            clusterHandleManualFailover();
            if (!(server.cluster_module_flags & CLUSTER_MODULE_FLAG_NO_FAILOVER)) clusterHandleReplicaFailover();
        }
    } else if (flags & CLUSTER_TODO_HANDLE_FAILOVER) {
        /* Handle failover as soon as possible so that won't have a 100ms
         * as it was handled only in clusterCron. This is needed when it
         * is likely that we can start the election or there is already
         * the quorum from primaries in order to react fast. */
        if (nodeIsReplica(myself) &&
            !(server.cluster_module_flags & CLUSTER_MODULE_FLAG_NO_FAILOVER)) {
            clusterHandleReplicaFailover();
        }
    }

    /* Update the cluster state. */
    if (flags & CLUSTER_TODO_UPDATE_STATE) clusterUpdateState();

    /* Save the config, possibly using fsync. */
    if (flags & CLUSTER_TODO_SAVE_CONFIG) {
        int fsync = flags & CLUSTER_TODO_FSYNC_CONFIG;
        clusterSaveConfigOrDie(fsync);
    }

    if (flags & CLUSTER_TODO_BROADCAST_ALL) {
        /* Broadcast a pong to all known nodes. This is useful when something changes
         * in the configuration and we want to make the cluster aware it before the
         * regular ping. */
        clusterBroadcastPong(CLUSTER_BROADCAST_ALL);
    }
}

void clusterDoBeforeSleep(int flags) {
    /* Clear the cache if there are config changes here. */
    if (flags & CLUSTER_TODO_SAVE_CONFIG) clearCachedClusterSlotsResponse();