java-dns-cache-manipulator
java-dns-cache-manipulator copied to clipboard
child class java.net.InetAddress$NameServiceAddresses for class InetAddress.Addresses should never happens
相同的服务运行在多台服务器上,其中有个别服务器偶现报错:
Caused by: java.lang.IllegalStateException: child class java.net.InetAddress$NameServiceAddresses for class InetAddress.Addresses should never happens, report issue for dns-cache-manipulator lib!
at com.alibaba.dcm.internal.InetAddressCacheUtilForNew.inetAddress$Addresses2DnsCacheEntry(InetAddressCacheUtilForNew.java:243)
at com.alibaba.dcm.internal.InetAddressCacheUtilForNew.getInetAddressCache(InetAddressCacheUtilForNew.java:203)
at com.alibaba.dcm.DnsCacheManipulator.getDnsCache(DnsCacheManipulator.java:156)
... 63 more
版本:1.8.0 环境信息:jdk8,amd
@Wackerle 收到。
给一下: ❤️
- 运行的
JDK版本 - 所用的
java-dns-cache-manipulator库版本
jdk 1.8.0_382 java-dns-cache-manipulator 1.8.0
复现代码
ExecutorService executorService = Executors.newFixedThreadPool(2);
executorService.execute(() -> {
try {
DnsCacheEntry entry = DnsCacheManipulator.getDnsCache("baidu.com");
} catch (Exception e) {
e.printStackTrace();
}
});
executorService.execute(() -> {
try {
HttpClient httpClient = HttpClients.custom().build();
HttpGet httpGet = new HttpGet("http://baidu.com");
httpClient.execute(httpGet);
} catch (Exception e) {
e.printStackTrace();
}
});
Thread.sleep(100_000);
报错
Caused by: java.lang.IllegalStateException: child class java.net.InetAddress$NameServiceAddresses for class InetAddress.Addresses should never happens, report issue for dns-cache-manipulator lib!
at com.alibaba.dcm.internal.InetAddressCacheUtilForNew.inetAddress$Addresses2DnsCacheEntry(InetAddressCacheUtilForNew.java:243)
at com.alibaba.dcm.internal.InetAddressCacheUtilForNew.getInetAddressCache(InetAddressCacheUtilForNew.java:203)
at com.alibaba.dcm.DnsCacheManipulator.getDnsCache(DnsCacheManipulator.java:156)
... 4 more
当调用getDnsCache获取域名解析时,同时访问这个域名,会出现这个问题。
原因分析
访问这个域名时,JDK(1.8.0_382)尝试解析这个域名,会先往cache里写入一个NameServiceAddresses,然后委托给NameService解析域名,并将解析结果封装为CachedAddresses替换到cache里。
代码位置:java.net.InetAddress#getAllByName0(java.lang.String, java.net.InetAddress, boolean, boolean)。
在没有替换之前,getDnsCache会从cache里获取到NameServiceAddresses,引发报错。
代码位置:com.alibaba.dcm.internal.InetAddressCacheUtilForNew#getInetAddressCache。
建议
如果从cache里获取到NameServiceAddresses时,不抛出异常,当做没有域名解析处理。
@Wackerle COOOL 👍 我看一下。💕
方便可以开个PR? 😄