Search in sources :

Example 11 with RPCResponse

use of alluxio.network.protocol.RPCResponse in project alluxio by Alluxio.

the class NettyRemoteBlockReader method readRemoteBlock.

@Override
public ByteBuffer readRemoteBlock(InetSocketAddress address, long blockId, long offset, long length, long lockId, long sessionId) throws IOException {
    Channel channel = null;
    ClientHandler clientHandler = null;
    Metrics.NETTY_BLOCK_READ_OPS.inc();
    try {
        channel = mContext.acquireNettyChannel(address);
        if (!(channel.pipeline().last() instanceof ClientHandler)) {
            channel.pipeline().addLast(new ClientHandler());
        }
        clientHandler = (ClientHandler) channel.pipeline().last();
        SingleResponseListener listener = new SingleResponseListener();
        clientHandler.addListener(listener);
        ChannelFuture channelFuture = channel.writeAndFlush(new RPCBlockReadRequest(blockId, offset, length, lockId, sessionId));
        channelFuture = channelFuture.sync();
        if (channelFuture.isDone() && !channelFuture.isSuccess()) {
            LOG.error("Failed to write to %s for block %d with error %s.", address.toString(), blockId, channelFuture.cause());
            throw new IOException(channelFuture.cause());
        }
        RPCResponse response = listener.get(NettyClient.TIMEOUT_MS, TimeUnit.MILLISECONDS);
        switch(response.getType()) {
            case RPC_BLOCK_READ_RESPONSE:
                RPCBlockReadResponse blockResponse = (RPCBlockReadResponse) response;
                LOG.debug("Data {} from remote machine {} received", blockId, address);
                RPCResponse.Status status = blockResponse.getStatus();
                if (status == RPCResponse.Status.SUCCESS) {
                    // always clear the previous response before reading another one
                    close();
                    mReadResponse = blockResponse;
                    return blockResponse.getPayloadDataBuffer().getReadOnlyByteBuffer();
                }
                throw new IOException(status.getMessage() + " response: " + blockResponse);
            case RPC_ERROR_RESPONSE:
                RPCErrorResponse error = (RPCErrorResponse) response;
                throw new IOException(error.getStatus().getMessage());
            default:
                throw new IOException(ExceptionMessage.UNEXPECTED_RPC_RESPONSE.getMessage(response.getType(), RPCMessage.Type.RPC_BLOCK_READ_RESPONSE));
        }
    } catch (Exception e) {
        Metrics.NETTY_BLOCK_READ_FAILURES.inc();
        try {
            if (channel != null) {
                channel.close().sync();
            }
        } catch (InterruptedException ee) {
            throw Throwables.propagate(ee);
        }
        throw new IOException(e);
    } finally {
        if (clientHandler != null) {
            clientHandler.removeListeners();
        }
        if (channel != null) {
            mContext.releaseNettyChannel(address, channel);
        }
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) RPCBlockReadRequest(alluxio.network.protocol.RPCBlockReadRequest) Channel(io.netty.channel.Channel) RPCErrorResponse(alluxio.network.protocol.RPCErrorResponse) RPCResponse(alluxio.network.protocol.RPCResponse) RPCBlockReadResponse(alluxio.network.protocol.RPCBlockReadResponse) IOException(java.io.IOException) IOException(java.io.IOException)

Example 12 with RPCResponse

use of alluxio.network.protocol.RPCResponse in project alluxio by Alluxio.

the class NettyDataServerTest method writeNewBlock.

@Test
public void writeNewBlock() throws Exception {
    long sessionId = 0;
    long blockId = 1;
    long length = 2;
    // Offset is set to 0 so that a new block will be created.
    long offset = 0;
    DataByteArrayChannel data = new DataByteArrayChannel("abc".getBytes(Charsets.UTF_8), 0, 3);
    MockBlockWriter blockWriter = new MockBlockWriter();
    when(mBlockWorker.getTempBlockWriterRemote(sessionId, blockId)).thenReturn(blockWriter);
    RPCResponse response = request(new RPCBlockWriteRequest(sessionId, blockId, offset, length, data));
    // Verify that the write request tells the worker to create a new block and write the specified
    // data to it.
    assertEquals(RPCResponse.Status.SUCCESS, response.getStatus());
    verify(mBlockWorker).createBlockRemote(sessionId, blockId, "MEM", length);
    assertEquals("ab", new String(blockWriter.getBytes(), Charsets.UTF_8));
}
Also used : DataByteArrayChannel(alluxio.network.protocol.databuffer.DataByteArrayChannel) RPCBlockWriteRequest(alluxio.network.protocol.RPCBlockWriteRequest) MockBlockWriter(alluxio.worker.block.io.MockBlockWriter) RPCResponse(alluxio.network.protocol.RPCResponse) Test(org.junit.Test)

Example 13 with RPCResponse

use of alluxio.network.protocol.RPCResponse in project alluxio by Alluxio.

the class NettyDataServerTest method blockWorkerExceptionCausesFailStatusOnRead.

@Test
public void blockWorkerExceptionCausesFailStatusOnRead() throws Exception {
    when(mBlockWorker.readBlockRemote(anyLong(), anyLong(), anyLong())).thenThrow(new RuntimeException());
    RPCResponse response = request(new RPCBlockReadRequest(1, 2, 3, 4, 0));
    // Verify that the write request failed.
    assertEquals(RPCResponse.Status.UFS_READ_FAILED, response.getStatus());
}
Also used : RPCBlockReadRequest(alluxio.network.protocol.RPCBlockReadRequest) RPCResponse(alluxio.network.protocol.RPCResponse) Test(org.junit.Test)

Example 14 with RPCResponse

use of alluxio.network.protocol.RPCResponse in project alluxio by Alluxio.

the class NettyDataServerTest method fileSystemWorkerExceptionCausesFailStatusOnWrite.

@Test
public void fileSystemWorkerExceptionCausesFailStatusOnWrite() throws Exception {
    DataByteArrayChannel data = new DataByteArrayChannel("abc".getBytes(Charsets.UTF_8), 0, 3);
    when(mFileSystemWorker.getUfsOutputStream(1)).thenThrow(new RuntimeException());
    RPCResponse response = request(new RPCFileWriteRequest(1, 0, 3, data));
    // Verify that the write request failed.
    assertEquals(RPCResponse.Status.UFS_WRITE_FAILED, response.getStatus());
}
Also used : DataByteArrayChannel(alluxio.network.protocol.databuffer.DataByteArrayChannel) RPCFileWriteRequest(alluxio.network.protocol.RPCFileWriteRequest) RPCResponse(alluxio.network.protocol.RPCResponse) Test(org.junit.Test)

Example 15 with RPCResponse

use of alluxio.network.protocol.RPCResponse in project alluxio by Alluxio.

the class NettyDataServerTest method fileSystemWorkerExceptionCausesFailStatusOnRead.

@Test
public void fileSystemWorkerExceptionCausesFailStatusOnRead() throws Exception {
    when(mFileSystemWorker.getUfsInputStream(1, 0)).thenThrow(new RuntimeException());
    RPCResponse response = request(new RPCFileReadRequest(1, 0, 3));
    // Verify that the write request failed.
    assertEquals(RPCResponse.Status.UFS_READ_FAILED, response.getStatus());
}
Also used : RPCResponse(alluxio.network.protocol.RPCResponse) RPCFileReadRequest(alluxio.network.protocol.RPCFileReadRequest) Test(org.junit.Test)

Aggregations

RPCResponse (alluxio.network.protocol.RPCResponse)16 Test (org.junit.Test)12 DataByteArrayChannel (alluxio.network.protocol.databuffer.DataByteArrayChannel)7 RPCBlockReadRequest (alluxio.network.protocol.RPCBlockReadRequest)5 RPCBlockWriteRequest (alluxio.network.protocol.RPCBlockWriteRequest)4 RPCErrorResponse (alluxio.network.protocol.RPCErrorResponse)4 Channel (io.netty.channel.Channel)4 ChannelFuture (io.netty.channel.ChannelFuture)4 IOException (java.io.IOException)4 RPCBlockReadResponse (alluxio.network.protocol.RPCBlockReadResponse)3 RPCFileWriteRequest (alluxio.network.protocol.RPCFileWriteRequest)3 RPCFileReadRequest (alluxio.network.protocol.RPCFileReadRequest)2 MockBlockWriter (alluxio.worker.block.io.MockBlockWriter)2 BlockDoesNotExistException (alluxio.exception.BlockDoesNotExistException)1 RPCBlockWriteResponse (alluxio.network.protocol.RPCBlockWriteResponse)1 RPCFileWriteResponse (alluxio.network.protocol.RPCFileWriteResponse)1 RPCUnderFileSystemBlockReadRequest (alluxio.network.protocol.RPCUnderFileSystemBlockReadRequest)1 DataBuffer (alluxio.network.protocol.databuffer.DataBuffer)1 MockBlockReader (alluxio.worker.block.io.MockBlockReader)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1