use of alluxio.network.protocol.RPCResponse 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);
}
use of alluxio.network.protocol.RPCResponse 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.RPCResponse in project alluxio by Alluxio.
the class NettyUnderFileSystemBlockReader method read.
@Override
public ByteBuffer read(InetSocketAddress address, long blockId, long offset, long length, long sessionId, boolean noCache) throws IOException {
Channel channel = null;
ClientHandler clientHandler = null;
Metrics.NETTY_UFS_BLOCK_READ_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 RPCUnderFileSystemBlockReadRequest(blockId, offset, length, sessionId, noCache));
channelFuture = channelFuture.sync();
if (channelFuture.isDone() && !channelFuture.isSuccess()) {
LOG.error("Failed to read from %s for block %d with error %s.", address.toString(), blockId, channelFuture.cause());
throw new IOException(channelFuture.cause());
}
RPCResponse response = listener.get(NettyClient.TIMEOUT_MS, TimeUnit.MILLISECONDS);
switch(response.getType()) {
case RPC_BLOCK_READ_RESPONSE:
RPCBlockReadResponse blockResponse = (RPCBlockReadResponse) response;
LOG.debug("Data {} from machine {} received", blockId, address);
RPCResponse.Status status = blockResponse.getStatus();
if (status == RPCResponse.Status.SUCCESS) {
// always clear the previous response before reading another one
close();
mReadResponse = blockResponse;
return blockResponse.getPayloadDataBuffer().getReadOnlyByteBuffer();
}
throw new IOException(status.getMessage() + " response: " + blockResponse);
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_READ_RESPONSE));
}
} catch (Exception e) {
Metrics.NETTY_UFS_BLOCK_READ_FAILURES.inc();
try {
if (channel != null) {
channel.close().sync();
}
} catch (InterruptedException ee) {
throw new RuntimeException(ee);
}
throw new IOException(e);
} finally {
if (clientHandler != null) {
clientHandler.removeListeners();
}
if (channel != null) {
mContext.releaseNettyChannel(address, channel);
}
}
}
use of alluxio.network.protocol.RPCResponse 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.RPCResponse in project alluxio by Alluxio.
the class NettyDataServerTest method readFile.
@Test
public void readFile() throws Exception {
long tempUfsFileId = 1;
long offset = 0;
long length = 3;
when(mFileSystemWorker.getUfsInputStream(tempUfsFileId, offset)).thenReturn(new ByteArrayInputStream("abc".getBytes(Charsets.UTF_8)));
RPCResponse response = request(new RPCFileReadRequest(tempUfsFileId, offset, length));
// Verify that the 3 bytes were read.
assertEquals("abc", Charsets.UTF_8.decode(response.getPayloadDataBuffer().getReadOnlyByteBuffer()).toString());
}
Aggregations