summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorh0x91b <h0x91b@gmail.com>2014-10-30 21:05:50 +0200
committerantirez <antirez@gmail.com>2014-12-11 15:07:49 +0100
commitb3349204b80a8a1df7acdbb7696138dd2f42ee85 (patch)
tree8ec3977f7dd2add413a5ce0f7385dd35261580d8
parent0f1107432acc59ad7fa992c0d9ce701ff2ddbee0 (diff)
downloadredis-b3349204b80a8a1df7acdbb7696138dd2f42ee85.tar.gz
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.
-rw-r--r--src/redis-cli.c56
1 files changed, 32 insertions, 24 deletions
diff --git a/src/redis-cli.c b/src/redis-cli.c
index d73d37390..2d04a5545 100644
--- a/src/redis-cli.c
+++ b/src/redis-cli.c
@@ -878,6 +878,33 @@ static char **convertToSds(int count, char** args) {
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) {
sds historyfile = NULL;
int history = 0;
@@ -934,26 +961,8 @@ static void repl(void) {
repeat = 1;
}
- while (1) {
- 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;
- }
- }
+ issueCommandRepeat(argc-skipargs, argv+skipargs, repeat);
+
elapsed = mstime()-start_time;
if (elapsed >= 500) {
printf("(%.2fs)\n",(double)elapsed/1000);
@@ -974,10 +983,9 @@ static int noninteractive(int argc, char **argv) {
if (config.stdinarg) {
argv = zrealloc(argv, (argc+1)*sizeof(char*));
argv[argc] = readArgFromStdin();
- retval = cliSendCommand(argc+1, argv, config.repeat);
+ retval = issueCommand(argc+1, argv);
} else {
- /* stdin is probably a tty, can be tested with S_ISCHR(s.st_mode) */
- retval = cliSendCommand(argc, argv, config.repeat);
+ retval = issueCommand(argc, argv);
}
return retval;
}
@@ -1021,7 +1029,7 @@ static int evalMode(int argc, char **argv) {
argv2[2] = sdscatprintf(sdsempty(),"%d",keys);
/* Call it */
- return cliSendCommand(argc+3-got_comma, argv2, config.repeat);
+ return issueCommand(argc+3-got_comma, argv2);
}
/*------------------------------------------------------------------------------