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

Code before:
            return 1;
        }
        sds client = catClientInfoShortString(sdsempty(), c, server.hide_user_data_from_log);
        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)*/
        /* Lookup the specified node in our table. */
        if (c->argc == 4) {
            if (0 != strcasecmp(c->argv[2]->ptr, "NO") || 0 != strcasecmp(c->argv[3]->ptr, "ONE")) {
                addReplySubcommandSyntaxError(c);
                return 1;
            }
            if (nodeIsPrimary(myself)) {
                addReply(c, shared.ok);
                return 1;
            }
            serverLog(LL_NOTICE, "Stop replication and turning myself into empty primary.");
            clusterSetNodeAsPrimary(myself);
            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

Review comment (suggestion):
```suggestion
            emptyData(-1, server.repl_replica_lazy_flush ? EMPTYDB_ASYNC : EMPTYDB_NO_FLAGS, NULL);
```

Code after:
        if (retval == C_OK)
            addReply(c, shared.ok);
        else
            addReplyErrorFormat(c, "error saving the cluster node config: %s", strerror(errno));
    } else if (!strcasecmp(c->argv[1]->ptr, "forget") && c->argc == 3) {
        /* CLUSTER FORGET <NODE ID> */
        clusterNode *n = clusterLookupNode(c->argv[2]->ptr, sdslen(c->argv[2]->ptr));
        if (!n) {
            if (clusterBlacklistExists((char *)c->argv[2]->ptr))
                /* Already forgotten. The deletion may have been gossipped by
                 * another node, so we pretend it succeeded. */
                addReply(c, shared.ok);
            else
                addReplyErrorFormat(c, "Unknown node %s", (char *)c->argv[2]->ptr);
            return 1;
        } else if (n == myself) {
            addReplyError(c, "I tried hard but I can't forget myself...");
            return 1;
        } else if (nodeIsReplica(myself) && myself->replicaof == n) {
            addReplyError(c, "Can't forget my master!");
            return 1;
        }
        sds client = catClientInfoShortString(sdsempty(), c, server.hide_user_data_from_log);
        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. */