Skip to content

Commit

Permalink
GEODE-9666: Avoid caching InetSocketAddress (apache#6938)
Browse files Browse the repository at this point in the history
The changes for GEODE-9139 changed the behavior of
org.apache.geode.distributed.internal.tcpserver.HostAndPort to
permanently cache the internal InetSocketAddress once it has tried one
time to resolve the address. This undoes part of the fix introduced by
GEODE-7808, in which HostAndPort was created as a way to hold an
unresolved hostname.

The issue is that the cached InetSocketAddress may contain a stale or
unresolved address which will be returned by getSocketInetAddress for
the lifetime of the HostAndPort/InetSocketWrapper object. This prevents
the address from being resolved correctly after changes in DNS records.
(Such changes are common in cloud environments.)

This commit removes the cached internal InetSocketAddress from
InetSocketWrapper so that getSocketInetAddress will try to resolve the
address each time it is called with an unresolved address.
  • Loading branch information
Aaron Lindsey authored Oct 13, 2021
1 parent 220bc2d commit b39958f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
class InetSocketWrapper {
String hostName;
protected InetSocketAddress inetSocketAddress;
private InetSocketAddress attemptedToResolve;

protected InetSocketWrapper() {}

Expand Down Expand Up @@ -77,13 +76,6 @@ public boolean equals(Object o) {
* value will not hold an InetAddress, so calling getAddress() on it will return null.
*/
public InetSocketAddress getSocketInetAddress() {
if (attemptedToResolve == null) {
attemptedToResolve = basicGetInetSocketAddress();
}
return attemptedToResolve;
}

private InetSocketAddress basicGetInetSocketAddress() {
if (inetSocketAddress.isUnresolved()) {
// note that this leaves the InetAddress null if the hostname isn't resolvable
return new InetSocketAddress(inetSocketAddress.getHostString(), inetSocketAddress.getPort());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void getSocketInetAddress_returns_resolved_SocketAddress() {
* Test that getSocketInentAddress returns unresolved InetSocketAddress
*/
@Test
public void getSocketInentAddress_returns_unresolved_SocketAddress() {
public void getSocketInetAddress_returns_unresolved_SocketAddress() {
HostAndPort locator1 = new HostAndPort("fakelocalhost", 8090);

InetSocketAddress actual = locator1.getSocketInetAddress();
Expand All @@ -59,6 +59,18 @@ public void getSocketInentAddress_returns_unresolved_SocketAddress() {
.isTrue();
}

@Test
public void cachesSocketAddressForIP() {
HostAndPort hostAndPort = new HostAndPort("127.0.0.1", 8080);
assertThat(hostAndPort.getSocketInetAddress()).isSameAs(hostAndPort.getSocketInetAddress());
}

@Test
public void doesNotCacheSocketAddressForHostname() {
HostAndPort hostAndPort = new HostAndPort("localhost", 8080);
assertThat(hostAndPort.getSocketInetAddress()).isNotSameAs(hostAndPort.getSocketInetAddress());
}

/**
* Test whether HostAndPort are equal, when created from resolved and unresolved
* InetSocketAddress
Expand Down

0 comments on commit b39958f

Please sign in to comment.