Reconnect redis-cli when cluster return "moved"

if redis works in cluster-mode and redis-cli was run with argv, reconnect if needs.
    example:
    ./redis-cli set foo bar

    if return is MOVED redis-cli just do nothing.
This commit is contained in:
h0x91b 2014-10-30 21:05:50 +02:00
parent 2c42b645bc
commit 7fcfbea0f5

View File

@ -877,6 +877,33 @@ static char **convertToSds(int count, char** args) {
return sds; return sds;
} }
static int issueCommandRepeat(int argc, char **argv, long repeat) {
while (1) {
config.cluster_reissue_command = 0;
if (cliSendCommand(argc,argv,repeat) != REDIS_OK) {
cliConnect(1);
/* If we still cannot send the command print error.
* We'll try to reconnect the next time. */
if (cliSendCommand(argc,argv,repeat) != REDIS_OK) {
cliPrintContextError();
return REDIS_ERR;
}
}
/* Issue the command again if we got redirected in cluster mode */
if (config.cluster_mode && config.cluster_reissue_command) {
cliConnect(1);
} else {
break;
}
}
return REDIS_OK;
}
static int issueCommand(int argc, char **argv) {
return issueCommandRepeat(argc, argv, config.repeat);
}
static void repl(void) { static void repl(void) {
sds historyfile = NULL; sds historyfile = NULL;
int history = 0; int history = 0;
@ -933,26 +960,8 @@ static void repl(void) {
repeat = 1; repeat = 1;
} }
while (1) { issueCommandRepeat(argc-skipargs, argv+skipargs, repeat);
config.cluster_reissue_command = 0;
if (cliSendCommand(argc-skipargs,argv+skipargs,repeat)
!= REDIS_OK)
{
cliConnect(1);
/* If we still cannot send the command print error.
* We'll try to reconnect the next time. */
if (cliSendCommand(argc-skipargs,argv+skipargs,repeat)
!= REDIS_OK)
cliPrintContextError();
}
/* Issue the command again if we got redirected in cluster mode */
if (config.cluster_mode && config.cluster_reissue_command) {
cliConnect(1);
} else {
break;
}
}
elapsed = mstime()-start_time; elapsed = mstime()-start_time;
if (elapsed >= 500) { if (elapsed >= 500) {
printf("(%.2fs)\n",(double)elapsed/1000); printf("(%.2fs)\n",(double)elapsed/1000);
@ -973,10 +982,9 @@ static int noninteractive(int argc, char **argv) {
if (config.stdinarg) { if (config.stdinarg) {
argv = zrealloc(argv, (argc+1)*sizeof(char*)); argv = zrealloc(argv, (argc+1)*sizeof(char*));
argv[argc] = readArgFromStdin(); argv[argc] = readArgFromStdin();
retval = cliSendCommand(argc+1, argv, config.repeat); retval = issueCommand(argc+1, argv);
} else { } else {
/* stdin is probably a tty, can be tested with S_ISCHR(s.st_mode) */ retval = issueCommand(argc, argv);
retval = cliSendCommand(argc, argv, config.repeat);
} }
return retval; return retval;
} }
@ -1020,7 +1028,7 @@ static int evalMode(int argc, char **argv) {
argv2[2] = sdscatprintf(sdsempty(),"%d",keys); argv2[2] = sdscatprintf(sdsempty(),"%d",keys);
/* Call it */ /* Call it */
return cliSendCommand(argc+3-got_comma, argv2, config.repeat); return issueCommand(argc+3-got_comma, argv2);
} }
/*------------------------------------------------------------------------------ /*------------------------------------------------------------------------------