Search in sources :

Example 1 with RpcKind

use of org.apache.hadoop.ipc.RPC.RpcKind in project hadoop by apache.

the class TestAsyncIPC method testCallIdAndRetry.

/**
   * Test if (1) the rpc server uses the call id/retry provided by the rpc
   * client, and (2) the rpc client receives the same call id/retry from the rpc
   * server.
   *
   * @throws ExecutionException
   * @throws InterruptedException
   */
@Test(timeout = 60000)
public void testCallIdAndRetry() throws IOException, InterruptedException, ExecutionException {
    final Map<Integer, CallInfo> infoMap = new HashMap<Integer, CallInfo>();
    // Override client to store the call info and check response
    final Client client = new Client(LongWritable.class, conf) {

        @Override
        Call createCall(RpcKind rpcKind, Writable rpcRequest) {
            // Set different call id and retry count for the next call
            Client.setCallIdAndRetryCount(Client.nextCallId(), TestIPC.RANDOM.nextInt(255), null);
            final Call call = super.createCall(rpcKind, rpcRequest);
            CallInfo info = new CallInfo();
            info.id = call.id;
            info.retry = call.retry;
            infoMap.put(call.id, info);
            return call;
        }

        @Override
        void checkResponse(RpcResponseHeaderProto header) throws IOException {
            super.checkResponse(header);
            Assert.assertEquals(infoMap.get(header.getCallId()).retry, header.getRetryCount());
        }
    };
    // Attach a listener that tracks every call received by the server.
    final TestServer server = new TestIPC.TestServer(1, false, conf);
    server.callListener = new Runnable() {

        @Override
        public void run() {
            Assert.assertEquals(infoMap.get(Server.getCallId()).retry, Server.getCallRetryCount());
        }
    };
    try {
        InetSocketAddress addr = NetUtils.getConnectAddress(server);
        server.start();
        final AsyncCaller caller = new AsyncCaller(client, addr, 4);
        caller.run();
        caller.assertReturnValues();
    } finally {
        client.stop();
        server.stop();
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) CallInfo(org.apache.hadoop.ipc.TestIPC.CallInfo) Writable(org.apache.hadoop.io.Writable) LongWritable(org.apache.hadoop.io.LongWritable) RpcKind(org.apache.hadoop.ipc.RPC.RpcKind) TestServer(org.apache.hadoop.ipc.TestIPC.TestServer) RpcResponseHeaderProto(org.apache.hadoop.ipc.protobuf.RpcHeaderProtos.RpcResponseHeaderProto) Test(org.junit.Test)

Example 2 with RpcKind

use of org.apache.hadoop.ipc.RPC.RpcKind in project hadoop by apache.

the class TestIPCServerResponder method testDeferResponse.

// Test that IPC calls can be marked for a deferred response.
// call 0: immediate
// call 1: immediate
// call 2: delayed with wait for 1 sendResponse, check if blocked
// call 3: immediate, proves handler is freed
// call 4: delayed with wait for 2 sendResponses, check if blocked
// call 2: sendResponse, should return
// call 4: sendResponse, should remain blocked
// call 5: immediate, prove handler is still free
// call 4: sendResponse, expect it to return
@Test(timeout = 10000)
public void testDeferResponse() throws IOException, InterruptedException {
    final AtomicReference<Call> deferredCall = new AtomicReference<Call>();
    final AtomicInteger count = new AtomicInteger();
    final Writable wait0 = new IntWritable(0);
    final Writable wait1 = new IntWritable(1);
    final Writable wait2 = new IntWritable(2);
    // use only 1 handler to prove it's freed after every call
    Server server = new Server(ADDRESS, 0, IntWritable.class, 1, conf) {

        @Override
        public Writable call(RPC.RpcKind rpcKind, String protocol, Writable waitCount, long receiveTime) throws IOException {
            Call call = Server.getCurCall().get();
            int wait = ((IntWritable) waitCount).get();
            while (wait-- > 0) {
                call.postponeResponse();
                deferredCall.set(call);
            }
            return new IntWritable(count.getAndIncrement());
        }
    };
    server.start();
    final InetSocketAddress address = NetUtils.getConnectAddress(server);
    final Client client = new Client(IntWritable.class, conf);
    Call[] waitingCalls = new Call[2];
    // calls should return immediately, check the sequence number is
    // increasing
    assertEquals(0, ((IntWritable) call(client, wait0, address)).get());
    assertEquals(1, ((IntWritable) call(client, wait0, address)).get());
    // do a call in the background that will have a deferred response
    final ExecutorService exec = Executors.newCachedThreadPool();
    Future<Integer> future1 = exec.submit(new Callable<Integer>() {

        @Override
        public Integer call() throws IOException {
            return ((IntWritable) TestIPCServerResponder.call(client, wait1, address)).get();
        }
    });
    // make sure it blocked
    try {
        future1.get(1, TimeUnit.SECONDS);
        Assert.fail("ipc shouldn't have responded");
    } catch (TimeoutException te) {
    // ignore, expected
    } catch (Exception ex) {
        Assert.fail("unexpected exception:" + ex);
    }
    assertFalse(future1.isDone());
    waitingCalls[0] = deferredCall.get();
    assertNotNull(waitingCalls[0]);
    // proves the handler isn't tied up, and that the prior sequence number
    // was consumed
    assertEquals(3, ((IntWritable) call(client, wait0, address)).get());
    // another call with wait count of 2
    Future<Integer> future2 = exec.submit(new Callable<Integer>() {

        @Override
        public Integer call() throws IOException {
            return ((IntWritable) TestIPCServerResponder.call(client, wait2, address)).get();
        }
    });
    // make sure it blocked
    try {
        future2.get(1, TimeUnit.SECONDS);
        Assert.fail("ipc shouldn't have responded");
    } catch (TimeoutException te) {
    // ignore, expected
    } catch (Exception ex) {
        Assert.fail("unexpected exception:" + ex);
    }
    assertFalse(future2.isDone());
    waitingCalls[1] = deferredCall.get();
    assertNotNull(waitingCalls[1]);
    // the background calls should still be blocked
    assertFalse(future1.isDone());
    assertFalse(future2.isDone());
    // trigger responses
    waitingCalls[0].sendResponse();
    waitingCalls[1].sendResponse();
    try {
        int val = future1.get(1, TimeUnit.SECONDS);
        assertEquals(2, val);
    } catch (Exception ex) {
        Assert.fail("unexpected exception:" + ex);
    }
    // make sure it's still blocked
    try {
        future2.get(1, TimeUnit.SECONDS);
        Assert.fail("ipc shouldn't have responded");
    } catch (TimeoutException te) {
    // ignore, expected
    } catch (Exception ex) {
        Assert.fail("unexpected exception:" + ex);
    }
    assertFalse(future2.isDone());
    // call should return immediately
    assertEquals(5, ((IntWritable) call(client, wait0, address)).get());
    // trigger last waiting call
    waitingCalls[1].sendResponse();
    try {
        int val = future2.get(1, TimeUnit.SECONDS);
        assertEquals(4, val);
    } catch (Exception ex) {
        Assert.fail("unexpected exception:" + ex);
    }
    server.stop();
}
Also used : Call(org.apache.hadoop.ipc.Server.Call) InetSocketAddress(java.net.InetSocketAddress) Writable(org.apache.hadoop.io.Writable) BytesWritable(org.apache.hadoop.io.BytesWritable) IntWritable(org.apache.hadoop.io.IntWritable) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) RpcKind(org.apache.hadoop.ipc.RPC.RpcKind) TimeoutException(java.util.concurrent.TimeoutException) IOException(java.io.IOException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExecutorService(java.util.concurrent.ExecutorService) IntWritable(org.apache.hadoop.io.IntWritable) TimeoutException(java.util.concurrent.TimeoutException) Test(org.junit.Test)

Example 3 with RpcKind

use of org.apache.hadoop.ipc.RPC.RpcKind in project hadoop by apache.

the class TestIPC method testCallIdAndRetry.

/**
   * Test if
   * (1) the rpc server uses the call id/retry provided by the rpc client, and
   * (2) the rpc client receives the same call id/retry from the rpc server.
   */
@Test(timeout = 60000)
public void testCallIdAndRetry() throws IOException {
    final CallInfo info = new CallInfo();
    // Override client to store the call info and check response
    final Client client = new Client(LongWritable.class, conf) {

        @Override
        Call createCall(RpcKind rpcKind, Writable rpcRequest) {
            final Call call = super.createCall(rpcKind, rpcRequest);
            info.id = call.id;
            info.retry = call.retry;
            return call;
        }

        @Override
        void checkResponse(RpcResponseHeaderProto header) throws IOException {
            super.checkResponse(header);
            Assert.assertEquals(info.id, header.getCallId());
            Assert.assertEquals(info.retry, header.getRetryCount());
        }
    };
    // Attach a listener that tracks every call received by the server.
    final TestServer server = new TestServer(1, false);
    server.callListener = new Runnable() {

        @Override
        public void run() {
            Assert.assertEquals(info.id, Server.getCallId());
            Assert.assertEquals(info.retry, Server.getCallRetryCount());
        }
    };
    try {
        InetSocketAddress addr = NetUtils.getConnectAddress(server);
        server.start();
        final SerialCaller caller = new SerialCaller(client, addr, 10);
        caller.run();
        assertFalse(caller.failed);
    } finally {
        client.stop();
        server.stop();
    }
}
Also used : Call(org.apache.hadoop.ipc.Server.Call) RpcResponseHeaderProto(org.apache.hadoop.ipc.protobuf.RpcHeaderProtos.RpcResponseHeaderProto) InetSocketAddress(java.net.InetSocketAddress) Writable(org.apache.hadoop.io.Writable) LongWritable(org.apache.hadoop.io.LongWritable) RpcKind(org.apache.hadoop.ipc.RPC.RpcKind) Test(org.junit.Test)

Aggregations

InetSocketAddress (java.net.InetSocketAddress)3 Writable (org.apache.hadoop.io.Writable)3 RpcKind (org.apache.hadoop.ipc.RPC.RpcKind)3 Test (org.junit.Test)3 LongWritable (org.apache.hadoop.io.LongWritable)2 Call (org.apache.hadoop.ipc.Server.Call)2 RpcResponseHeaderProto (org.apache.hadoop.ipc.protobuf.RpcHeaderProtos.RpcResponseHeaderProto)2 IOException (java.io.IOException)1 ExecutorService (java.util.concurrent.ExecutorService)1 TimeoutException (java.util.concurrent.TimeoutException)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 BytesWritable (org.apache.hadoop.io.BytesWritable)1 IntWritable (org.apache.hadoop.io.IntWritable)1 CallInfo (org.apache.hadoop.ipc.TestIPC.CallInfo)1 TestServer (org.apache.hadoop.ipc.TestIPC.TestServer)1