Search in sources :

Example 1 with TestServer

use of org.apache.hadoop.ipc.TestIPC.TestServer 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 TestServer

use of org.apache.hadoop.ipc.TestIPC.TestServer in project hadoop by apache.

the class TestAsyncIPC method testInitialCallRetryCount.

/**
   * Test if the rpc server gets the default retry count (0) from client.
   *
   * @throws ExecutionException
   * @throws InterruptedException
   */
@Test(timeout = 60000)
public void testInitialCallRetryCount() throws IOException, InterruptedException, ExecutionException {
    // Override client to store the call id
    final Client client = new Client(LongWritable.class, conf);
    // Attach a listener that tracks every call ID received by the server.
    final TestServer server = new TestIPC.TestServer(1, false, conf);
    server.callListener = new Runnable() {

        @Override
        public void run() {
            // we have not set the retry count for the client, thus on the server
            // side we should see retry count as 0
            Assert.assertEquals(0, Server.getCallRetryCount());
        }
    };
    try {
        InetSocketAddress addr = NetUtils.getConnectAddress(server);
        server.start();
        final AsyncCaller caller = new AsyncCaller(client, addr, 10);
        caller.run();
        caller.assertReturnValues();
    } finally {
        client.stop();
        server.stop();
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) TestServer(org.apache.hadoop.ipc.TestIPC.TestServer) Test(org.junit.Test)

Example 3 with TestServer

use of org.apache.hadoop.ipc.TestIPC.TestServer in project hadoop by apache.

the class TestAsyncIPC method testUniqueSequentialCallIds.

/**
   * Tests that client generates a unique sequential call ID for each RPC call,
   * even if multiple threads are using the same client.
   *
   * @throws InterruptedException
   * @throws ExecutionException
   */
@Test(timeout = 60000)
public void testUniqueSequentialCallIds() throws IOException, InterruptedException, ExecutionException {
    int serverThreads = 10, callerCount = 100, perCallerCallCount = 100;
    TestServer server = new TestIPC.TestServer(serverThreads, false, conf);
    // Attach a listener that tracks every call ID received by the server. This
    // list must be synchronized, because multiple server threads will add to
    // it.
    final List<Integer> callIds = Collections.synchronizedList(new ArrayList<Integer>());
    server.callListener = new Runnable() {

        @Override
        public void run() {
            callIds.add(Server.getCallId());
        }
    };
    Client client = new Client(LongWritable.class, conf);
    try {
        InetSocketAddress addr = NetUtils.getConnectAddress(server);
        server.start();
        AsyncCaller[] callers = new AsyncCaller[callerCount];
        for (int i = 0; i < callerCount; ++i) {
            callers[i] = new AsyncCaller(client, addr, perCallerCallCount);
            callers[i].start();
        }
        for (int i = 0; i < callerCount; ++i) {
            callers[i].join();
            callers[i].assertReturnValues();
        }
    } finally {
        client.stop();
        server.stop();
    }
    int expectedCallCount = callerCount * perCallerCallCount;
    assertEquals(expectedCallCount, callIds.size());
    // It is not guaranteed that the server executes requests in sequential
    // order
    // of client call ID, so we must sort the call IDs before checking that it
    // contains every expected value.
    Collections.sort(callIds);
    final int startID = callIds.get(0).intValue();
    for (int i = 0; i < expectedCallCount; ++i) {
        assertEquals(startID + i, callIds.get(i).intValue());
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) TestServer(org.apache.hadoop.ipc.TestIPC.TestServer) Test(org.junit.Test)

Example 4 with TestServer

use of org.apache.hadoop.ipc.TestIPC.TestServer in project hadoop by apache.

the class TestAsyncIPC method testCallRetryCount.

/**
   * Test if the rpc server gets the retry count from client.
   *
   * @throws ExecutionException
   * @throws InterruptedException
   */
@Test(timeout = 60000)
public void testCallRetryCount() throws IOException, InterruptedException, ExecutionException {
    final int retryCount = 255;
    // Override client to store the call id
    final Client client = new Client(LongWritable.class, conf);
    Client.setCallIdAndRetryCount(Client.nextCallId(), retryCount, null);
    // Attach a listener that tracks every call ID received by the server.
    final TestServer server = new TestIPC.TestServer(1, false, conf);
    server.callListener = new Runnable() {

        @Override
        public void run() {
            // we have not set the retry count for the client, thus on the server
            // side we should see retry count as 0
            Assert.assertEquals(retryCount, Server.getCallRetryCount());
        }
    };
    try {
        InetSocketAddress addr = NetUtils.getConnectAddress(server);
        server.start();
        final AsyncCaller caller = new AsyncCaller(client, addr, 10);
        caller.run();
        caller.assertReturnValues();
    } finally {
        client.stop();
        server.stop();
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) TestServer(org.apache.hadoop.ipc.TestIPC.TestServer) Test(org.junit.Test)

Aggregations

InetSocketAddress (java.net.InetSocketAddress)4 TestServer (org.apache.hadoop.ipc.TestIPC.TestServer)4 Test (org.junit.Test)4 LongWritable (org.apache.hadoop.io.LongWritable)1 Writable (org.apache.hadoop.io.Writable)1 RpcKind (org.apache.hadoop.ipc.RPC.RpcKind)1 CallInfo (org.apache.hadoop.ipc.TestIPC.CallInfo)1 RpcResponseHeaderProto (org.apache.hadoop.ipc.protobuf.RpcHeaderProtos.RpcResponseHeaderProto)1