MCPcopy Create free account
hub / github.com/Snapchat/KeyDB / nodeUpdateAddressIfNeeded

Function nodeUpdateAddressIfNeeded

src/cluster.cpp:1609–1648  ·  view source on GitHub ↗

Update the node address to the IP address that can be extracted * from link->fd, or if hdr->myip is non empty, to the address the node * is announcing us. The port is taken from the packet header as well. * * If the address or port changed, disconnect the node link so that we'll * connect again to the new address. * * If the ip/port pair are already correct no operation is performed at * a

Source from the content-addressed store, hash-verified

1607 * The function returns 0 if the node address is still the same,
1608 * otherwise 1 is returned. */
1609int nodeUpdateAddressIfNeeded(clusterNode *node, clusterLink *link,
1610 clusterMsg *hdr)
1611{
1612 char ip[NET_IP_STR_LEN] = {0};
1613 int port = ntohs(hdr->port);
1614 int pport = ntohs(hdr->pport);
1615 int cport = ntohs(hdr->cport);
1616
1617 /* We don't proceed if the link is the same as the sender link, as this
1618 * function is designed to see if the node link is consistent with the
1619 * symmetric link that is used to receive PINGs from the node.
1620 *
1621 * As a side effect this function never frees the passed 'link', so
1622 * it is safe to call during packet processing. */
1623 if (link == node->link) return 0;
1624
1625 nodeIp2String(ip,link,hdr->myip);
1626 if (node->port == port && node->cport == cport && node->pport == pport &&
1627 strcmp(ip,node->ip) == 0) return 0;
1628
1629 /* IP / port is different, update it. */
1630 memcpy(node->ip,ip,sizeof(ip));
1631 node->port = port;
1632 node->pport = pport;
1633 node->cport = cport;
1634 if (node->link) freeClusterLink(node->link);
1635 node->flags &= ~CLUSTER_NODE_NOADDR;
1636 serverLog(LL_WARNING,"Address updated for node %.40s, now %s:%d",
1637 node->name, node->ip, node->port);
1638
1639 /* Check if this is our master and we have to change the
1640 * replication target as well. */
1641 if (nodeIsSlave(myself) && myself->slaveof == node)
1642 {
1643 serverAssert(listLength(g_pserver->masters) == 1);
1644 replicationAddMaster(node->ip, node->port);
1645 }
1646
1647 return 1;
1648}
1649
1650/* Reconfigure the specified node 'n' as a master. This function is called when
1651 * a node that we believed to be a slave is now acting as master in order to

Callers 1

clusterProcessPacketFunction · 0.85

Calls 4

nodeIp2StringFunction · 0.85
freeClusterLinkFunction · 0.85
serverLogFunction · 0.85
replicationAddMasterFunction · 0.85

Tested by

no test coverage detected