Skip to content

Commit 74e23d2

Browse files
committed
simplify
1 parent 379803a commit 74e23d2

3 files changed

Lines changed: 28 additions & 31 deletions

File tree

dd-java-agent/agent-bootstrap/src/main/java/datadog/trace/bootstrap/instrumentation/java/net/HostNameResolver.java

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,47 +7,41 @@
77
import java.net.InetAddress;
88

99
public final class HostNameResolver {
10-
private static volatile MethodHandle HOLDER_GET;
11-
private static volatile MethodHandle HOSTNAME_GET;
10+
private static final MethodHandle HOLDER_GET;
11+
private static final MethodHandle HOSTNAME_GET;
1212

1313
private static final DDCache<String, String> HOSTNAME_CACHE = DDCaches.newFixedSizeCache(64);
1414

15-
private HostNameResolver() {}
16-
17-
public static void tryInitialize() {
18-
if (HOLDER_GET != null) {
19-
return; // fast path: already initialized
20-
}
21-
synchronized (HostNameResolver.class) {
22-
if (HOLDER_GET != null) {
23-
return; // double-check: another thread just succeeded
24-
}
25-
MethodHandle holderTmp = null, hostnameTmp = null;
26-
try {
27-
final ClassLoader cl = HostNameResolver.class.getClassLoader();
28-
final MethodHandles methodHandles = new MethodHandles(cl);
15+
static {
16+
MethodHandle holderTmp = null, hostnameTmp = null;
17+
try {
18+
final ClassLoader cl = HostNameResolver.class.getClassLoader();
19+
final MethodHandles methodHandles = new MethodHandles(cl);
20+
// forces the JPMS opener for this class to get executed
21+
InetAddress.getLocalHost();
2922

30-
final Class<?> holderClass =
31-
Class.forName("java.net.InetAddress$InetAddressHolder", false, cl);
32-
holderTmp = methodHandles.method(InetAddress.class, "holder");
33-
if (holderTmp != null) {
34-
hostnameTmp = methodHandles.method(holderClass, "getHostName");
35-
}
36-
} catch (Throwable ignored) {
37-
holderTmp = null;
23+
final Class<?> holderClass =
24+
Class.forName("java.net.InetAddress$InetAddressHolder", false, cl);
25+
holderTmp = methodHandles.method(InetAddress.class, "holder");
26+
if (holderTmp != null) {
27+
hostnameTmp = methodHandles.method(holderClass, "getHostName");
3828
}
39-
// volatile writes ensure visibility to other threads
29+
} catch (Throwable ignored) {
30+
holderTmp = null;
31+
} finally {
4032
if (holderTmp != null && hostnameTmp != null) {
33+
HOLDER_GET = holderTmp;
4134
HOSTNAME_GET = hostnameTmp;
42-
HOLDER_GET = holderTmp; // written last: signals successful initialization
35+
} else {
36+
HOLDER_GET = null;
37+
HOSTNAME_GET = null;
4338
}
4439
}
4540
}
4641

42+
private HostNameResolver() {}
43+
4744
static String getAlreadyResolvedHostName(InetAddress address) {
48-
if (HOLDER_GET == null) {
49-
tryInitialize();
50-
}
5145
if (HOLDER_GET == null) {
5246
return null;
5347
}
@@ -67,6 +61,9 @@ private static String fromCache(InetAddress remoteAddress, String ip) {
6761
}
6862

6963
public static String hostName(InetAddress address, String ip) {
64+
if (address == null) {
65+
return null;
66+
}
7067
final String alreadyResolved = getAlreadyResolvedHostName(address);
7168
if (alreadyResolved != null) {
7269
return alreadyResolved;

dd-java-agent/instrumentation/java/java-net/java-net-11.0/src/main/java11/datadog/trace/instrumentation/httpclient/JpmsInetAddressClearanceAdvice.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ public static void openOnReturn() {
1414
// The code of this advice is inlined into the constructor of InetAddress (java.base),
1515
// so it will work. Moving the same call to a helper class won't.
1616
InetAddress.class.getModule().addOpens("java.net", HostNameResolver.class.getModule());
17-
// Now that java.net is open for deep reflection, initialize the HostNameResolver handles
18-
HostNameResolver.tryInitialize();
1917
}
2018
}
2119
}

dd-java-agent/instrumentation/java/java-net/java-net-11.0/src/test/groovy/datadog/trace/instrumentation/httpclient/JpmsInetAddressForkedTest.groovy

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ class JpmsInetAddressForkedTest extends InstrumentationSpecification {
1818
*/
1919
def "instrumentation opens java.net so hostname is resolved correctly when IP is shared"() {
2020
given:
21+
// emulate an early initialisation
22+
HostNameResolver.hostName(null, "192.0.2.1")
2123
def ip = [192, 0, 2, 1] as byte[] // TEST-NET, will never appear in real DNS cache
2224
def addr1 = InetAddress.getByAddress("service1.example.com", ip)
2325
// Warm the IP→hostname cache with service1's hostname

0 commit comments

Comments
 (0)