Unverified Commit 279b4a14 authored by 马永泽's avatar 马永泽 Committed by GitHub
Browse files

fix benchmark in cluster mode fails to authenticate (#7488)

Co-authored-by: Oran Agra <oran@redislabs.com> (styling)
parent d5648d61
...@@ -183,6 +183,8 @@ static void *execBenchmarkThread(void *ptr); ...@@ -183,6 +183,8 @@ static void *execBenchmarkThread(void *ptr);
static clusterNode *createClusterNode(char *ip, int port); static clusterNode *createClusterNode(char *ip, int port);
static redisConfig *getRedisConfig(const char *ip, int port, static redisConfig *getRedisConfig(const char *ip, int port,
const char *hostsocket); const char *hostsocket);
static redisContext *getRedisContext(const char *ip, int port,
const char *hostsocket);
static void freeRedisConfig(redisConfig *cfg); static void freeRedisConfig(redisConfig *cfg);
static int fetchClusterSlotsConfiguration(client c); static int fetchClusterSlotsConfiguration(client c);
static void updateClusterSlotsConfiguration(); static void updateClusterSlotsConfiguration();
...@@ -238,40 +240,64 @@ void _serverAssert(const char *estr, const char *file, int line) { ...@@ -238,40 +240,64 @@ void _serverAssert(const char *estr, const char *file, int line) {
*((char*)-1) = 'x'; *((char*)-1) = 'x';
} }
static redisConfig *getRedisConfig(const char *ip, int port, static redisContext *getRedisContext(const char *ip, int port,
const char *hostsocket) const char *hostsocket)
{ {
redisConfig *cfg = zcalloc(sizeof(*cfg)); redisContext *ctx = NULL;
if (!cfg) return NULL; redisReply *reply = NULL;
redisContext *c = NULL;
redisReply *reply = NULL, *sub_reply = NULL;
if (hostsocket == NULL) if (hostsocket == NULL)
c = redisConnect(ip, port); ctx = redisConnect(ip, port);
else else
c = redisConnectUnix(hostsocket); ctx = redisConnectUnix(hostsocket);
if (c == NULL || c->err) { if (ctx == NULL || ctx->err) {
fprintf(stderr,"Could not connect to Redis at "); fprintf(stderr,"Could not connect to Redis at ");
char *err = (c != NULL ? c->errstr : ""); char *err = (ctx != NULL ? ctx->errstr : "");
if (hostsocket == NULL) fprintf(stderr,"%s:%d: %s\n",ip,port,err); if (hostsocket == NULL)
else fprintf(stderr,"%s: %s\n",hostsocket,err); fprintf(stderr,"%s:%d: %s\n",ip,port,err);
goto fail; else
fprintf(stderr,"%s: %s\n",hostsocket,err);
goto cleanup;
} }
if (config.auth == NULL)
if(config.auth) { return ctx;
void *authReply = NULL;
if (config.user == NULL) if (config.user == NULL)
redisAppendCommand(c, "AUTH %s", config.auth); reply = redisCommand(ctx,"AUTH %s", config.auth);
else else
redisAppendCommand(c, "AUTH %s %s", config.user, config.auth); reply = redisCommand(ctx,"AUTH %s %s", config.user, config.auth);
if (REDIS_OK != redisGetReply(c, &authReply)) goto fail; if (reply != NULL) {
if (reply) freeReplyObject(reply);
reply = ((redisReply *) authReply);
if (reply->type == REDIS_REPLY_ERROR) { if (reply->type == REDIS_REPLY_ERROR) {
fprintf(stderr, "ERROR: %s\n", reply->str); if (hostsocket == NULL)
goto fail; 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", "save");
redisAppendCommand(c, "CONFIG GET %s", "appendonly"); redisAppendCommand(c, "CONFIG GET %s", "appendonly");
int i = 0; int i = 0;
...@@ -994,16 +1020,8 @@ static int fetchClusterConfiguration() { ...@@ -994,16 +1020,8 @@ static int fetchClusterConfiguration() {
int success = 1; int success = 1;
redisContext *ctx = NULL; redisContext *ctx = NULL;
redisReply *reply = NULL; redisReply *reply = NULL;
if (config.hostsocket == NULL) ctx = getRedisContext(config.hostip, config.hostport, config.hostsocket);
ctx = redisConnect(config.hostip,config.hostport); if (ctx == NULL) {
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);
exit(1); exit(1);
} }
clusterNode *firstNode = createClusterNode((char *) config.hostip, clusterNode *firstNode = createClusterNode((char *) config.hostip,
...@@ -1199,11 +1217,9 @@ static int fetchClusterSlotsConfiguration(client c) { ...@@ -1199,11 +1217,9 @@ static int fetchClusterSlotsConfiguration(client c) {
assert(node->port); assert(node->port);
/* Use first node as entry point to connect to. */ /* Use first node as entry point to connect to. */
if (ctx == NULL) { if (ctx == NULL) {
ctx = redisConnect(node->ip, node->port); ctx = getRedisContext(node->ip, node->port, NULL);
if (!ctx || ctx->err) { if (!ctx) {
success = 0; success = 0;
if (ctx && ctx->err)
fprintf(stderr, "REDIS CONNECTION ERROR: %s\n", ctx->errstr);
goto cleanup; goto cleanup;
} }
} }
......
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