use of alluxio.network.protocol.RPCFileWriteRequest in project alluxio by Alluxio.
the class DataServerHandler method channelRead0.
@Override
public void channelRead0(final ChannelHandlerContext ctx, final RPCMessage msg) throws IOException {
LOG.debug("Enter: {}", msg);
try {
switch(msg.getType()) {
case RPC_BLOCK_READ_REQUEST:
assert msg instanceof RPCBlockReadRequest;
mBlockHandler.handleBlockReadRequest(ctx, (RPCBlockReadRequest) msg);
break;
case RPC_BLOCK_WRITE_REQUEST:
assert msg instanceof RPCBlockWriteRequest;
mBlockHandler.handleBlockWriteRequest(ctx, (RPCBlockWriteRequest) msg);
break;
case RPC_FILE_READ_REQUEST:
assert msg instanceof RPCFileReadRequest;
mUnderFileSystemHandler.handleFileReadRequest(ctx, (RPCFileReadRequest) msg);
break;
case RPC_FILE_WRITE_REQUEST:
assert msg instanceof RPCFileWriteRequest;
mUnderFileSystemHandler.handleFileWriteRequest(ctx, (RPCFileWriteRequest) msg);
break;
case RPC_UFS_BLOCK_READ_REQUEST:
assert msg instanceof RPCUnderFileSystemBlockReadRequest;
mBlockHandler.handleUnderFileSystemBlockReadRequest(ctx, (RPCUnderFileSystemBlockReadRequest) msg);
break;
case RPC_ERROR_RESPONSE:
assert msg instanceof RPCErrorResponse;
LOG.error("Received an error response from the client: " + msg.toString());
break;
case RPC_READ_REQUEST:
case RPC_WRITE_REQUEST:
case RPC_RESPONSE:
assert msg instanceof RPCProtoMessage;
ctx.fireChannelRead(msg);
break;
default:
RPCErrorResponse resp = new RPCErrorResponse(RPCResponse.Status.UNKNOWN_MESSAGE_ERROR);
ctx.writeAndFlush(resp);
// TODO(peis): Fix this. We should not throw an exception here.
throw new IllegalArgumentException("No handler implementation for " + msg.getType());
}
} catch (IllegalArgumentException | IOException e) {
LOG.warn("{}, Error={}", msg, e.getMessage());
LOG.debug("{}", msg, e);
LOG.debug("Exit (Error): {}, Error={}", msg, e.getMessage());
// Rethrow the exception to use Netty's control flow.
throw e;
}
LOG.debug("Exit (OK): {}", msg);
}
use of alluxio.network.protocol.RPCFileWriteRequest in project alluxio by Alluxio.
the class NettyDataServerTest method writeFile.
@Test
public void writeFile() throws Exception {
long tempUfsFileId = 1;
long offset = 0;
long length = 3;
DataByteArrayChannel data = new DataByteArrayChannel("abc".getBytes(Charsets.UTF_8), 0, 3);
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
when(mFileSystemWorker.getUfsOutputStream(tempUfsFileId)).thenReturn(outStream);
RPCResponse response = request(new RPCFileWriteRequest(tempUfsFileId, offset, length, data));
// Verify that the write request writes to the OutputStream returned by the worker.
assertEquals(RPCResponse.Status.SUCCESS, response.getStatus());
assertEquals("abc", new String(outStream.toByteArray(), Charsets.UTF_8));
}
use of alluxio.network.protocol.RPCFileWriteRequest in project alluxio by Alluxio.
the class NettyUnderFileSystemFileWriter method write.
@Override
public void write(InetSocketAddress address, long ufsFileId, long fileOffset, byte[] source, int offset, int length) throws IOException {
Channel channel = null;
ClientHandler clientHandler = null;
Metrics.NETTY_UFS_WRITE_OPS.inc();
try {
channel = mContext.acquireNettyChannel(address);
if (!(channel.pipeline().last() instanceof ClientHandler)) {
channel.pipeline().addLast(new ClientHandler());
}
clientHandler = (ClientHandler) channel.pipeline().last();
SingleResponseListener listener = new SingleResponseListener();
clientHandler.addListener(listener);
ChannelFuture channelFuture = channel.writeAndFlush(new RPCFileWriteRequest(ufsFileId, fileOffset, length, new DataByteArrayChannel(source, offset, length))).sync();
if (channelFuture.isDone() && !channelFuture.isSuccess()) {
LOG.error("Failed to read ufs file from %s for ufsFilId %d with error %s.", address.toString(), ufsFileId, channelFuture.cause());
throw new IOException(channelFuture.cause());
}
RPCResponse response = listener.get(NettyClient.TIMEOUT_MS, TimeUnit.MILLISECONDS);
switch(response.getType()) {
case RPC_FILE_WRITE_RESPONSE:
RPCFileWriteResponse resp = (RPCFileWriteResponse) response;
RPCResponse.Status status = resp.getStatus();
LOG.debug("status: {} from remote machine {} received", status, address);
if (status != RPCResponse.Status.SUCCESS) {
throw new IOException(ExceptionMessage.UNDER_FILE_WRITE_ERROR.getMessage(ufsFileId, address, status.getMessage()));
}
break;
case RPC_ERROR_RESPONSE:
RPCErrorResponse error = (RPCErrorResponse) response;
throw new IOException(error.getStatus().getMessage());
default:
throw new IOException(ExceptionMessage.UNEXPECTED_RPC_RESPONSE.getMessage(response.getType(), RPCMessage.Type.RPC_FILE_WRITE_RESPONSE));
}
} catch (Exception e) {
Metrics.NETTY_UFS_WRITE_FAILURES.inc();
try {
if (channel != null) {
channel.close().sync();
}
} catch (InterruptedException ee) {
Throwables.propagate(ee);
}
throw new IOException(e);
} finally {
if (clientHandler != null) {
clientHandler.removeListeners();
}
if (channel != null) {
mContext.releaseNettyChannel(address, channel);
}
}
}
use of alluxio.network.protocol.RPCFileWriteRequest in project alluxio by Alluxio.
the class NettyDataServerTest method fileSystemWorkerExceptionCausesFailStatusOnWrite.
@Test
public void fileSystemWorkerExceptionCausesFailStatusOnWrite() throws Exception {
DataByteArrayChannel data = new DataByteArrayChannel("abc".getBytes(Charsets.UTF_8), 0, 3);
when(mFileSystemWorker.getUfsOutputStream(1)).thenThrow(new RuntimeException());
RPCResponse response = request(new RPCFileWriteRequest(1, 0, 3, data));
// Verify that the write request failed.
assertEquals(RPCResponse.Status.UFS_WRITE_FAILED, response.getStatus());
}
Aggregations