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

Code before:
            }
            delKeysInSlot(j);
        }
    }
    if (update_config) clusterSaveConfigOrDie(1);
    return C_OK;
}

/* Remove all the shard channel related information not owned by the current shard. */
static inline void removeAllNotOwnedShardChannelSubscriptions(void) {
    if (!kvstoreSize(server.pubsubshard_channels)) return;
    clusterNode *cur_primary = clusterNodeIsPrimary(myself) ? myself : myself->replicaof;
    for (int j = 0; j < CLUSTER_SLOTS; j++) {
        if (server.cluster->slots[j] != cur_primary) {
            removeChannelsInSlot(j);
        }
    }
}

/* -----------------------------------------------------------------------------
 * REPLICA nodes handling
 * -------------------------------------------------------------------------- */

/* Set the specified node 'n' as primary for this node.
 * If this node is currently a primary, it is turned into a replica. */
void clusterSetPrimary(clusterNode *n, int closeSlots, int try_psync) {
    serverAssert(n != myself);
    serverAssert(myself->numslots == 0);

    if (clusterNodeIsPrimary(myself)) {
        myself->flags &= ~(CLUSTER_NODE_PRIMARY | CLUSTER_NODE_MIGRATE_TO);
        myself->flags |= CLUSTER_NODE_REPLICA;
    } else {
        if (myself->replicaof) clusterNodeRemoveReplica(myself->replicaof, myself);
    }
    if (closeSlots) clusterCloseAllSlots();
    myself->replicaof = n;
    updateShardId(myself, n->shard_id);
    clusterNodeAddReplica(n, myself);
    replicationSetPrimary(n->ip, getNodeDefaultReplicationPort(n), try_psync);
    removeAllNotOwnedShardChannelSubscriptions();
    resetManualFailover();

    if (server.cluster->failover_auth_time) {
        /* Since we have changed to a new primary node, the previously set
         * failover_auth_time should no longer be used, whether it is in
         * progress or timed out. */
        server.cluster->failover_auth_time = 0;
    }
}


Review comment (suggestion):
Nit - consider `full_sync_required`?

```suggestion
void clusterSetPrimary(clusterNode *n, int closeSlots, int full_sync_required) {
```

Code after:
            }
            delKeysInSlot(j);
        }
    }
    if (update_config) clusterSaveConfigOrDie(1);
    return C_OK;
}

/* Remove all the shard channel related information not owned by the current shard. */
static inline void removeAllNotOwnedShardChannelSubscriptions(void) {
    if (!kvstoreSize(server.pubsubshard_channels)) return;
    clusterNode *cur_primary = clusterNodeIsPrimary(myself) ? myself : myself->replicaof;
    for (int j = 0; j < CLUSTER_SLOTS; j++) {
        if (server.cluster->slots[j] != cur_primary) {
            removeChannelsInSlot(j);
        }
    }
}

/* -----------------------------------------------------------------------------
 * REPLICA nodes handling
 * -------------------------------------------------------------------------- */

/* Set the specified node 'n' as primary for this node.
 * If this node is currently a primary, it is turned into a replica. */
static void clusterSetPrimary(clusterNode *n, int closeSlots, int full_sync_required) {
    serverAssert(n != myself);
    serverAssert(myself->numslots == 0);

    if (clusterNodeIsPrimary(myself)) {
        myself->flags &= ~(CLUSTER_NODE_PRIMARY | CLUSTER_NODE_MIGRATE_TO);
        myself->flags |= CLUSTER_NODE_REPLICA;
    } else {
        if (myself->replicaof) clusterNodeRemoveReplica(myself->replicaof, myself);
    }
    if (closeSlots) clusterCloseAllSlots();
    myself->replicaof = n;
    updateShardId(myself, n->shard_id);
    clusterNodeAddReplica(n, myself);
    replicationSetPrimary(n->ip, getNodeDefaultReplicationPort(n), full_sync_required);
    removeAllNotOwnedShardChannelSubscriptions();
    resetManualFailover();

    if (server.cluster->failover_auth_time) {
        /* Since we have changed to a new primary node, the previously set
         * failover_auth_time should no longer be used, whether it is in
         * progress or timed out. */
        server.cluster->failover_auth_time = 0;
    }
}