diff --git a/src/networking.c b/src/networking.c index c7b1c9ba78945ba556561dc4932e6838b22060a7..cc9bbd98c97e4c93da50d2528d1c650e3fb0f55f 100644 --- a/src/networking.c +++ b/src/networking.c @@ -678,12 +678,8 @@ void freeClient(redisClient *c) { /* Log link disconnection with slave */ if ((c->flags & REDIS_SLAVE) && !(c->flags & REDIS_MONITOR)) { - char ip[REDIS_IP_STR_LEN]; - - if (anetPeerToString(c->fd,ip,sizeof(ip),NULL) != -1) { - redisLog(REDIS_WARNING,"Connection with slave %s:%d lost.", - ip, c->slave_listening_port); - } + redisLog(REDIS_WARNING,"Connection with slave %s lost.", + replicationGetSlaveName(c)); } /* Free the query buffer */ diff --git a/src/redis.h b/src/redis.h index f4f933eb935ab0020032bcd8e2cefb2f25fcf411..f5301ab26351dea415f5227da7706e2087a0585c 100644 --- a/src/redis.h +++ b/src/redis.h @@ -1162,6 +1162,7 @@ void unblockClientWaitingReplicas(redisClient *c); int replicationCountAcksByOffset(long long offset); void replicationSendNewlineToMaster(void); long long replicationGetSlaveOffset(void); +char *replicationGetSlaveName(redisClient *c); /* Generic persistence functions */ void startLoading(FILE *fp); diff --git a/src/replication.c b/src/replication.c index e9c98c5c6478be3a492b277c44078c6b5dd59e8f..048acfaab34c420f19664d57cfa3c71200869739 100644 --- a/src/replication.c +++ b/src/replication.c @@ -41,6 +41,30 @@ void replicationDiscardCachedMaster(void); void replicationResurrectCachedMaster(int newfd); void replicationSendAck(void); +/* --------------------------- Utility functions ---------------------------- */ + +/* Return the pointer to a string representing the slave ip:listening_port + * pair. Mostly useful for logging, since we want to log a slave using its + * IP address and it's listening port which is more clear for the user, for + * example: "Closing connection with slave 10.1.2.3:6380". */ +char *replicationGetSlaveName(redisClient *c) { + static char buf[REDIS_PEER_ID_LEN]; + char ip[REDIS_IP_STR_LEN]; + + ip[0] = '\0'; + buf[0] = '\0'; + if (anetPeerToString(c->fd,ip,sizeof(ip),NULL) != -1) { + if (c->slave_listening_port) + snprintf(buf,sizeof(buf),"%s:%d",ip,c->slave_listening_port); + else + snprintf(buf,sizeof(buf),"%s:",ip); + } else { + snprintf(buf,sizeof(buf),"client id #%llu", + (unsigned long long) c->id); + } + return buf; +} + /* ---------------------------------- MASTER -------------------------------- */ void createReplicationBacklog(void) { @@ -1973,15 +1997,8 @@ void replicationCron(void) { if (slave->flags & REDIS_PRE_PSYNC) continue; if ((server.unixtime - slave->repl_ack_time) > server.repl_timeout) { - char ip[REDIS_IP_STR_LEN]; - int port; - - if (anetPeerToString(slave->fd,ip,sizeof(ip),&port) != -1) { - redisLog(REDIS_WARNING, - "Disconnecting timedout slave: %s:%d", - ip, slave->slave_listening_port); - } - freeClient(slave); + redisLog(REDIS_WARNING, "Disconnecting timedout slave: %s", + replicationGetSlaveName(slave)); } } }