Search in sources :

Example 16 with InetAddress

use of java.net.InetAddress in project flink by apache.

the class ConnectionUtilsTest method testReturnLocalHostAddressUsingHeuristics.

@Test
public void testReturnLocalHostAddressUsingHeuristics() throws Exception {
    try (ServerSocket blocker = new ServerSocket(0, 1, InetAddress.getLocalHost())) {
        // the "blocker" server socket simply does not accept connections
        // this address is consequently "unreachable"
        InetSocketAddress unreachable = new InetSocketAddress("localhost", blocker.getLocalPort());
        final long start = System.nanoTime();
        InetAddress add = ConnectionUtils.findConnectingAddress(unreachable, 2000, 400);
        // check that it did not take forever (max 30 seconds)
        // this check can unfortunately not be too tight, or it will be flaky on some CI infrastructure
        assertTrue(System.nanoTime() - start < 30_000_000_000L);
        // we should have found a heuristic address
        assertNotNull(add);
        // make sure that we returned the InetAddress.getLocalHost as a heuristic
        assertEquals(InetAddress.getLocalHost(), add);
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) ServerSocket(java.net.ServerSocket) InetAddress(java.net.InetAddress) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 17 with InetAddress

use of java.net.InetAddress in project flink by apache.

the class ConnectionUtilsTest method testFindConnectingAddressWhenGetLocalHostThrows.

@Test
public void testFindConnectingAddressWhenGetLocalHostThrows() throws Exception {
    PowerMockito.mockStatic(InetAddress.class);
    Mockito.when(InetAddress.getLocalHost()).thenThrow(new UnknownHostException()).thenCallRealMethod();
    final InetAddress loopbackAddress = Inet4Address.getByName("127.0.0.1");
    Thread socketServerThread;
    try (ServerSocket socket = new ServerSocket(0, 1, loopbackAddress)) {
        // Make sure that the thread will eventually die even if something else goes wrong
        socket.setSoTimeout(10_000);
        socketServerThread = new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    socket.accept();
                } catch (IOException e) {
                // ignore
                }
            }
        });
        socketServerThread.start();
        final InetSocketAddress socketAddress = new InetSocketAddress(loopbackAddress, socket.getLocalPort());
        final InetAddress address = ConnectionUtils.findConnectingAddress(socketAddress, 2000, 400);
        PowerMockito.verifyStatic();
        // Make sure we got an address via alternative means
        assertNotNull(address);
    }
}
Also used : UnknownHostException(java.net.UnknownHostException) InetSocketAddress(java.net.InetSocketAddress) ServerSocket(java.net.ServerSocket) IOException(java.io.IOException) InetAddress(java.net.InetAddress) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 18 with InetAddress

use of java.net.InetAddress in project flink by apache.

the class InstanceManagerTest method testInstanceRegistering.

@Test
public void testInstanceRegistering() {
    try {
        InstanceManager cm = new InstanceManager();
        final int dataPort = 20000;
        HardwareDescription hardwareDescription = HardwareDescription.extractFromSystem(4096);
        InetAddress address = InetAddress.getByName("127.0.0.1");
        // register three instances
        ResourceID resID1 = ResourceID.generate();
        ResourceID resID2 = ResourceID.generate();
        ResourceID resID3 = ResourceID.generate();
        TaskManagerLocation ici1 = new TaskManagerLocation(resID1, address, dataPort);
        TaskManagerLocation ici2 = new TaskManagerLocation(resID2, address, dataPort + 15);
        TaskManagerLocation ici3 = new TaskManagerLocation(resID3, address, dataPort + 30);
        final JavaTestKit probe1 = new JavaTestKit(system);
        final JavaTestKit probe2 = new JavaTestKit(system);
        final JavaTestKit probe3 = new JavaTestKit(system);
        cm.registerTaskManager(new ActorTaskManagerGateway(new AkkaActorGateway(probe1.getRef(), leaderSessionID)), ici1, hardwareDescription, 1);
        cm.registerTaskManager(new ActorTaskManagerGateway(new AkkaActorGateway(probe2.getRef(), leaderSessionID)), ici2, hardwareDescription, 2);
        cm.registerTaskManager(new ActorTaskManagerGateway(new AkkaActorGateway(probe3.getRef(), leaderSessionID)), ici3, hardwareDescription, 5);
        assertEquals(3, cm.getNumberOfRegisteredTaskManagers());
        assertEquals(8, cm.getTotalNumberOfSlots());
        Collection<Instance> instances = cm.getAllRegisteredInstances();
        Set<TaskManagerLocation> taskManagerLocations = new HashSet<TaskManagerLocation>();
        for (Instance instance : instances) {
            taskManagerLocations.add(instance.getTaskManagerLocation());
        }
        assertTrue(taskManagerLocations.contains(ici1));
        assertTrue(taskManagerLocations.contains(ici2));
        assertTrue(taskManagerLocations.contains(ici3));
        cm.shutdown();
    } catch (Exception e) {
        System.err.println(e.getMessage());
        e.printStackTrace();
        Assert.fail("Test erroneous: " + e.getMessage());
    }
}
Also used : TaskManagerLocation(org.apache.flink.runtime.taskmanager.TaskManagerLocation) ActorTaskManagerGateway(org.apache.flink.runtime.jobmanager.slots.ActorTaskManagerGateway) ResourceID(org.apache.flink.runtime.clusterframework.types.ResourceID) InetAddress(java.net.InetAddress) JavaTestKit(akka.testkit.JavaTestKit) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 19 with InetAddress

use of java.net.InetAddress in project flink by apache.

the class InstanceManagerTest method testShutdown.

@Test
public void testShutdown() {
    try {
        InstanceManager cm = new InstanceManager();
        cm.shutdown();
        try {
            ResourceID resID = ResourceID.generate();
            HardwareDescription resources = HardwareDescription.extractFromSystem(4096);
            InetAddress address = InetAddress.getByName("127.0.0.1");
            TaskManagerLocation ici = new TaskManagerLocation(resID, address, 20000);
            JavaTestKit probe = new JavaTestKit(system);
            cm.registerTaskManager(new ActorTaskManagerGateway(new AkkaActorGateway(probe.getRef(), leaderSessionID)), ici, resources, 1);
            fail("Should raise exception in shutdown state");
        } catch (IllegalStateException e) {
        // expected
        }
        assertFalse(cm.reportHeartBeat(new InstanceID()));
    } catch (Exception e) {
        System.err.println(e.getMessage());
        e.printStackTrace();
        Assert.fail("Test erroneous: " + e.getMessage());
    }
}
Also used : ResourceID(org.apache.flink.runtime.clusterframework.types.ResourceID) TaskManagerLocation(org.apache.flink.runtime.taskmanager.TaskManagerLocation) InetAddress(java.net.InetAddress) JavaTestKit(akka.testkit.JavaTestKit) ActorTaskManagerGateway(org.apache.flink.runtime.jobmanager.slots.ActorTaskManagerGateway) Test(org.junit.Test)

Example 20 with InetAddress

use of java.net.InetAddress in project flink by apache.

the class ExecutionGraphTestUtils method getInstance.

public static Instance getInstance(final TaskManagerGateway gateway, final int numberOfSlots) throws Exception {
    ResourceID resourceID = ResourceID.generate();
    HardwareDescription hardwareDescription = new HardwareDescription(4, 2L * 1024 * 1024 * 1024, 1024 * 1024 * 1024, 512 * 1024 * 1024);
    InetAddress address = InetAddress.getByName("127.0.0.1");
    TaskManagerLocation connection = new TaskManagerLocation(resourceID, address, 10001);
    return new Instance(gateway, connection, new InstanceID(), hardwareDescription, numberOfSlots);
}
Also used : HardwareDescription(org.apache.flink.runtime.instance.HardwareDescription) ResourceID(org.apache.flink.runtime.clusterframework.types.ResourceID) Instance(org.apache.flink.runtime.instance.Instance) InstanceID(org.apache.flink.runtime.instance.InstanceID) TaskManagerLocation(org.apache.flink.runtime.taskmanager.TaskManagerLocation) InetAddress(java.net.InetAddress)

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