Search in sources :

Example 11 with InetAddress

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;
    }
}
Also used : ActorSystem(akka.actor.ActorSystem) Configuration(org.apache.flink.configuration.Configuration) InetSocketAddress(java.net.InetSocketAddress) NetworkInterface(java.net.NetworkInterface) Inet6Address(java.net.Inet6Address) ServerSocket(java.net.ServerSocket) IOException(java.io.IOException) IOException(java.io.IOException) Tuple2(org.apache.flink.api.java.tuple.Tuple2) InetAddress(java.net.InetAddress)

Example 12 with InetAddress

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());
    }
}
Also used : InetAddress(java.net.InetAddress) Test(org.junit.Test)

Example 13 with InetAddress

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());
    }
}
Also used : InetAddress(java.net.InetAddress) Test(org.junit.Test)

Example 14 with InetAddress

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
            }
        }
    }
}
Also used : Configuration(org.apache.flink.configuration.Configuration) ArrayList(java.util.ArrayList) BindException(java.net.BindException) ServerSocket(java.net.ServerSocket) ArrayList(java.util.ArrayList) List(java.util.List) IOException(java.io.IOException) InetAddress(java.net.InetAddress) IllegalConfigurationException(org.apache.flink.configuration.IllegalConfigurationException) IOException(java.io.IOException) BindException(java.net.BindException) Test(org.junit.Test)

Example 15 with InetAddress

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());
    }
}
Also used : InetAddress(java.net.InetAddress) Test(org.junit.Test)

Aggregations

InetAddress (java.net.InetAddress)2063 UnknownHostException (java.net.UnknownHostException)377 IOException (java.io.IOException)284 Test (org.junit.Test)272 InetSocketAddress (java.net.InetSocketAddress)240 NetworkInterface (java.net.NetworkInterface)178 ArrayList (java.util.ArrayList)158 SocketException (java.net.SocketException)148 Inet6Address (java.net.Inet6Address)97 HashMap (java.util.HashMap)95 Inet4Address (java.net.Inet4Address)88 Socket (java.net.Socket)73 LinkAddress (android.net.LinkAddress)70 DatagramPacket (java.net.DatagramPacket)67 Token (org.apache.cassandra.dht.Token)67 DatagramSocket (java.net.DatagramSocket)61 RouteInfo (android.net.RouteInfo)56 Map (java.util.Map)55 LinkProperties (android.net.LinkProperties)52 ServerSocket (java.net.ServerSocket)50