From dc98907e5067c554250f9022ae13d877de971457 Mon Sep 17 00:00:00 2001 From: antirez Date: Fri, 22 Jan 2016 13:26:43 +0100 Subject: [PATCH] Cluster announce ip: take myself->ip always in sync. --- src/cluster.c | 36 ++++++++++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/src/cluster.c b/src/cluster.c index 7fa120e87..9006e9cb6 100644 --- a/src/cluster.c +++ b/src/cluster.c @@ -1658,7 +1658,7 @@ int clusterProcessPacket(clusterLink *link) { /* 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 + * MEET messages 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 @@ -1667,13 +1667,12 @@ int clusterProcessPacket(clusterLink *link) { * However if we don't have an address at all, we update the address * even with a normal PING packet. If it's wrong it will be fixed * by MEET later. */ - if (type == CLUSTERMSG_TYPE_MEET || myself->ip[0] == '\0') { + if ((type == CLUSTERMSG_TYPE_MEET || myself->ip[0] == '\0') && + server.cluster_announce_ip == NULL) + { char ip[NET_IP_STR_LEN]; - if (server.cluster_announce_ip) { - strncpy(myself->ip,server.cluster_announce_ip,NET_IP_STR_LEN); - myself->ip[NET_IP_STR_LEN-1] = '\0'; - } else if (anetSockName(link->fd,ip,sizeof(ip),NULL) != -1 && + if (anetSockName(link->fd,ip,sizeof(ip),NULL) != -1 && strcmp(ip,myself->ip)) { memcpy(myself->ip,ip,NET_IP_STR_LEN); @@ -3118,6 +3117,31 @@ void clusterCron(void) { iteration++; /* Number of times this function was called so far. */ + /* We want to take myself->ip in sync with the cluster-announce-ip option. + * The option can be set at runtime via CONFIG SET, so we periodically check + * if the option changed to reflect this into myself->ip. */ + { + static char *prev_ip = NULL; + char *curr_ip = server.cluster_announce_ip; + int changed = 0; + + if (prev_ip == NULL && curr_ip != NULL) changed = 1; + if (prev_ip != NULL && curr_ip == NULL) changed = 1; + if (prev_ip && curr_ip && strcmp(prev_ip,curr_ip)) changed = 1; + + if (changed) { + prev_ip = curr_ip; + if (prev_ip) prev_ip = zstrdup(prev_ip); + + if (curr_ip) { + strncpy(myself->ip,server.cluster_announce_ip,NET_IP_STR_LEN); + myself->ip[NET_IP_STR_LEN-1] = '\0'; + } else { + myself->ip[0] = '\0'; /* Force autodetection. */ + } + } + } + /* The handshake timeout is the time after which a handshake node that was * not turned into a normal node is removed from the nodes. Usually it is * just the NODE_TIMEOUT value, but when NODE_TIMEOUT is too small we use