use of alluxio.network.protocol.RPCBlockWriteResponse in project alluxio by Alluxio.
the class NettyRemoteBlockWriter method write.
@Override
public void write(byte[] bytes, int offset, int length) throws IOException {
Channel channel = null;
ClientHandler clientHandler = null;
Metrics.NETTY_BLOCK_WRITE_OPS.inc();
try {
channel = mContext.acquireNettyChannel(mAddress);
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 RPCBlockWriteRequest(mSessionId, mBlockId, mWrittenBytes, length, new DataByteArrayChannel(bytes, offset, length))).sync();
if (channelFuture.isDone() && !channelFuture.isSuccess()) {
LOG.error("Failed to write to %s for block %d with error %s.", mAddress.toString(), mBlockId, channelFuture.cause());
throw new IOException(channelFuture.cause());
}
RPCResponse response = listener.get(NettyClient.TIMEOUT_MS, TimeUnit.MILLISECONDS);
switch(response.getType()) {
case RPC_BLOCK_WRITE_RESPONSE:
RPCBlockWriteResponse resp = (RPCBlockWriteResponse) response;
RPCResponse.Status status = resp.getStatus();
LOG.debug("status: {} from remote machine {} received", status, mAddress);
if (status != RPCResponse.Status.SUCCESS) {
throw new IOException(ExceptionMessage.BLOCK_WRITE_ERROR.getMessage(mBlockId, mSessionId, mAddress, status.getMessage()));
}
mWrittenBytes += length;
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_BLOCK_WRITE_RESPONSE));
}
} catch (Exception e) {
Metrics.NETTY_BLOCK_WRITE_FAILURES.inc();
try {
// TODO(peis): We should not close the channel unless it is an exception caused by network.
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(mAddress, channel);
}
}
}
use of alluxio.network.protocol.RPCBlockWriteResponse in project alluxio by Alluxio.
the class BlockDataServerHandler method handleBlockWriteRequest.
/**
* Handles a {@link RPCBlockWriteRequest} by writing the data through a {@link BlockWriter}
* provided by the block worker. This method takes care of requesting space and creating the
* block if necessary.
*
* @param ctx The context of this request which handles the result of this operation
* @param req The initiating {@link RPCBlockWriteRequest}
* @throws IOException if an I/O exception occurs when writing the data
*/
// TODO(hy): This write request handler is very simple in order to be stateless. Therefore, the
// block file is opened and closed for every request. If this is too slow, then this handler
// should be optimized to keep state.
void handleBlockWriteRequest(final ChannelHandlerContext ctx, final RPCBlockWriteRequest req) throws IOException {
final long sessionId = req.getSessionId();
final long blockId = req.getBlockId();
final long offset = req.getOffset();
final long length = req.getLength();
final DataBuffer data = req.getPayloadDataBuffer();
BlockWriter writer = null;
try {
req.validate();
ByteBuffer buffer = data.getReadOnlyByteBuffer();
if (offset == 0) {
// This is the first write to the block, so create the temp block file. The file will only
// be created if the first write starts at offset 0. This allocates enough space for the
// write.
mWorker.createBlockRemote(sessionId, blockId, mStorageTierAssoc.getAlias(0), length);
} else {
// Allocate enough space in the existing temporary block for the write.
mWorker.requestSpace(sessionId, blockId, length);
}
writer = mWorker.getTempBlockWriterRemote(sessionId, blockId);
writer.append(buffer);
Metrics.BYTES_WRITTEN_REMOTE.inc(data.getLength());
RPCBlockWriteResponse resp = new RPCBlockWriteResponse(sessionId, blockId, offset, length, RPCResponse.Status.SUCCESS);
ChannelFuture future = ctx.writeAndFlush(resp);
future.addListener(new ClosableResourceChannelListener(writer));
} catch (Exception e) {
LOG.error("Error writing remote block : {}", e.getMessage(), e);
RPCBlockWriteResponse resp = RPCBlockWriteResponse.createErrorResponse(req, RPCResponse.Status.WRITE_ERROR);
ChannelFuture future = ctx.writeAndFlush(resp);
future.addListener(ChannelFutureListener.CLOSE);
if (writer != null) {
writer.close();
}
}
}
Aggregations