Search in sources :

Example 86 with InetSocketAddress

use of java.net.InetSocketAddress in project hadoop by apache.

the class TestIPC method testIpcTimeout.

@Test(timeout = 60000)
public void testIpcTimeout() throws IOException {
    // start server
    Server server = new TestServer(1, true);
    InetSocketAddress addr = NetUtils.getConnectAddress(server);
    server.start();
    // start client
    Client client = new Client(LongWritable.class, conf);
    // set timeout to be less than MIN_SLEEP_TIME
    try {
        call(client, new LongWritable(RANDOM.nextLong()), addr, MIN_SLEEP_TIME / 2, conf);
        fail("Expected an exception to have been thrown");
    } catch (SocketTimeoutException e) {
        LOG.info("Get a SocketTimeoutException ", e);
    }
    // set timeout to be bigger than 3*ping interval
    call(client, new LongWritable(RANDOM.nextLong()), addr, 3 * PING_INTERVAL + MIN_SLEEP_TIME, conf);
    client.stop();
}
Also used : SocketTimeoutException(java.net.SocketTimeoutException) InetSocketAddress(java.net.InetSocketAddress) LongWritable(org.apache.hadoop.io.LongWritable) Test(org.junit.Test)

Example 87 with InetSocketAddress

use of java.net.InetSocketAddress in project hadoop by apache.

the class TestIPC method internalTestSerial.

public void internalTestSerial(int handlerCount, boolean handlerSleep, int clientCount, int callerCount, int callCount) throws IOException, InterruptedException {
    Server server = new TestServer(handlerCount, handlerSleep);
    InetSocketAddress addr = NetUtils.getConnectAddress(server);
    server.start();
    Client[] clients = new Client[clientCount];
    for (int i = 0; i < clientCount; i++) {
        clients[i] = new Client(LongWritable.class, conf);
    }
    SerialCaller[] callers = new SerialCaller[callerCount];
    for (int i = 0; i < callerCount; i++) {
        callers[i] = new SerialCaller(clients[i % clientCount], addr, callCount);
        callers[i].start();
    }
    for (int i = 0; i < callerCount; i++) {
        callers[i].join();
        assertFalse(callers[i].failed);
    }
    for (int i = 0; i < clientCount; i++) {
        clients[i].stop();
    }
    server.stop();
}
Also used : InetSocketAddress(java.net.InetSocketAddress) LongWritable(org.apache.hadoop.io.LongWritable)

Example 88 with InetSocketAddress

use of java.net.InetSocketAddress in project hadoop by apache.

the class TestIPC method checkBlocking.

// goal is to jam a handler with a connection, fill the callq with
// connections, in turn jamming the readers - then flood the server and
// ensure that the listener blocks when the reader connection queues fill
@SuppressWarnings("unchecked")
private void checkBlocking(int readers, int readerQ, int callQ) throws Exception {
    // makes it easier
    int handlers = 1;
    final Configuration conf = new Configuration();
    conf.setInt(CommonConfigurationKeys.IPC_SERVER_RPC_READ_CONNECTION_QUEUE_SIZE_KEY, readerQ);
    // send in enough clients to block up the handlers, callq, and readers
    final int initialClients = readers + callQ + handlers;
    // max connections we should ever end up accepting at once
    // 1 = listener
    final int maxAccept = initialClients + readers * readerQ + 1;
    // stress it with 2X the max
    int clients = maxAccept * 2;
    final AtomicInteger failures = new AtomicInteger(0);
    final CountDownLatch callFinishedLatch = new CountDownLatch(clients);
    // start server
    final TestServerQueue server = new TestServerQueue(clients, readers, callQ, handlers, conf);
    CallQueueManager<Call> spy = spy((CallQueueManager<Call>) Whitebox.getInternalState(server, "callQueue"));
    Whitebox.setInternalState(server, "callQueue", spy);
    final InetSocketAddress addr = NetUtils.getConnectAddress(server);
    server.start();
    Client.setConnectTimeout(conf, 10000);
    // instantiate the threads, will start in batches
    Thread[] threads = new Thread[clients];
    for (int i = 0; i < clients; i++) {
        threads[i] = new Thread(new Runnable() {

            @Override
            public void run() {
                Client client = new Client(LongWritable.class, conf);
                try {
                    call(client, new LongWritable(Thread.currentThread().getId()), addr, 60000, conf);
                } catch (Throwable e) {
                    LOG.error(e);
                    failures.incrementAndGet();
                    return;
                } finally {
                    callFinishedLatch.countDown();
                    client.stop();
                }
            }
        });
    }
    // and others not blocking in the race to fill the callq
    for (int i = 0; i < initialClients; i++) {
        threads[i].start();
        if (i == 0) {
            // let first reader block in a call
            server.firstCallLatch.await();
        }
        // wait until reader put a call to callQueue, to make sure all readers
        // are blocking on the queue after initialClients threads are started.
        verify(spy, timeout(100).times(i + 1)).put(Mockito.<Call>anyObject());
    }
    try {
        // wait till everything is slotted, should happen immediately
        GenericTestUtils.waitFor(new Supplier<Boolean>() {

            @Override
            public Boolean get() {
                return server.getNumOpenConnections() >= initialClients;
            }
        }, 100, 3000);
    } catch (TimeoutException e) {
        fail("timed out while waiting for connections to open.");
    }
    LOG.info("(initial clients) need:" + initialClients + " connections have:" + server.getNumOpenConnections());
    LOG.info("ipc layer should be blocked");
    assertEquals(callQ, server.getCallQueueLen());
    assertEquals(initialClients, server.getNumOpenConnections());
    // connection queues should fill and then the listener should block
    for (int i = initialClients; i < clients; i++) {
        threads[i].start();
    }
    Thread.sleep(10);
    try {
        GenericTestUtils.waitFor(new Supplier<Boolean>() {

            @Override
            public Boolean get() {
                return server.getNumOpenConnections() >= maxAccept;
            }
        }, 100, 3000);
    } catch (TimeoutException e) {
        fail("timed out while waiting for connections to open until maxAccept.");
    }
    LOG.info("(max clients) need:" + maxAccept + " connections have:" + server.getNumOpenConnections());
    // check a few times to make sure we didn't go over
    for (int i = 0; i < 4; i++) {
        assertEquals(maxAccept, server.getNumOpenConnections());
        Thread.sleep(100);
    }
    // sanity check that no calls have finished
    assertEquals(clients, callFinishedLatch.getCount());
    LOG.info("releasing the calls");
    server.callBlockLatch.countDown();
    callFinishedLatch.await();
    for (Thread t : threads) {
        t.join();
    }
    assertEquals(0, failures.get());
    server.stop();
}
Also used : Call(org.apache.hadoop.ipc.Server.Call) Configuration(org.apache.hadoop.conf.Configuration) InetSocketAddress(java.net.InetSocketAddress) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) LongWritable(org.apache.hadoop.io.LongWritable) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TimeoutException(java.util.concurrent.TimeoutException) ConnectTimeoutException(org.apache.hadoop.net.ConnectTimeoutException) SocketTimeoutException(java.net.SocketTimeoutException)

Example 89 with InetSocketAddress

use of java.net.InetSocketAddress in project hadoop by apache.

the class TestIPC method doIpcVersionTest.

private void doIpcVersionTest(byte[] requestData, byte[] expectedResponse) throws IOException {
    Server server = new TestServer(1, true);
    InetSocketAddress addr = NetUtils.getConnectAddress(server);
    server.start();
    Socket socket = new Socket();
    try {
        NetUtils.connect(socket, addr, 5000);
        OutputStream out = socket.getOutputStream();
        InputStream in = socket.getInputStream();
        out.write(requestData, 0, requestData.length);
        out.flush();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        IOUtils.copyBytes(in, baos, 256);
        byte[] responseData = baos.toByteArray();
        assertEquals(StringUtils.byteToHexString(expectedResponse), StringUtils.byteToHexString(responseData));
    } finally {
        IOUtils.closeSocket(socket);
        server.stop();
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ServerSocket(java.net.ServerSocket) Socket(java.net.Socket)

Example 90 with InetSocketAddress

use of java.net.InetSocketAddress in project hadoop by apache.

the class TestIPC method testStandAloneClient.

@Test(timeout = 60000)
public void testStandAloneClient() throws IOException {
    Client client = new Client(LongWritable.class, conf);
    InetSocketAddress address = new InetSocketAddress("127.0.0.1", 10);
    try {
        call(client, RANDOM.nextLong(), address, conf);
        fail("Expected an exception to have been thrown");
    } catch (IOException e) {
        String message = e.getMessage();
        String addressText = address.getHostName() + ":" + address.getPort();
        assertTrue("Did not find " + addressText + " in " + message, message.contains(addressText));
        Throwable cause = e.getCause();
        assertNotNull("No nested exception in " + e, cause);
        String causeText = cause.getMessage();
        assertTrue("Did not find " + causeText + " in " + message, message.contains(causeText));
    } finally {
        client.stop();
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) IOException(java.io.IOException) Test(org.junit.Test)

Aggregations

InetSocketAddress (java.net.InetSocketAddress)2586 Test (org.junit.Test)595 IOException (java.io.IOException)592 Socket (java.net.Socket)345 InetAddress (java.net.InetAddress)242 SocketAddress (java.net.SocketAddress)176 ServerSocket (java.net.ServerSocket)170 ArrayList (java.util.ArrayList)168 Configuration (org.apache.hadoop.conf.Configuration)140 ByteBuffer (java.nio.ByteBuffer)129 UnknownHostException (java.net.UnknownHostException)122 InputStream (java.io.InputStream)102 OutputStream (java.io.OutputStream)101 SocketChannel (java.nio.channels.SocketChannel)101 SocketException (java.net.SocketException)89 File (java.io.File)88 HashMap (java.util.HashMap)78 URI (java.net.URI)72 Proxy (java.net.Proxy)65 SocketTimeoutException (java.net.SocketTimeoutException)65