use of java.net.InetAddress in project flink by apache.
the class IPv6HostnamesITCase method getLocalIPv6Address.
private Inet6Address getLocalIPv6Address() {
try {
Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces();
while (e.hasMoreElements()) {
NetworkInterface netInterface = e.nextElement();
// for each address of the network interface
Enumeration<InetAddress> ee = netInterface.getInetAddresses();
while (ee.hasMoreElements()) {
InetAddress addr = ee.nextElement();
if (addr instanceof Inet6Address && (!addr.isLoopbackAddress()) && (!addr.isAnyLocalAddress())) {
// see if it is possible to bind to the address
InetSocketAddress socketAddress = new InetSocketAddress(addr, 0);
try {
log.info("Considering address " + addr);
// test whether we can bind a socket to that address
log.info("Testing whether sockets can bind to " + addr);
ServerSocket sock = new ServerSocket();
sock.bind(socketAddress);
sock.close();
// test whether Akka's netty can bind to the address
log.info("Testing whether Akka can use " + addr);
int port = NetUtils.getAvailablePort();
ActorSystem as = AkkaUtils.createActorSystem(new Configuration(), new Some<scala.Tuple2<String, Object>>(new scala.Tuple2<String, Object>(addr.getHostAddress(), port)));
as.shutdown();
log.info("Using address " + addr);
return (Inet6Address) addr;
} catch (IOException ignored) {
// fall through the loop
}
}
}
}
return null;
} catch (Exception e) {
return null;
}
}
use of java.net.InetAddress in project flink by apache.
the class TaskManagerLocationTest method testGetHostname0.
@Test
public void testGetHostname0() {
try {
InetAddress address = mock(InetAddress.class);
when(address.getCanonicalHostName()).thenReturn("worker2.cluster.mycompany.com");
when(address.getHostName()).thenReturn("worker2.cluster.mycompany.com");
when(address.getHostAddress()).thenReturn("127.0.0.1");
final TaskManagerLocation info = new TaskManagerLocation(ResourceID.generate(), address, 19871);
Assert.assertEquals("worker2", info.getHostname());
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
}
use of java.net.InetAddress in project flink by apache.
the class TaskManagerLocationTest method testGetHostname2.
@Test
public void testGetHostname2() {
try {
final String addressString = "192.168.254.254";
// we mock the addresses to save the times of the reverse name lookups
InetAddress address = mock(InetAddress.class);
when(address.getCanonicalHostName()).thenReturn("192.168.254.254");
when(address.getHostName()).thenReturn("192.168.254.254");
when(address.getHostAddress()).thenReturn("192.168.254.254");
when(address.getAddress()).thenReturn(new byte[] { (byte) 192, (byte) 168, (byte) 254, (byte) 254 });
TaskManagerLocation info = new TaskManagerLocation(ResourceID.generate(), address, 54152);
assertNotNull(info.getFQDNHostname());
assertTrue(info.getFQDNHostname().equals(addressString));
assertNotNull(info.getHostname());
assertTrue(info.getHostname().equals(addressString));
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
}
use of java.net.InetAddress in project flink by apache.
the class TaskManagerStartupTest method testStartupWhenTaskmanagerActorPortIsUsed.
/**
* Tests that the TaskManager fails synchronously when the actor system port
* is in use.
*
* @throws Throwable
*/
@Test(expected = BindException.class)
public void testStartupWhenTaskmanagerActorPortIsUsed() throws BindException {
ServerSocket blocker = null;
try {
final String localHostName = "localhost";
final InetAddress localBindAddress = InetAddress.getByName(NetUtils.getWildcardIPAddress());
// block some port
blocker = new ServerSocket(0, 50, localBindAddress);
final int port = blocker.getLocalPort();
TaskManager.runTaskManager(localHostName, ResourceID.generate(), port, new Configuration(), TaskManager.class);
fail("This should fail with an IOException");
} catch (IOException e) {
// expected. validate the error message
List<Throwable> causes = StartupUtils.getExceptionCauses(e, new ArrayList<Throwable>());
for (Throwable cause : causes) {
if (cause instanceof BindException) {
throw (BindException) cause;
}
}
fail("This should fail with an exception caused by BindException");
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
} finally {
if (blocker != null) {
try {
blocker.close();
} catch (IOException e) {
// no need to log here
}
}
}
}
use of java.net.InetAddress in project flink by apache.
the class TaskManagerLocationTest method testGetHostname1.
@Test
public void testGetHostname1() {
try {
InetAddress address = mock(InetAddress.class);
when(address.getCanonicalHostName()).thenReturn("worker10");
when(address.getHostName()).thenReturn("worker10");
when(address.getHostAddress()).thenReturn("127.0.0.1");
TaskManagerLocation info = new TaskManagerLocation(ResourceID.generate(), address, 19871);
Assert.assertEquals("worker10", info.getHostname());
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
}
Aggregations