clusterManagerAddSlots: changed the way ADDSLOTS command is built

This commit is contained in:
artix 2018-03-06 13:06:04 +02:00
parent d518733073
commit a4cfd503ea

View File

@ -2354,32 +2354,28 @@ static int clusterManagerAddSlots(clusterManagerNode *node, char**err)
redisReply *reply = NULL; redisReply *reply = NULL;
void *_reply = NULL; void *_reply = NULL;
int is_err = 0, success = 1; int is_err = 0, success = 1;
int argc; /* First two args are used for the command itself. */
sds *argv = NULL; int argc = node->slots_count + 2;
size_t *argvlen = NULL; sds *argv = zmalloc(argc * sizeof(*argv));
size_t *argvlen = zmalloc(argc * sizeof(*argvlen));
argv[0] = "CLUSTER";
argv[1] = "ADDSLOTS";
argvlen[0] = 7;
argvlen[1] = 8;
*err = NULL; *err = NULL;
sds cmd = sdsnew("CLUSTER ADDSLOTS "); int i, argv_idx = 2;
int i, added = 0;
for (i = 0; i < CLUSTER_MANAGER_SLOTS; i++) { for (i = 0; i < CLUSTER_MANAGER_SLOTS; i++) {
int last_slot = (i == (CLUSTER_MANAGER_SLOTS - 1)); if (argv_idx >= argc) break;
if (node->slots[i]) { if (node->slots[i]) {
char *fmt = (!last_slot ? "%u " : "%u"); argv[argv_idx] = sdsfromlonglong((long long) i);
cmd = sdscatfmt(cmd, fmt, i); argvlen[argv_idx] = sdslen(argv[argv_idx]);
added++; argv_idx++;
} }
} }
if (!added) { if (!argv_idx) {
success = 0; success = 0;
goto cleanup; goto cleanup;
} }
argv = cliSplitArgs(cmd, &argc);
if (argc == 0 || argv == NULL) {
success = 0;
goto cleanup;
}
argvlen = zmalloc(argc*sizeof(size_t));
for (i = 0; i < argc; i++)
argvlen[i] = sdslen(argv[i]);
redisAppendCommandArgv(node->context,argc,(const char**)argv,argvlen); redisAppendCommandArgv(node->context,argc,(const char**)argv,argvlen);
if (redisGetReply(node->context, &_reply) != REDIS_OK) { if (redisGetReply(node->context, &_reply) != REDIS_OK) {
success = 0; success = 0;
@ -2395,9 +2391,11 @@ static int clusterManagerAddSlots(clusterManagerNode *node, char**err)
goto cleanup; goto cleanup;
} }
cleanup: cleanup:
sdsfree(cmd);
zfree(argvlen); zfree(argvlen);
if (argv != NULL) sdsfreesplitres(argv,argc); if (argv != NULL) {
for (i = 2; i < argc; i++) sdsfree(argv[i]);
zfree(argv);
}
if (reply != NULL) freeReplyObject(reply); if (reply != NULL) freeReplyObject(reply);
return success; return success;
} }