扩展redis cluster addslots 命令
众所周知,redis集群集群模式下必须将16384个指派完,也就是16384个槽点都在处理时,集群才处于上线上线状态。在指派槽时用的命令是这样的 “cluster addslots 1 2 3” 这样。这里有个不好的地方就是每次添加只能一个槽点一个槽点这样添加,着实不方便,我想扩展一下redis的这个命令,令他支持""cluster addslots {1…5000} "这样的语法
我们先看一下redis中 处理“ cluster addslots”命令的相关的代码,在 cluster.c的clusterCommand方法中,
具体代码如下(为了便于阅读我会添加一些中文注释以便于读者理解):
else if ((!strcasecmp(c->argv[1]->ptr,"addslots") ||
!strcasecmp(c->argv[1]->ptr,"delslots")) && c->argc >= 3)
{
//如果是"cluster addslots或者 cluster "delslots命令
/* CLUSTER ADDSLOTS <slot> [slot] ... */
/* CLUSTER DELSLOTS <slot> [slot] ... */
int j, slot;
unsigned char *slots = zmalloc(CLUSTER_SLOTS); //一个数组,记录所有要添加或者删除的槽 slots[519] == 1 代表 519槽位置要删除或者添加
int del = !strcasecmp(c->argv[1]->ptr,"delslots"); //是否是删除命令
memset(slots,0,CLUSTER_SLOTS); //常规操作
/* Check that all the arguments are parseable and that all the
* slots are not already busy. */
/*遍历参数列表 比如cluster addslots 1 2 3 4就是遍历 1 2 3 4 */
for (j = 2; j < c->argc; j++) {
if ((slot = getSlotOrReply(c,c->argv[j])) == -1) {
zfree(slots);
return;
}
if (del && server.cluster->slots[slot] == NULL) {
addReplyErrorFormat(c,"Slot %d is already unassigned", slot);
zfree(slots);
return;
} else if (!del && server.cluster->slots[slo