Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,19 @@
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Stream;
import org.apache.ignite.testframework.junits.WithSystemProperty;
import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

/** */
@WithSystemProperty(key = "sun.net.inetaddr.ttl", value = "0")
public class TcpDiscoveryVmIpFinderDnsResolveTest extends GridCommonAbstractTest {

/** Fqnd */
Expand Down Expand Up @@ -149,11 +151,6 @@ public void fqdnResolveAfterDnsHostChange(String fqdn, int expectedCount) throws

InetSocketAddress addr1 = resolved1.iterator().next();

//because of JAVA networkaddress cache ttl can be turn on.
//will be great to change current test and run it in separate JVM.
//and set there -Dsun.net.inetaddr.ttl=0 -Dsun.net.inetaddr.negative.ttl=0
Thread.sleep(50_000);

Collection<InetSocketAddress> resolved2 = ipFinder.getRegisteredAddresses();

assertEquals(expectedCount, resolved2.size());
Expand Down Expand Up @@ -527,59 +524,38 @@ else if (multipleFqdn.equals(paramStr)) {
/** */
public interface INameService extends InvocationHandler {
/** */
static void install(
INameService dns
) throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException, ClassNotFoundException {
final Class<?> inetAddrCls = InetAddress.class;

Object neu;

Field nameSvcField;

try {
//JAVA 9+ class
final Class<?> iface = Class.forName("java.net.InetAddress$NameService");

nameSvcField = inetAddrCls.getDeclaredField("nameService");

neu = Proxy.newProxyInstance(iface.getClassLoader(), new Class<?>[] {iface}, dns);
}
catch (final ClassNotFoundException | NoSuchFieldException e) {
//JAVA <8 class
nameSvcField = inetAddrCls.getDeclaredField("nameServices");
static void install(INameService dns) throws ReflectiveOperationException {
Field nameSvcField = nameServiceField();

final Class<?> iface = Class.forName("sun.net.spi.nameservice.NameService");
Class<?> iface = nameSvcField.getType();

neu = Collections.singletonList(Proxy.newProxyInstance(iface.getClassLoader(), new Class<?>[] {iface}, dns));
}

nameSvcField.setAccessible(true);
nameSvc = nameSvcField.get(null);

nameSvc = nameSvcField.get(inetAddrCls);

nameSvcField.set(inetAddrCls, neu);
nameSvcField.set(null, Proxy.newProxyInstance(iface.getClassLoader(), new Class<?>[] {iface}, dns));
}

/** */
static void uninstall() throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException {
final Class<?> inetAddrCls = InetAddress.class;
static void uninstall() throws ReflectiveOperationException {
nameServiceField().set(null, nameSvc);
}

/**
* @return Accessible {@link InetAddress} static field holding the system-wide name service:
* {@code InetAddressResolver resolver} since JDK 18, {@code InetAddress.NameService nameService} before.
*/
private static Field nameServiceField() throws NoSuchFieldException {
Field nameSvcField;

try {
//JAVA 9+ class
Class.forName("java.net.InetAddress$NameService");

nameSvcField = inetAddrCls.getDeclaredField("nameService");
nameSvcField = InetAddress.class.getDeclaredField("nameService");
}
catch (final ClassNotFoundException | NoSuchFieldException e) {
//JAVA <8 class
nameSvcField = inetAddrCls.getDeclaredField("nameServices");
catch (NoSuchFieldException e) {
nameSvcField = InetAddress.class.getDeclaredField("resolver");
}

nameSvcField.setAccessible(true);

nameSvcField.set(inetAddrCls, nameSvc);
return nameSvcField;
}

/**
Expand All @@ -604,10 +580,16 @@ static void uninstall() throws IllegalArgumentException, IllegalAccessException,
@Override default Object invoke(final Object proxy, final Method method,
final Object[] args) throws Throwable {
switch (method.getName()) {
// InetAddress.NameService, JDK 9-17.
case "lookupAllHostAddr":
return lookupAllHostAddr((String)args[0]);
case "getHostByAddr":
return getHostByAddr((byte[])args[0]);
// java.net.spi.InetAddressResolver, JDK 18+.
case "lookupByName":
return Stream.of(lookupAllHostAddr((String)args[0]));
case "lookupByAddress":
return getHostByAddr((byte[])args[0]);
default:
final StringBuilder o = new StringBuilder();

Expand Down
Loading