Search in sources :

Example 1 with RPCFileReadResponse

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

RPCFileReadResponse (alluxio.network.protocol.RPCFileReadResponse)1 DataBuffer (alluxio.network.protocol.databuffer.DataBuffer)1 DataByteBuffer (alluxio.network.protocol.databuffer.DataByteBuffer)1 ChannelFuture (io.netty.channel.ChannelFuture)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1