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

Code before:
            clusterPromoteSelfToPrimary();
            int empty_db_flags = server.repl_replica_lazy_flush ? EMPTYDB_ASYNC : EMPTYDB_NO_FLAGS;
            emptyData(-1, empty_db_flags, NULL);
            /* Reset manual failover state. */
            resetManualFailover();
            clusterDoBeforeSleep(CLUSTER_TODO_UPDATE_STATE | CLUSTER_TODO_SAVE_CONFIG | CLUSTER_TODO_BROADCAST_ALL);
            addReply(c, shared.ok);
            return 1;
        }
        clusterNode *n = clusterLookupNode(c->argv[2]->ptr, sdslen(c->argv[2]->ptr));
        if (!n) {
            addReplyErrorFormat(c, "Unknown node %s", (char *)c->argv[2]->ptr);
            return 1;
        }

        /* I can't replicate myself. */
        if (n == myself) {
            addReplyError(c, "Can't replicate myself");
            return 1;
        }

        /* Can't replicate a replica. */
        if (nodeIsReplica(n)) {
            addReplyError(c, "I can only replicate a master, not a replica.");
            return 1;
        }

        /* If the instance is currently a primary, it should have no assigned
         * slots nor keys to accept to replicate some other node.
         * Replicas can switch to another primary without issues. */
        if (clusterNodeIsPrimary(myself) && (myself->numslots != 0 || kvstoreSize(server.db[0].keys) != 0)) {
            addReplyError(c, "To set a master the node must be empty and "
                             "without assigned slots.");
            return 1;
        }

        /* If `n` is already my primary, there is no need to re-establish the
         * replication connection. */
        if (myself->replicaof == n) {
            addReply(c, shared.ok);
            return 1;
        }

        /* Set the primary.
         * If the instance is a primary, it is an empty primary.
         * If the instance is a replica, it had a totally different replication history.
         * In these both cases, myself as a replica has to do a full sync. */
        clusterSetPrimary(n, 1, 1);
        clusterDoBeforeSleep(CLUSTER_TODO_UPDATE_STATE | CLUSTER_TODO_SAVE_CONFIG | CLUSTER_TODO_BROADCAST_ALL);
        addReply(c, shared.ok);
    } else if (!strcasecmp(c->argv[1]->ptr, "count-failure-reports") && c->argc == 3) {

Review comment (bug):
it become a primary, do we need this reset? it has no way to enter the failover check logic. or at least we remove the comment since this line is super easy.
```suggestion
            resetManualFailover();
```

Code after:
        serverLog(LL_NOTICE, "Cluster forget %s (user request from '%s').", (char *)c->argv[2]->ptr, client);
        sdsfree(client);
        clusterBlacklistAddNode(n);
        clusterDelNode(n);
        clusterDoBeforeSleep(CLUSTER_TODO_UPDATE_STATE | CLUSTER_TODO_SAVE_CONFIG);
        addReply(c, shared.ok);
    } else if (!strcasecmp(c->argv[1]->ptr, "replicate") && (c->argc == 3 || c->argc == 4)) {
        /* CLUSTER REPLICATE (<NODE ID> | NO ONE)*/
        if (c->argc == 4) {
            /* CLUSTER REPLICATE NO ONE */
            if (strcasecmp(c->argv[2]->ptr, "NO") != 0 || strcasecmp(c->argv[3]->ptr, "ONE") != 0) {
                addReplySubcommandSyntaxError(c);
                return 1;
            }
            if (nodeIsPrimary(myself)) {
                addReply(c, shared.ok);
                return 1;
            }
            sds client = catClientInfoShortString(sdsempty(), c, server.hide_user_data_from_log);
            serverLog(LL_NOTICE, "Stop replication and turning myself into empty primary (request from '%s').", client);
            sdsfree(client);
            clusterSetNodeAsPrimary(myself);
            clusterPromoteSelfToPrimary();
            flushAllDataAndResetRDB(server.repl_replica_lazy_flush ? EMPTYDB_ASYNC : EMPTYDB_NO_FLAGS);
            clusterCloseAllSlots();
            resetManualFailover();

            /* Moving new primary to its own shard. */
            char new_shard_id[CLUSTER_NAMELEN];
            getRandomHexChars(new_shard_id, CLUSTER_NAMELEN);
            updateShardId(myself, new_shard_id);

            clusterDoBeforeSleep(CLUSTER_TODO_UPDATE_STATE | CLUSTER_TODO_SAVE_CONFIG | CLUSTER_TODO_BROADCAST_ALL);
            addReply(c, shared.ok);
            return 1;
        }
        /* CLUSTER REPLICATE <NODE ID> */
        /* Lookup the specified node in our table. */
        clusterNode *n = clusterLookupNode(c->argv[2]->ptr, sdslen(c->argv[2]->ptr));
        if (!n) {
            addReplyErrorFormat(c, "Unknown node %s", (char *)c->argv[2]->ptr);
            return 1;
        }

        /* I can't replicate myself. */
        if (n == myself) {
            addReplyError(c, "Can't replicate myself");
            return 1;
        }

        /* Can't replicate a replica. */