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

Code before:
        }

        /* Check if I'm the replica candidate for the migration: attached
         * to a primary with the maximum number of replicas and with the smallest
         * node ID. */
        if (ok_replicas == max_replicas) {
            for (j = 0; j < node->num_replicas; j++) {
                if (memcmp(node->replicas[j]->name, candidate->name, CLUSTER_NAMELEN) < 0) {
                    candidate = node->replicas[j];
                }
            }
        }
    }
    dictReleaseIterator(di);

    /* Step 4: perform the migration if there is a target, and if I'm the
     * candidate, but only if the primary is continuously orphaned for a
     * couple of seconds, so that during failovers, we give some time to
     * the natural replicas of this instance to advertise their switch from
     * the old primary to the new one. */
    if (target && candidate == myself && (mstime() - target->orphaned_time) > CLUSTER_REPLICA_MIGRATION_DELAY &&
        !(server.cluster_module_flags & CLUSTER_MODULE_FLAG_NO_FAILOVER)) {
        serverLog(LL_NOTICE, "Migrating to orphaned primary %.40s (%s) in shard %.40s", target->name,
                  target->human_nodename, target->shard_id);
        /* We are migrating to a different primary, a total different replication
         * history, so a full sync is required. */
        clusterSetPrimary(target, 1, 0);
    }
}

/* -----------------------------------------------------------------------------
 * CLUSTER manual failover
 *
 * This are the important steps performed by replicas during a manual failover:
 * 1) User send CLUSTER FAILOVER command. The failover state is initialized
 *    setting mf_end to the millisecond unix time at which we'll abort the
 *    attempt.
 * 2) Replica sends a MFSTART message to the primary requesting to pause clients
 *    for two times the manual failover timeout CLUSTER_MF_TIMEOUT.
 *    When primary is paused for manual failover, it also starts to flag
 *    packets with CLUSTERMSG_FLAG0_PAUSED.
 * 3) Replica waits for primary to send its replication offset flagged as PAUSED.
 * 4) If replica received the offset from the primary, and its offset matches,
 *    mf_can_start is set to 1, and clusterHandleReplicaFailover() will perform
 *    the failover as usually, with the difference that the vote request
 *    will be modified to force primaries to vote for a replica that has a
 *    working primary.
 *
 * From the point of view of the primary things are simpler: when a
 * PAUSE_CLIENTS packet is received the primary sets mf_end as well and
 * the sender in mf_replica. During the time limit for the manual failover

Review comment (suggestion):
```suggestion
        /* We are migrating to a different shard that has a completely different replication
         * history, so a full sync is required. */
```

Code after:
        }

        /* Check if I'm the replica candidate for the migration: attached
         * to a primary with the maximum number of replicas and with the smallest
         * node ID. */
        if (ok_replicas == max_replicas) {
            for (j = 0; j < node->num_replicas; j++) {
                if (memcmp(node->replicas[j]->name, candidate->name, CLUSTER_NAMELEN) < 0) {
                    candidate = node->replicas[j];
                }
            }
        }
    }
    dictReleaseIterator(di);

    /* Step 4: perform the migration if there is a target, and if I'm the
     * candidate, but only if the primary is continuously orphaned for a
     * couple of seconds, so that during failovers, we give some time to
     * the natural replicas of this instance to advertise their switch from
     * the old primary to the new one. */
    if (target && candidate == myself && (mstime() - target->orphaned_time) > CLUSTER_REPLICA_MIGRATION_DELAY &&
        !(server.cluster_module_flags & CLUSTER_MODULE_FLAG_NO_FAILOVER)) {
        serverLog(LL_NOTICE, "Migrating to orphaned primary %.40s (%s) in shard %.40s", target->name,
                  target->human_nodename, target->shard_id);
        /* We are migrating to a different shard that has a completely different
         * replication history, so a full sync is required. */
        clusterSetPrimary(target, 1, 1);
    }
}

/* -----------------------------------------------------------------------------
 * CLUSTER manual failover
 *
 * This are the important steps performed by replicas during a manual failover:
 * 1) User send CLUSTER FAILOVER command. The failover state is initialized
 *    setting mf_end to the millisecond unix time at which we'll abort the
 *    attempt.
 * 2) Replica sends a MFSTART message to the primary requesting to pause clients
 *    for two times the manual failover timeout CLUSTER_MF_TIMEOUT.
 *    When primary is paused for manual failover, it also starts to flag
 *    packets with CLUSTERMSG_FLAG0_PAUSED.
 * 3) Replica waits for primary to send its replication offset flagged as PAUSED.
 * 4) If replica received the offset from the primary, and its offset matches,
 *    mf_can_start is set to 1, and clusterHandleReplicaFailover() will perform
 *    the failover as usually, with the difference that the vote request
 *    will be modified to force primaries to vote for a replica that has a
 *    working primary.
 *
 * From the point of view of the primary things are simpler: when a
 * PAUSE_CLIENTS packet is received the primary sets mf_end as well and
 * the sender in mf_replica. During the time limit for the manual failover