use of alluxio.network.protocol.RPCBlockWriteRequest 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.RPCBlockWriteRequest 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.RPCBlockWriteRequest in project alluxio by Alluxio.
the class NettyDataServerTest method blockWorkerExceptionCausesFailStatusOnWrite.
@Test
public void blockWorkerExceptionCausesFailStatusOnWrite() throws Exception {
long sessionId = 0;
long blockId = 1;
long offset = 0;
long length = 2;
when(mBlockWorker.getTempBlockWriterRemote(sessionId, blockId)).thenThrow(new RuntimeException());
DataByteArrayChannel data = new DataByteArrayChannel("abc".getBytes(Charsets.UTF_8), 0, 3);
RPCResponse response = request(new RPCBlockWriteRequest(sessionId, blockId, offset, length, data));
// Verify that the write request failed.
assertEquals(RPCResponse.Status.WRITE_ERROR, response.getStatus());
}
use of alluxio.network.protocol.RPCBlockWriteRequest in project alluxio by Alluxio.
the class NettyDataServerTest method writeExistingBlock.
@Test
public void writeExistingBlock() throws Exception {
long sessionId = 0;
long blockId = 1;
// Offset is set to 1 so that the write is directed to an existing block
long offset = 1;
long length = 2;
DataByteArrayChannel data = new DataByteArrayChannel("abc".getBytes(Charsets.UTF_8), 0, 3);
MockBlockWriter blockWriter = new MockBlockWriter();
when(mBlockWorker.getTempBlockWriterRemote(sessionId, blockId)).thenReturn(blockWriter);
RPCResponse response = request(new RPCBlockWriteRequest(sessionId, blockId, offset, length, data));
// Verify that the write request requests space on an existing block and then writes the
// specified data.
assertEquals(RPCResponse.Status.SUCCESS, response.getStatus());
verify(mBlockWorker).requestSpace(sessionId, blockId, length);
assertEquals("ab", new String(blockWriter.getBytes(), Charsets.UTF_8));
}
use of alluxio.network.protocol.RPCBlockWriteRequest in project alluxio by Alluxio.
the class NettyDataServerTest method writeNewBlock.
@Test
public void writeNewBlock() throws Exception {
long sessionId = 0;
long blockId = 1;
long length = 2;
// Offset is set to 0 so that a new block will be created.
long offset = 0;
DataByteArrayChannel data = new DataByteArrayChannel("abc".getBytes(Charsets.UTF_8), 0, 3);
MockBlockWriter blockWriter = new MockBlockWriter();
when(mBlockWorker.getTempBlockWriterRemote(sessionId, blockId)).thenReturn(blockWriter);
RPCResponse response = request(new RPCBlockWriteRequest(sessionId, blockId, offset, length, data));
// Verify that the write request tells the worker to create a new block and write the specified
// data to it.
assertEquals(RPCResponse.Status.SUCCESS, response.getStatus());
verify(mBlockWorker).createBlockRemote(sessionId, blockId, "MEM", length);
assertEquals("ab", new String(blockWriter.getBytes(), Charsets.UTF_8));
}
Aggregations