summaryrefslogtreecommitdiff
path: root/src/redis-benchmark.c
diff options
context:
space:
mode:
author马永泽 <1014057907@qq.com>2020-07-10 21:37:11 +0800
committerGitHub <noreply@github.com>2020-07-10 16:37:11 +0300
commit279b4a14643f247365637da13439fcb407072a9d (patch)
tree23f96f8ed4182de51ef905cf38f68213278267c9 /src/redis-benchmark.c
parentd5648d617e1ed5b9cfa575ad412bc9d450b16afd (diff)
downloadredis-279b4a14643f247365637da13439fcb407072a9d.tar.gz
fix benchmark in cluster mode fails to authenticate (#7488)
Co-authored-by: Oran Agra <oran@redislabs.com> (styling)
Diffstat (limited to 'src/redis-benchmark.c')
-rw-r--r--src/redis-benchmark.c96
1 files changed, 56 insertions, 40 deletions
diff --git a/src/redis-benchmark.c b/src/redis-benchmark.c
index 38d4ca51b..f47cbe333 100644
--- a/src/redis-benchmark.c
+++ b/src/redis-benchmark.c
@@ -1,4 +1,4 @@
-/* Redis benchmark utility.
+/* Redis benchmark utility.
*
* Copyright (c) 2009-2012, Salvatore Sanfilippo <antirez at gmail dot com>
* All rights reserved.
@@ -183,6 +183,8 @@ static void *execBenchmarkThread(void *ptr);
static clusterNode *createClusterNode(char *ip, int port);
static redisConfig *getRedisConfig(const char *ip, int port,
const char *hostsocket);
+static redisContext *getRedisContext(const char *ip, int port,
+ const char *hostsocket);
static void freeRedisConfig(redisConfig *cfg);
static int fetchClusterSlotsConfiguration(client c);
static void updateClusterSlotsConfiguration();
@@ -238,40 +240,64 @@ void _serverAssert(const char *estr, const char *file, int line) {
*((char*)-1) = 'x';
}
-static redisConfig *getRedisConfig(const char *ip, int port,
- const char *hostsocket)
+static redisContext *getRedisContext(const char *ip, int port,
+ const char *hostsocket)
{
- redisConfig *cfg = zcalloc(sizeof(*cfg));
- if (!cfg) return NULL;
- redisContext *c = NULL;
- redisReply *reply = NULL, *sub_reply = NULL;
+ redisContext *ctx = NULL;
+ redisReply *reply = NULL;
if (hostsocket == NULL)
- c = redisConnect(ip, port);
+ ctx = redisConnect(ip, port);
else
- c = redisConnectUnix(hostsocket);
- if (c == NULL || c->err) {
+ ctx = redisConnectUnix(hostsocket);
+ if (ctx == NULL || ctx->err) {
fprintf(stderr,"Could not connect to Redis at ");
- char *err = (c != NULL ? c->errstr : "");
- if (hostsocket == NULL) fprintf(stderr,"%s:%d: %s\n",ip,port,err);
- else fprintf(stderr,"%s: %s\n",hostsocket,err);
- goto fail;
- }
-
- if(config.auth) {
- void *authReply = NULL;
- if (config.user == NULL)
- redisAppendCommand(c, "AUTH %s", config.auth);
+ char *err = (ctx != NULL ? ctx->errstr : "");
+ if (hostsocket == NULL)
+ fprintf(stderr,"%s:%d: %s\n",ip,port,err);
else
- redisAppendCommand(c, "AUTH %s %s", config.user, config.auth);
- if (REDIS_OK != redisGetReply(c, &authReply)) goto fail;
- if (reply) freeReplyObject(reply);
- reply = ((redisReply *) authReply);
+ fprintf(stderr,"%s: %s\n",hostsocket,err);
+ goto cleanup;
+ }
+ if (config.auth == NULL)
+ return ctx;
+ if (config.user == NULL)
+ reply = redisCommand(ctx,"AUTH %s", config.auth);
+ else
+ reply = redisCommand(ctx,"AUTH %s %s", config.user, config.auth);
+ if (reply != NULL) {
if (reply->type == REDIS_REPLY_ERROR) {
- fprintf(stderr, "ERROR: %s\n", reply->str);
- goto fail;
+ if (hostsocket == NULL)
+ fprintf(stderr, "Node %s:%d replied with error:\n%s\n", ip, port, reply->str);
+ else
+ fprintf(stderr, "Node %s replied with error:\n%s\n", hostsocket, reply->str);
+ goto cleanup;
}
+ freeReplyObject(reply);
+ return ctx;
}
+ fprintf(stderr, "ERROR: failed to fetch reply from ");
+ if (hostsocket == NULL)
+ fprintf(stderr, "%s:%d\n", ip, port);
+ else
+ fprintf(stderr, "%s\n", hostsocket);
+cleanup:
+ freeReplyObject(reply);
+ redisFree(ctx);
+ return NULL;
+}
+static redisConfig *getRedisConfig(const char *ip, int port,
+ const char *hostsocket)
+{
+ redisConfig *cfg = zcalloc(sizeof(*cfg));
+ if (!cfg) return NULL;
+ redisContext *c = NULL;
+ redisReply *reply = NULL, *sub_reply = NULL;
+ c = getRedisContext(ip, port, hostsocket);
+ if (c == NULL) {
+ freeRedisConfig(cfg);
+ return NULL;
+ }
redisAppendCommand(c, "CONFIG GET %s", "save");
redisAppendCommand(c, "CONFIG GET %s", "appendonly");
int i = 0;
@@ -994,16 +1020,8 @@ static int fetchClusterConfiguration() {
int success = 1;
redisContext *ctx = NULL;
redisReply *reply = NULL;
- if (config.hostsocket == NULL)
- ctx = redisConnect(config.hostip,config.hostport);
- else
- ctx = redisConnectUnix(config.hostsocket);
- if (ctx->err) {
- fprintf(stderr,"Could not connect to Redis at ");
- if (config.hostsocket == NULL) {
- fprintf(stderr,"%s:%d: %s\n",config.hostip,config.hostport,
- ctx->errstr);
- } else fprintf(stderr,"%s: %s\n",config.hostsocket,ctx->errstr);
+ ctx = getRedisContext(config.hostip, config.hostport, config.hostsocket);
+ if (ctx == NULL) {
exit(1);
}
clusterNode *firstNode = createClusterNode((char *) config.hostip,
@@ -1199,11 +1217,9 @@ static int fetchClusterSlotsConfiguration(client c) {
assert(node->port);
/* Use first node as entry point to connect to. */
if (ctx == NULL) {
- ctx = redisConnect(node->ip, node->port);
- if (!ctx || ctx->err) {
+ ctx = getRedisContext(node->ip, node->port, NULL);
+ if (!ctx) {
success = 0;
- if (ctx && ctx->err)
- fprintf(stderr, "REDIS CONNECTION ERROR: %s\n", ctx->errstr);
goto cleanup;
}
}