Unverified Commit 8351a10b authored by Binbin's avatar Binbin Committed by GitHub
Browse files

redis-cli: Sleep for a while in each cliConnect when we got connect error in cluster mode. (#8884)



There's an infinite loop when redis-cli fails to connect in cluster mode.
This commit adds a 1 second sleep to prevent flooding the console with errors.
It also adds a specific error print in a few places that could have error without printing anything.
Co-authored-by: default avatarOran Agra <oran@redislabs.com>
parent 0ecc814c
......@@ -800,13 +800,19 @@ static int cliAuth(redisContext *ctx, char *user, char *auth) {
reply = redisCommand(ctx,"AUTH %s",auth);
else
reply = redisCommand(ctx,"AUTH %s %s",user,auth);
if (reply != NULL) {
if (reply->type == REDIS_REPLY_ERROR)
fprintf(stderr,"Warning: AUTH failed\n");
freeReplyObject(reply);
return REDIS_OK;
}
if (reply == NULL) {
fprintf(stderr, "\nI/O error\n");
return REDIS_ERR;
}
int result = REDIS_OK;
if (reply->type == REDIS_REPLY_ERROR) {
result = REDIS_ERR;
fprintf(stderr, "AUTH failed: %s\n", reply->str);
}
freeReplyObject(reply);
return result;
}
/* Send SELECT input_dbnum to the server */
......@@ -815,7 +821,11 @@ static int cliSelect(void) {
if (config.input_dbnum == config.dbnum) return REDIS_OK;
reply = redisCommand(context,"SELECT %d",config.input_dbnum);
if (reply != NULL) {
if (reply == NULL) {
fprintf(stderr, "\nI/O error\n");
return REDIS_ERR;
}
int result = REDIS_OK;
if (reply->type == REDIS_REPLY_ERROR) {
result = REDIS_ERR;
......@@ -826,8 +836,6 @@ static int cliSelect(void) {
}
freeReplyObject(reply);
return result;
}
return REDIS_ERR;
}
/* Select RESP3 mode if redis-cli was started with the -3 option. */
......@@ -836,7 +844,11 @@ static int cliSwitchProto(void) {
if (config.resp3 == 0) return REDIS_OK;
reply = redisCommand(context,"HELLO 3");
if (reply != NULL) {
if (reply == NULL) {
fprintf(stderr, "\nI/O error\n");
return REDIS_ERR;
}
int result = REDIS_OK;
if (reply->type == REDIS_REPLY_ERROR) {
result = REDIS_ERR;
......@@ -844,8 +856,6 @@ static int cliSwitchProto(void) {
}
freeReplyObject(reply);
return result;
}
return REDIS_ERR;
}
/* Connect to the server. It is possible to pass certain flags to the function:
......@@ -882,13 +892,16 @@ static int cliConnect(int flags) {
if (context->err) {
if (!(flags & CC_QUIET)) {
fprintf(stderr,"Could not connect to Redis at ");
if (config.hostsocket == NULL)
fprintf(stderr,"%s:%d: %s\n",
if (config.hostsocket == NULL ||
(config.cluster_mode && config.cluster_reissue_command))
{
fprintf(stderr, "%s:%d: %s\n",
config.hostip,config.hostport,context->errstr);
else
} else {
fprintf(stderr,"%s: %s\n",
config.hostsocket,context->errstr);
}
}
redisFree(context);
context = NULL;
return REDIS_ERR;
......@@ -1476,7 +1489,7 @@ static int cliSendCommand(int argc, char **argv, long repeat) {
}
if (config.cluster_reissue_command){
/* If we need to reissue the command, break to prevent a
further 'repeat' number of dud interations */
further 'repeat' number of dud interactions */
break;
}
if (config.interval) usleep(config.interval);
......@@ -2022,9 +2035,12 @@ static int issueCommandRepeat(int argc, char **argv, long repeat) {
return REDIS_ERR;
}
}
/* Issue the command again if we got redirected in cluster mode */
if (config.cluster_mode && config.cluster_reissue_command) {
cliConnect(CC_FORCE);
/* If cliConnect fails, sleep for a while and try again. */
if (cliConnect(CC_FORCE) != REDIS_OK)
sleep(1);
} else {
break;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment