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);
}
}
Aggregations