Skip to content

Commit

Permalink
Cluster: myself->ip autodiscovery.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
antirez committed Jun 26, 2014
1 parent b56849c commit 8fdc857
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/cluster.c
Expand Up @@ -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();
}

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 8fdc857

Please sign in to comment.