diff --git a/src/cluster.c b/src/cluster.c index 07708cc4..25c5950c 100644 --- a/src/cluster.c +++ b/src/cluster.c @@ -1498,6 +1498,24 @@ int verifyClusterConfigWithData(void) { return REDIS_OK; } +/* ----------------------------------------------------------------------------- + * SLAVE nodes handling + * -------------------------------------------------------------------------- */ + +/* Set the specified node 'n' as master. */ +void clusterSetMaster(clusterNode *n) { + clusterNode *myself = server.cluster->myself; + + redisAssert(n != myself); + + if (myself->flags & REDIS_NODE_MASTER) { + myself->flags &= ~REDIS_NODE_MASTER; + myself->flags |= REDIS_NODE_SLAVE; + } + myself->slaveof = n; + /* TODO: actually handle replication to point to the new slave. */ +} + /* ----------------------------------------------------------------------------- * CLUSTER command * -------------------------------------------------------------------------- */ @@ -1849,6 +1867,40 @@ void clusterCommand(redisClient *c) { clusterUpdateState(); clusterSaveConfigOrDie(); addReply(c,shared.ok); + } else if (!strcasecmp(c->argv[1]->ptr,"replicate") && c->argc == 3) { + /* CLUSTER REPLICATE */ + clusterNode *n = clusterLookupNode(c->argv[2]->ptr); + + /* Lookup the specified node in our table. */ + if (!n) { + addReplyErrorFormat(c,"Unknown node %s", (char*)c->argv[2]->ptr); + return; + } + + /* I can't replicate myself. */ + if (n == server.cluster->myself) { + addReplyError(c,"Can't replicate myself"); + return; + } + + /* Can't replicate a slave. */ + if (n->slaveof != NULL) { + addReplyError(c,"I can only replicate a master, not a slave."); + return; + } + + /* We should have no assigned slots to accept to replicate some + * other node. */ + if (server.cluster->myself->numslots != 0 || + dictSize(server.db[0].dict) != 0) + { + addReplyError(c,"To set a master the node must be empty and without assigned slots."); + return; + } + + /* Set the master. */ + clusterSetMaster(n); + addReply(c,shared.ok); } else { addReplyError(c,"Wrong CLUSTER subcommand or number of arguments"); }