Search in sources :

Example 1 with RPCUnderFileSystemBlockReadRequest

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

the class NettyUnderFileSystemBlockReader method read.

@Override
public ByteBuffer read(InetSocketAddress address, long blockId, long offset, long length, long sessionId, boolean noCache) throws IOException {
    Channel channel = null;
    ClientHandler clientHandler = null;
    Metrics.NETTY_UFS_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 RPCUnderFileSystemBlockReadRequest(blockId, offset, length, sessionId, noCache));
        channelFuture = channelFuture.sync();
        if (channelFuture.isDone() && !channelFuture.isSuccess()) {
            LOG.error("Failed to read from %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 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_UFS_BLOCK_READ_FAILURES.inc();
        try {
            if (channel != null) {
                channel.close().sync();
            }
        } catch (InterruptedException ee) {
            throw new RuntimeException(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) RPCUnderFileSystemBlockReadRequest(alluxio.network.protocol.RPCUnderFileSystemBlockReadRequest) Channel(io.netty.channel.Channel) RPCResponse(alluxio.network.protocol.RPCResponse) IOException(java.io.IOException) IOException(java.io.IOException) RPCErrorResponse(alluxio.network.protocol.RPCErrorResponse) RPCBlockReadResponse(alluxio.network.protocol.RPCBlockReadResponse)

Example 2 with RPCUnderFileSystemBlockReadRequest

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

Aggregations

RPCErrorResponse (alluxio.network.protocol.RPCErrorResponse)2 RPCUnderFileSystemBlockReadRequest (alluxio.network.protocol.RPCUnderFileSystemBlockReadRequest)2 IOException (java.io.IOException)2 RPCBlockReadRequest (alluxio.network.protocol.RPCBlockReadRequest)1 RPCBlockReadResponse (alluxio.network.protocol.RPCBlockReadResponse)1 RPCBlockWriteRequest (alluxio.network.protocol.RPCBlockWriteRequest)1 RPCFileReadRequest (alluxio.network.protocol.RPCFileReadRequest)1 RPCFileWriteRequest (alluxio.network.protocol.RPCFileWriteRequest)1 RPCProtoMessage (alluxio.network.protocol.RPCProtoMessage)1 RPCResponse (alluxio.network.protocol.RPCResponse)1 Channel (io.netty.channel.Channel)1 ChannelFuture (io.netty.channel.ChannelFuture)1