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

Code before:
        server.cluster->importing_slots_from[slot] = NULL;
        server.cluster->migrating_slots_to[slot] = NULL;
    } else if (!strcasecmp(c->argv[3]->ptr, "node")) {
        /* CLUSTER SETSLOT <SLOT> NODE <NODE ID> */
        serverLog(LL_NOTICE, "Assigning slot %d to node %.40s (%s) in shard %.40s", slot, n->name, n->human_nodename,
                  n->shard_id);

        /* If this slot is in migrating status but we have no keys
         * for it assigning the slot to another node will clear
         * the migrating status. */
        if (countKeysInSlot(slot) == 0 && server.cluster->migrating_slots_to[slot]) {
            server.cluster->migrating_slots_to[slot] = NULL;
        }

        int slot_was_mine = server.cluster->slots[slot] == myself;
        clusterDelSlot(slot);
        clusterAddSlot(n, slot);

        /* If we are a primary left without slots, we should turn into a
         * replica of the new primary. */
        if (slot_was_mine && n != myself && myself->numslots == 0 && server.cluster_allow_replica_migration) {
            serverLog(LL_NOTICE,
                      "Lost my last slot during slot migration. Reconfiguring myself "
                      "as a replica of %.40s (%s) in shard %.40s",
                      n->name, n->human_nodename, n->shard_id);
            /* We are migrating to a different primary, a total different replication
             * history, so a full sync is required. */
            clusterSetPrimary(n, 1, 0);
            clusterDoBeforeSleep(CLUSTER_TODO_SAVE_CONFIG | CLUSTER_TODO_UPDATE_STATE | CLUSTER_TODO_FSYNC_CONFIG);
        }

        /* If this node or this node's primary was importing this slot,
         * assigning the slot to itself also clears the importing status. */
        if ((n == myself || n == myself->replicaof) && server.cluster->importing_slots_from[slot]) {
            server.cluster->importing_slots_from[slot] = NULL;

            /* Only primary broadcasts the updates */
            if (n == myself) {
                /* This slot was manually migrated, set this node configEpoch
                 * to a new epoch so that the new version can be propagated
                 * by the cluster.
                 *
                 * Note that if this ever results in a collision with another
                 * node getting the same configEpoch, for example because a
                 * failover happens at the same time we close the slot, the
                 * configEpoch collision resolution will fix it assigning
                 * a different epoch to each node. */
                if (clusterBumpConfigEpochWithoutConsensus() == C_OK) {
                    serverLog(LL_NOTICE, "ConfigEpoch updated after importing slot %d", slot);
                }
                /* After importing this slot, let the other nodes know as

Review comment (suggestion):
nit 
```suggestion
            /* We are migrating to a different shard that has a completely different replication
```

Code after:
        server.cluster->importing_slots_from[slot] = NULL;
        server.cluster->migrating_slots_to[slot] = NULL;
    } else if (!strcasecmp(c->argv[3]->ptr, "node")) {
        /* CLUSTER SETSLOT <SLOT> NODE <NODE ID> */
        serverLog(LL_NOTICE, "Assigning slot %d to node %.40s (%s) in shard %.40s", slot, n->name, n->human_nodename,
                  n->shard_id);

        /* If this slot is in migrating status but we have no keys
         * for it assigning the slot to another node will clear
         * the migrating status. */
        if (countKeysInSlot(slot) == 0 && server.cluster->migrating_slots_to[slot]) {
            server.cluster->migrating_slots_to[slot] = NULL;
        }

        int slot_was_mine = server.cluster->slots[slot] == myself;
        clusterDelSlot(slot);
        clusterAddSlot(n, slot);

        /* If we are a primary left without slots, we should turn into a
         * replica of the new primary. */
        if (slot_was_mine && n != myself && myself->numslots == 0 && server.cluster_allow_replica_migration) {
            serverLog(LL_NOTICE,
                      "Lost my last slot during slot migration. Reconfiguring myself "
                      "as a replica of %.40s (%s) in shard %.40s",
                      n->name, n->human_nodename, n->shard_id);
            /* We are migrating to a different shard that has a completely different
             * replication history, so a full sync is required. */
            clusterSetPrimary(n, 1, 1);
            clusterDoBeforeSleep(CLUSTER_TODO_SAVE_CONFIG | CLUSTER_TODO_UPDATE_STATE | CLUSTER_TODO_FSYNC_CONFIG);
        }

        /* If this node or this node's primary was importing this slot,
         * assigning the slot to itself also clears the importing status. */
        if ((n == myself || n == myself->replicaof) && server.cluster->importing_slots_from[slot]) {
            server.cluster->importing_slots_from[slot] = NULL;

            /* Only primary broadcasts the updates */
            if (n == myself) {
                /* This slot was manually migrated, set this node configEpoch
                 * to a new epoch so that the new version can be propagated
                 * by the cluster.
                 *
                 * Note that if this ever results in a collision with another
                 * node getting the same configEpoch, for example because a
                 * failover happens at the same time we close the slot, the
                 * configEpoch collision resolution will fix it assigning
                 * a different epoch to each node. */
                if (clusterBumpConfigEpochWithoutConsensus() == C_OK) {
                    serverLog(LL_NOTICE, "ConfigEpoch updated after importing slot %d", slot);
                }
                /* After importing this slot, let the other nodes know as