Cluster: node replication role change handle improved.

The code handling a master that turns into a slave or the contrary was
improved in order to avoid repeating the same operations. Also
the readability and conceptual simplicity was improved.
This commit is contained in:
antirez 2013-03-19 16:01:30 +01:00
parent 88221f88c0
commit 4d62623015

View File

@ -884,34 +884,33 @@ int clusterProcessPacket(clusterLink *link) {
sizeof(hdr->slaveof))) sizeof(hdr->slaveof)))
{ {
/* Node is a master. */ /* Node is a master. */
if (sender->flags & REDIS_NODE_SLAVE && if (sender->flags & REDIS_NODE_SLAVE) {
sender->slaveof != NULL) /* Slave turned into master? Reconfigure it. */
{ if (sender->slaveof)
/* If the node changed role and is now a master, remove clusterNodeRemoveSlave(sender->slaveof,sender);
* it from the list of slaves of its old master. */ sender->flags &= ~REDIS_NODE_SLAVE;
clusterNodeRemoveSlave(sender->slaveof,sender); sender->flags |= REDIS_NODE_MASTER;
sender->slaveof = NULL;
update_state = 1; update_state = 1;
update_config = 1; update_config = 1;
} }
sender->flags &= ~REDIS_NODE_SLAVE;
sender->flags |= REDIS_NODE_MASTER;
sender->slaveof = NULL;
} else { } else {
/* Node is a slave. */ /* Node is a slave. */
clusterNode *master = clusterLookupNode(hdr->slaveof); clusterNode *master = clusterLookupNode(hdr->slaveof);
if (sender->flags & REDIS_NODE_MASTER) { if (sender->flags & REDIS_NODE_MASTER) {
/* If the node changed role and is now a slave, clear all /* Master just turned into a slave? Reconfigure the node. */
* its slots as them are no longer served. */
clusterDelNodeSlots(sender); clusterDelNodeSlots(sender);
sender->flags &= ~REDIS_NODE_MASTER;
sender->flags |= REDIS_NODE_SLAVE;
/* Remove the list of slaves from the node. */
if (sender->numslaves) clusterNodeResetSlaves(sender);
update_state = 1; update_state = 1;
update_config = 1; update_config = 1;
} }
sender->flags &= ~REDIS_NODE_MASTER; /* Master node changed for this slave? */
sender->flags |= REDIS_NODE_SLAVE; if (sender->slaveof != master) {
if (sender->numslaves) clusterNodeResetSlaves(sender);
if (master) {
clusterNodeAddSlave(master,sender); clusterNodeAddSlave(master,sender);
sender->slaveof = master; sender->slaveof = master;
} }