From 8fdc857a9f83e59b3062d051dc0155dd53c89ea7 Mon Sep 17 00:00:00 2001 From: antirez Date: Wed, 25 Jun 2014 11:28:57 +0200 Subject: [PATCH] Cluster: myself->ip autodiscovery. Instead of having an hardcoded IP address in the node configuration, we autodiscover it via MEET messages for automatic update when the node is restarted with a different IP address. This mechanism was discussed in the context of PR #1782. --- src/cluster.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/cluster.c b/src/cluster.c index 20da6d519656..c33e89486d48 100644 --- a/src/cluster.c +++ b/src/cluster.c @@ -466,6 +466,11 @@ void clusterInit(void) { /* The slots -> keys map is a sorted set. Init it. */ server.cluster->slots_to_keys = zslCreate(); + + /* Set myself->port to my listening port, we'll just need to discover + * the IP address via MEET messages. */ + myself->port = server.port; + resetManualFailover(); } @@ -1531,6 +1536,26 @@ int clusterProcessPacket(clusterLink *link) { if (type == CLUSTERMSG_TYPE_PING || type == CLUSTERMSG_TYPE_MEET) { redisLog(REDIS_DEBUG,"Ping packet received: %p", (void*)link->node); + /* We use incoming MEET messages in order to set the address + * for 'myself', since only other cluster nodes will send us + * MEET messagses on handshakes, when the cluster joins, or + * later if we changed address, and those nodes will use our + * official address to connect to us. So by obtaining this address + * from the socket is a simple way to discover / update our own + * address in the cluster without it being hardcoded in the config. */ + if (type == CLUSTERMSG_TYPE_MEET) { + char ip[REDIS_IP_STR_LEN]; + + if (anetSockName(link->fd,ip,sizeof(ip),NULL) != -1 && + strcmp(ip,myself->ip)) + { + memcpy(myself->ip,ip,REDIS_IP_STR_LEN); + redisLog(REDIS_WARNING,"IP address for this node updated to %s", + myself->ip); + clusterDoBeforeSleep(CLUSTER_TODO_SAVE_CONFIG); + } + } + /* Add this node if it is new for us and the msg type is MEET. * In this stage we don't try to add the node with the right * flags, slaveof pointer, and so forth, as this details will be