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

Co-authored-by: Oran Agra <oran@redislabs.com> (styling)
This commit is contained in:
马永泽 2020-07-10 21:37:11 +08:00 committed by GitHub
parent d5648d617e
commit 279b4a1464
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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,6 +240,52 @@ void _serverAssert(const char *estr, const char *file, int line) {
*((char*)-1) = 'x'; *((char*)-1) = 'x';
} }
static redisContext *getRedisContext(const char *ip, int port,
const char *hostsocket)
{
redisContext *ctx = NULL;
redisReply *reply = NULL;
if (hostsocket == NULL)
ctx = redisConnect(ip, port);
else
ctx = redisConnectUnix(hostsocket);
if (ctx == NULL || ctx->err) {
fprintf(stderr,"Could not connect to Redis at ");
char *err = (ctx != NULL ? ctx->errstr : "");
if (hostsocket == NULL)
fprintf(stderr,"%s:%d: %s\n",ip,port,err);
else
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) {
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, static redisConfig *getRedisConfig(const char *ip, int port,
const char *hostsocket) const char *hostsocket)
{ {
@ -245,33 +293,11 @@ static redisConfig *getRedisConfig(const char *ip, int port,
if (!cfg) return NULL; if (!cfg) return NULL;
redisContext *c = NULL; redisContext *c = NULL;
redisReply *reply = NULL, *sub_reply = NULL; redisReply *reply = NULL, *sub_reply = NULL;
if (hostsocket == NULL) c = getRedisContext(ip, port, hostsocket);
c = redisConnect(ip, port); if (c == NULL) {
else freeRedisConfig(cfg);
c = redisConnectUnix(hostsocket); return NULL;
if (c == NULL || c->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);
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);
if (reply->type == REDIS_REPLY_ERROR) {
fprintf(stderr, "ERROR: %s\n", reply->str);
goto fail;
}
}
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() {
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) {
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;
} }
} }