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

Code before:
        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 we are a replica and executing the CLUSTER SETSLOT command from
         * my primary, and my primary left without slots, we should turn into a
         * replica of the new primary. */
        if (nodeIsReplica(myself) && slot_was_my_primary && my_primary->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 a replica and the client is actually my primary, the following
             * clusterSetPrimary will free the client so we need to protect it. */
            protectClient(c);
            /* We are migrating to a different shard that has a completely different
             * replication history, so a full sync is required. */
            clusterSetPrimary(n, 1, 1);
            unprotectClient(c);
            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
                 * soon as possible. */

Review comment (refactor):
Maybe simplify this a bit? I think we can skip `unprotectClient`

```c
          clusterNode *my_primary = clusterNodeGetPrimary(myself);
          int slot_was_mine = server.cluster->slots[slot] == my_primary;
          clusterDelSlot(slot);
          clusterAddSlot(n, slot);

          /* If replica migration is allowed, check if the primary of this shard loses its last slot and the shard becomes empty. */
          if (server.cluster_allow_replica_migration && slot_was_mine && my_primary->numslots == 0) {
              serverAssert(n != my_primary);
              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);
              /* `c` is the primary client if `myself` is a replica, prevent it
               * from being freed by clusterSetPrimary. */
              if (nodeIsReplica(myself)) protectClient(c);
              /* 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);
          }
```

Code after:
        }

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

        /* If replica migration is allowed, check if the primary of this shard
         * loses its last slot and the shard becomes empty. In this case, we
         * should turn into a replica of the new primary. */
        if (server.cluster_allow_replica_migration && slot_was_mine && my_primary->numslots == 0) {
            serverAssert(n != my_primary);
            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);
            /* `c` is the primary client if `myself` is a replica, prevent it
             * from being freed by clusterSetPrimary. */
            if (nodeIsReplica(myself)) protectClient(c);
            /* We are migrating to a different shard that has a completely different
             * replication history, so a full sync is required. */
            clusterSetPrimary(n, 1, 1);
            if (nodeIsReplica(myself)) unprotectClient(c);
            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
                 * soon as possible. */
                clusterBroadcastPong(CLUSTER_BROADCAST_ALL);
            }
        }
    }