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