diff options
author | charsyam <charsyam@gmail.com> | 2013-02-17 20:10:48 -0800 |
---|---|---|
committer | antirez <antirez@gmail.com> | 2014-08-08 15:39:44 +0200 |
commit | de5465baf7f30b1040ae7e8c36ad8fec14edfea4 (patch) | |
tree | aed40997a4fdb4798e7c8f8b92f508a28dde82fe /src | |
parent | da0132638f15d81782c5543ed49a1d1f17594d9d (diff) | |
download | redis-de5465baf7f30b1040ae7e8c36ad8fec14edfea4.tar.gz |
Refactor cluster flag printing
Less copy/paste code duplication.
Closes #952
Diffstat (limited to 'src')
-rw-r--r-- | src/cluster.c | 56 |
1 files changed, 36 insertions, 20 deletions
diff --git a/src/cluster.c b/src/cluster.c index cb9efc642..cd1100ae3 100644 --- a/src/cluster.c +++ b/src/cluster.c @@ -73,6 +73,39 @@ void clusterCloseAllSlots(void); void clusterSetNodeAsMaster(clusterNode *n); void clusterDelNode(clusterNode *delnode); +struct redisNodeFlags { + uint16_t flag; + char *attr; +}; + +static struct redisNodeFlags nodeflags[] = { + {REDIS_NODE_MYSELF, "myself,"}, + {REDIS_NODE_MASTER, "master,"}, + {REDIS_NODE_SLAVE, "slave,"}, + {REDIS_NODE_PFAIL, "fail?,"}, + {REDIS_NODE_FAIL, "fail,"}, + {REDIS_NODE_HANDSHAKE, "handshake,"}, + {REDIS_NODE_NOADDR, "noaddr,"} +}; + +sds representRedisNodeFlags(sds ci, uint16_t flags) { + if (flags == 0) { + ci = sdscat(ci,"noflags"); + } else { + int size = sizeof(nodeflags) / sizeof(struct redisNodeFlags); + for (int i=0; i < size; i++) { + struct redisNodeFlags *nodeflag = nodeflags + i; + if (flags & nodeflag->flag) { + ci = sdscat(ci, nodeflag->attr); + } + } + + if (ci[sdslen(ci)-1] == ',') ci[sdslen(ci)-1] = ' '; + } + + return ci; +} + /* ----------------------------------------------------------------------------- * Initialization * -------------------------------------------------------------------------- */ @@ -1145,20 +1178,11 @@ void clusterProcessGossipSection(clusterMsg *hdr, clusterLink *link) { clusterNode *sender = link->node ? link->node : clusterLookupNode(hdr->sender); while(count--) { - sds ci = sdsempty(); uint16_t flags = ntohs(g->flags); clusterNode *node; - if (flags == 0) ci = sdscat(ci,"noflags,"); - if (flags & REDIS_NODE_MYSELF) ci = sdscat(ci,"myself,"); - if (flags & REDIS_NODE_MASTER) ci = sdscat(ci,"master,"); - if (flags & REDIS_NODE_SLAVE) ci = sdscat(ci,"slave,"); - if (flags & REDIS_NODE_PFAIL) ci = sdscat(ci,"fail?,"); - if (flags & REDIS_NODE_FAIL) ci = sdscat(ci,"fail,"); - if (flags & REDIS_NODE_HANDSHAKE) ci = sdscat(ci,"handshake,"); - if (flags & REDIS_NODE_NOADDR) ci = sdscat(ci,"noaddr,"); - if (ci[sdslen(ci)-1] == ',') ci[sdslen(ci)-1] = ' '; - + sds ci = sdsempty(); + ci = representRedisNodeFlags(ci, flags); redisLog(REDIS_DEBUG,"GOSSIP %.40s %s:%d %s", g->nodename, g->ip, @@ -3355,15 +3379,7 @@ sds clusterGenNodeDescription(clusterNode *node) { node->port); /* Flags */ - if (node->flags == 0) ci = sdscat(ci,"noflags,"); - if (node->flags & REDIS_NODE_MYSELF) ci = sdscat(ci,"myself,"); - if (node->flags & REDIS_NODE_MASTER) ci = sdscat(ci,"master,"); - if (node->flags & REDIS_NODE_SLAVE) ci = sdscat(ci,"slave,"); - if (node->flags & REDIS_NODE_PFAIL) ci = sdscat(ci,"fail?,"); - if (node->flags & REDIS_NODE_FAIL) ci = sdscat(ci,"fail,"); - if (node->flags & REDIS_NODE_HANDSHAKE) ci =sdscat(ci,"handshake,"); - if (node->flags & REDIS_NODE_NOADDR) ci = sdscat(ci,"noaddr,"); - if (ci[sdslen(ci)-1] == ',') ci[sdslen(ci)-1] = ' '; + ci = representRedisNodeFlags(ci, node->flags); /* Slave of... or just "-" */ if (node->slaveof) |