Cluster announce ip: take myself->ip always in sync.

This commit is contained in:
antirez 2016-01-22 13:26:43 +01:00
parent 11436b1449
commit dc98907e50

View File

@ -1658,7 +1658,7 @@ int clusterProcessPacket(clusterLink *link) {
/* We use incoming MEET messages in order to set the address /* We use incoming MEET messages in order to set the address
* for 'myself', since only other cluster nodes will send us * 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 * later if we changed address, and those nodes will use our
* official address to connect to us. So by obtaining this address * official address to connect to us. So by obtaining this address
* from the socket is a simple way to discover / update our own * 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 * 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 * even with a normal PING packet. If it's wrong it will be fixed
* by MEET later. */ * 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]; char ip[NET_IP_STR_LEN];
if (server.cluster_announce_ip) { if (anetSockName(link->fd,ip,sizeof(ip),NULL) != -1 &&
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 &&
strcmp(ip,myself->ip)) strcmp(ip,myself->ip))
{ {
memcpy(myself->ip,ip,NET_IP_STR_LEN); 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. */ 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 /* 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 * 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 * just the NODE_TIMEOUT value, but when NODE_TIMEOUT is too small we use