Search in sources :

Example 1 with DataBuffer

use of alluxio.network.protocol.databuffer.DataBuffer in project alluxio by Alluxio.

the class BlockDataServerHandler method handleUnderFileSystemBlockReadRequest.

/**
   * Handles a {@link RPCUnderFileSystemBlockReadRequest} by reading the data through a
   * {@link BlockReader} provided by the block worker. This method assumes the data is available
   * in the UFS returns an error status if the data is not available.
   *
   * @param ctx The context of this request which handles the result of this operation
   * @param req The initiating {@link RPCBlockReadRequest}
   * @throws IOException if an I/O error occurs when reading the data requested
   */
public void handleUnderFileSystemBlockReadRequest(final ChannelHandlerContext ctx, final RPCUnderFileSystemBlockReadRequest req) throws IOException {
    final long blockId = req.getBlockId();
    final long offset = req.getOffset();
    final long len = req.getLength();
    final long sessionId = req.getSessionId();
    final boolean noCache = req.getNoCache();
    try {
        DataBuffer buffer = null;
        req.validate();
        BlockReader reader = mWorker.readUfsBlock(sessionId, blockId, offset, noCache);
        ByteBuffer data = reader.read(offset, len);
        if (data != null && data.remaining() > 0) {
            buffer = new DataByteBuffer(data, data.remaining());
            Metrics.BYTES_READ_UFS.inc(buffer.getLength());
        }
        RPCBlockReadResponse resp = new RPCBlockReadResponse(blockId, offset, data.remaining(), buffer, RPCResponse.Status.SUCCESS);
        ChannelFuture future = ctx.writeAndFlush(resp);
        if (buffer != null) {
            future.addListener(new ReleasableResourceChannelListener(buffer));
        }
        LOG.debug("Preparation for responding to remote block request for: {} done.", blockId);
    } catch (Exception e) {
        LOG.error("Exception reading block {}", blockId, e);
        RPCBlockReadResponse resp;
        if (e instanceof BlockDoesNotExistException) {
            resp = RPCBlockReadResponse.createErrorResponse(req, RPCResponse.Status.FILE_DNE);
        } else {
            resp = RPCBlockReadResponse.createErrorResponse(req, RPCResponse.Status.UFS_READ_FAILED);
        }
        ChannelFuture future = ctx.writeAndFlush(resp);
        future.addListener(ChannelFutureListener.CLOSE);
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) BlockReader(alluxio.worker.block.io.BlockReader) DataByteBuffer(alluxio.network.protocol.databuffer.DataByteBuffer) RPCBlockReadResponse(alluxio.network.protocol.RPCBlockReadResponse) ByteBuffer(java.nio.ByteBuffer) DataByteBuffer(alluxio.network.protocol.databuffer.DataByteBuffer) BlockDoesNotExistException(alluxio.exception.BlockDoesNotExistException) IOException(java.io.IOException) BlockDoesNotExistException(alluxio.exception.BlockDoesNotExistException) DataBuffer(alluxio.network.protocol.databuffer.DataBuffer)

Example 2 with DataBuffer

use of alluxio.network.protocol.databuffer.DataBuffer in project alluxio by Alluxio.

the class UnderFileSystemDataServerHandler method handleFileWriteRequest.

/**
   * Handles a {@link RPCFileWriteRequest} by writing the data through an output stream provided
   * by the file worker. This method only allows appending data to the file and does not support
   * writing at arbitrary offsets.
   *
   * @param ctx The context of this request which handles the result of this operation
   * @param req The initiating {@link RPCFileWriteRequest}
   * @throws IOException if an I/O error occurs when interacting with the UFS
   */
public void handleFileWriteRequest(ChannelHandlerContext ctx, RPCFileWriteRequest req) throws IOException {
    long ufsFileId = req.getTempUfsFileId();
    // Currently unused as only sequential write is supported
    long offset = req.getOffset();
    long length = req.getLength();
    final DataBuffer data = req.getPayloadDataBuffer();
    try {
        OutputStream out = mWorker.getUfsOutputStream(ufsFileId);
        // This channel will not be closed because the underlying stream should not be closed, the
        // channel will be cleaned up when the underlying stream is closed.
        WritableByteChannel channel = Channels.newChannel(out);
        channel.write(data.getReadOnlyByteBuffer());
        RPCFileWriteResponse resp = new RPCFileWriteResponse(ufsFileId, offset, length, RPCResponse.Status.SUCCESS);
        ctx.writeAndFlush(resp);
    } catch (Exception e) {
        // TODO(peis): Fix this. The exception here should never be caused netty related issue.
        LOG.error("Failed to write ufs file.", e);
        RPCFileWriteResponse resp = RPCFileWriteResponse.createErrorResponse(req, RPCResponse.Status.UFS_WRITE_FAILED);
        ChannelFuture future = ctx.writeAndFlush(resp);
        future.addListener(ChannelFutureListener.CLOSE);
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) OutputStream(java.io.OutputStream) WritableByteChannel(java.nio.channels.WritableByteChannel) RPCFileWriteResponse(alluxio.network.protocol.RPCFileWriteResponse) IOException(java.io.IOException) DataBuffer(alluxio.network.protocol.databuffer.DataBuffer)

Example 3 with DataBuffer

use of alluxio.network.protocol.databuffer.DataBuffer in project alluxio by Alluxio.

the class RPCFileWriteRequestTest method encodeDecodeData.

/**
   * Tests the {@link RPCFileWriteRequest#encode(ByteBuf)} and
   * {@link RPCFileWriteRequest#decode(ByteBuf)} methods with data.
   */
@Test
public void encodeDecodeData() {
    int length = 10;
    DataBuffer buffer = new DataByteBuffer(ByteBuffer.allocate(length), length);
    RPCFileWriteRequest req = new RPCFileWriteRequest(TEMP_UFS_FILE_ID, OFFSET, length, buffer);
    req.encode(mBuffer);
    mBuffer.writeBytes(buffer.getReadOnlyByteBuffer());
    RPCFileWriteRequest req2 = RPCFileWriteRequest.decode(mBuffer);
    assertValid(TEMP_UFS_FILE_ID, OFFSET, length, req);
    assertValid(TEMP_UFS_FILE_ID, OFFSET, length, req2);
}
Also used : DataByteBuffer(alluxio.network.protocol.databuffer.DataByteBuffer) DataBuffer(alluxio.network.protocol.databuffer.DataBuffer) Test(org.junit.Test)

Example 4 with DataBuffer

use of alluxio.network.protocol.databuffer.DataBuffer in project alluxio by Alluxio.

the class DataServerBlockWriteHandlerTest method buildWriteRequest.

@Override
protected RPCProtoMessage buildWriteRequest(long offset, int len) {
    Protocol.WriteRequest writeRequest = Protocol.WriteRequest.newBuilder().setId(1L).setOffset(offset).setSessionId(1L).setType(Protocol.RequestType.ALLUXIO_BLOCK).build();
    DataBuffer buffer = null;
    if (len > 0) {
        ByteBuf buf = PooledByteBufAllocator.DEFAULT.buffer(len);
        for (int i = 0; i < len; i++) {
            byte value = (byte) (mRandom.nextInt() % Byte.MAX_VALUE);
            buf.writeByte(value);
            mChecksum += BufferUtils.byteToInt(value);
        }
        buffer = new DataNettyBufferV2(buf);
    }
    return new RPCProtoMessage(new ProtoMessage(writeRequest), buffer);
}
Also used : RPCProtoMessage(alluxio.network.protocol.RPCProtoMessage) ProtoMessage(alluxio.util.proto.ProtoMessage) RPCProtoMessage(alluxio.network.protocol.RPCProtoMessage) DataNettyBufferV2(alluxio.network.protocol.databuffer.DataNettyBufferV2) Protocol(alluxio.proto.dataserver.Protocol) ByteBuf(io.netty.buffer.ByteBuf) DataBuffer(alluxio.network.protocol.databuffer.DataBuffer)

Example 5 with DataBuffer

use of alluxio.network.protocol.databuffer.DataBuffer in project alluxio by Alluxio.

the class ClientHandlerTest method channelRead0ResponseReceived.

/**
   * Makes sure that the response is received as expected.
   */
@Test
public void channelRead0ResponseReceived() throws IOException {
    final ClientHandler.ResponseListener listener = Mockito.mock(ClientHandler.ResponseListener.class);
    final DataBuffer buffer = Mockito.mock(DataBuffer.class);
    final RPCResponse response = new RPCBlockReadResponse(0, 0, 0, buffer, RPCResponse.Status.SUCCESS);
    mHandler.addListener(listener);
    mHandler.channelRead0(mContext, response);
    Mockito.verify(listener).onResponseReceived(response);
}
Also used : RPCResponse(alluxio.network.protocol.RPCResponse) RPCBlockReadResponse(alluxio.network.protocol.RPCBlockReadResponse) DataBuffer(alluxio.network.protocol.databuffer.DataBuffer) Test(org.junit.Test)

Aggregations

DataBuffer (alluxio.network.protocol.databuffer.DataBuffer)21 IOException (java.io.IOException)7 RPCProtoMessage (alluxio.network.protocol.RPCProtoMessage)6 DataByteBuffer (alluxio.network.protocol.databuffer.DataByteBuffer)6 ByteBuf (io.netty.buffer.ByteBuf)6 DataNettyBufferV2 (alluxio.network.protocol.databuffer.DataNettyBufferV2)5 Protocol (alluxio.proto.dataserver.Protocol)5 ProtoMessage (alluxio.util.proto.ProtoMessage)5 ChannelFuture (io.netty.channel.ChannelFuture)5 ByteBuffer (java.nio.ByteBuffer)5 RPCBlockReadResponse (alluxio.network.protocol.RPCBlockReadResponse)4 BlockDoesNotExistException (alluxio.exception.BlockDoesNotExistException)3 Test (org.junit.Test)3 DataNettyBuffer (alluxio.network.protocol.databuffer.DataNettyBuffer)2 BlockReader (alluxio.worker.block.io.BlockReader)2 RPCBlockWriteResponse (alluxio.network.protocol.RPCBlockWriteResponse)1 RPCFileReadResponse (alluxio.network.protocol.RPCFileReadResponse)1 RPCFileWriteResponse (alluxio.network.protocol.RPCFileWriteResponse)1 RPCResponse (alluxio.network.protocol.RPCResponse)1 DataFileChannel (alluxio.network.protocol.databuffer.DataFileChannel)1