Search in sources :

Example 1 with RPCFileWriteRequest

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

the class DataServerHandler method channelRead0.

@Override
public void channelRead0(final ChannelHandlerContext ctx, final RPCMessage msg) throws IOException {
    LOG.debug("Enter: {}", msg);
    try {
        switch(msg.getType()) {
            case RPC_BLOCK_READ_REQUEST:
                assert msg instanceof RPCBlockReadRequest;
                mBlockHandler.handleBlockReadRequest(ctx, (RPCBlockReadRequest) msg);
                break;
            case RPC_BLOCK_WRITE_REQUEST:
                assert msg instanceof RPCBlockWriteRequest;
                mBlockHandler.handleBlockWriteRequest(ctx, (RPCBlockWriteRequest) msg);
                break;
            case RPC_FILE_READ_REQUEST:
                assert msg instanceof RPCFileReadRequest;
                mUnderFileSystemHandler.handleFileReadRequest(ctx, (RPCFileReadRequest) msg);
                break;
            case RPC_FILE_WRITE_REQUEST:
                assert msg instanceof RPCFileWriteRequest;
                mUnderFileSystemHandler.handleFileWriteRequest(ctx, (RPCFileWriteRequest) msg);
                break;
            case RPC_UFS_BLOCK_READ_REQUEST:
                assert msg instanceof RPCUnderFileSystemBlockReadRequest;
                mBlockHandler.handleUnderFileSystemBlockReadRequest(ctx, (RPCUnderFileSystemBlockReadRequest) msg);
                break;
            case RPC_ERROR_RESPONSE:
                assert msg instanceof RPCErrorResponse;
                LOG.error("Received an error response from the client: " + msg.toString());
                break;
            case RPC_READ_REQUEST:
            case RPC_WRITE_REQUEST:
            case RPC_RESPONSE:
                assert msg instanceof RPCProtoMessage;
                ctx.fireChannelRead(msg);
                break;
            default:
                RPCErrorResponse resp = new RPCErrorResponse(RPCResponse.Status.UNKNOWN_MESSAGE_ERROR);
                ctx.writeAndFlush(resp);
                // TODO(peis): Fix this. We should not throw an exception here.
                throw new IllegalArgumentException("No handler implementation for " + msg.getType());
        }
    } catch (IllegalArgumentException | IOException e) {
        LOG.warn("{}, Error={}", msg, e.getMessage());
        LOG.debug("{}", msg, e);
        LOG.debug("Exit (Error): {}, Error={}", msg, e.getMessage());
        // Rethrow the exception to use Netty's control flow.
        throw e;
    }
    LOG.debug("Exit (OK): {}", msg);
}
Also used : RPCUnderFileSystemBlockReadRequest(alluxio.network.protocol.RPCUnderFileSystemBlockReadRequest) RPCBlockWriteRequest(alluxio.network.protocol.RPCBlockWriteRequest) RPCBlockReadRequest(alluxio.network.protocol.RPCBlockReadRequest) RPCFileWriteRequest(alluxio.network.protocol.RPCFileWriteRequest) RPCErrorResponse(alluxio.network.protocol.RPCErrorResponse) RPCProtoMessage(alluxio.network.protocol.RPCProtoMessage) IOException(java.io.IOException) RPCFileReadRequest(alluxio.network.protocol.RPCFileReadRequest)

Example 2 with RPCFileWriteRequest

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

the class NettyDataServerTest method writeFile.

@Test
public void writeFile() throws Exception {
    long tempUfsFileId = 1;
    long offset = 0;
    long length = 3;
    DataByteArrayChannel data = new DataByteArrayChannel("abc".getBytes(Charsets.UTF_8), 0, 3);
    ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    when(mFileSystemWorker.getUfsOutputStream(tempUfsFileId)).thenReturn(outStream);
    RPCResponse response = request(new RPCFileWriteRequest(tempUfsFileId, offset, length, data));
    // Verify that the write request writes to the OutputStream returned by the worker.
    assertEquals(RPCResponse.Status.SUCCESS, response.getStatus());
    assertEquals("abc", new String(outStream.toByteArray(), Charsets.UTF_8));
}
Also used : DataByteArrayChannel(alluxio.network.protocol.databuffer.DataByteArrayChannel) RPCFileWriteRequest(alluxio.network.protocol.RPCFileWriteRequest) RPCResponse(alluxio.network.protocol.RPCResponse) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 3 with RPCFileWriteRequest

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

the class NettyUnderFileSystemFileWriter method write.

@Override
public void write(InetSocketAddress address, long ufsFileId, long fileOffset, byte[] source, int offset, int length) throws IOException {
    Channel channel = null;
    ClientHandler clientHandler = null;
    Metrics.NETTY_UFS_WRITE_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 RPCFileWriteRequest(ufsFileId, fileOffset, length, new DataByteArrayChannel(source, offset, length))).sync();
        if (channelFuture.isDone() && !channelFuture.isSuccess()) {
            LOG.error("Failed to read ufs file from %s for ufsFilId %d with error %s.", address.toString(), ufsFileId, channelFuture.cause());
            throw new IOException(channelFuture.cause());
        }
        RPCResponse response = listener.get(NettyClient.TIMEOUT_MS, TimeUnit.MILLISECONDS);
        switch(response.getType()) {
            case RPC_FILE_WRITE_RESPONSE:
                RPCFileWriteResponse resp = (RPCFileWriteResponse) response;
                RPCResponse.Status status = resp.getStatus();
                LOG.debug("status: {} from remote machine {} received", status, address);
                if (status != RPCResponse.Status.SUCCESS) {
                    throw new IOException(ExceptionMessage.UNDER_FILE_WRITE_ERROR.getMessage(ufsFileId, address, status.getMessage()));
                }
                break;
            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_FILE_WRITE_RESPONSE));
        }
    } catch (Exception e) {
        Metrics.NETTY_UFS_WRITE_FAILURES.inc();
        try {
            if (channel != null) {
                channel.close().sync();
            }
        } catch (InterruptedException ee) {
            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) Channel(io.netty.channel.Channel) DataByteArrayChannel(alluxio.network.protocol.databuffer.DataByteArrayChannel) RPCFileWriteRequest(alluxio.network.protocol.RPCFileWriteRequest) RPCResponse(alluxio.network.protocol.RPCResponse) IOException(java.io.IOException) IOException(java.io.IOException) DataByteArrayChannel(alluxio.network.protocol.databuffer.DataByteArrayChannel) RPCErrorResponse(alluxio.network.protocol.RPCErrorResponse) RPCFileWriteResponse(alluxio.network.protocol.RPCFileWriteResponse)

Example 4 with RPCFileWriteRequest

use of alluxio.network.protocol.RPCFileWriteRequest 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)

Aggregations

RPCFileWriteRequest (alluxio.network.protocol.RPCFileWriteRequest)4 RPCResponse (alluxio.network.protocol.RPCResponse)3 DataByteArrayChannel (alluxio.network.protocol.databuffer.DataByteArrayChannel)3 RPCErrorResponse (alluxio.network.protocol.RPCErrorResponse)2 IOException (java.io.IOException)2 Test (org.junit.Test)2 RPCBlockReadRequest (alluxio.network.protocol.RPCBlockReadRequest)1 RPCBlockWriteRequest (alluxio.network.protocol.RPCBlockWriteRequest)1 RPCFileReadRequest (alluxio.network.protocol.RPCFileReadRequest)1 RPCFileWriteResponse (alluxio.network.protocol.RPCFileWriteResponse)1 RPCProtoMessage (alluxio.network.protocol.RPCProtoMessage)1 RPCUnderFileSystemBlockReadRequest (alluxio.network.protocol.RPCUnderFileSystemBlockReadRequest)1 Channel (io.netty.channel.Channel)1 ChannelFuture (io.netty.channel.ChannelFuture)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1