use of alluxio.network.protocol.RPCFileWriteResponse 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.RPCFileWriteResponse 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);
}
}
Aggregations