Search in sources :

Example 1 with DataByteBuffer

use of alluxio.network.protocol.databuffer.DataByteBuffer 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 DataByteBuffer

use of alluxio.network.protocol.databuffer.DataByteBuffer 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 3 with DataByteBuffer

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

the class RPCFileWriteRequestTest method getPayloadDataBuffer.

/**
   * Tests the {@link RPCFileWriteRequest#getPayloadDataBuffer()} method.
   */
@Test
public void getPayloadDataBuffer() {
    int length = 10;
    DataByteBuffer payload = new DataByteBuffer(ByteBuffer.allocate(length), length);
    RPCFileWriteRequest req = new RPCFileWriteRequest(TEMP_UFS_FILE_ID, OFFSET, LENGTH, payload);
    assertValid(req);
    Assert.assertEquals(payload, req.getPayloadDataBuffer());
}
Also used : DataByteBuffer(alluxio.network.protocol.databuffer.DataByteBuffer) Test(org.junit.Test)

Example 4 with DataByteBuffer

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

the class RPCMessageIntegrationTest method RPCBlockWriteRequest.

@Test
public void RPCBlockWriteRequest() {
    ByteBuffer payload = BufferUtils.getIncreasingByteBuffer((int) OFFSET, (int) LENGTH);
    RPCBlockWriteRequest msg = new RPCBlockWriteRequest(SESSION_ID, BLOCK_ID, OFFSET, LENGTH, new DataByteBuffer(payload, LENGTH));
    RPCBlockWriteRequest decoded = (RPCBlockWriteRequest) encodeThenDecode(msg);
    assertValid(msg, decoded);
}
Also used : DataByteBuffer(alluxio.network.protocol.databuffer.DataByteBuffer) ByteBuffer(java.nio.ByteBuffer) DataByteBuffer(alluxio.network.protocol.databuffer.DataByteBuffer) Test(org.junit.Test)

Example 5 with DataByteBuffer

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

the class UnderFileSystemDataServerHandler method handleFileReadRequest.

/**
   * Handles a {@link RPCFileReadRequest} by reading the data through an input stream provided by
   * the file worker. This method assumes the length to read is less than or equal to the unread
   * data in the file.
   *
   * @param ctx The context of this request which handles the result of this operation
   * @param req The initiating {@link RPCFileReadRequest}
   * @throws IOException if an I/O error occurs when interacting with the UFS
   */
public void handleFileReadRequest(ChannelHandlerContext ctx, RPCFileReadRequest req) throws IOException {
    req.validate();
    long ufsFileId = req.getTempUfsFileId();
    long offset = req.getOffset();
    long length = req.getLength();
    byte[] data = new byte[(int) length];
    try {
        InputStream in = mWorker.getUfsInputStream(ufsFileId, offset);
        int bytesRead = 0;
        if (in != null) {
            // if we have not reached the end of the file
            while (bytesRead < length) {
                // TODO(peis): Fix this. It is not recommended to do heavy blocking IO operation
                // in Netty's IO event group. We should do it in another threadpool.
                int read = in.read(data, bytesRead, (int) length - bytesRead);
                if (read == -1) {
                    break;
                }
                bytesRead += read;
            }
        }
        DataBuffer buf = bytesRead != 0 ? new DataByteBuffer(ByteBuffer.wrap(data, 0, bytesRead), bytesRead) : null;
        RPCFileReadResponse resp = new RPCFileReadResponse(ufsFileId, offset, bytesRead, buf, 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 read ufs file, may have been closed due to a client timeout.", e);
        RPCFileReadResponse resp = RPCFileReadResponse.createErrorResponse(req, RPCResponse.Status.UFS_READ_FAILED);
        ChannelFuture future = ctx.writeAndFlush(resp);
        future.addListener(ChannelFutureListener.CLOSE);
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) InputStream(java.io.InputStream) DataByteBuffer(alluxio.network.protocol.databuffer.DataByteBuffer) RPCFileReadResponse(alluxio.network.protocol.RPCFileReadResponse) IOException(java.io.IOException) DataBuffer(alluxio.network.protocol.databuffer.DataBuffer)

Aggregations

DataByteBuffer (alluxio.network.protocol.databuffer.DataByteBuffer)12 ByteBuffer (java.nio.ByteBuffer)7 Test (org.junit.Test)6 DataBuffer (alluxio.network.protocol.databuffer.DataBuffer)5 RPCBlockReadResponse (alluxio.network.protocol.RPCBlockReadResponse)2 ChannelFuture (io.netty.channel.ChannelFuture)2 IOException (java.io.IOException)2 BlockDoesNotExistException (alluxio.exception.BlockDoesNotExistException)1 RPCFileReadResponse (alluxio.network.protocol.RPCFileReadResponse)1 BlockReader (alluxio.worker.block.io.BlockReader)1 InputStream (java.io.InputStream)1